CF2185B.Prefix Max

传统题 时间 2000 ms 内存 256 MiB 3 尝试 1 已通过 1 标签

Prefix Max

You are given an array of nn integers a1,a2,,ana_1, a_2, \ldots, a_n.

The value of an array is the sum of the maximums of each prefix of the array. More formally, the value of an array aa is umi=1nmax(a1,,ai)um_{i=1} ^{n} \operatorname{max}(a_1, \ldots, a_i). For example, the value of the array [1,2,11, 2, 1] is $\operatorname{max}(1) + \operatorname{max}(1, 2) + \operatorname{max}(1, 2, 1) = 1 + 2 + 2 = 5$.

You can choose two indices ii and jj and swap elements aia_i and aja_j; this operation can be applied at most one time.

Find the maximum possible value of the array aa after at most one operation.

Input

The first line of the input contains a single integer tt (1t1001 \leq t \leq 100) — the number of test cases.

The first line of each test case contains a single integer nn (2n502 \le n \le 50) — the length of the array aa.

The second line contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (1ai1041 \le a_i \le 10^4) — the array aa.

Output

For each test case, output the maximum possible value of the array aa after the swap has been performed.

Note

For the first test case, we can swap a1a_1 with a4a_4 to get the array [5,1,4,2,35, 1, 4, 2, 3], which has a value of $\operatorname{max}(5) + \operatorname{max}(5, 1) + \operatorname{max}(5, 1, 4) + \operatorname{max}(5, 1, 4, 2) + \operatorname{max}(5, 1, 4, 2, 3) = 25$.

For the second test case, the current value of the array is $\operatorname{max}(5) + \operatorname{max}(5, 1) = 10$. If we were to swap a1a_1 and a2a_2, aa would be equal to [1,51, 5], which has a value of $\operatorname{max}(1) + \operatorname{max}(1, 5) = 6$, meaning the best option is to not perform a swap.

Samples

4
5
2 1 4 5 3
2
5 1
3
3 2 1
2
6 7
25
10
9
14

在线编程 IDE

建议全屏模式获得最佳体验