<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 {
public:


    vector<int> v1;
    vector<int> v2;

    int find(int target,int k,int n){

        // find elements from k to 0 for which target is min
        // find elements from k+1 to n for which target is min

        

        int elemt1 =  k+1 - (lower_bound(v1.begin(),v1.end(),target)-v1.begin());
        int elemt2 = v2.size() -  (lower_bound(v2.begin(),v2.end(),target)-v2.begin());

        return target * (elemt1 +elemt2 );


    }

    int maximumScore(vector<int>& nums, int k) {

        stack<int> s;

        int n = nums.size();

        vector<int> minArr(n,-1);

        minArr[k] = nums[k];

        for(int i = k;i>=0;i--){

            while(!s.empty() && nums[i] < s.top()){
                s.pop();
            }
            
            if(s.empty() || nums[i] < s.top())
            s.push(nums[i]);
            minArr[i] = s.top();

        }

        while(!s.empty()) s.pop();

         for(int i = k;i<n;i++){

            while(!s.empty() && nums[i] < s.top()){
                s.pop();
            }
            
            if(s.empty() || nums[i] < s.top())
            s.push(nums[i]);
            minArr[i] = s.top();

        }

        for(int i = 0;i<n;i++){

            cout<<minArr[i]<<" ";

            if(i<=k){
                v1.push_back(minArr[i]);
                
            }else{
                v2.push_back(minArr[i]);
            }
        }

        reverse(v2.begin(),v2.end());
       

       int ans = 0;

         for(int i =0;i<n;i++){
           ans = max(ans , find(minArr[i],k,n));
         }   


        return ans;
        
    }
};

/**

nums = [1,4,3,7,4,5], k = 3

num   = [1,4,3,7,4,5]
minar = [1,3,3,7,4,4]




**/
109000