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

int dp1[1010];
int dp2[1010];

int main(){
	int N;
	while( cin >> N && N ){
		vector< pair<int,int> > b(N);
		int rsum = 0, wsum = 0;
		for(int i = 0 ; i < N ; i++){
			int r,w;
			cin >> r >> w;
			rsum += r;
			wsum += w;
			b[i] = make_pair(r,w);
		}
		sort(b.begin(),b.end());
		int maxRVal = b.back().first;
		int margin = max(0,2*maxRVal-rsum);
		int required = rsum + margin;
		
		vector<int> normal,special;
		int specialWSum = 0;
		for(int i = 0 ; i < N ; i++){
			if( b[i].first < maxRVal ){
				normal.push_back(b[i].second);
			}else{
				special.push_back(b[i].second);
				specialWSum += b[i].second;
			}
		}
		sort(normal.begin(),normal.end());
		sort(special.begin(),special.end());
		for(int i = 0 ; i < 1010 ; i++)
			dp1[i] = dp2[i] = 0;
		dp1[0] = dp2[0] = 1;
		for(int i = 0 ; i < normal.size() ; i++)
			for(int j = 1000 ; j >= normal[i] ; j--)
				dp1[j] |= dp1[j-normal[i]];
		for(int i = 0 ; i < special.size() ; i++)
			for(int j = 1000 ; j >= special[i] ; j--)
				dp2[j] |= dp2[j-special[i]];
		for(int i = 0 ; i <= 1000 ; i++){
			if( dp1[i] > 0 ) dp1[i] = i;
			if( dp2[i] > 0 ) dp2[i] = i;
		}
		for(int i = 1 ; i <= 1000 ; i++){
			dp1[i] = max(dp1[i-1],dp1[i]);
			dp2[i] = max(dp2[i-1],dp2[i]);
		}
		
		int answer = 0;
		for(int i = 0 ; i <= margin ; i++){
			int x = dp1[i];
			int y = dp2[margin-i];
			if( y == specialWSum ){
				y -= special[0];
			}
			answer = max(answer,x+y);
		}
		
		cout << required + wsum - answer << endl; // 
			
	}
}