CF2124B.Minimise Sum

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

Minimise Sum

This problem differs from problem G. In this problem, you must output the minimum sum of prefix minimums after at most one operation.

You are given an array aa of length nn, with elements satisfying 0ain\boldsymbol{0 \le a_i \le n}. You can perform the following operation at most once:

  • Choose two indices ii and jj such that i<ji \lt j. Set ai:=ai+aja_i := a_i + a_j. Then, set aj=0a_j = 0.

Output the minimum possible value of $\min(a_1) + \min(a_1,a_2) + \ldots + \min(a_1, a_2, \ldots, a_n)$ that you can get.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1t1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains an integer nn (2n21052 \leq n \leq 2\cdot 10^5) — the length of aa.

The following line contains nn space-separated integers a1,a2,,ana_1, a_2, \ldots, a_n (0ain0 \le a_i \le n) — denoting the array aa.

It is guaranteed that the sum of nn over all test cases does not exceed 21052\cdot 10^5.

Output

For each test case, output an integer on a new line, the minimum possible value of $\min(a_1) + \min(a_1,a_2) + \ldots + \min(a_1, a_2, \ldots, a_n)$.

Note

In the second test case, it is optimal to perform the operation with i=2i=2 and j=3j=3.

In the third test case, it is optimal to not perform any operations. The answer is 33.

Samples

3
2
1 2
3
1 2 3
4
3 0 2 3
2
2
3

在线编程 IDE

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