Skip to content

Instantly share code, notes, and snippets.

@oilover
Created September 11, 2014 13:48
Show Gist options
  • Save oilover/4be595645cac0e9c06d5 to your computer and use it in GitHub Desktop.
Save oilover/4be595645cac0e9c06d5 to your computer and use it in GitHub Desktop.
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
using namespace std;
typedef long long ll;
#define prt(k) cout << #k " = " << k << endl;
const int N = 50020;
struct data
{
int w, s;
};
data a[N];
bool cmp(data a1, data a2)
{
int w1 = a1.w, s1 = a1.s;
int w2 = a2.w, s2 = a2.s;
return max(-s1, w1-s2) < max(-s2, w2-s1);
}
int n;
int main()
{
while(cin >> n, n)
{
for(int i = 0; i < n; i ++) scanf("%d%d", &a[i].w, &a[i].s);
sort(a, a+n, cmp);
ll ans = -1e9, tot = 0;
for(int i = 0; i < n; i++)
{
ans = max(ans, tot - a[i].s);
tot += a[i].w;
}
cout << ans << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment