Little C has books, each with a weight, and he decides to merge them into a pile.
Each time when Little C merges, he can put one pile of books on top of another to merge them into one pile. If Little C puts the -th pile of books on top of the -th pile of books, the energy Little C needs to consume is the weight of the -th pile of books plus the wear value of the two piles of books.
Initially, each book is in its own pile and the wear values are all . Whenever Little C merges two piles of books, the wear value of the new pile of books formed is twice the larger of the wear values of the two piles of books before merging, plus one. The weight of the new pile of books is the sum of the weights of the two piles of books before merging.
Your task is to design a merging order to minimize the total energy consumption of Little C and output the minimum total energy consumption.
Input Specification
This problem has multiple test data sets.
The first line of the input contains an integers , which represents the number of test data sets.
Then, each set of test data is given as input in order. For each set of test data:
The first line of input contains a positive intege , which represents the number of books.
The second line of input contains positive integers , where represents the weight of the -th book.
Output Specification
For each set of test data, output a line containing an integer, representing the minimum total energy consumption.
Sample Input 1
1
4
1 1 1 1
Sample Output 1
6
Explanation for Sample Output 1
If Little C combines the four books in pairs and then combines the two pairs into one, the energy consumption of the first two merging operations is each, and in the third time, a pile of books with weight of is placed on top of the other pile. The wear value of the two piles of books is each, and the energy consumption is . Therefore, if Little C chooses this plan, the total energy consumption is only . It can be proved that in the above sample, is the minimum total energy consumption.
Additional Samples
Sample inputs and outputs can be found here.
- Sample 2 (
ex_book2.in
andex_book2.ans
). - Sample 3 (
ex_book3.in
andex_book3.ans
). - Sample 4 (
ex_book4.in
andex_book4.ans
).
Constraints
For all test data, it is guaranteed that: .
Test ID | Additional Constraints | |
---|---|---|
None | ||
A | ||
None |
Additional Constraint A: It is guaranteed that .
Comments