<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>

#define ll long long
#define pb push_back
class Solution {
public:
    ll f(ll index,ll cnt,vector<vector<ll>>& dp,vector<ll>& pref,vector<vector<pair<ll,ll>>>& store,ll k,ll n){
        if(dp[index][cnt]!=-1){
            return dp[index][cnt];
        }
        if(cnt==0){
            return dp[index][cnt]=0;
        }
        if(index==n){
            return dp[index][cnt]=-1e11;
        }
        ll b = -1;
        ll a = f(index+1,cnt,dp,pref,store,k,n);
        if(index+k<n) b = f(index+k,cnt-1,dp,pref,store,k,n)+pref[index+k]-pref[index];
        if(b>=a){
            store[index][cnt]={index+k,cnt-1};
        }else{
             store[index][cnt]={index+1,cnt};
        }
        return dp[index][cnt]=max(a,b);
    }
    vector<int> maxSumOfThreeSubarrays(vector<int>& arr, int k) {
        ll n = arr.size();

        vector<vector<ll>>dp(n+2,vector<ll>(4,-1));
        vector<vector<pair<ll,ll>>>store(n+2,vector<pair<ll,ll>>(4));
        vector<ll>pref(n+1);
        for(ll i=1;i<=n;i++){
            pref[i]=pref[i-1]+arr[i-1];
        }


        ll ans = f(0,3,dp,pref,store,k,n+1);

        cout<<ans<<endl;
        vector<int>poke;
        ll num = 3;
        ll indi = 0;
        while(num>0){
            ll indi2 = store[indi][num].first;
            ll num2 = store[indi][num].second;
            // cout<<indi2<<" "<<num2<<endl;
            if(num2<num){
                poke.pb(indi);
            }
            num=num2;
            indi=indi2;
        }
        return poke;

    }
};
47095