<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:
    int maxResult(vector<int>& nums, int k) {
        map<int, int, std::greater<int>> lastK;
        lastK[nums[0]] = 0;
        for (int i = 1; i < nums.size(); i++) {
            auto itM = lastK.begin();
            cout << " " << itM->second << endl;
            nums[i] = nums[itM->second] + nums[i];
            lastK[nums[i]] = i;

            auto it = lastK.upper_bound(nums[i]);
            cout << it->second << endl;
            lastK.erase(it, lastK.end());

            while (i - lastK.begin()->second >= k) lastK.erase(lastK.begin());
        }

        return nums.back();
    }
    int maxResult0(vector<int>& nums, int k) {
        for (int i = 1; i < nums.size(); i++) {
            int best = INT_MIN;
            for (size_t j = max(0, i-k); j < i; j++) {
                best = max(best, nums[j] + nums[i]);
            }
            nums[i] = best;
        }

        return nums.back();
    }
    int maxResult1(vector<int>& nums, int k) {
        vector<int> dp(nums.size(), INT_MIN);
        dp[0] = nums[0];
        for (int i = 1; i < nums.size(); i++) {
            for (size_t j = max(0, i-k); j < i; j++) {
                dp[i] = max(dp[i], dp[j] + nums[i]);
            }
        }

        return dp.back();
    }
    int maxResult2(vector<int>& nums, int k) {
        long long sum = nums[0];
        size_t n = 1;
        while (n < nums.size()) {
            int m = INT_MIN;
            int mi = -1;
            for (size_t i = n; i < n+k && i < nums.size(); i++) {
                if (m < nums[i]) {
                    m = nums[i];
                    mi = i;
                }
                if (m > 0)
                    break;
            }
            //cout << mi << ":" << m << endl;
            sum += m;
            n = mi+1;
        }

        return sum;
    }
};
100171