“[Riddle] Balanced Stone Heaps solution codeforces”

There are 𝑛n heaps of stone. The 𝑖i-th heap has 𝑖hi 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 𝑛n-th heap, in this order.
  • Let 𝑖i be the number of the current heap.
  • You can choose a number 𝑑d (03𝑑𝑖0≤3⋅d≤hi), move 𝑑d stones from the 𝑖i-th heap to the (𝑖1)(i−1)-th heap, and 2𝑑2⋅d stones from the 𝑖i-th heap to the (𝑖2)(i−2)-th heap.
  • So after that 𝑖hi is decreased by 3𝑑3⋅d𝑖1hi−1 is increased by 𝑑d, and 𝑖2hi−2 is increased by 2𝑑2⋅d.
  • You can choose different or same 𝑑d for different operations. Some heaps may become empty, but they still count as heaps.
1 https://wordpress.com 92 88
2 https://www.weebly.com 93 83
3 https://www.wix.com 94 86
4 https://sites.google.com 98 86
5 https://www.tumblr.com

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


“[Riddle] Balanced Stone Heaps solution codeforces”

Each test contains multiple test cases. The first line contains the number of test cases 𝑡t (1𝑡21051≤t≤2⋅105). Description of the test cases follows.

The first line of each test case contains a single integer 𝑛n (3𝑛21053≤n≤2⋅105).

The second lines of each test case contains 𝑛n integers 1,2,3,,𝑛h1,h2,h3,…,hn (1𝑖1091≤hi≤109).

It is guaranteed that the sum of 𝑛n over all test cases does not exceed 21052⋅105.


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


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


“[Riddle] Balanced Stone Heaps solution codeforces”

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].

Leave a Comment

Your email address will not be published.