Math solution [O(N) Time][O(1) Space][100% Time & Memory]
The task can be represented as follows:\nWe have to find the maximum value of the sequence cost_i1 - cashback_i1 + cost_j2 - cashback_i2 + ... + cost_j:\n\n\n\n
The task can be represented as follows:\nWe have to find the maximum value of the sequence `cost_i1` - `cashback_i1` + `cost_j2` - `cashback_i2` + ... + `cost_j`:\n<img src="https://latex.codecogs.com/svg.image?result=max_{t_i,j}{\\left&space;(\\sum_{t_i&space;\\neq&space;j}(cost_{t_i}-cashback_{t_i})&plus;cost_j\\right&space;)}">\n\n\nIt will take O(2^N) time to calculate all the options, so this solution is unacceptable. \nBut we can notice that we needed to sum only that differences `cost_i - cashback_i` where `cost_i` is greater than `cashback_i`:\n<img src="https://latex.codecogs.com/svg.image?max_{t_i,j}{\\left&space;(\\sum_{t_i&space;\\neq&space;j}(cost_{t_i}-cashback_{})&plus;cost_j\\right&space;)}\\leqslant&space;max_j{\\left&space;(\\sum_{i&space;\\neq&space;j,&space;cost_i>cashback_i}(cost_i-cashback_i)&plus;cost_j\\right&space;)}">\n\n\nSo we can extract the sum from under the maximum operation: \n<img src="https://latex.codecogs.com/svg.image?max_j{\\left&space;(\\sum_{i&space;\\neq&space;j,&space;cost_i>cashback_i}(cost_i-cashback_i)&plus;cost_j\\right&space;)}&space;=&space;\\\\\\\\\\\\\\sum_{cost_i>cashback_i}(cost_i-cashback_i)&plus;max_j(cost_j-(cost_j-cashback_j)\\cdot&space;\\left&space;[&space;cost_j&space;>&space;cashback_j&space;\\right&space;])&space;=&space;\\\\\\\\\\sum_{cost_i>cashback_i}(cost_i-cashback_i)&plus;max_j(cashback_j&space;\\;\\;if\\;(cost_j&space;>&space;cashback_j)&space;\\;else&space;\\;\\;&space;cost_j)&space;">\n\nSo we can solve the task just in one pass:\n```\nclass Solution:\n    def minimumMoney(self, transactions: List[List[int]]) -> int:\n        sum_diff = 0\n        max_term = 0\n        \n        for cost, cashback in transactions:\n            if cost > cashback:\n                sum_diff += cost - cashback\n                max_term = max(max_term, cashback)\n            else:\n                max_term = max(max_term, cost)\n\n        return sum_diff + max_term\n```
['Math', 'Python']