1665. Minimum Initial Energy to Finish Tasks.cpp
Code\n\nclass Solution {\npublic:    \n    static bool cmp(const pair<int,pair<int,int>>&a,const pair<int,pair<int,int>>&b)\n    {\n        if(a.first==b.first)
**Code**\n```\nclass Solution {\npublic:    \n    static bool cmp(const pair<int,pair<int,int>>&a,const pair<int,pair<int,int>>&b)\n    {\n        if(a.first==b.first)\n        {\n            return a.second.second>b.second.second;\n        }\n        else\n            return a.first>b.first;            \n    }    \n    int minimumEffort(vector<vector<int>>& tasks) \n    {        \n        vector<pair<int,pair<int,int>>>v;\n        for(int i=0;i<tasks.size();i++)\n            v.push_back({tasks[i][1]-tasks[i][0],{tasks[i][0],tasks[i][1]}});        \n        sort(v.begin(),v.end(),cmp);        \n        int sum=0;\n        int n=tasks.size();        \n        for(int i=0;i<n;i++)\n            sum+=v[i].second.first;        \n        sum=max(sum,v[0].second.second);        \n        int temp=sum;        \n        for(int i=0;i<v.size();i++)\n        {\n            if(temp>=v[i].second.second)\n            {\n                temp=temp-v[i].second.first;\n            }\n            else\n            {\n                sum+=abs(temp-v[i].second.second);\n                temp+=abs(temp-v[i].second.second);\n                temp=temp-v[i].second.first;\n            }\n        }       \n        return sum;        \n    }\n};\n```
['C']