Swap Space (LiveArchive 7587) by letter
Contest: 3420    RunID: 2168287    Status: Wrong answer    Date: Mon Apr 24 23:41:28 JST 2017


#include <bits/stdc++.h>
using namespace std;

#define REP(i, s, n) for(int i = s; i < n; ++i)
#define rep(i, n)		 REP(i, 0, n)
#define SORT(c)			 sort((c).begin(), (c).end())
#define IINF					INT_MAX
#define LLINF					LLONG_MAX
#define DEBUG					false

typedef long long				ll;
typedef pair <int, int> ii;

struct Inp{
	
	int a;
	int b;
	
	Inp(){
		a = 0;
		b = 0;
	}
	
	Inp(int a, int b):a(a), b(b){}
	
	bool operator<(const Inp r) const {
		int lsa =b - a;
		int rsa =r.b - r.a; 
		if(lsa < 0){
			if(rsa < 0) return (b > r.b);
			else return false;
		}
		else{
			if(rsa < 0) return true;
			else  return (a < r.a);
		}
		return true;
	}
};


int main() {
	
	int n;
	while(cin >> n, n){
		
		vector<Inp> item(n);
		rep(i, n){
			int a, b;
			cin >> a >> b;
			item[i] = Inp(a, b);
		}
		
		SORT(item);
		
		ll ans = 0;
		
		ll tmp = 0;
		
		rep(i, n){
			tmp += item[i].a;
			ans = max(ans, tmp);
			tmp -= item[i].b;
			
		}
		
		cout << ans << endl;
		n = 0;
	}
	
	return 0;
}