#define _USE_MATH_DEFINES

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <list>

using namespace std;

typedef pair<long long int, long long int> P;

long long int INF = 1e18;
long long int MOD = 1e9 + 7;

int main(){
	
	while(true){
		int N;
		cin >> N;
		if(N == 0){
			break;
		}
		
		int r[1100], w[1100], r_sum = 0, w_sum = 0;
		
		for(int i = 0; i < N; i++){
			cin >> r[i] >> w[i];
			r_sum += r[i];
			w_sum += w[i];
		}
		
		bool flag = true;
		int pos, rem;
		for(int i = 0; i < N; i++){
			if(r[i] * 2 > r_sum){
				pos = i;
				rem = 2 * r[i] - r_sum;
				flag = false;
			}
		}
		
		if(flag){
			cout << r_sum + w_sum << endl;
			continue;
		}
		
		bool DP[1100] = {};
		DP[0] = true;
		for(int i = 0; i < N; i++){
			if(i == pos){
				continue;
			}
			for(int j = rem; j >= w[i]; j--){
				DP[j] |= DP[j - w[i]];
			}
		}
		
		for(int j = rem; j >= 0; j--){
			if(DP[j]){
				cout << r_sum + w_sum + rem - j << endl;
				break;
			}
		}
	}
	return 0;
}
