Problem D
Canvas Painting
After last year’s success, Samuel W. E. R. Craft’s fame continues to grow and now he has funds for all kinds of projects that cross his mind. His newest idea involves creating arrays of canvasses with color patterns having no repeated colors.
Samuel bought a set of white canvasses of varying sizes.
Since painting them manually would take too much time, he
devised a huge machine to automate the painting process.
The painting process works as follows:
-
Assemble all canvasses in a line in the machine’s conveyor belt, disposed in some chosen order.
-
Pick a color $C$ and a number $F$ (which should be less than the number of color $C$ canvasses).
-
Going from left to right, all canvasses with color $C$ are painted. The first $F$ color $C$ canvasses are painted with a new color $X$ and the remaining color $C$ canvasses are painted with a new color $Y$. Colors $X$ and $Y$ are selected by the machine, are distinct, and are different from any color used previously. The amount of ink spent in this step is equal to the sum of the sizes of the painted canvasses.
-
Repeat 2) and 3) until all canvasses have distinct colors.
Consider for example that Samuel bought four canvasses of sizes $3$, $5$, $5$ and $7$. The following picture shows $2$ different options for painting them.
Task
Given the sizes of the canvasses Samuel bought, can you help Samuel finding the minimum amount of ink the machine needs to spend in order to have all canvasses with different colors?
Input
The first line consists of a single integer $T$, the number of test cases. Each test case is composed by two lines. The first line consists of a single integer $N$ representing the number of canvasses. The next line contains $N$ space separated integers representing the sizes of the canvasses.
Constraints
$1 \leq T \leq 100$ |
Number of test cases. |
$1 \leq N_ i \leq 100\, 000$ |
Number of canvasses in the $i^{th}$ test case. |
$1 \leq s \leq 100\, 000$ |
Size of each canvas. |
$1 \leq \sum _{i=1}^{T} N_ i \leq 100\, 000$ |
Number of canvasses over all test cases in one test file. |
Output
The output contains $T$ lines, one for each test case: the minimum amount of ink the machine needs in order to have all canvasses with different colors.
Sample Input 1 | Sample Output 1 |
---|---|
2 3 7 4 7 4 5 3 7 5 |
29 40 |