<p>Given an integer array <code>nums</code> and an integer <code>k</code>, find three non-overlapping subarrays of length <code>k</code> with maximum sum and return them.</p>

<p>Return the result as a list of indices representing the starting position of each interval (<strong>0-indexed</strong>). If there are multiple answers, return the lexicographically smallest one.</p>

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

<pre>
<strong>Input:</strong> nums = [1,2,1,2,6,7,5,1], k = 2
<strong>Output:</strong> [0,3,5]
<strong>Explanation:</strong> Subarrays [1, 2], [2, 6], [7, 5] correspond to the starting indices [0, 3, 5].
We could have also taken [2, 1], but an answer of [1, 3, 5] would be lexicographically larger.
</pre>

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

<pre>
<strong>Input:</strong> nums = [1,2,1,2,1,2,1,2,1], k = 2
<strong>Output:</strong> [0,2,4]
</pre>

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

<ul>
	<li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>4</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;&nbsp;2<sup>16</sup></code></li>
	<li><code>1 &lt;= k &lt;= floor(nums.length / 3)</code></li>
</ul>

class Solution {
public:
   
    vector<int> maxSumOfThreeSubarrays(vector<int>& nums, int k) {
        
        int n  = nums.size() ; 

        vector<int> pre(n) ; 
        pre[0] = nums[0] ; 

        for(int j=1 ; j<n ;j++){
            pre[j] = pre[j-1] + nums[j] ; 
        }

        vector<pair<int , int > > dp_1(n) ; 

        for(int j=0 ; j<k-1 ; j++){
            dp_1[j] = {-1 , 0} ; 
        }

        for(int j=k-1 ; j<n ; j++){
            if (j-k+1==0){
                dp_1[j] = {j-k+1 , pre[j]} ; 
            }
            else{
                if (pre[j] - pre[j-k] > dp_1[j-1].second){
                    dp_1[j]  = {j-k+1 , pre[j] - pre[j-k]} ; 
                }
                else{
                    dp_1[j] = dp_1[j-1] ; 
                }
            }
        }

        vector<pair<int , int>> dp_2(n) ; 

        for(int j = n-k+1  ; j<n ; j++){
            dp_2[j] = {-1 , 0} ; 
        }

        for(int j = n-k ; j>=0 ; j--){
            if (j+ k==n){
                
                if (j-1 <0){
                    dp_2[j] = {j , pre[n-1]} ; 
                }
                else{
                    dp_2[j] = { j , pre[n-1] - pre[j-1]} ; 
                }
            }
            else{

                int sum  ; 

                if (j-1 <0){
                    sum = pre[j+ k-1] ; 
                }
                else{
                    sum = pre[j+k-1] - pre[j-1] ; 
                }

                if (sum >=dp_2[j+1].second ){
                    dp_2[j] = {j , sum} ;
                }
                else{
                    dp_2[j] = dp_2[j+1] ; 
                }


            }
        }

        pair<vector<int> , int> p ; 

        p = {{} , 0} ; 

        for(int i = k-1  ; i<n  ; i++){
            pair<int , int> x1 = {-1 , 0} ; 
            pair<int , int> x2  = {-1 , 0} ; 

            if (i-k >=0){
                x1 = dp_1[i-k]  ;
            }

            if (i+1 < n){
                x2 =  dp_2[i+1] ; 
            }

            if (x1.first !=-1 && x2.first !=-1){
                vector<int> v ; 
                v.push_back(x1.first) ; 
                v.push_back(i-k+1) ; 
                v.push_back(x2.first) ; 

                int sum = x1.second  + pre[i] - pre[i-k]  + x2.second ; 

                if (sum > p.second){
                    p = {v , sum} ; 
                }
                else if (sum == p.second){
                
                    if (v < p.first){
                        p = { v , sum} ; 
                    }
                }
            }


        }

        return p.first ; 











            
        
        

    }
};
34370