证明过程如下
使用邻项交换法
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 50005;
typedef long long ll;
struct node
{
int w, s;
bool operator<(const node b)const
{
return w + s < b.w + b.s ? true : false;
}
}a[N];
int main()
{
ll w = 0;
ll res =0;
int n;
cin >> n;
for (int i = 0;i < n;i++)
{
cin >> a[i].w >> a[i].s;
res=min(res,-(ll)a[i].s); //一头牛前面可能没有牛,这时候他会减去本身
//所以要这么做,或者res定义为-OO
}
sort(a, a + n);
for (int i = 0;i < n;i++)
{
res = max(res, w - a[i].s);
w += a[i].w;
}
cout << res;
}