<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:
    def maxResult(self, nums: List[int], k: int) -> int:

        # # Approach 1
        # # set an array score, where score[i] is the max score to reach index i

        # n = len(nums)
        # score = [0]*n
        # score[0] = nums[0]

        # # need to keep track of at most k previous scores and pick the one with the highest score. >> heap?
        # prev_scores = deque([(nums[0], 0)])

        # for i in range(1, n):
        #     score[i] = nums[i] + prev_scores[0][0]

        #     # because of the post-processing below, the max score will always be located in the front of the queue.
        #     while prev_scores and score[i] > prev_scores[-1][0]:
        #         prev_scores.pop()
            
        #     prev_scores.append([score[i], i])

        #     # now remove the item in front, if that is out of reach k
        #     if prev_scores[0][1] <= i-k:
        #         prev_scores.popleft()

        # return score[-1]

        # Approach 1A
        # Same approach, try using heap instead of pq

        n = len(nums)
        score = [0]*n
        score[0] = nums[0]

        # need to keep track of at most k previous scores and pick the one with the highest score. >> heap?
        prev_scores = [(-nums[0], 0)]
        heapify(prev_scores)

        for i in range(1, n):
            while prev_scores[0][1] < i-k:
                heappop(prev_scores)

            score[i] = nums[i] - prev_scores[0][0]
            heappush(prev_scores, (-score[i], i))

        return score[-1]

        # Approach 2
        # set an array score, where score[i] is the max score to reach the end of the arrary starting at index i

        n = len(nums)
        score = [0]*n
        score[-1] = nums[-1]

        heap = [(-score[-1], n-1)]
        heapify(heap)

        for i in range(n-2, -1, -1):
            while heap[0][1] > i+k:
                heappop(heap)
            score[i] = nums[i] - heap[0][0]
            heappush(heap, (-score[i], i))
        return score[0]

        # n = len(nums)
        # score = [0]*n
        # score[0] = nums[0]
        # prev_score = deque([(nums[0], 0)])   # maintain a window of size k, where the highest score is stored. 
        # # because this is a priority queue, with manipulation the biggest score will be in the front of the queue. 

        # for i in range(1, n):
        #     score[i] = nums[i] + prev_score[0][0]

        #     while prev_score and prev_score[-1][0] < score[i]:
        #         prev_score.pop()
        #     prev_score.append((score[i], i))

        #     if prev_score[0][1] <= i-k:
        #         prev_score.popleft()

        # return score[-1]
826