Problem C: 合并果子

Problem C: 合并果子

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 43  Solved: 14
[Submit] [Status] [Web Board] [Creator:]

Description

在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆。多多决定把所有的果子合成一堆。每一次合并,多多可以把两堆果子合并到一起,消耗的体力等于两堆果子的重量之和。可以看出,所有的果子经过n-1次合并之后,就只剩下一堆了。多多在合并果子时总共消耗的体力等于每次合并所耗体力之和。
因为还要花大力气把这些果子搬回家,所以多多在合并果子时要尽可能地节省体力。假定每个果子重量都为1,并且已知果子的种类数和每种果子的数目,你的任务是设计出合并的次序方案,使多多耗费的体力最少,并输出这个最小的体力耗费值。
例如有3种果子,数目依次为1,2,9。可以先将 1、2堆合并,新堆数目为3,耗费体力为3。接着,将新堆与原先的第三堆合并,又得到新的堆,数目为12,耗费体力为 12。所以多多总共耗费体力=3+12=15。可以证明15为最小的体力耗费值。


In an orchard, Dodo had already knocked down all the fruits, and divided them into different piles according to the different types of fruits. Dodo decided to combine all the fruits into one pile. Every time you merge, Dodo can merge two piles of fruits together, and the energy consumed is equal to the sum of the weight of the two piles of fruits. It can be said that after all the fruits are merged n-1 times, there is only one pile left. The total stamina consumed by Dodo when merging the fruits is equal to the sum of the stamina consumed for each merge.

Because it will take great effort to move these fruits home, Dodo should save as much energy as possible when merging the fruits. Assuming that the weight of each fruit is 1, and the number of fruit types and the number of each fruit are known, your task is to design a combined sequence plan to minimize the amount of physical effort that Dodo consumes, and output the minimum value of energy spent.

For example, there are 3 kinds of fruits, the numbers are 1, 2, and 9. You can merge piles 1 and 2 first, the number of the new pile is 3, and the energy spent is 3. Then, merge the new pile with the original third pile, and get a new pile, the number is 12, and the physical effort is 12. So Dodo consumes a total of physical energy: 3 + 12 = 15. We have proven that 15 is the minimum physical expenditure value.


Input

输入文件fruit.in包括两行,第一行是一个整数n(1 <= n <= 10000),表示果子的种类数。第二行包含n个整数,用空格分隔,第i个整数ai(1 <= ai <= 20000)是第i种果子的数目。


The input file fruit.in consists of two lines. The first line is an integer n (1 <= n <= 10000), which represents the number of fruit types. The second line contains n integers, separated by spaces. The i-th integer ai (1 <= ai <= 20000) is the number of the i-th fruit.

 


Output

输出文件fruit.out包括一行,这一行只包含一个整数,也就是最小的体力耗费值。输入数据保证这个值小于231。


The output file fruit.out includes one line, which contains only an integer, is the minimum physical exertion value. The input number ensures that this value is less than 231.

 


Sample Input Copy

3
1 2 9

Sample Output Copy

15

HINT

【数据规模】
对于30%的数据,保证有n <= 1000;
对于50%的数据,保证有n <= 5000;
对于全部的数据,保证有n <= 10000。


For 30% of the data, it is guaranteed that n <= 1000;

For 50% of the data, it is guaranteed that n <= 5000;

For all data, it is guaranteed that n <= 10000.