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

    int maxResult(vector<int>& nums, int k) {
        cache = vector<int>(nums.size(), -1e9);
        return solve(nums, k, 0);
        // return solveWithDP(nums, k);
    }

    int solveWithDP(vector<int>& nums, int k) {
        vector<int> dp(nums.size(), -1e9);
        dp[nums.size() - 1] = nums[nums.size() - 1];
        for (int idx = nums.size() - 2; idx >= 0; idx--) {
            for (int i = idx + 1; i <= min((int)nums.size() - 1, idx + k); i++) {
                if (nums[i] >= 0) {
                    dp[idx] = nums[idx] + dp[i];
                    break;
                }
            }

            vector<int> st;
            for (int i = idx + 1; i <= min((int)nums.size() - 1, idx + k); i++) {
                if (st.empty() || nums[i] < nums[st.back()])
                    st.push_back(i);
                else {
                    int numIdx = st.back(); st.pop_back();
                    while (!st.empty() && nums[i] > nums[numIdx]) {
                        numIdx = st.back(); st.pop_back();
                    }
                    st.push_back(i);
                }
            }

            while (!st.empty()) {
                int i = st.back(); st.pop_back();
                dp[idx] = max(dp[idx], nums[idx] + dp[i]);
            }
        }

        return dp[0];
    }

    int solve(vector<int>& nums, int k, int idx) {
        if (idx == nums.size() - 1) return nums[idx];

        int i = idx;
        while (i < min((int)nums.size() - 1, idx + k))
            if (nums[++i] >= 0) {
                if (cache[i] == -1e9) cache[i] = solve(nums, k, i);
                return nums[idx] + cache[i];
            }
                
        vector<int> st;
        for (int i = idx + 1; i <= min((int)nums.size() - 1, idx + k); i++)
            if (st.empty() || nums[i] < nums[st.back()])
                st.push_back(i);
            else {
                int numIdx = st.back(); st.pop_back();
                while (!st.empty() && nums[i] > nums[numIdx]) {
                    numIdx = st.back(); st.pop_back();
                }
                st.push_back(i);
            }

        int ans = -1e9;
        while (!st.empty()) {
            int i = st.back(); st.pop_back();
            if (cache[i] == -1e9) cache[i] = solve(nums, k, i);
            ans = max(ans, nums[idx] + cache[i]);
        }

        return ans;
    }
};
109396