Java || O(N) with comments
\nclass Solution {\n    public long minimumMoney(int[][] transactions) {\n        long res = 0;\n        long maxCashBack = 0; // record maxCashBack when cost i
```\nclass Solution {\n    public long minimumMoney(int[][] transactions) {\n        long res = 0;\n        long maxCashBack = 0; // record maxCashBack when cost i > cashback i\n        long maxCost = 0; // record maxCost when cost i < cashback i\n        /* \n        The worst arrange is we finish all transactions with cost i > cashback i\n        then campare the money between :\n        1. finish all transactions with cost i than pay the most cost with cost i < cashback i\n        2. pay the last transactions with cost i > cashback i before get cashback\n        */\n        for (int[] trans : transactions) {\n            if (trans[0] > trans[1]) {\n                res += trans[0] - trans[1];\n                maxCashBack = Math.max(maxCashBack, trans[1]);\n            } else {\n                maxCost = Math.max(maxCost, trans[0]);\n            }\n        }\n        res += Math.max(maxCashBack, maxCost);\n        return res;\n    }\n}\n\n```
[]