<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>k</code>.</p>

<p>You are initially standing at index <code>0</code>. In one move, you can jump at most <code>k</code> steps forward without going outside the boundaries of the array. That is, you can jump from index <code>i</code> to any index in the range <code>[i + 1, min(n - 1, i + k)]</code> <strong>inclusive</strong>.</p>

<p>You want to reach the last index of the array (index <code>n - 1</code>). Your <strong>score</strong> is the <strong>sum</strong> of all <code>nums[j]</code> for each index <code>j</code> you visited in the array.</p>

<p>Return <em>the <strong>maximum score</strong> you can get</em>.</p>

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

<pre>
<strong>Input:</strong> nums = [<u>1</u>,<u>-1</u>,-2,<u>4</u>,-7,<u>3</u>], k = 2
<strong>Output:</strong> 7
<strong>Explanation:</strong> You can choose your jumps forming the subsequence [1,-1,4,3] (underlined above). The sum is 7.
</pre>

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

<pre>
<strong>Input:</strong> nums = [<u>10</u>,-5,-2,<u>4</u>,0,<u>3</u>], k = 3
<strong>Output:</strong> 17
<strong>Explanation:</strong> You can choose your jumps forming the subsequence [10,4,3] (underlined above). The sum is 17.
</pre>

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

<pre>
<strong>Input:</strong> nums = [1,-5,-20,4,-1,3,-6,-3], k = 2
<strong>Output:</strong> 0
</pre>

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

<ul>
	<li><code>1 &lt;= nums.length, k &lt;= 10<sup>5</sup></code></li>
	<li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
</ul>

class Solution {
public:
    vector<int>dp;
    int findingMaxSum(int i,vector<int>&arr,int k){
        if(i==arr.size()-1) return arr[arr.size()-1];
        if(i>=arr.size()) return INT_MIN;
        if(dp[i]!=INT_MIN) return dp[i];
        int sum=INT_MIN;
        for(int j=1;j<=k;j++){
            int temp=findingMaxSum(i+j,arr,k);
            if(temp!=INT_MIN)sum=max(sum,temp+arr[i]);
        }
        return dp[i]=sum;
    }
    int maxResult(vector<int>& nums, int k) {
        int n=nums.size();

        //dp.resize(n+1,INT_MIN);
        //return findingMaxSum(0,nums,k);
        // dp[n-1]=nums[n-1];
        // for(int i=n-2;i>=0;i--){
        //     int sum=INT_MIN;
        //     for(int j=1;j<=k && (i+j)<=n;j++){
        //         int temp=dp[i+j];
        //         if(temp!=INT_MIN)sum=max(sum,temp+nums[i]);
        //     }
        //     dp[i]=sum;
        // }
        // return dp[0];
        multiset<int>st;
        st.insert(nums[0]);
        vector<int>dp(n,INT_MIN);
        int ans=0;
        dp[0]=nums[0];
        for(int i=1;i<n;i++){
            if(i>k){
                st.erase(st.find(dp[i-k-1]));
            }
            dp[i]=*rbegin(st)+nums[i];
            st.insert(dp[i]);
        }
        return dp[n-1];
    }
};
113240