vijos P1097 merge fruit

In an orchard, Duoduo has already knocked down all the fruits and divided them into different piles according to different types of fruits. Duoduo decided to combine all the fruits into a pile. 
Each time you merge, you can combine two piles of fruit together, and the energy consumed is equal to the sum of the weights of the two piles. It can be seen that after all the fruits are merged n-1 times, only one pile remains. The total physical energy consumed when merging fruits is equal to the sum of physical energy consumed in each merger. 
Because it will take a lot of effort to move these fruits back home, it is necessary to save energy as much as possible when merging the fruits. Assuming that each fruit has a weight of 1, and the number of types of fruits and the number of each type of fruit are known, your task is to design a combined sequence scheme to minimize the amount of physical effort and output this minimum physical effort value. 
For example, there are 3 kinds of fruits, the number is 1, 2, 9 in order. You can merge the 1st and 2nd heaps first, the number of new heaps is 3, and the physical effort is 3. Then, the new heap was merged with the original third heap, and a new heap was obtained, with a number of 12, and a physical cost of 12. So a lot of physical effort = 3 + 12 = 15. It can be proved that 15 is the minimum physical cost.

format

Input format

The input consists of two lines, the first line is an integer n (1 <= n <= 10000), which indicates the number of kinds of fruits. 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 format

The output includes a line, which contains only an integer, which is the minimum physical cost. The input data ensures that this value is less than 2 ^ 31.

Sample 1

Sample input 1 [Copy]

3 
1 2 9

Sample output 1 [Copy]

In an orchard, Duoduo has already knocked down all the fruits and divided them into different piles according to different types of fruits. Duoduo decided to combine all the fruits into a pile. 
Each time you merge, you can combine two piles of fruit together, and the energy consumed is equal to the sum of the weights of the two piles. It can be seen that after all the fruits are merged n-1 times, only one pile remains. The total physical energy consumed when merging fruits is equal to the sum of physical energy consumed in each merger. 
Because it will take a lot of effort to move these fruits back home, it is necessary to save energy as much as possible when merging the fruits. Assuming that each fruit has a weight of 1, and the number of types of fruits and the number of each type of fruit are known, your task is to design a combined sequence scheme to minimize the amount of physical effort and output this minimum physical effort value. 
For example, there are 3 kinds of fruits, the number is 1, 2, 9 in order. You can merge the 1st and 2nd heaps first, the number of new heaps is 3, and the physical effort is 3. Then, the new heap was merged with the original third heap, and a new heap was obtained, with a number of 12, and a physical cost of 12. So a lot of physical effort = 3 + 12 = 15. It can be proved that 15 is the minimum physical cost.

format

Input format

The input consists of two lines, the first line is an integer n (1 <= n <= 10000), which indicates the number of kinds of fruits. 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 format

The output includes a line, which contains only an integer, which is the minimum physical cost. The input data ensures that this value is less than 2 ^ 31.


发布了16 篇原创文章 · 获赞 1 · 访问量 3823

Guess you like

Origin blog.csdn.net/GYJ_love/article/details/50532873