<p>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 &lt; j</code>, the condition <code>j - i &lt;= 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.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [10,2,-10,5,20], k = 2
<strong>Output:</strong> 37
<b>Explanation:</b> The subsequence is [10, 2, 5, 20].
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [-1,-2,-3], k = 1
<strong>Output:</strong> -1
<b>Explanation:</b> The subsequence must be non-empty, so we choose the largest number.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [10,-2,-10,-5,20], k = 2
<strong>Output:</strong> 23
<b>Explanation:</b> The subsequence is [10, -2, -5, 20].
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= k &lt;= nums.length &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 getResultFromIdx(vector<int> &nums, vector<int> &idxs, int k,int lastIdx) {
        if(!idxs.size()) return 0;
        if(idxs.size() == 1) return nums[idxs[0]];
        
        int result = 0;
        if(idxs.size() > 1) result += nums[idxs[idxs.size()-1]];

        for(int i=0;i<idxs.size()-1;i++) {
            if(idxs[i+1] - lastIdx <= k) continue;
            lastIdx = idxs[i];
            result += nums[idxs[i]];
        }


        return result;
    }

    // Processes vector of all negative integers
    int resultantNagativeFactor(vector<int> &nums,int start, int end, int k) {
        auto cmp = [](const pair<int, int>& lhs, const pair<int, int>& rhs) {
        return lhs.first == rhs.first ? lhs.second < rhs.second : lhs.first < rhs.first;
        };
        priority_queue<pair<int,int>,vector<pair<int,int>>,decltype(cmp)> Store;
        int lastVis=start-1, idx=start;
        vector<int> resultIdx;

        while(idx<=end + 1) {
            if(idx - lastVis > k) {
                while(Store.size() && Store.top().second <= lastVis) Store.pop();
                if(Store.size()) {
                    cout<<Store.top().first<<" "<<Store.top().second<<"   "<<idx<<" "<<lastVis<<endl;
                    resultIdx.emplace_back(Store.top().second);
                    lastVis = Store.top().second;
                    Store.pop();
                }
            }
            
            if(idx == end+1) break;

            Store.push({nums[idx],idx});
            idx++;
        }

        return getResultFromIdx(nums, resultIdx, k, start-1);
    }

    int constrainedSubsetSum(vector<int>& nums, int k) {
        int maxSum = -1e9-1, sum = 0,idx = 0;

        for(auto n:nums) maxSum = max(maxSum,n);

        while(idx < nums.size()) {
            int val = nums[idx];
            if(val < 0) {
                int start = idx;
                while(idx < nums.size() && nums[idx] < 0) idx++;
                idx--;
                val = resultantNagativeFactor(nums,start,idx,k);
            }


            sum += val;
            // cout<<sum<<" "<<val<<endl;
            maxSum = max(maxSum, sum);
            if(sum < 0) sum = 0; 

            idx++;
        }

        return maxSum;
    }
};
137762