C++ || O(NlogN) || DP + Binary Search || Priority Queue || Sorting
Connect with me on LinkedIn: https://www.linkedin.com/in/abhay5349singh/\n\nApproach:\n\n1.Sorting array on basis of start time to have sequential acces to all 
**Connect with me on LinkedIn**: https://www.linkedin.com/in/abhay5349singh/\n\n**Approach:**\n\n1.Sorting array on basis of start time to have sequential acces to all events.\n2.Each event can be attended in 2 ways: `single` or `pair-up with max profitable non conflicting event`\n\n```\nclass Solution {\npublic:\n    \n    int nextNonConflictingEvent(vector<vector<int>>& events, int l, int r, int end){\n        while(l<r){\n            int m = l+(r-l)/2;\n            \n            if(events[m][0] > end){\n                r=m;\n            }else{\n                l=m+1;\n            }\n        }\n        \n        return (events[l][0] > end ? l:-1);\n    }\n    \n    int maxTwoEvents(vector<vector<int>>& events) {\n        sort(events.begin(), events.end());\n        \n        int n=events.size();\n        vector<int> single(n,0), pair(n,0);\n        single[n-1] = pair[n-1] = events[n-1][2];\n        \n        int ans = events[n-1][2];\n        \n        for(int i=n-2;i>=0;i--){\n            // max profit from 1 event from [i,n-1]\n            single[i] = max(events[i][2], single[i+1]);\n            \n            // max profit from current + most proftable event from [idx,n-1]\n            int idx = nextNonConflictingEvent(events, i+1, n-1, events[i][1]);\n            pair[i] = events[i][2] + (idx != -1 ? single[idx]:0);\n            \n            ans = max(ans, max(single[i], pair[i]));\n        }\n        \n        return ans;\n    }\n};\n```\n\n**Approach:**\n\n1.Sorting array on basis of start time to have sequential acces to all events.\n2.Keeping track of max value which can be obtained from valid events w.r.t current event\n\n```\nclass Solution {\npublic:\n    \n    struct node{\n        int value;\n        int end;\n    };\n    \n    struct pqCompare{\n        bool operator()(const node &a, const node &b){\n            if(a.end == b.end) return a.value < b.value; // max heap\n            return a.end > b.end; // min heap\n        }\n    };\n    \n    int maxTwoEvents(vector<vector<int>>& events) {\n        sort(events.begin(), events.end());\n        \n        int n=events.size();\n        priority_queue<node, vector<node>, pqCompare> pq;\n        \n        int ans=0;\n        int maxPrevValFromValidEvent=0;\n        \n        for(int i=0;i<n;i++){\n            while(pq.size()>0 && pq.top().end < events[i][0]){\n                maxPrevValFromValidEvent = max(maxPrevValFromValidEvent, pq.top().value);\n                pq.pop();\n            }\n            \n            ans = max(ans, events[i][2]+maxPrevValFromValidEvent);\n            pq.push({events[i][2], events[i][1]});\n        }\n        \n        return ans;\n    }\n};\n```\n\n**Do upvote if it helps :)**
['Dynamic Programming', 'Sorting', 'Binary Tree', 'C++']