A. 【例59.1】 合并果子

    Type: Default 1000ms 256MiB

【例59.1】 合并果子

You cannot submit for this problem because the contest is ended. You can click "Open in Problem Set" to view this problem in normal mode.

Description

In an orchard, Duoduo has picked all the fruits and sorted them into different piles based on their types. Duoduo decides to combine all the fruits into one pile.
Each time, Duoduo can merge two piles of fruits, and the energy consumed is equal to the sum of the weights of the two piles. It can be seen that after n1n-1 merges, only one pile remains. The total energy Duoduo consumes during the merging process is equal to the sum of the energy consumed in each merge.
Since Duoduo still needs to exert great effort to carry these fruits home, he wants to save as much energy as possible during the merging process. Assuming each fruit weighs 11, and given the number of fruit types and the quantity of each type, your task is to design a merging order that minimizes the energy Duoduo consumes and output this minimum energy value.
For example, if there are 33 types of fruits with quantities of 11, 22, and 99, you can first merge the piles of 11 and 22, resulting in a new pile of 33, consuming 33 units of energy. Then, merge this new pile with the original third pile, resulting in a new pile of 1212, consuming 1212 units of energy. Therefore, Duoduo's total energy consumption is 3+12=153+12=15. It can be proven that 1515 is the minimum energy consumption.

Input Format

Two lines: the first line contains an integer nn (1n300001≤n≤30000), representing the number of fruit types. The second line contains nn integers separated by spaces, where the ii-th integer aia_i (1ai200001≤a_i≤20000) represents the quantity of the ii-th type of fruit.

Output Format

A single line containing an integer, representing the minimum energy consumption. The input data guarantees this value is less than 2312^{31}.

Sample

3
1 2 9
15

20251018D班作业(3)

Not Claimed
Status
Done
Problem
3
Open Since
2025-10-23 0:00
Deadline
2025-10-30 23:59
Extension
24 hour(s)