<p>You are given three positive integers:&nbsp;<code>n</code>, <code>index</code>, and <code>maxSum</code>. You want to construct an array <code>nums</code> (<strong>0-indexed</strong>)<strong> </strong>that satisfies the following conditions:</p>

<ul>
	<li><code>nums.length == n</code></li>
	<li><code>nums[i]</code> is a <strong>positive</strong> integer where <code>0 &lt;= i &lt; n</code>.</li>
	<li><code>abs(nums[i] - nums[i+1]) &lt;= 1</code> where <code>0 &lt;= i &lt; n-1</code>.</li>
	<li>The sum of all the elements of <code>nums</code> does not exceed <code>maxSum</code>.</li>
	<li><code>nums[index]</code> is <strong>maximized</strong>.</li>
</ul>

<p>Return <code>nums[index]</code><em> of the constructed array</em>.</p>

<p>Note that <code>abs(x)</code> equals <code>x</code> if <code>x &gt;= 0</code>, and <code>-x</code> otherwise.</p>

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

<pre>
<strong>Input:</strong> n = 4, index = 2,  maxSum = 6
<strong>Output:</strong> 2
<strong>Explanation:</strong> nums = [1,2,<u><strong>2</strong></u>,1] is one array that satisfies all the conditions.
There are no arrays that satisfy all the conditions and have nums[2] == 3, so 2 is the maximum nums[2].
</pre>

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

<pre>
<strong>Input:</strong> n = 6, index = 1,  maxSum = 10
<strong>Output:</strong> 3
</pre>

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

<ul>
	<li><code>1 &lt;= n &lt;= maxSum &lt;= 10<sup>9</sup></code></li>
	<li><code>0 &lt;= index &lt; n</code></li>
</ul>

class Solution:
    def maxValue(self, n: int, index: int, maxSum: int) -> int:
        # edge case:
        if n > maxSum:
            return -1 # invalid
        
        res = 1
        adds = 1
        left, right = index, index
        maxSum -= n
        while maxSum - adds >= 0 and adds < n:
            maxSum -= adds
            # right_idx + 1, left_idx - 1
            if right + 1 < n:
                adds += 1
                right += 1
            if left - 1 >= 0:
                adds += 1
                left -= 1
            res += 1
        res += maxSum // adds
        return res

"""
nums
min pos ints -> 1,1,1,1, ...
nums[index] is maximized
4, 2, 6
1, 2, 2, 1

edge case:
n < maxSum

        3
      2   2
1 1 1       1

binary search?
val 1 <= res <= maxSum

if our index is:
(1 + n) * n - n
idx
10 - 6
sep into 3 halves:
res
left_len = min(res, index)
right_len = min(res, n - index)
(res - 1 + res - index) * index / 2 
+ res
+ (res - 1 + res - (n - index - 1)) * (n - index - 1) / 2
+ 1 *  
res-n+index+1 to res-1
n-index-2
2 3 4 3 2
5, 2, ?


(res + res - (index) + 1)

fill all with 1s
then add up to exceed maxSum
4,2,6
1,1,1,1
2
1,1,2,1
1
adding min(len, prev + 2)
as long as nnot exceeding, all possible
"""
393