CF BUDDY
← Problems·

1623C · Balanced Stone Heaps

1600 · binary search, greedy

Problem: There are nn heaps of stone. The ii-th heap has hih_i stones. You want to change the number of stones in the heap by performing the following process once:

  • You go through the heaps from the 33-rd heap to the nn-th heap, in this order.
  • Let ii be the number of the current heap.
  • You can choose a number dd (03dhi0 \le 3 \cdot d \le h_i), move dd stones from the ii-th heap to the (i1)(i - 1)-th heap, and 2d2 \cdot d stones from the ii-th heap to the (i2)(i - 2)-th heap.
  • So after that hih_i is decreased by 3d3 \cdot d, hi1h_{i - 1} is increased by dd, and hi2h_{i - 2} is increased by 2d2 \cdot d.
  • You can choose different or same dd for different operations. Some heaps may become empty, but they still count as heaps.

What is the maximum number of stones in the smallest heap after the process?

Input Format: Each test contains multiple test cases. The first line contains the number of test cases tt (1t21051 \le t \le 2\cdot 10^5). Description of the test cases follows.

The first line of each test case contains a single integer nn (3n21053 \le n \le 2 \cdot 10^5).

The second lines of each test case contains nn integers h1,h2,h3,,hnh_1, h_2, h_3, \ldots, h_n (1hi1091 \le h_i \le 10^9).

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

Output Format: For each test case, print the maximum number of stones that the smallest heap can contain.

Note: In the first test case, the initial heap sizes are [1,2,10,100][1, 2, 10, 100]. We can move the stones as follows.

  • move 33 stones and 66 from the 33-rd heap to the 22-nd and 11 heap respectively. The heap sizes will be [7,5,1,100][7, 5, 1, 100];
  • move 66 stones and 1212 stones from the last heap to the 33-rd and 22-nd heap respectively. The heap sizes will be [7,17,7,82][7, 17, 7, 82].

In the second test case, the last heap is 11, and we can not increase its size.

In the third test case, it is better not to move any stones.

In the last test case, the final achievable configuration of the heaps can be [3,5,3,4,3,3][3, 5, 3, 4, 3, 3].

Sample Cases

Case 1

Input

4
4
1 2 10 100
4
100 100 100 1
5
5 1 1 1 8
6
1 2 3 4 5 6

Output

7
1
1
3

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback