C++ || Intuitions Explained || Binary Search || Thanks to chipbk10
\n#define ll long long\n\nclass Solution {\npublic:\n    // Basically this question is based amount of profit u can get \n    \n    // so u want to invest on th
```\n#define ll long long\n\nclass Solution {\npublic:\n    // Basically this question is based amount of profit u can get \n    \n    // so u want to invest on those tasks where u will get more profits \n    \n    // so sorting must be done on the basis of differnces \n    \n    // take firstly those pairs which has more differnence.\n    \n    // This quote which is written below is from chipbk10  , all credit to him \n    \n    // I want to add as it clarifies problem a lot !! Thanx to him .\n    \n    //If you want to invest on a project [x,y], you must have y money. Once finished, you gain y-x money.\n\t//So, which project you should invest first to build up a good capital for the next investments?\n\n//My advice is to invest on projects which are able to bring back the highest profitability y-x. Even, in \n//the case y is very big, don\'t be afraid, you can loan extra money from other sources (banks, family, \n//friends) to have enough y money. The secret is you know that at this moment, y-x is the most profitable.\n\n//So, we can arrange the projects by decreasing profits, and borrow money to invest if our capital is not enough.\n    \n    \n    static bool compare( vector<int> &a , vector<int> &b)\n    {\n        return (a[1]-a[0]) < (b[1]-b[0]);\n    }\n    \n    bool func(vector<vector<int>> &nums , int  mid)\n    {\n        int n=nums.size();\n        \n        for(int i=n-1;i>=0;i--)\n        {\n            if(mid<nums[i][1])\n            {\n                return false;\n            }\n            else\n            {\n                mid-=nums[i][0];\n            }\n        }\n        \n        return true;\n    }\n    int minimumEffort(vector<vector<int>>& nums) {\n        \n        sort(nums.begin() , nums.end() , compare);\n        \n        int n=nums.size();\n        \n        // We know that answer will be in the range of\n        \n        //Summation Minimum i - Summation actual i\n        \n        ll sum1=0 , sum2=0;\n        \n        for(int i=0;i<n;i++)\n        {\n            sum1+=nums[i][0];\n            sum2+=nums[i][1];\n        }\n        \n        // Apply binary search in the range\n        \n        ll l=sum1 , r=sum2;\n        \n        ll ans=sum1;\n        \n        while(l<=r)\n        {\n            ll mid=(l + (r-l)/2);\n            \n            if(func(nums , mid )==true)\n            {\n                ans=mid;\n                r=mid-1;\n            }\n            else\n            {\n                l=mid+1;\n            }\n        }\n        \n        return ans;\n      \n    }\n```
['Greedy', 'C', 'Sorting', 'Binary Tree', 'C++']