C++ Dynamic Programming
Starting from first day, each day has three possible works in this problem:\n\n1. End the first event (Case 1)\n2. Start the second event (Case 2)\n3. Do nothin
**Starting from first day, each day has three possible works in this problem:**\n\n1. **End the first event** (Case 1)\n2. **Start the second event** (Case 2)\n3. **Do nothing (or in event)** (Case 3)\n\n\n**And their relationship:**\n\n1. **`firstEnd = iValEnd`** (Case 1)\n2. **`secondBegin = max(old_firstEnd + iValBegin, secondBegin)`** (Case 2)\n3. **`old_firstEnd = max(old_firstEnd,  firstEnd)`**  (Case 3)\n\n#### variables:\n* **`iValEnd`** stands for the maximum value of the events which ends at day i\n* **`iValBegin`** stands for the maximum value of the events which begins at day i\n* **`firstEnd`** stands for the maximum value at day i if you just choose one event to attend\n* **`old_firstEnd`** stands for the yesterday\'s maximum value if you just choose one event to attend\n* **`secondBegin`** stands for the maximum value at day i if you choose the second event to attend\n\n\n```c++\nclass Solution {\npublic:\n    int maxTwoEvents(vector<vector<int>>& events) {\n        int maxDay = 0;\n        // Dynamic Programming\n        \n        unordered_map<int, vector<int>> endDay; // end Day with its value\n        unordered_map<int, vector<int>> begDay; // begin Day with its value\n        set<int> days;\n        \n        for (auto event : events) {\n            if (event[1] > maxDay) maxDay = event[1];\n            endDay[event[1]].push_back(event[2]);\n            begDay[event[0]].push_back(event[2]);\n            \n            days.insert(event[1]);\n            days.insert(event[0]);\n        }\n        int ret = 0;\n        \n        // Start\n        int fee = 0; // first event end\n        int seb = 0; // second event begin\n        for (auto i : days) {\n            int prefee = fee;\n            int preseb = seb;\n            for (int value : endDay[i]) {\n                // first event end today\n                fee = max(fee, value);\n            }\n            \n            fee = max(prefee, fee); // do nothing\n            \n            for (int value : begDay[i]) {\n                // second event start today\n                seb = max(prefee + value, seb);\n            }\n            ret = max(ret, max(seb, fee));\n        }\n        return ret;\n    }\n};\n\n   \n```
[]