125. 耍杂技的牛【贪心】

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
#define x first
#define y second
typedef pair<int,int> PII;
typedef long long int LL;
LL n,ans=-1e9,s;
vector<PII>ve;
bool cmp(PII a,PII b){
    
    return a.x+a.y<b.x+b.y;}
int main(void)
{
    
    
    cin>>n;
    for(int i=0;i<n;i++)
    {
    
    
        int a,b; cin>>a>>b;
        ve.push_back({
    
    a,b});
    }
    sort(ve.begin(),ve.end(),cmp);
    for(int i=0;i<ve.size();i++)
    {
    
    
        ans=max(ans,s-ve[i].y);
        s+=ve[i].x;
    }
    cout<<ans;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_46527915/article/details/123700974