AcWing 125. 耍杂技的牛
原题链接
中等
作者:
_如鲸向海
,
2022-06-07 17:29:13
,
所有人可见
,
阅读 175
算法1
C++ 代码
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 50010;
typedef pair<int,int> PII;
PII cow[N];
bool comp(PII a,PII b){
return (a.first+a.second)<(b.first+b.second);
}
int main(){
int n;
cin>>n;
for(int i=0;i<n;i++){
cin>>cow[i].first>>cow[i].second;
}
int res = -0x3f3f3f3f;
int sum=0;
sort(cow,cow+n,comp);
for(int i=0;i<n;i++){
res = max(res,sum-cow[i].second);
sum+=cow[i].first;
}
cout<<res<<endl;
}