感觉还是这个解释比较有说服力
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100100;
struct task
{
int r,o;
int der;
};
task ts[MAXN];
bool cmp(const task& a, const task& b)
{
if(a.der != b.der)
return a.der > b.der;
else
return a.r > b.r;
}
int main()
{
int n;
scanf("%d",&n);
for(int i = 0; i < n; ++i)
{
scanf("%d %d",&ts[i].r,&ts[i].o);
ts[i].der = ts[i].r - ts[i].o;
}
sort(ts,ts+n,cmp);
int res = 0;
int ss = 0;
int nexe;//现在执行所占用的空间
for(int i = 0; i < n; ++i)
{
nexe = ss + ts[i].r;
if(nexe > res) res = nexe;
ss += ts[i].o;
if(ss > res)
res = ss;
}
printf("%d\n",res);
return 0;
}