[Python] Short solution and detailed explanation
Put the losing-money transactions infront and the earning-money back. This is because putting the earning transactions in front will gain money and reduce the a
1. Put the losing-money transactions infront and the earning-money back. This is because putting the earning transactions in front will gain money and reduce the amount we need.\n2. Assume we have 4 transactions that lose money for simplicity: [c1,g1], [c2,g2], [c3,g3], [c4,g4]. c means cost and g means gain.\n3. The total amount will be c1 + (c2-g1) + (c3-g2) + (c4-g3) = c1+...+c4 - (g1+...+g3) =**totalLoss + g4**. After the 4 transactions, we will gain g4 as cashback, which will be used later.\n4. For the transactions that gain money, the minimum amount needed for the worst case will be the max of all cost (maxCostGaining). After getting the cashback of the max cost, we can finish the other transactions without any extra money.\n5. The answer looks like **totalLoss + g4 + maxCostGaining - g4 = totalLoss + maxCostGaining**\n6. However, it is possible that g4 is larger than maxCostGaining. In that case, maxCostGaining - g4 is a negative number. We can\'t spend a negative amount. We spend zero in that case and the total amount needed is **totalLoss + g4**.\n7. totalLoss + g4 could be larger than totalLoss + maxCostGaining. Once g4 is larger than maxCostEarning, we found a even larger amount that we need. We therefore get the max cashback in losing-money transactions. **If maxCashBackLosing > maxCostGaining, answer =  totalLoss + maxCashBackLosing**. \n\n```\nclass Solution:\n    def minimumMoney(self, transactions: List[List[int]]) -> int:\n        totalLoss = 0\n        maxCostGaining = float(\'-inf\')\n        maxCashBackLosing = float(\'-inf\')\n        for cost,cashBack in transactions:\n            if cost < cashBack:\n                maxCostGaining = max(maxCostGaining,cost)\n            else:\n                totalLoss += cost - cashBack\n                maxCashBackLosing = max(maxCashBackLosing,cashBack)\n        return totalLoss + max(maxCostGaining,maxCashBackLosing)\n```
['Python']