#CF2237C. 鸭子盈余 / C. Duck Surplus
鸭子盈余 / 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 piles of rubber ducks arranged in a row from left to right. Initially, the -th pile contains rubber ducks.
While the sequence 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 such that and . Then replace the adjacent pair with .
For example, if two adjacent piles contain and rubber ducks, then after the operation they contain and 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 (). The description of the test cases follows.
The first line of each test case contains () — the number of piles.
The second line of each test case contains integers () — the number of ducks in each pile.
It is guaranteed that the sum of over all test cases does not exceed .
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 .
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 .
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 ducks. If Ja first chooses the last two piles instead, the final largest pile would contain ducks. Therefore the answer is .
In the fourth test case, Ja cannot choose the first two piles at the beginning, because is not greater than . 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 .
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 .
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