算法2
(暴力枚举) $O(n^2)$
// 一个牛的风险f = W - s ; (W代表它上面牛的总重量)
// 自己w和s越大,越要在底部
// 那么按照w+s排个序然后模拟即可 ;
C++ 代码
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
typedef long long LL;
#define x first
#define y second
const int N = 5e4 + 10 ;
// 风险f = W - s
// 自己w和s越大,越要在底部
PII a[N] ;
int main(){
int n ; cin >> n ;
for(int i=0;i<n;i++){
int w , s ; cin >> w >> s ;
a[i].x = w + s ;
a[i].y = s ;
}
sort(a,a+n) ;
LL ans = -1e9+7 , pre;
for(int i=0;i<n;i++){
ans = max(ans , pre - a[i].y) ;
pre += a[i].x - a[i].y ;
}
cout << ans << endl ;
}