[C++] O(N) Short solution with explanation
\nclass Solution {\npublic:\n    long long minimumMoney(vector<vector<int>>& a) {\n        long long total = 0, ans = 0;\n        for (auto x : a) if (x[1] < x[
```\nclass Solution {\npublic:\n    long long minimumMoney(vector<vector<int>>& a) {\n        long long total = 0, ans = 0;\n        for (auto x : a) if (x[1] < x[0]) total += x[0] - x[1];\n        for (auto x : a) ans = max(ans, x[0] + total - max(0, x[0] - x[1]));\n        return ans;\n    }\n};\n```\n\nThe core point of this solution is to loop through exactly which transaction will be the bar to set the initial money.\n\nTo calculate that, we pre-calculate `total =  all possibly money owed` after running the transactions.\nThen we loop through the critical transaction `x` where `x` will be the last bar.\nIn this case `x` will happen after all transactions that is losing money.\n\nSince `total` may also include the money owed from `x`, so we undo it by `total - max(0, x[0] - x[1])` Thus the answer for `x` will be `x[0] + total - max(0, x[0] - x[1])`
[]