分析:先按照w+s排序,从前到后遍历即可
#include <bits/stdc++.h> using namespace std; const int N = 50050; typedef pair<int,int> PII; PII f[N]; int n; int main() { scanf("%d",&n); for(int i = 0; i < n; i++) { int w, s; scanf("%d%d",&w,&s); f[i] = {w+s,w}; } sort(f,f+n); int res = -2e9, sum = 0; for(int i = 0; i < n; i++) { int t = f[i].first, w = f[i].second; res = max(res,sum-(t-w)); sum += w; } printf("%d ",res); return 0; }