<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>

from functools import cmp_to_key

class Solution:

    def sortbyCond(self,a, b):
        if (a[1] != b[1]):
            return (a[1] - b[1])
        else:
            return a[0] - b[0]

    def minimumMoney(self, t: List[List[int]]) -> int:
        maxi=0
        ans2=0
        ans=0
        l=[]
        n=len(t)
        for i in range(n):
            if(t[i][1]>=t[i][0]):
                maxi=max(maxi,t[i][0])
            elif(t[i][1]==0):
                ans2+=t[i][0]
            else:
                l.append(t[i])
        
        p=len(l)
        l.sort(key = cmp_to_key(self.sortbyCond))
        for i in range(p):
            ans+=(l[i][0]-l[i][1])
            if(i==p-1):
                ans+=l[i][1]

        ans+=ans2

        if(len(l)>0 and maxi>l[len(l)-1][1]):
            ans+=maxi-l[p-1][1]
        elif(len(l)==0 and maxi>0):
            ans+=maxi
        return ans
1207