> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Constrained Subsequence Sum Python Solution

> Tested Python solution for LeetCode 1425 with 30 pytest cases. Generate a practice environment with lcpy.

LeetCode 1425, [Hard](/catalog/hard). Topics: [Array](/catalog/topics/array), [Dynamic Programming](/catalog/topics/dynamic-programming), [Queue](/catalog/topics/queue), [Sliding Window](/catalog/topics/sliding-window), [Heap (Priority Queue)](/catalog/topics/heap-priority-queue), Monotonic Queue. [View on LeetCode](https://leetcode.com/problems/constrained-subsequence-sum/description/).

Generate this problem as a practice environment: tested reference solution, 30 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1425   # by problem number
lcpy gen -s constrained_subsequence_sum   # by problem name
```

## Problem

Given an integer array \<code>nums\</code> and an integer \<code>k\</code>, return the maximum sum of a \<strong>non-empty\</strong> subsequence of that array such that for every two \<strong>consecutive\</strong> integers in the subsequence, \<code>nums\[i]\</code> and \<code>nums\[j]\</code>, where \<code>i \< j\</code>, the condition \<code>j - i \<= k\</code> is satisfied.\</p>

\<p>A \<em>subsequence\</em> of an array is obtained by deleting some number of elements (can be zero) from the array, leaving the remaining elements in their original order.

### Examples

```
Input: nums = [10,2,-10,5,20], k = 2
Output: 37
Explanation: The subsequence is [10, 2, 5, 20].
```

```
Input: nums = [-1,-2,-3], k = 1
Output: -1
Explanation: The subsequence must be non-empty, so we choose the largest number.
```

```
Input: nums = [10,-2,-10,-5,20], k = 2
Output: 23
Explanation: The subsequence is [10, -2, -5, 20].
```

### Constraints

* 1 \<= k \<= nums.length \<= 10^5
* -10^4 \<= nums\[i] \<= 10^4

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/constrained_subsequence_sum/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/constrained_subsequence_sum/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from collections import deque


class Solution:
    # Time: O(n)
    # Space: O(k)
    def constrained_subset_sum(self, nums: list[int], k: int) -> int:
        dp = [0] * len(nums)
        window = deque()
        for i, num in enumerate(nums):
            dp[i] = num + (dp[window[0]] if window and dp[window[0]] > 0 else 0)
            while window and dp[window[-1]] <= dp[i]:
                window.pop()
            window.append(i)
            if window[0] <= i - k:
                window.popleft()
        return max(dp)
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(k) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.