Card Gym - 101257H

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
5
1 2 3 2 1
Output
23
Input
6
7 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 <bits/stdc++.h>

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <string>
#include <map>
#include <set>
#include <queue>
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<n;i++)
        scanf("%d",&a[i]);
    sort(a,a+n);
    for(int i=0;i<n;i++)
        sum+=a[i]*i;
    printf("%lld\n",sum);
    
    
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值