按S+W排序即可。
1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 #define MAXN 100005 5 typedef long long LL; 6 struct ppp{ 7 int s, w; 8 bool operator < (const ppp& p) const { 9 return s + w <= p.s + p.w; 10 } 11 }p[MAXN]; 12 int n; 13 int main() { 14 while (scanf("%d", &n) != EOF) { 15 for (int i = 0; i < n; i++) 16 scanf("%d%d", &p[i].w, &p[i].s); 17 std::sort(p, p + n); 18 LL ans=0, totw = 0; 19 for (int i = 0; i < n; i++) 20 ans = std::max(ans, totw - p[i].s), 21 totw += p[i].w; 22 printf("%I64d\n", ans); 23 } 24 return 0; 25 }