#ifndef _WIN32
#include<iostream>
#endif 

#include<cmath>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;

#define FOR(i,bg,ed) for(int i = (bg);i<(ed);i++)
#define REP(i,n) FOR(i,0,n)
struct cww {
	cww() {
		ios::sync_with_stdio(false);
		cin.tie(0);
	}
}star;
template<typename T>
istream & operator>> (istream &is, vector<T>&v) {
	for (auto &it : v)is >> it;
	return is;
}
#define fi first
#define se second
#define ALL(x) (x).begin(),(x).end()
typedef pair<int, int> P;
const int INF = 1e7;
int main() {
	int n;
	while (cin >> n, n) {
		vector<P> rw(n);
		REP(i, n)cin >> rw[i].fi >> rw[i].se;
		sort(ALL(rw));
		reverse(ALL(rw));
		int rsum = 0;
		int wsum = 0;
		REP(i, n) {
			rsum += rw[i].fi;
			wsum += rw[i].se;
		}
		if (rw.front().first * 2 <= rsum) {
			cout << rsum + wsum << endl;
		}
		else {
			int rest = rw.front().first * 2 - rsum;
			vector<int> dp(rest + 1, 0);
			dp[0] = 1;
			FOR(i, 1, n) {
				for (int j = rest; j >= rw[i].se; j--) {
					dp[j] |= dp[j - rw[i].se];
				}
			}
			int ans = 0;
			REP(i, rest + 1)if (dp[i])ans = i;
			cout << rest - ans + rsum + wsum << endl;
		}


	}


	return 0;
}