#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
typedef long long Int;
string str;
int dp[1080], n;

int main(){
	int r[1080], w[1080];
	while(cin >> n, n){
		int m = 0, sr = 0, sw = 0;
		for(int i = 0;i < n;i++){
			cin >> r[i] >> w[i];
			m = max(m, r[i]);
			sr += r[i];
			sw += w[i];
		}
		if(m * 2 <= sr){
			cout << sr + sw << endl;
			continue;
		}
		fill(dp, dp + 1080, 0);
		dp[0] = 1;
		for(int i = 0;i < n;i++){
			if(r[i] == m)continue;
			for(int j = 1079;j - w[i] >= 0;j--){
				dp[j] |= dp[j - w[i]];
			}
		}
		int ok = m * 2 - sr;
		while(dp[ok] == 0)ok--;
		cout << m + m + sw - ok << endl;
	}
	return 0;
}