O(n) solution || python
\n# Complexity\n- Time complexity:O(n)\n\n- Space complexity:o(n)\n\n# Code\n\nfrom functools import cmp_to_key\n\nclass Solution:\n\n    def sortbyCond(self,a,
\n# Complexity\n- Time complexity:O(n)\n\n- Space complexity:o(n)\n\n# Code\n```\nfrom functools import cmp_to_key\n\nclass Solution:\n\n    def sortbyCond(self,a, b):\n        if (a[1] != b[1]):\n            return (a[1] - b[1])\n        else:\n            return a[0] - b[0]\n\n    def minimumMoney(self, t: List[List[int]]) -> int:\n        maxi=0\n        ans2=0\n        ans=0\n        l=[]\n        n=len(t)\n        for i in range(n):\n            if(t[i][1]>=t[i][0]):\n                maxi=max(maxi,t[i][0])\n            elif(t[i][1]==0):\n                ans2+=t[i][0]\n            else:\n                l.append(t[i])\n        \n        p=len(l)\n        l.sort(key = cmp_to_key(self.sortbyCond))\n        for i in range(p):\n            ans+=(l[i][0]-l[i][1])\n            if(i==p-1):\n                ans+=l[i][1]\n\n        ans+=ans2\n\n        if(len(l)>0 and maxi>l[len(l)-1][1]):\n            ans+=maxi-l[p-1][1]\n        elif(len(l)==0 and maxi>0):\n            ans+=maxi\n        return ans\n\n\n```
['Python3']