<p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p>

<p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i &lt;= k &lt;= j</code>.</p>

<p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p>

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

<pre>
<strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15. 
</pre>

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

<pre>
<strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0
<strong>Output:</strong> 20
<strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
</pre>

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

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 2 * 10<sup>4</sup></code></li>
	<li><code>0 &lt;= k &lt; nums.length</code></li>
</ul>

class Solution:
    def maximumScore(self, nums: List[int], k: int) -> int:
        # # montonix stack
        # n = len(nums)
        # left = [-1] * n
        # st = []
        # for i, x in enumerate(nums):
        #     while st and x <= nums[st[-1]]:
        #         st.pop()
        #     if st:
        #         left[i] = st[-1]
        #     st.append(i)

        # right = [n] * n
        # st.clear()
        # for i in range(n - 1, -1, -1):
        #     x = nums[i]
        #     while st and x <= nums[st[-1]]:
        #         st.pop()
        #     if st:
        #         right[i] = st[-1]
        #     st.append(i)

        # ans = 0
        # for h, l, r in zip(nums, left, right):
        #     if l < k < r:  # 相比 84 题多了这一行
        #         ans = max(ans, h * (r - l - 1))
        # return ans

        
        # assume m -> smallest number between solution (L, R), wts -> always find L and R via expanding ptr via side of i or j that is higher i.e. max(i - 1, j + 1)
        # if i reaches L first, then j < R and nums[i-1] = nums[L-1] < m since if bigger then (L, R) is not optimal
        # now we know also that any number from j + 1 to R >= m , since m is smallest number in [L, R]
        # thus nums[i-1] = nums[L-1] < m <= nums[j + 1]
        # so ptr always go to R -> vice versa if reaches R first. 
        n = len(nums)
        ans = min_h = nums[k]
        i = j = k
        for _ in range(n - 1):
            if j == n - 1 or i and nums[i - 1] > nums[j + 1]:  
                i -= 1
                min_h = min(min_h, nums[i])
            else:
                j += 1
                min_h = min(min_h, nums[j])
            ans = max(ans, min_h * (j - i + 1))
        return ans
 
896