The greedy approach with O(n)
The initial min money should be at least the maximum of the followings:
The max cost ost among all deficit transaction: maxNegativeCost.
Worst-Case Last Transac
The initial min money should be at least the maximum of the followings:
- The max cost ost among all deficit transaction: `maxNegativeCost`.
- Worst-Case Last Transaction (`maxCostsCachback`): If a deficit transaction is executed last, we must have at least `cost_last` money to execute it so the initial money should be: `totalDiff_of_all_ather_loss_makings + cost`.
- The highest cost among gainful transactions + the total deficit: If any gainful transactions is executed first after all of deficit ones.

Complexity: _Time:_ `O(n)` & _Space:_ `O(1)`
```cpp []
class Solution {
public:
    long long minimumMoney(vector<vector<int>>& transactions) {
        int n = transactions.size();
        long long totalDiff = 0;
        long long maxNegativeCost = numeric_limits<long long>::min(),
                  maxPositiveCost = numeric_limits<long long>::min(),
                  maxCostsCachback = numeric_limits<long long>::min();

        for (int i = 0; i < n; i++) {
            auto& ts = transactions[i];
            int diff = (ts[1] - ts[0]);
            if (diff < 0) {
                totalDiff -= diff;
                maxNegativeCost = max(maxNegativeCost, (long long)ts[0]);
            } else {
                maxPositiveCost = max(maxPositiveCost, (long long)ts[0]);
            }
        }
        for (int i = 0; i < n; i++) {
            auto& ts = transactions[i];
            if (ts[1] < ts[0]) {
                maxCostsCachback = max(maxCostsCachback, totalDiff + ts[1]);
            }
        }
        return max(
            {maxNegativeCost, maxPositiveCost + totalDiff, maxCostsCachback});
    }
};
```
['C++']