Find maximum sum of subsequence where adjacent elements are at most k apart. dp[i] = max sum ending at i. Transition: dp[i]=nums[i]+max(0,maxj∈[i−k,i−1]dp[j]). The max(0,...) handles starting fresh. Example: [10,2,−10,5,20], k=2. dp=[10,12,2,17,37]. At each step, query window max and add current.
Answer: max(dp)=37 (subsequence 10, 2, 5, 20). Monotonic queue handles the window max in O(1) amortized.