#P1264. 合并果子

合并果子

Description

    在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆。多多决定把所有的果子合成一堆。

    每一次合并,多多可以把两堆果子合并到一起,消耗的体力等于两堆果子的重量之和。可以看出,所有的果子经过 <math xmlns="http://www.w3.org/1998/Math/MathML">n1 次合并之后, 就只剩下一堆了。多多在合并果子时总共消耗的体力等于每次合并所耗体力之和。

    因为还要花大力气把这些果子搬回家,所以多多在合并果子时要尽可能地节省体力。假定每个果子重量都为 <math xmlns="http://www.w3.org/1998/Math/MathML">1 ,并且已知果子的种类 数和每种果子的数目,你的任务是设计出合并的次序方案,使多多耗费的体力最少,并输出这个最小的体力耗费值。

    例如有 <math xmlns="http://www.w3.org/1998/Math/MathML">3 种果子,数目依次为 <math xmlns="http://www.w3.org/1998/Math/MathML">1 ,<math xmlns="http://www.w3.org/1998/Math/MathML">2 ,<math xmlns="http://www.w3.org/1998/Math/MathML">9 。可以先将 <math xmlns="http://www.w3.org/1998/Math/MathML">1 、<math xmlns="http://www.w3.org/1998/Math/MathML">2 堆合并,新堆数目为<math xmlns="http://www.w3.org/1998/Math/MathML">3 ,耗费体力为<math xmlns="http://www.w3.org/1998/Math/MathML">3 。接着,将新堆与原先的第三堆合并,又得到新的堆,数目为12 。所以多多总共耗费体力 <math xmlns="http://www.w3.org/1998/Math/MathML">=3+12=15 。可以证明 <math xmlns="http://www.w3.org/1998/Math/MathML">15 为最小的体力耗费值。

Input Format

共两行。

第一行是一个整数 <math xmlns="http://www.w3.org/1998/Math/MathML">n(1n10000) ,表示果子的种类数。

第二行包含 <math xmlns="http://www.w3.org/1998/Math/MathML">n 个整数,用空格分隔,第 <math xmlns="http://www.w3.org/1998/Math/MathML">i 个整数 <math xmlns="http://www.w3.org/1998/Math/MathML">ai(1ai20000) 是第 <math xmlns="http://www.w3.org/1998/Math/MathML">i 种果子的数目。

Output Format

一个整数,也就是最小的体力耗费值。输入数据保证这个值小于2<math xmlns="http://www.w3.org/1998/Math/MathML">
3 
1 2 9
15

Hint

对于 <math xmlns="http://www.w3.org/1998/Math/MathML">30% 的数据,保证有 <math xmlns="http://www.w3.org/1998/Math/MathML">n1000

对于 <math xmlns="http://www.w3.org/1998/Math/MathML">50% 的数据,保证有 <math xmlns="http://www.w3.org/1998/Math/MathML">n5000

对于全部的数据,保证有 <math xmlns="http://www.w3.org/1998/Math/MathML">n10000

Source

优先队列