Balanced Stone Heaps
Contest: Codeforces Round 763 (Div. 2)
Difficulty rating: 1600
Tags: binary search, greedy

There are $$$n$$$ heaps of stone. The $$$i$$$-th heap has $$$h_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 $$$3$$$-rd heap to the $$$n$$$-th heap, in this order.
- Let $$$i$$$ be the number of the current heap.
- You can choose a number $$$d$$$ ($$$0 \le 3 \cdot d \le h_i$$$), move $$$d$$$ stones from the $$$i$$$-th heap to the $$$(i - 1)$$$-th heap, and $$$2 \cdot d$$$ stones from the $$$i$$$-th heap to the $$$(i - 2)$$$-th heap.
- So after that $$$h_i$$$ is decreased by $$$3 \cdot d$$$, $$$h_{i - 1}$$$ is increased by $$$d$$$, and $$$h_{i - 2}$$$ is increased by $$$2 \cdot d$$$.
- You can choose different or same $$$d$$$ 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 $$$t$$$ ($$$1 \le t \le 2\cdot 10^5$$$). Description of the test cases follows.

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

The second lines of each test case contains $$$n$$$ integers $$$h_1, h_2, h_3, \ldots, h_n$$$ ($$$1 \le h_i \le 10^9$$$).

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

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

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

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

- move $$$3$$$ stones and $$$6$$$ from the $$$3$$$-rd heap to the $$$2$$$-nd and $$$1$$$ heap respectively. The heap sizes will be $$$[7, 5, 1, 100]$$$;
- move $$$6$$$ stones and $$$12$$$ stones from the last heap to the $$$3$$$-rd and $$$2$$$-nd heap respectively. The heap sizes will be $$$[7, 17, 7, 82]$$$.

In the second test case, the last heap is $$$1$$$, 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]$$$.

Example 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

Example output:
7
1
1
3

Editorial:
The answer can be binary searched. That is, we can find the biggest number $$$x$$$, such that we can make all heap having at least $$$x$$$ stones. So our job here is to check if we can satisfy the said condition with a number $$$x$$$.
Let's consider a reversed problem: we go from $$$1$$$ to $$$n - 2$$$, pick a number $$$d$$$ ($$$0 \le 3\cdot d \le h_i$$$), move $$$d$$$ and $$$2\cdot d$$$ stones from the $$$i$$$-th heap to the $$$i + 1$$$ heap and $$$i + 2$$$-th heap respectively. In this problem, we can greedily move the stones: since $$$x$$$ is the minimum required stones, if at some moment, we have $$$h_i < x$$$, then we can not satisfy the condition for $$$x$$$ anymore. Otherwise, it is always the best to use as many stones as we can, that is, choose $$$d = \left \lfloor \frac {h_i - x} 3 \right \rfloor$$$, and move $$$d$$$ and $$$2\cdot d$$$ stones to the $$$i + 1$$$ and $$$i + 2$$$ heaps respectively. In the end, if all the heap are not less than $$$x$$$, we can conclude that we can make all heaps having not less than $$$x$$$ stones with this process.
Going back to our original problem, it seems like we can solve it by doing the process in the reversed order, as discussed above. But there is a catch! The number of stones that can be moved must not exceed the number of the stones in the original heap! So, if we call $$$h_i$$$ — the original heap size, and $$$h'_i$$$ — the current modified heap size, then the number of stones that we should move is $$$d = \left \lfloor \frac {\min \{ h_i, h'_i - x \}} 3 \right\rfloor $$$ at each step.