<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) {
        // 1 2 1 2 6 7 5 1 5 4
        // 4 5 9 15 18 13 11 10
        int n = nums.size();
        vector<int> sums;
        int sum = 0;
        for(int i=0;i<k;++i){
            sum+=nums[i];
        }
        sums.push_back(sum);
        for(int i=k;i<n;++i){
            sum+=nums[i]-nums[i-k];
            sums.push_back(sum);
        }
        int m = sums.size();
        vector<pair<int,int>> maximum_left;
        maximum_left.push_back({sums[0],0});
        for(int i=1;i<m;++i){
            int already_max = maximum_left[i-1].first;
            if(sums[i]>already_max){
                maximum_left.push_back({sums[i],i});
            }
            else{
                maximum_left.push_back(maximum_left[i-1]);
            }
        }
        vector<pair<int,int>> maximum_right(m,{0,0});
        maximum_right[m-1] = {sums[m-1],m-1};
        for(int i=m-2;i>=0;--i){
            int already_max = maximum_right[i+1].first;
            cout<<already_max<<endl;
            if(sums[i]>=already_max){
                maximum_right[i] = {sums[i],i};
            }
            else{
                maximum_right[i] = maximum_right[i+1];
            }
        }
        int max_ans = 0;
        vector<int> res_vect={-1,-1,-1};
        for(int i=k;i<m-k;++i){
            int middle = sums[i];
            int left = maximum_left[i-k].first;
            int left_pos = maximum_left[i-k].second;
            int right = maximum_right[i+k].first;
            int right_pos = maximum_right[i+k].second;
            vector<int> cur = {left_pos, i, right_pos};
            if(left+middle+right>max_ans || (left+middle+right==max_ans && cur<res_vect)){
                max_ans = left+middle+right;
                res_vect = cur;
            }
        }
        return res_vect;
    }   
};
29916