<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:
    static bool cmp(pair<int, int>p1, pair<int, int>p2){
        if(p1.first > p2.first)return false;
        if(p1.first < p2.first)return true;
        if(p1.second < p2.second)return false;
        return true;
    }
    vector<int> maxSumOfThreeSubarrays(vector<int>& nums, int k) {
        int n = nums.size();
        if(k == 1){
            vector<pair<int, int>>vp;
            for(int i = 0; i < n; i++)vp.push_back({nums[i], i});
            sort(vp.begin(), vp.end(), cmp);
            vector<int>ans;
            ans = {vp[n - 1].second, vp[n - 2].second, vp[n - 3].second};
            // for(auto x : vp)cout<<x.first<<" "<<x.second<<endl;
            sort(ans.begin(), ans.end());
            return ans;
        }
        

        for(int i = 1; i < n; i++)nums[i] += nums[i - 1];

        vector<vector<int>>dp(n, vector<int>(3, -1));

        for(int i = 0; i < n; i++){
            if(i < k - 1)continue;
            
            int a = nums[i] - (i == k - 1 ? 0 : nums[i - k]);
            int b = dp[i - 1][0];

            if(a > b){
                dp[i][0] = a;
            }
            else{
                dp[i][0] = b; 
            } 

            if(i < (2 * k) - 1)continue;

            a = nums[i] - nums[i - k] + dp[i - k][0];

            b = dp[i - 1][1];

            if(a > b)dp[i][1] = a;
            else dp[i][1] = b;

            if(i < (3 * k) - 1)continue;

            a = nums[i] - nums[i - k] + dp[i - k][1];

            b = dp[i - 1][2];

            if(a > b)dp[i][2] = a;
            else dp[i][2] = b;
        }
        // for(int i = 0; i < n; i++)cout<<dp[i][0]<<" ";
        // cout<<endl;
        // for(int i = 0; i < n; i++)cout<<dp[i][1]<<" ";
        // cout<<endl;
        // for(int i = 0; i < n; i++)cout<<dp[i][2]<<" ";

        // return {};
        int j = 2;
        vector<int>ans;
        for(int i = n - 1; i >= 0 && j >= 0; i--){
            if(dp[i][j] != dp[i - 1][j]){
                ans.push_back(i - k + 1);
                i-=(k - 1);
                j--;
            }
        }
        reverse(ans.begin(), ans.end());
        return ans;
    }
};
31825