<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 constrainedSubsetSum(vector<int>& nums, int k) {

        int n=nums.size();
        
        vector<pair<int,int>>v;
        int l=-1;
        int count=0;
        for(int i=0;i<nums.size();i++)
        {
            if(nums[i]<0)
            {
                count++;
                if(l==-1)
                {
                    l=i;
                }
            }
            else
            {
                if(l!=-1)
                {
                    v.push_back({l,i-1});
                    l=-1;
                }
            }
        }
        if(l!=-1)
        {
            v.push_back({l,n-1});
            l=-1;
        }
        if(count==n)
        {
            sort(nums.begin(),nums.end());
            return nums[n-1];
        }


        for(int i=0;i<v.size();i++)
        {
            
            int a=v[i].first;
            int b=v[i].second;cout<<a<<" "<<b<<endl; 
            vector<int>pop(b-a+2,0);
            multiset<int>s;
            s.insert(0);
            int flag=0;
            for(int j=b;j>=a;j--)
            {
                
                    int newnum=*(--s.end())+nums[j];
                    pop[b-a-(b-j)]=newnum;
                    s.insert(newnum);
                    if(s.size()==k+1)
                    {
                    auto it=s.find(pop[b-a-(b-j)+k]);
                    s.erase(it);
                    }
                    

                
                
            }
            int equi=*(--s.end());
            v[i].second=equi;
            cout<<equi<<endl;
        }
        int ans=-1e9+7;
        int sum=0;
        int ind=0;
        for(int i=0;i<nums.size();i++)
        {
            if(nums[i]>=0)
            {
                sum+=nums[i];
                ans=max(ans,sum);
            }
            else 
            {
                if(ind<v.size()&&v[ind].first==i)
                {
                int equi=v[ind].second;
                sum+=equi;ans=max(ans,sum);
                if(sum<0)
                sum=0;
                ind++;
                }
            }
        }
        return ans;
        



















        // for(int i=n-1;i>=0;i--)
        // {
        //     if(nums[i]>=0)
        //     {
        //         if(now!=-1e9+7)
        //         pop.push_back(now);
        //         now=-1e9+7;
        //         pop.push_back(nums[i]);
        //         s.clear();
        //         s.insert(0);
        //     }
        //     else
        //     {
        //         if(s.size()<k)
        //         {s.insert(nums[i]);
        //         now=*s.begin();}
        //         else
        //         {
        //         if(nums[i])
        //         }

        //     }
        // }
        
    }
};
170662