QOJ.ac

QOJ

時間限制: 1.5 s 記憶體限制: 512 MB 總分: 100 难度: [顯示]

#6760. Merging Books

统计

Little C has $n$ books, each with a certain weight, and he decides to merge them into one stack.

In each merge operation, Little C can place one stack of books on top of another to combine them into a single stack. If Little C places stack $i$ on top of stack $j$, the physical effort consumed is the weight of stack $i$ plus the sum of the wear values of the two stacks.

Initially, each book forms its own stack with a wear value of 0. Whenever Little C merges two stacks, the wear value of the new stack formed is twice the maximum of the wear values of the two stacks before merging, plus one. The weight of the new stack is the sum of the weights of the two stacks before merging.

Your task is to design a merging sequence to minimize the total physical effort consumed by Little C and output this minimum value.

Input

The input contains multiple test cases. The first line contains a positive integer $t$, representing the number of test cases. For each test case: The first line contains a positive integer $n$, representing the number of books. The second line contains $n$ positive integers, where the $i$-th number $w_i$ represents the weight of the $i$-th book.

Output

For each test case, output a single integer on a new line, representing the minimum physical effort required to merge the $n$ books into one stack.

Examples

Input 1

1
4
1 1 1 1

Output 1

6

Note

If Little C merges the 4 books in pairs and then merges the two resulting stacks into one: The physical effort for the first two merges is 1 each. For the third merge, placing a stack of weight 2 on top of another stack (where both stacks have a wear value of 1), the effort consumed is $2 + 1 + 1 = 4$. Therefore, if this strategy is chosen, the total physical effort consumed by Little C is $1 + 1 + 4 = 6$. It can be proven that in the example above, 6 is the minimum physical effort.

Constraints

For all test cases, it is guaranteed that $1 \le t \le 10$, $1 \le n \le 100$, and $1 \le w_i \le 10^9$.

Test Case ID $n \le$ Special Property
1 ~ 2 7 No
3 11 No
4 13 No
5 ~ 6 22 No
7 ~ 8 28 No
9 ~ 13 50 No
14 60 No
15 70 No
16 80 No
17 ~ 18 100 Yes
19 ~ 20 100 No

Special property: Guaranteed $w_i = 1$.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.