If you still struggled to understand after reading lee215 and Bakerston's posts
Read lee215 and Balerston\'s posts first.\n\nCode optimized for easy understanding:\n\n\nclass Solution {\n    public long minimumMoney(int[][] txs) {\n        
Read lee215 and Balerston\'s posts first.\n\nCode optimized for easy understanding:\n\n```\nclass Solution {\n    public long minimumMoney(int[][] txs) {\n        long netLosingCost = 0;\n        int maxCashBack = 0;\n        int maxCost = 0;\n        for (int[] t : txs) {\n            if (t[0] > t[1]) { // losing transactions\n                netLosingCost += t[0] - t[1];\n                maxCashBack = Math.max(maxCashBack, t[1]);\n            } else { // earning transactions\n                maxCost = Math.max(maxCost, t[0]);\n            }\n        }\n        return netLosingCost + Math.max(maxCashBack, maxCost);\n    }\n}\n```
['Java']