C++ | Dp | Binary Search | Sliding Window
i) First create all ranges i.e take all values as starting points and again all of them as ending points using sliding window. Store all these ranges and their 
i) First create all ranges i.e take all values as starting points and again all of them as ending points using sliding window. Store all these ranges and their sums.\nii) Sort the values according to starting index and apply dp to choose 2 non-overlapping intervals with maximum sum. Use Binary search to avoid overlapping intervals. Note here we consider all non-overlapping ranges whose starting point is greater than ending point of given range.\niii) Sort the values according to ending index and apply dp to choose 2 non-overlapping intervals with maximum sum. Use Binary search to avoid overlapping intervals. Note here we consider all non-overlapping ranges whose ending point is smaller than starting point of given range.\n```\nclass Solution {\npublic:\n    static bool sortcol(const vector<long>& v1, const vector<long>& v2){\n    return v1[1] > v2[1];\n    }\n    int maxiW1(vector<vector<long>>& pq,int k,int ind,vector<vector<int>>& dp1,vector<long>& v1){\n        if(k==0)\n            return 0;\n        if(ind==pq.size())\n            return 0;\n        if(dp1[ind][k]!=-1)\n            return dp1[ind][k];\n        int tk=0;\n        int ntk=0;\n        if(k==2){\n            int t=upper_bound(v1.begin(),v1.end(),pq[ind][1])-v1.begin();\n            tk=pq[ind][2]+maxiW1(pq,k-1,t,dp1,v1);\n            ntk=maxiW1(pq,k,ind+1,dp1,v1);\n        }\n        else if(k==1){\n            tk=pq[ind][2]+maxiW1(pq,0,ind+1,dp1,v1);\n            ntk=maxiW1(pq,k,ind+1,dp1,v1);\n        }\n        return dp1[ind][k]=max(tk,ntk);\n    }\n    int maxiW2(vector<vector<long>>& pq1,int k,int ind,vector<vector<int>>& dp2,vector<long>& v2){\n        if(k==0)\n            return 0;\n        if(ind==pq1.size())\n            return 0;\n        if(dp2[ind][k]!=-1)\n            return dp2[ind][k];\n        int tk=0;\n        int ntk=0;\n        if(k==2){\n            int t=upper_bound(v2.begin(),v2.end(),pq1[ind][1],greater<long>())-v2.begin();\n            tk=pq1[ind][2]+maxiW2(pq1,k-1,t,dp2,v2);\n            ntk=maxiW2(pq1,k,ind+1,dp2,v2);\n        }\n        else if(k==1){\n            tk=pq1[ind][2]+maxiW2(pq1,0,ind+1,dp2,v2);\n            ntk=maxiW2(pq1,k,ind+1,dp2,v2);\n        }\n        return dp2[ind][k]=max(tk,ntk);\n    }\n    int maximizeWin(vector<int>& pp, int k) {\n        vector<vector<long>>pq;\n        map<long,long>m1;\n        int i;\n        for(i=0;i<pp.size();i++)\n            m1[pp[i]]++;\n        long start=pp[0]-k;\n        long end=pp[0];\n        long sum=0;\n        sum+=m1[end];\n        vector<long>v2,v4;\n        v2.push_back(start);\n        v2.push_back(end);\n        v2.push_back(sum);\n        pq.push_back(v2);\n        long t2=end;\n        int ind=upper_bound(pp.begin(),pp.end(),end)-pp.begin();\n        if(ind==pp.size())\n            return sum;\n        long temp=start;\n        while(ind<pp.size()){\n            end=pp[ind];\n            start=end-k;\n            while(temp<start){\n                sum=sum-m1[temp];\n                int ind1=upper_bound(pp.begin(),pp.end(),temp)-pp.begin();\n                temp=pp[ind1];\n            }\n            sum+=m1[end];\n            vector<long>v3;\n            v3.push_back(start);\n            v3.push_back(end);\n            v3.push_back(sum);\n            pq.push_back(v3);\n            ind=upper_bound(pp.begin(),pp.end(),end)-pp.begin();\n        }\n        reverse(pp.begin(),pp.end());\n        start=pp[0]+k;\n        end=pp[0];\n        sum=0;\n        sum+=m1[end];\n        v4.push_back(end);\n        v4.push_back(start);\n        v4.push_back(sum);\n        pq.push_back(v4);\n        ind=upper_bound(pp.begin(),pp.end(),end,greater<int>())-pp.begin();\n        temp=start;\n        while(ind<pp.size()){\n            end=pp[ind];\n            start=end+k;\n            while(temp>start){\n                sum=sum-m1[temp];\n                int ind1=upper_bound(pp.begin(),pp.end(),temp,greater<int>())-pp.begin();\n                temp=pp[ind1];\n            }\n            sum+=m1[end];\n            vector<long>v3;\n            v3.push_back(end);\n            v3.push_back(start);\n            v3.push_back(sum);\n            pq.push_back(v3);\n            ind=upper_bound(pp.begin(),pp.end(),end,greater<int>())-pp.begin();\n        }\n        sort(pq.begin(),pq.end());\n        vector<vector<long>>pq1(pq.size(),vector<long>(3));\n        vector<long>v1,v5;\n        for(i=0;i<pq.size();i++){\n            v1.push_back(pq[i][0]);\n            pq1[i][0]=pq[i][1];\n            pq1[i][1]=pq[i][0];\n            pq1[i][2]=pq[i][2];\n        }\n        sort(pq1.begin(),pq1.end(),sortcol);\n        for(i=0;i<pq.size();i++)\n            v5.push_back(pq1[i][0]);\n        vector<vector<int>>dp1(pq.size()+1,vector<int>(3,-1));\n        vector<vector<int>>dp2(pq.size()+1,vector<int>(3,-1));\n        int ans1=maxiW1(pq,2,0,dp1,v1);\n        int ans2=maxiW2(pq1,2,0,dp2,v5);\n        return max(ans1,ans2);\n    }\n};\n```
['Dynamic Programming', 'C', 'Sliding Window', 'Binary Tree']