<p>You are given a <strong>0-indexed</strong> 2D integer array <code><font face="monospace">transactions</font></code>, where <code>transactions[i] = [cost<sub>i</sub>, cashback<sub>i</sub>]</code>.</p>

<p>The array describes transactions, where each transaction must be completed exactly once in <strong>some order</strong>. At any given moment, you have a certain amount of <code>money</code>. In order to complete transaction <code>i</code>, <code>money &gt;= cost<sub>i</sub></code> must hold true. After performing a transaction, <code>money</code> becomes <code>money - cost<sub>i</sub> + cashback<sub>i</sub></code>.</p>

<p>Return<em> the minimum amount of </em><code>money</code><em> required before any transaction so that all of the transactions can be completed <strong>regardless of the order</strong> of the transactions.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> transactions = [[2,1],[5,0],[4,2]]
<strong>Output:</strong> 10
<strong>Explanation:
</strong>Starting with money = 10, the transactions can be performed in any order.
It can be shown that starting with money &lt; 10 will fail to complete all transactions in some order.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> transactions = [[3,0],[0,3]]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
- If transactions are in the order [[3,0],[0,3]], the minimum money required to complete the transactions is 3.
- If transactions are in the order [[0,3],[3,0]], the minimum money required to complete the transactions is 0.
Thus, starting with money = 3, the transactions can be performed in any order.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= transactions.length &lt;= 10<sup>5</sup></code></li>
	<li><code>transactions[i].length == 2</code></li>
	<li><code>0 &lt;= cost<sub>i</sub>, cashback<sub>i</sub> &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution:
    def minimumMoney(self, transactions: List[List[int]]) -> int:
        # in order to complete a transaction i, money >= cost_i 
        # after transaction, money becomes money - cost_i + change_i 
        # return the minimum amount of money required before any transaction so that 
        # all of the transactions can be completed, regardless of order 
            # return the minimum amount of money required so that transactions 
            # with the highest cost and lowest cashback can be completed 
        # business logic problem 
        # the capital needed is what the business requires to be succesful through the stages of the coming transactions 
        capital_needed = 0 
        # sunk costs are those where you must spend money and get nothing for it 
        sunk_costs = [transaction[0] for transaction in transactions if transaction[1] == 0] 
        # opportunities are chances to make money where you'll need to spend some and it's not a sunk cost 
        opportunities = [transaction[1] for transaction in transactions if (transaction[0] > transaction[1] and transaction[1] != 0)]
        # opportunity costs are the differences in costs and profit when you will make some profit but ultimately lose capital overall
        opportunity_costs = [cost - profit for cost, profit in transactions if (cost > profit and profit != 0)] 
        # upfront_costs is the costs where costs are strictly matched out by the profits -> cost of doing business 
        upfront_costs = [transaction[0] for transaction in transactions if transaction[0] <= transaction[1]]
        # max upfront cost is the max of those if it exists, otherwise 0 
        max_upfront_cost = 0 if len(upfront_costs) == 0 else max(upfront_costs)
        # opportunities has the max upfront cost appended so we have to consider it as part of the capital layout 
        opportunities.append(max_upfront_cost) 
        # capital needed is the total of your sunk costs, plus the total of your losers where you make some money, plus the max of 
        # either the opportunities profit, or the max upfront cost. If the opportunities profit, you need at least that much to make
        # up for the opportunities costs. If max upfront cost, you need at least that much to kick off the virtuous cycle of doing business 
        capital_needed = sum(sunk_costs) + sum(opportunity_costs) + max(opportunities)
        return capital_needed
1169