#P9483. [NOI2023] 合并书本

[NOI2023] 合并书本

Problem Description

Xiao C has nn books, each with a weight. He decides to merge them into one pile.

In each merge, Xiao C can put one pile of books on top of another pile, so that they become one pile. If Xiao C puts the ii-th pile onto the jj-th pile, the stamina cost is the weight of the ii-th pile plus the sum of the wear values of the two piles.

Initially, each book forms its own pile and all wear values are 00. Each time Xiao C merges two piles, the new pile’s wear value becomes twice the larger wear value of the two piles plus one, and its weight becomes the sum of the weights of the two piles.

Your task is to design the merging order to minimize Xiao C’s stamina cost, and output this minimum cost.

Input Format

This problem has multiple test cases.

The first line contains a positive integer tt, the number of test cases.

Then for each test case:

The first line contains a positive integer nn, meaning there are nn books.

The second line contains nn positive integers; the ii-th number wiw_i is the weight of the ii-th book.

Output Format

For each test case, output one integer per line, representing the minimum stamina required to merge the nn books into one pile.

1
4
1 1 1 1

6

见附件中的 book/book2.in。
见附件中的 book/book2.ans。
见附件中的 book/book3.in。
见附件中的 book/book3.ans。
见附件中的 book/book4.in。
见附件中的 book/book4.ans。

Hint

[Sample Explanation #1]

If Xiao C first merges the 44 books in pairs, and then merges the two resulting piles into one pile, then the stamina costs of the first two merges are both 11. For the third merge, he puts a pile of weight 22 onto the other pile; both piles have wear value 11, so the stamina cost is 2+1+1=42 + 1 + 1 = 4.

Therefore, under this plan, Xiao C’s total stamina cost is only 1+1+4=61 + 1 + 4 = 6.

It can be proven that, in the above example, 66 is the minimum stamina cost.

[Constraints]

For all test cases: 1t101 \le t \le 10, 1n1001 \le n \le 100, 1wi1091 \le w_i \le 10 ^ 9.

Test Point ID nn \le Special Property
121 \sim 2 77 No
33 1111
44 1313
565 \sim 6 2222
787 \sim 8 2828
9139 \sim 13 5050
1414 6060
1515 7070
1616 8080
171817 \sim 18 100100 Yes
192019 \sim 20 No

Special property: it is guaranteed that wi=1w_i = 1.

Translated by ChatGPT 5