<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:
        maxNum,maxInd = self.findMax(nums)
        if maxInd == 0:
            return maxNum

        i = 1
        lastBegin = 0

        currMax = maxNum
        while True:
            #print("HEREE")
            while i < len(nums):
                if nums[i] < nums[i-1]:
                    #print("percolating at", lastBegin, i, nums[i], nums[i-1])
                    self.percolateAvg(nums, lastBegin, i)
                    lastBegin = i
                if i == len(nums) - 1 and nums[i] >= nums[i-1]:
                    #print("percolating at", lastBegin, i, nums[i], nums[i-1])
                    self.percolateAvg(nums, lastBegin, i + 1)
                    lastBegin = i
                i += 1
            
            maxNum,maxInd = self.findMax(nums)
            if maxNum < currMax:
                currMax = maxNum
                i = 1
                lastBegin = 0
            else:
                return currMax

        maxNum,maxInd = self.findMax(nums)
        return maxNum

    def findMax(self, nums):
        maxNum = -1
        maxInd = -1
        for i in range(0, len(nums)):
            if nums[i] > maxNum:
                maxNum = nums[i]
                maxInd = i

        return maxNum, maxInd

    def percolateAvg(self, nums, beginning, ending):
        currSum = sum(nums[beginning:ending])
        length = ending - beginning
        base = currSum // length
        leftover = currSum - (base * length)
        #print("BBB", beginning, ending, nums[beginning:ending], currSum, length, base, leftover)
        for i in range(ending - 1, beginning - 1, -1):
            nums[i] = base
            if leftover > 0:
                nums[i] += 1
                leftover -= 1
        #print("NUMS IS NOW", nums)
28100