[Java] Simple solution using TreeMap - binary search, storing past max profit for the exit time
Simple Steps:\n1. Split each event into 2 sub events - start and  end (identify by -ve value of profit)\n2. If new start sub event comes see if the maxProfit pr
**Simple Steps:**\n1. Split each event into 2 sub events - start and  end (identify by -ve value of profit)\n2. If new start sub event comes see if the maxProfit present in an end sub event which was before current start using TreeMap lowerEntry function.\n3. When end sub event comes store maxRemoved and make an entry for that end event time in a TreeMap.\n```\nclass Solution {\n    public int maxTwoEvents(int[][] events) {\n        int n = events.length;\n        int[][] inout = new int[2*n][2];\n        for(int i=0;i<n;i++) {\n            int[] event = events[i];\n            inout[i*2] = new int[]{event[0], event[2]};\n            inout[i*2 +1] = new int[]{event[1], -event[2]};\n        }\n        Arrays.sort(inout, (a,b)-> a[0]-b[0]);\n        int max = 0, maxRemoved = 0;\n        TreeMap<Integer, Integer> maxUntil = new TreeMap<>();\n        for(int[] eve : inout) {\n            if(eve[1] > 0 ) { //  identifying if it is start of event or end of event by -ve value or +ve value\n                Map.Entry<Integer, Integer> lower = maxUntil.lowerEntry(eve[0]);\n                int lowerProfit = (lower==null? 0 : lower.getValue());\n                max = Math.max(max, lowerProfit+eve[1]);\n            } else {\n                maxRemoved = Math.max(-eve[1], maxRemoved);\n                maxUntil.put(eve[0], maxRemoved);\n            }\n        }\n        return max;\n    }\n}\n```
['Binary Tree', 'Java']