#include <iostream>
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REP(i,b) FOR(i,0,b)
typedef long long LL;
using namespace std;
const int SIZE=1001;
int r[2*SIZE],w[2*SIZE];
bool DP[2*SIZE][2*SIZE];
int N;
int leastSpace(int n,int maximN){
	DP[0][0]=true;
	if(maximN) DP[0][w[0]]=true;
	int maxim=0;
	FOR(i,1,N){
		FOR(j,0,n+1){
			if(maximN!=i && j>=w[i])
				DP[i][j]=DP[i-1][j]|DP[i-1][j-w[i]];
			else DP[i][j]=DP[i-1][j];
			if(DP[i][j]) maxim=max(maxim,j);
		}
	}
	return n-maxim;
}
int main() {
	
	
	while(cin >> N && N){
		REP(i,SIZE*2){
			r[i]=w[i]=0;
		}
		REP(i,SIZE)
			REP(j,SIZE)
				DP[i][j]=0;
		int sum=0,maxim=0,sumW=0,maximN=0;
		REP(i,N){
			cin >> r[i] >> w[i];
			sum+=r[i];
			maxim=max(maxim,r[i]);
			if(r[i]==maxim) maximN=i;
			sumW+=w[i];
		}
		if(maxim*2<=sum){
			cout << sum+sumW << endl;
			continue;
		}
		int space=maxim*2-sum;
		cout << sum+sumW+leastSpace(space,maximN) << endl;
	}
	// your code goes here
	return 0;
}