#t357. 合并果子
合并果子
Merging Fruits
Problem Description
In an orchard, Duoduo has already picked all the fruits and sorted them into different piles by type. Duoduo decides to merge all the fruits into one pile.
Each time, Duoduo can merge two piles of fruit together; the physical effort consumed equals the sum of the weights of the two piles. It can be seen that after merges, only one pile remains. The total physical effort Duoduo consumes while merging equals the sum of the effort consumed in each merge.
Since Duoduo also has to spend great effort carrying these fruits home, Duoduo wants to save as much effort as possible when merging the fruits. Assuming each fruit weighs , and given the number of fruit types and the quantity of each type, your task is to design a merging order that minimizes Duoduo's physical effort, and output this minimum effort.
For example, with types of fruit, with quantities , , respectively: we can first merge piles and , producing a new pile of size and consuming effort; then merge this new pile with the original third pile, producing a new pile of size and consuming effort. So Duoduo consumes a total of effort. It can be proven that is the minimum possible effort.
Input Format
Two lines in total. The first line is an integer , the number of fruit types. The second line contains integers separated by spaces, where the -th integer is the quantity of the -th type of fruit.
Output Format
A single integer, the minimum physical effort. The input guarantees this value is less than .
Sample Input
3
1 2 9
Sample Output
15
Hint
For of the data, is guaranteed.
For of the data, is guaranteed.
For all data, is guaranteed.