Greedy || C++
\n\n# Code\n\nclass Solution {\npublic:\n    static bool cmp(vector<int>&a,vector<int>&b){\n        return a[1]<b[1];\n    }\n    long long minimumMoney(vector<
\n\n# Code\n```\nclass Solution {\npublic:\n    static bool cmp(vector<int>&a,vector<int>&b){\n        return a[1]<b[1];\n    }\n    long long minimumMoney(vector<vector<int>>& v) {\n        long long res=0;\n        vector<vector<int>>v1;\n        vector<vector<int>>v2;\n        for(int i=0;i<v.size();i++){\n            if(v[i][0]<=v[i][1]) v1.push_back(v[i]);\n            else v2.push_back(v[i]);\n        }\n        long long ans=0;\n        sort(v1.rbegin(),v1.rend());\n        sort(v2.begin(),v2.end(),cmp);\n        for(int i=0;i<v2.size();i++){\n            if(res<v2[i][0]){\n                ans+=abs(res-v2[i][0]);\n                res=v2[i][0];\n            }\n            res=res-v2[i][0]+v2[i][1];\n        }\n        for(int i=0;i<v1.size();i++){\n            if(res<v1[i][0]){\n                ans+=abs(res-v1[i][0]);\n                res=v1[i][0];\n            }\n            res=res-v1[i][0]+v1[i][1];\n        }\n        return ans;\n    }\n};\n```
['C++']