//c
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
typedef pair<int, int> pii;

int N;

int solve(vector<pii> v){
	
	sort(v.begin(), v.end());
	
	int rl, rs = 0, ws = 0;
	bool dp[1010] = {0};
	
	rl = v[N-1].first;
	
	for(int i=0; i<N; ++i){
		rs += v[i].first;
		ws += v[i].second;
	}
	
	rs -= rl;
	
	if(rs >= rl){
		return rs + rl + ws;
	}
	
	dp[0] = true;
	
	for(int i = 0; i < N-1; ++i){
		for(int j=rl-rs; j >=0; --j){
			
			if( dp[j] && j + v[i].second <= rl - rs )
				dp[j + v[i].second] = true;
				
		}
	}
	
	int maxwrite = 0;
	
	for(int i = rl - rs; i >= 0; --i){
	
		if(dp[i])
			return 2*rl + ws - i;
	}
}

int main(){
	
	while(cin>>N, N){
		
		vector<pii> v;
		int r, w;
		
		for(int i=0; i<N; ++i){
			
			cin >> r >> w;
			v.push_back(make_pair(r, w));
		}
		
		cout << solve(v) << endl;
	}
	
	return 0;
}