博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Card Gym - 101257H
阅读量:4204 次
发布时间:2019-05-26

本文共 2780 字,大约阅读时间需要 9 分钟。

After Hasan ruined the Worm puzzle, Rula brought a new game to the office, the Card game.

The game has a group of cards, each card has an integer value Vi written on it.

Initially, N cards are placed side by side in a straight line (refer to the picture above). In each turn you will pick two adjacent card sets (a single card is also considered a card set) and merge the cards of the two sets together to form one new set.

After each turn, you will add the maximum card value in each card set to your score. The game is over when no more merges could be performed.

What is the maximum score you can get at the end of the game, given that your initial score is 0?

Input

The first line of input contains a single integer N (2 ≤ N ≤ 105), the number of cards in the game.

The second line contains N space-separated integers, each integer is Vi (1 ≤ Vi ≤ 5000), which represents the number written on the ith card.

Output

On a single line, print the maximum score you can get.

Example
Input
51 2 3 2 1
Output
23
Input
67 9 7 5 4 3
Output
108
Note

One of the possible solutions for the first sample is as follows:

  • Initially, each set will contain a single card: {1} {2} {3} {2} {1}.
  • Merge {1} with {2}, the resulting sets are: {1 2} {3} {2} {1}, which will add (2 + 3 + 2 + 1 = 8) to your score.
  • Merge {2} with {1}, the resulting sets are: {1 2} {3} {2 1}, which will add (2 + 3 + 2 = 7) to your score.
  • Merge {1 2} with {3}, the resulting sets are: {1 2 3} {2 1}, which will add (3 + 2 = 5) to your score.
  • Merge {1 2 3} with {2 1}, the resulting sets are: {1 2 3 2 1}, which will add 3 to your score.

Final score is 8 + 7 + 5 + 3 = 23.

哈桑破坏了Worm拼图,Rula带来了一个新的游戏到办公室,卡牌游戏。
游戏有一组牌,每张牌都有一个整数值Vi写在它上面。
最初,N张卡并排放置在一条直线上(参见上图)。在每个回合,你会选择两个相邻的卡集(一个卡也被认为是一个卡集),并将两个集合的卡合并成一个新集。
每回合后,您将在每个卡组中将最大卡值添加到您的总谱。游戏结束时,没有更多的合并可以执行。
考虑到你的初始分数是0,你在游戏结束时可以获得的最高分数是多少?
输入
第一行输入包含单个整数N(2≤N≤105),游戏中的牌数。
第二行包含N个空格分隔的整数,每个整数为Vi(1≤Vi≤5000),表示写在第i张卡上的数字。
输出
在一行上,打印你可以得到的最大得分。
输入
5
1 2 3 2 1
输出
23
输入
6
7 9 7 5 4 3
输出
108
注意
第一个样本的可能解决方案之一如下:
最初,每个集合将包含一张牌:{1} {2} {3} {2} {1}。
将{1}与{2}合并,得到的集合为:{1 2} {3} {2} {1},它将为您的总分添加(2 + 3 + 2 + 1 = 8)。
将{2}与{1}合并,生成的集合为:{1 2} {3} {2 1},它将为您的总分添加(2 + 3 + 2 = 7)。
将{1 2}与{3}合并,生成的集合为:{1 2 3} {2 1},它将为您的总分添加(3 + 2 = 5)。
将{1 2 3}与{2 1}合并,结果集为:{1 2 3 2 1},它将为您的分数增加3。
最终得分为8 + 7 + 5 + 3 = 23。
////  main.cpp//  160929////  Created by liuzhe on 16/9/29.//  Copyright © 2016年 my_code. All rights reserved.////#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;int a[100010];int n;int main(){ //freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout); scanf("%d",&n); long long sum=0; for(int i=0;i

转载地址:http://tpali.baihongyu.com/

你可能感兴趣的文章
Jsp 出现异常IllegalArgumentException:Control character in cookie value or attribute解决方法
查看>>
Servlet 使用字符流读取文件乱码解决方法
查看>>
设计模式 Concurrency 之 ReadWriteLock 读写锁
查看>>
Spring 复习总结
查看>>
剑指Offer 二叉树的镜像
查看>>
剑指Offer 含有Min函数的栈
查看>>
Mybatis 主键配置
查看>>
JVM 参数使用总结
查看>>
剑指Offer 最小的K个数
查看>>
剑指Offer 调整数组顺序使奇数位于偶数前面
查看>>
剑指Offer SnakeNumber 蛇形填数
查看>>
剑指Offer TurnOnLight 开灯问题
查看>>
剑指Offer RotateArray 旋转数组的最小数字
查看>>
剑指Offer FindNumberMoreThanHalf 找出数组中出现次数超过一半的数字
查看>>
剑指Offer AccurateFactorial 计算精确的阶乘
查看>>
剑指Offer CalCarryBit 计算进位个数
查看>>
剑指Offer ReverseList 反转列表
查看>>
剑指Offer MergeOrderedList 合并两个排序的链表
查看>>
剑指Offer LevelTraversalTree 层序遍历二叉树
查看>>
IDEA Maven搭建的Web项目出现ClassNotFoundException
查看>>