
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

ll n,m;
ll x[1200],y[1200];
ll dp[3000];
ll i,j;
ll sumx,sumy;
ll ans = 0;

bool solve(){
	cin >> n;
	if(n == 0){
		return false;
	}
	sumx = 0;
	sumy = 0;
	ll v = -1;
	ll sumv = -1;
	for(i=0;i<n;i++){

		cin >> x[i] >> y[i];
		if(x[i] > sumv){
			sumv = x[i];
			v = i;
		}
		sumx += x[i];
		sumy += y[i];
	}
	sort(x,x+n);
	reverse(x,x+n);
	if(x[0] * 2 > sumx){
		//cout << "!" << endl;
		ll w = x[0] - (sumx - x[0]);
		dp[0] = 1;
		for(i=1;i<=w;i++)dp[i] = 0;
		for(i=0;i<n;i++){
			if(i == v)continue;
			for(j=w;j>=1;j--){
				if(j - y[i] >= 0){
					dp[j] |= dp[j - y[i]];
				}
			}
		}
		ll ma = 0;
		for(i=1;i<=w;i++){
			//cout << dp[i] << " ";
			if(dp[i])ma = i;
		}
		//cout << endl;
		ll ans1,ans2;
		ans1 = sumx + sumy + w - ma;
		ans2 = 0;
		//cout << "ma = " << ma << endl;
		cout << max(ans1,ans2) << endl;
	}else{
		cout << sumx + sumy << endl;
	}
	return true;
}

int main(){
	while(solve()){}
}
