<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:
    # [5, 1 , 6, 7]
    # [5, 1, 7, 6]
    # [5, 2, 7, 5]
    # [5, 4, 5, 5]

    # 1, 7
    # 4 
    # [5, 1 , 6, 7] -> [5, 1 , 9, 4] -> [5, 6 , 4, 4] -> [6, 4 , 4, 4] 

    # [5, 1 , 1000, 8] 
    # 1, 1000
    # 500 


    def minimizeArrayValue(self, nums: List[int]) -> int:
        k = max(nums)
        if k == nums[0]:
            return k
         
        n = len(nums)
        # while max(nums) != nums[0]:
        #     val = max(nums)
        #     for i in range(1, n):
        #         if nums[i] == val:
        #             nums[i] -= 1
        #             nums[i-1] += 1

        # return nums[0]
        def check(val): 
            temp = nums[:]
            for i in range(n-1, 0, -1):
                # print(temp)
                if temp[i] > val:
                    temp[i-1] += (temp[i] - val) 
                    temp[i] -= (temp[i] - val)      
            # print(temp)
            return temp[0] > val

        l = 0
        r = max(nums)

        while l < r:
            mid = (l+r)//2
            c = check(mid)
            # print(c, mid, l, r)
            if c:
                l = mid + 1
            else:
                r = mid 
        return l

                


        
1359