You are given a 0-indexed 2D integer array of events where events[i] = [startTimei, endTimei, valuei]. The ith event starts at startTimei and ends at endTimei, and if you attend this event, you will receive a value of valuei. You can choose at most two non-overlapping events to attend such that the sum of their values is maximized.

Return this maximum sum.

Note that the start time and end time is inclusive: that is, you cannot attend two events where one of them starts and the other ends at the same time. More specifically, if you attend an event with end time t, the next event must start at or after t + 1.

&nbsp;
Example 1:

Input: events = [[1,3,2],[4,5,2],[2,4,3]]
Output: 4
Explanation: Choose the green events, 0 and 1 for a sum of 2 + 2 = 4.


Example 2:

Input: events = [[1,3,2],[4,5,2],[1,5,5]]
Output: 5
Explanation: Choose event 2 for a sum of 5.


Example 3:

Input: events = [[1,5,3],[1,5,1],[6,6,5]]
Output: 8
Explanation: Choose events 0 and 2 for a sum of 3 + 5 = 8.

&nbsp;
Constraints:


	2 &lt;= events.length &lt;= 105
	events[i].length == 3
	1 &lt;= startTimei &lt;= endTimei &lt;= 109
	1 &lt;= valuei &lt;= 106


class Solution:
def maxTwoEvents(self, events: List[List[int]]) -> int:
    
    events.sort()
    heap = []
    res2,res1 = 0,0
    for s,e,p in events:
        while heap and heap[0][0]<s:
            res1 = max(res1,heapq.heappop(heap)[1])
        
        res2 = max(res2,res1+p)
        heapq.heappush(heap,(e,p))
    
    return res2
class Solution {
    public int maxTwoEvents(int[][] events) {
        Arrays.sort(events, (a, b) -> a[0] - b[0]);
        int onRight = 0, maxOne = 0, n = events.length;
        int[] rightMax = new int[n+1];
        for (int i = n - 1; i >= 0; i--) {
            int start = events[i][0], end = events[i][1], val = events[i][2];
            maxOne = Math.max(val, maxOne);
            rightMax[i] = Math.max(rightMax[i+1], val);
        }
        int two = 0;
        for (int i = 0; i < n; i++) {
            int start = events[i][0], end = events[i][1], val = events[i][2];
            int idx = binarySearch(end, events);
            if (idx < n) {
                two = Math.max(rightMax[idx] + val, two);
            }
        }
        return Math.max(two, maxOne);
    }

    public int binarySearch(int end, int[][] arr) {
        int left = 0, right = arr.length;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (arr[mid][0] > end) {
                right = mid;
            } else {
                left = mid + 1;
            }
        }
        return left;
    }
}
#define ipair pair<int,int>
class Solution {
public:
    int maxTwoEvents(vector<vector<int>>& events) {
	
        //sort based on smaller start time
       sort(events.begin(), events.end());
        
        int mx = 0, ans = 0, n = events.size();
        priority_queue<ipair, vector<ipair>, greater<>> pq;
        // pq conatins {event_endtime , even_value}
        // for every event check the max-value of earlier events whose 
        // deadline is less than start time of curr event
        for(int i=0; i<n; i++)
        {
            while(!pq.empty() && pq.top().first < events[i][0])
                mx = max(mx, pq.top().second), pq.pop();
            
            ans = max(ans, mx+events[i][2]);
            pq.push({events[i][1], events[i][2]});
        }
        
        return ans;
    }
};
var maxTwoEvents = function(events) {
    const n = events.length;

    events.sort((a, b) => a[0] - b[0]);

    const minHeap = new MinPriorityQueue({ priority: x => x[1] });

    let maxVal = 0;
    let maxSum = 0;

    for (let i = 0; i < n; ++i) {
        const [currStart, currEnd, currVal] = events[i];

        while (!minHeap.isEmpty()) {
            const topElement = minHeap.front().element;
            const [topIdx, topEnd] = topElement;

            if (topEnd < currStart) {
                maxVal = Math.max(maxVal, events[topIdx][2]);
                minHeap.dequeue();
            }
            else {
                break;
            }
        }

        const sum = maxVal + currVal;
        maxSum = Math.max(maxSum, sum);
        minHeap.enqueue([i, currEnd]);
    }

    return maxSum;
};
Two Best Non-Overlapping Events