Intuitive Approach || Greedy || Sorting
IntuitionThe question asks us to calculate the minimum amount of money required initially to perform all transactions in any random order. We can find the answe
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The question asks us to calculate the minimum amount of money required initially to perform all transactions in any random order. We can find the answer by looking for the worst order of transactions possibe.

# Approach
<!-- Describe your approach to solving the problem. -->
Each transaction has some `cost` and `cashback`
- If `cost > cashback`: then the money after transaction is increased.
- Else: the money is reduced after transaction

1. We will separate the transactions which have `cost>cashback` because these are the transactions which will reduce our money and therefore more money will be required to perform next transaction. 
2. We'll sort this array by the amount of money left after transaction i.e. cashback amount.
3. We'll calcaulate the money required to perform these separately sorted transactions.
4. Now for thr `cost <= cashback`, we'll look for the maximum cost out of these and perform that transaction just after the above sorted transaction. 
WHY MAXIMUM TRANSACTION ONLY? because these transactions which have `cost<=cashback` are performed sequentially then each transaction will increase the current money after the transaction, which is not out worst case. So we'll directly take the maximum cost transaction to have the highest money jump possible.

# Complexity
- Time complexity: $$O(n*log(n))$$
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: $$O(n)$$
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
class Solution {
public:
    long long minimumMoney(vector<vector<int>>& transactions) {
        long long maxprofitcost = 0, curr, ans = 0;
        vector<pair<long long, long long>> loss;
        // 5 1, 4 3, 1 2
        for(auto &it: transactions){
            if(it[0]>it[1])
                loss.push_back({it[1], it[0]});
            else
                maxprofitcost = max(maxprofitcost, (long long)it[0]);
        }
        sort(loss.begin(), loss.end());

        curr = 0;
        for(auto &it: loss){
            ans += it.second - curr;
            curr = it.first;
        }
        ans += max((long long)0, maxprofitcost - curr);
        return ans;
    }
};
```
['Greedy', 'Sorting', 'C++']