trick to sort the array using key and cmp_to_key
\n\n# Code\n\nfrom functools import cmp_to_key\nclass Solution:\n    def minimumMoney(self, transactions: List[List[int]]) -> int:\n        def compare(a: List[
\n\n# Code\n```\nfrom functools import cmp_to_key\nclass Solution:\n    def minimumMoney(self, transactions: List[List[int]]) -> int:\n        def compare(a: List[int], b: List[int]) -> int:\n            total_result_a = a[1] - a[0]\n            total_result_b = b[1] - b[0]\n            # if the result of running both transactions are negative, we want the one with the lower cashback first\n            if total_result_a < 0 and total_result_b < 0:\n                result = b[1] - a[1]\n                return result\n            # if the result of running both transactions are positive, we want the one with the larger cost first\n            elif total_result_a > 0 and total_result_b > 0:\n                return a[0] - b[0]\n            else:\n                # we want the one with the negative result first\n                return total_result_b - total_result_a\n\n            # we want positive cashback to before negative cashback\n\n        transactions.sort(# key=cmp_to_key(compare),\n            key=cmp_to_key(compare), reverse=True)\n        minimum = 0\n        current_money = 0\n        for cost, cashback in transactions:\n            current_money -= cost\n            minimum = min(minimum, current_money)\n            current_money += cashback  # print("current_money after cashback:", current_money)\n        return minimum * -1\n```
['Python3']