Proof of the greedy approach
Suppose the energe we have before starting is E, and the order of the taskes is (a[0], m[0]), ... (a[n-1], m[n-1])\n\nE should satisfy the following rules:\n\nE
Suppose the energe we have before starting is E, and the order of the taskes is ```(a[0], m[0]), ... (a[n-1], m[n-1])```\n\nE should satisfy the following rules:\n```\nE >= m[0],\nE >= a[0] + m[1],\n...\nE >= sum(a[0:i)) + m[i]\n...\n```\n\nLet ```c[i] = sum(a[0:i)) + m[i]```, we would have ```E >= max(c[0], c[1],.... c[n-1])```, so for a given task list, the minimum energe we need is ```max(c[0], c[1],.... c[n-1])```\n\nLet\'s now suppose the optimized solution can only be achieved when the taskes is ordered by its energe difference ```(m[i] - a[i])``` descending, if not, then there must exist a pair of neighboring task ```(i, i + 1)``` where```m[i] - a[i] < m[i+1] - a[i+1]```\n\nThen we can swap this two taskes and get a new task list and denote it as ```(a\'[i], m\'[i])```, similarly, we denote ```c\'[i]``` as ```c\'[i] = sum(a\'[0:i) + m\'[i])```, and the minumum energy ```E\'``` would be ```E\' = max(c\'[0], c\'[1], ....c\'[n-1])```\n\nAs the only difference from the new task list is the swapped two taskes, so only ```c\'[i]``` and ```c\'[i+1]``` have been changed, let\'s denote the others as ```A = {c[0], c[1], ...c[i-1], c[i+2], ..c[n-1]}```\n\nAfter some math work, we got \n```\nc\'[i] = c[i+1] - a[i] < c[i+1]\nc\'[i+1] = c[i+1] + (m[i] - a[i]) - (m[i+1] - a[i+1]) < c[i + 1]\nso max(c\'[i], c\'[i+1]) < c[i + 1] <= max(A, c[i], c[i+1]) = E  \nand A <= max(A, c[i], c[i + 1]) = E\n```\nso ```E\' = max(A, c\'[i], c\'[i + 1]) <= E```, which means after swapping we can get an optimized energy as well.\n  \nThe code:\n```\nclass Solution {\npublic:\n    int minimumEffort(vector<vector<int>>& tasks) {\n        sort(tasks.begin(), tasks.end(), [](const vector<int>& a, const vector<int>& b){\n           return a[1] - a[0] > b[1] - b[0]; \n        });\n\n        int ans = 0;\n        int s = 0;\n        for (auto& t : tasks)\n        {\n            ans = max(ans, t[1] + s);\n            s += t[0];\n        }\n        return ans;\n    }\n};\n```
[]