#P6033. [NOIP 2004 提高组] 合并果子 加强版
[NOIP 2004 提高组] 合并果子 加强版
Background
Except for [Constraints and Specifications], this problem is exactly the same as P1090.
Problem Description
In an orchard, Duoduo has picked all the fruits and separated them into different piles according to their types. Duoduo decides to merge all the fruits into one pile.
In each merge, Duoduo can merge two piles of fruits into one, and the physical effort consumed equals the sum of the weights of the two piles. It can be seen that after merges, there will be only one pile left. The total physical effort Duoduo spends while merging fruits equals the sum of the effort consumed in each merge.
Because Duoduo still needs to spend a lot of effort to carry these fruits home, Duoduo wants to save as much effort as possible when merging. Assume each fruit weighs . Given the number of fruit types and the number of fruits of each type, your task is to design a merging order so that Duoduo’s total physical effort is minimized, and output this minimum effort value.
For example, there are piles of fruits with counts . You can first merge the piles with and fruits, obtaining a new pile of size and consuming effort. Then merge this new pile with the original third pile, obtaining a new pile of size and consuming effort. Therefore, Duoduo’s total effort is . It can be proven that is the minimum possible effort.
Input Format
The first line contains an integer , representing the number of fruit piles.
The second line contains integers separated by spaces. The -th integer represents the number of fruits in the -th pile, .
Output Format
Output one line containing one integer, representing the minimum physical effort.
3
1 2 9
15
Hint
[Constraints and Specifications]
This problem uses bundled multi-testpoint evaluation, with four subtasks in total.
- Subtask 1 (10 points): .
- Subtask 2 (20 points): .
- Subtask 3 (30 points): .
- Subtask 4 (40 points): .
For all testdata, it is guaranteed that .
[Hints]
- Please note the impact of constant factors on program efficiency.
- Please use variables of suitable types to store the result of this problem.
- The input size of this problem is large, so please pay attention to input reading efficiency.
Translated by ChatGPT 5