题目链接:http://poj.org/problem?id=3045
按weight+strength 排序就行了
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef struct
{
int weight;
int stregth;
} COW;
COW cow[50010];
int n;
int cmp(COW a,COW b)
{
return a.weight+a.stregth < b.weight+b.stregth;
}
void solve()
{
sort(cow,cow+n,cmp);
int sum=0,ans=-1000000,risk=0;
for(int i=0; i<n; i++)
{
risk=sum-cow[i].stregth;
if(risk>ans) ans=risk;
sum+=cow[i].weight;
}
printf("%d\n",ans);
}
int main()
{
while(~scanf("%d",&n))
{
for(int i=0; i<n; i++)
scanf("%d%d",&cow[i].weight,&cow[i].stregth);
solve();
}
}