<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:
        # greedy: only perform op if nums[i] > nums[i-1] + 1 (if diff=1, we're just swapping values)
        #   - so just add and subtract diff/2
        #   - BUT greedy wont work bc we may want to keep decreasing even if the num behind grows, because there's a future num even greater we want to decrease
        # KEY INSIGHT 1: the sum never changes
        #   - so we want to distribute the values as evenly as possible accross the array
        #   - if not for the directional restriction of only being able to increase right to left, ans would just be ceil(sum/n)
        #   - our answer will ideally become all 1 num. we want to minimize the diff when this is impossible, but max diff will never exceed 1
        # KEY INSIGHT 2: if we apply op to all nums in a range, the first num is increased, last num is decreased, all other ops cancel out
        #   - therefore we can perform the op on any pair of indicies, not just adjacent ones
        #   - this means we can make any index any value we want, provided nums[-1] isnt increased and nums[0] isnt decreased and the sum remains the same
        #   - nums[0] is the lower bound because it can't be decreased
        # at any i, we can increase all nums before it and decrease all nums after it to any value we want provided sum doesnt change
        # KEY INSIGHT 3: at any i, our choise is to increase the values before it or do nothing
        #   - so at any i, we should redistribute the number backwards if it's greater than the current ans. 
        #   - otherwise, we don't want to increase the nums before
        # strategy: continually make array ~uniform by updating ans to ceil(sum/n) if the current num > ans

        ans = total = nums[0]
        for i in range(1, len(nums)):
            total += nums[i]
            if nums[i] > ans:
                count = i + 1
                ans = max(ans, ceil(total / count))  # we need max here bc ans can't ever go down, even if there's a new better distribution
        return ans

525