#P1090. [NOIP 2004 提高组] 合并果子

    ID: 90 远端评测题 800ms 125MiB 尝试: 8 已通过: 5 难度: 5 上传者: 标签>贪心20042006USACO二叉堆NOIp 提高组优先队列

[NOIP 2004 提高组] 合并果子

Description

In an orchard, Duoduo has already knocked down all the fruits and separated them into different piles by type. Duoduo decides to merge all the fruits into a single pile.

In each merge, Duoduo can combine two piles of fruits, and the effort spent equals the sum of the weights of the two piles. It is clear that after n1n-1 merges, there will be only one pile left. The total effort spent by Duoduo when merging the fruits equals the sum of the efforts of each merge.

Because it also takes a lot of effort to carry these fruits home, Duoduo wants to save as much effort as possible when merging. Assume each fruit has weight 11, and you are given the number of types of fruits and the count of each type. Your task is to design a merging order that minimizes the total effort and output this minimum effort.

For example, there are 33 types of fruits, with counts 11, 22, and 99. You can first merge the 11 and 22 piles to get a new pile of 33, costing 33 effort. Then merge this new pile with the original third pile to get a new pile of 1212, costing 1212 effort. So the total effort equals =3+12=15=3+12=15. It can be proven that 1515 is the minimum total effort.

Input Format

Two lines. The first line is an integer n(1n10000)n(1\leq n\leq 10000), representing the number of fruit types. The second line contains nn integers separated by spaces. The ii-th integer ai(1ai20000)a_i(1\leq a_i\leq 20000) is the number of fruits of type ii.

Output Format

A single integer, which is the minimum total effort. The input guarantees that this value is less than 2312^{31}.

3 
1 2 9 

15

Hint

Constraints:

  • For 30%30\% of the testdata, n1000n \le 1000.
  • For 50%50\% of the testdata, n5000n \le 5000.
  • For all testdata, n10000n \le 10000.

Translated by ChatGPT 5