In an orchard, has a lot to all of the fruit of the play down, and in accordance with the different kinds of fruit into different pile, many decided to synthesize all the fruit of a pile,
Every merger, many can be stacked two fruit together, the consumption of physical strength is equal to the sum of the weight of the two piles of fruit, it can be seen that after merger n - 1, leaves a pile, the combination of fruit when the total consumption of physical strength is equal to the sum of each energy consumption by the merger of,
Because moved back home take the great effort to put these fruit, so a lot of fruit in merger to save energy as much as possible, when you assume that every fruit weight is 1, and fruit known species number and the number of each kind of fruit, your task is to design the order of the merger plan, minimize the cost a lot of physical strength, and output the minimum energy cost value,
If there are three kinds of fruit, the number of 1,2,9, can put 1, 2, merger, a new pile number 3, exhausting to 3, then, will the new pile of merged with the original third pile, and get a new heap, number of 12, exhausting of 12, so the total energy consuming a lot=3 + 12=15, 15 for minimum energy cost value, can prove that
Input
Enter the first behavior integer M, said there are M case (test case),
Then each case contains 2 lines, the first integer n (1 & lt;=n<=10000), said the types of fruit number, the second line contains n integers, separated by Spaces, respectively has each kind of fruit, the number of
The Output
In each case the output minimum energy cost value
The Sample Input Copy
2
3
1, 2, 9
4
1 2 September 4
The Sample Output Copy
15
26
#include
#include
Void the sort (int a [], int n)
{
Int I, temp;
for(i=0; I
If (a [I] {
Temp=a, [I].
A [I]=a, [I + 1].
A=\ [I + 1];
}
}
}
Int main ()
{
Int N, N, a [10001], s, m;
The scanf (" % d ", & amp; N);
While (N -)
{
The scanf (" % d ", & amp; N);
for(int i=0; I
S=0;
While (n> 1)
{
for(int i=0; I<2; I++)
Sort (a, n);
A [n - 2)=a (n - 1) + a (n - 2),
S +=a [n - 2);
n--;
}
Printf (" % d \ n ", s);
}
return 0;
}