<p>You are given a <strong>0-indexed</strong> array <code>nums</code> comprising of <code>n</code> non-negative integers.</p>

<p>In one operation, you must:</p>

<ul>
	<li>Choose an integer <code>i</code> such that <code>1 &lt;= i &lt; n</code> and <code>nums[i] &gt; 0</code>.</li>
	<li>Decrease <code>nums[i]</code> by 1.</li>
	<li>Increase <code>nums[i - 1]</code> by 1.</li>
</ul>

<p>Return<em> the <strong>minimum</strong> possible value of the <strong>maximum</strong> integer of </em><code>nums</code><em> after performing <strong>any</strong> number of operations</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [3,7,1,6]
<strong>Output:</strong> 5
<strong>Explanation:</strong>
One set of optimal operations is as follows:
1. Choose i = 1, and nums becomes [4,6,1,6].
2. Choose i = 3, and nums becomes [4,6,2,5].
3. Choose i = 1, and nums becomes [5,5,2,5].
The maximum integer of nums is 5. It can be shown that the maximum number cannot be less than 5.
Therefore, we return 5.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [10,1]
<strong>Output:</strong> 10
<strong>Explanation:</strong>
It is optimal to leave nums as is, and since 10 is the maximum value, we return 10.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == nums.length</code></li>
	<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution:
    def minimizeArrayValue(self, nums: List[int]) -> int:
        n = len(nums)
        # 猜的是操作后数组最大值的最小可能的值
        # 制约的条件是看能不能通过加减操作将数组的最大值控制在这个猜的值以内
        # 从后往前是因为进行操作的对当前的数进行减操作的时候需要对前一个数进行加操作，会逆向往0坐标方向继续滚动，所以反向的话就可以做到一次遍历就能全滚好
        def check(target):
            carry = 0
            for i in range(n - 1, 0, -1):
                if nums[i] + carry > target:
                    # 多出来的这部分需要带着往前看能不能消掉
                    carry = nums[i] + carry - target
                else:
                    # 可以消掉就重置carry, 没有负担了
                    carry = 0
            # 最后检查一下第一个数 + carry 是否 <= target
            # 注意第一个数只可能增加不可能减少，也就是它可以作为第二个数的减少操作的受益者
            return nums[0] + carry <= target
            
        left = nums[0]
        right = max(nums)

        while left <= right:
            mid = (left + right) // 2
            if check(mid):
                right = mid - 1
            else:
                left = mid + 1
        return right + 1



        
930