#CF2237A. 摧毁高塔 / A. Destroying Towers

    ID: 18575 传统题 2000ms 512MiB 尝试: 0 已通过: 0 难度: (无) 上传者: 标签>CodeforcesOrder Capital Round 2 (Codeforces Round 1104, Div. 1 + Div. 2)

摧毁高塔 / A. Destroying Towers

A. Destroying Towers

Source: Codeforces 2237A
Contest: Order Capital Round 2 (Codeforces Round 1104, Div. 1 + Div. 2)
Time limit: 1 second
Memory limit: 256 megabytes

Statement

Quack the Duck has returned to his homeland and found nn towers standing in a line. The height of the ii-th tower is aia_i. He wants vengeance for the destruction of his ecosystem, and has vowed to wreak as much havoc as possible with his laser gun.

Quack will operate on each tower exactly once, in any order he chooses. The operation on tower ii is as follows:

  • Quack climbs to the top of tower ii and shoots a laser to the right, cutting the first taller tower it hits down to the same height as tower ii. Formally, let jj be the smallest index such that j>ij \gt i and aj>aia_j \gt a_i, where aia_i and aja_j are the current heights of the towers. If such jj exists, then aja_j is replaced with aia_i. Otherwise, nothing happens.

Find the minimum possible final sum of tower heights over all possible orders of operations.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1t5001 \le t \le 500). The description of the test cases follows.

The first line of each test case contains an integer nn (1n1001\le n\le 100) — the number of towers.

The following line contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (1ai10001\le a_i\le 1000) — the heights of the towers.

Output

For each test case, output a single integer — the minimum possible final sum of tower heights over all possible orders of operations.

Note

In the first test case, one optimal order is 3,1,23,1,2. The heights change as follows:

[1,3,5][1,3,5][1,1,5][1,1,1].[1,3,5]\to [1,3,5]\to [1,1,5]\to [1,1,1].

Thus the final sum is 1+1+1=31+1+1=3.

In the second test case, no operation can change any tower. For every tower, there is no higher tower to its right. Therefore the final heights remain [5,4,3][5,4,3], and the answer is 5+4+3=125+4+3=12.

In the third test case, one optimal order is 4,1,3,24,1,3,2. The heights change as follows:

$$[3,2,5,1]\to [3,2,5,1]\to [3,2,3,1]\to [3,2,3,1]\to [3,2,2,1].$$

Therefore the final sum is 3+2+2+1=83+2+2+1=8.

Examples

10
3
1 3 5
3
5 4 3
4
3 2 5 1
4
2 1 4 3
5
4 1 3 5 2
5
2 2 3 1 4
1
7
6
6 1 5 2 4 3
4
1 1 1 1
5
10 3 8 6 9
3
12
8
5
8
8
7
11
4
22