C++ Greedy Explanation
Approach:\n\n first find out the total money that we can lose.\n i.e the sum of lost money is\n sum_lose = sum(max(0, cost[i] - cashback[i]))\n let the last tra
**Approach:**\n\n* first find out the total money that we can lose.\n* i.e the sum of lost money is\n* ```sum_lose = sum(max(0, cost[i] - cashback[i]))```\n* let the last transaction be at i\n* now, the money we would have lost before the last transaction would be\n\t* ``` total_loss - loss at i```\n* in order to perform the last transaction we want ```money>=cost[i]```\n* so money should be atleast \n\t* ```money = total_loss - loss at i + cost[i]```\n* now, ```loss at i = min(0,cost[i]-cashback[i])```\n* consider the below equations to understand better\n\t* ![image](https://assets.leetcode.com/users/images/07fd25d1-d7e8-4f85-b338-674694688ad6_1663440603.2570097.jpeg)\n* from the above equations we can say that \n\t* ```k = min(cost[i],cashback[i])```\n* we will consider the worst case so take maximum value of k\n* ```k =max(k, min(v[0], v[1]));```\n\n**Code:**\n\n```\nclass Solution\n{\npublic:\n    long long minimumMoney(vector<vector<int>> &transactions)\n    {\n        long long total_loss = 0;\n        int k = 0;\n        for (auto &v : transactions)\n        {\n            k = max(k, min(v[0], v[1])); // v[0] = cost[i], v[1] = cashback[i]\n            total_loss += max(v[0] - v[1], 0);\n        }\n        return total_loss + k;\n    }\n};\n```\n
['Greedy', 'C', 'C++']