#CF2237C. 鸭子盈余 / C. Duck Surplus

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

鸭子盈余 / C. Duck Surplus

C. Duck Surplus

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

Statement

Ja the Ghost is playing with rubber ducks again! There are nn piles of rubber ducks arranged in a row from left to right. Initially, the ii-th pile contains aia_i rubber ducks.

While the sequence aa is not sorted in nondecreasing order, Ja must perform the following operation:

  • Choose two adjacent piles such that the left pile contains more ducks than the right pile. Ja swaps these two piles, and then adds the number of ducks in the new left pile to the new right pile. Formally, choose an index ii such that 1i<n1\le i \lt n and ai>ai+1a_i \gt a_{i+1}. Then replace the adjacent pair (ai,ai+1)(a_i,a_{i+1}) with (ai+1,ai+ai+1)(a_{i+1},a_i+a_{i+1}).

For example, if two adjacent piles contain 77 and 33 rubber ducks, then after the operation they contain 33 and 1010 rubber ducks.

Ja may choose any index satisfying the condition above at each step. It can be shown that, regardless of his choices, the process eventually ends with the sequence sorted in nondecreasing order.

Ja wants the largest pile at the end of the process to contain as few rubber ducks as possible. Determine the minimum possible value of the largest pile.

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 nn (1n21051 \le n \le 2 \cdot 10^5) — the number of piles.

The second line of each test case contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (1ai1091\le a_i\le 10^9) — the number of ducks in each pile.

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 a single integer — the minimum possible value of the largest pile.

Note

In the transformations below, the two underlined numbers are the adjacent pair just obtained by the operation.

In the first test case, the sequence is already sorted in nondecreasing order. Therefore Ja does not perform any operation, and the answer is 55.

In the second test case, Ja has only one possible operation: $$ [7,3]\to [\underline{3},\underline{10}]. $$ The sequence is then sorted, so the answer is 1010.

In the third test case, Ja can perform the following operations: $$ [3,2,1]\to [\underline{2},\underline{5},1]\to [2,\underline{1},\underline{6}]\to [\underline{1},\underline{3},6]. $$ The largest pile contains 66 ducks. If Ja first chooses the last two piles instead, the final largest pile would contain 77 ducks. Therefore the answer is 66.

In the fourth test case, Ja cannot choose the first two piles at the beginning, because 22 is not greater than 22. One possible process is $$ [2,2,1,3,3]\to [2,\underline{1},\underline{3},3,3]\to [\underline{1},\underline{3},3,3,3]. $$ Thus the answer is 33.

In the fifth test case, one optimal process is $$ [3,1,4,2]\to [\underline{1},\underline{4},4,2]\to [1,4,\underline{2},\underline{6}]\to [1,\underline{2},\underline{6},6]. $$ Therefore the answer is 66.

Examples

10
4
1 2 2 5
2
7 3
3
3 2 1
5
2 2 1 3 3
4
3 1 4 2
5
1 4 3 2 5
6
6 2 5 1 4 3
7
2 7 1 6 3 5 4
8
8 1 7 2 6 3 5 4
5
1000000000 999999999 999999998 999999997 999999996
5
10
6
3
6
14
21
26
36
4999999990