<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:
        # There are two kinds of transactions:
        # Consider the following transactions:
        # (c1, b1), (c2, b2), (c3, b3)
        # If the initial money is X, we have:
        # X >= c1
        # X - c1 + b1 >= c2
        # X - c1 + b1 - c2 + b2 >= c3
        # X - c1 + b1 - c2 + b2 - c3 + b3 >= 0
        # Equivalently, we have:
        # X >= ci + sum(ci - bi for 1 <= i < i)
        # 1. If all transactions are losing, that means ci >= bi.
        # The answer is max(sum(ci - bi), max(ck + sum(ci - bi without i == k))).
        # 2. If some transactions are not losing, absolutely we will not include them in the sum(ci - bi). Then the answer is the max of:
        # 2.1 max(c_k) + sum(ci - bi) for c_k is the maximum c for non-losing transactions.
        # 2.2 The answer inside the losing transactions.
        lt = [t for t in transactions if t[0] >= t[1]]
        wt = [t for t in transactions if t[0] < t[1]]
        total_lose = sum(t[0] - t[1] for t in lt) if len(lt) > 0 else 0
        ans = total_lose
        if len(lt) > 0:
            ans = max(ans, total_lose + max(t[1] for t in lt))
        if len(wt) > 0:
            ans = max(ans, total_lose + max(t[0] for t in wt))
        return ans

1192