#include <bits/stdc++.h>
#define range(i, a, b) for(int i = (a); i < (b); i++)
#define rep(i, a) range(i, 0, a)
using namespace std;

int main() {
	int n;
	while (cin >> n, n) {
		vector <int> r, w;
		r = w = vector <int> (n);
		int sumR = 0, sumW = 0;
		rep (i, n) {
			cin >> r[i] >> w[i];
			sumR += r[i];
			sumW += w[i];
		}

		vector <int> copyR = r;
		sort(r.begin(), r.end());

		if (sumR >= 2*r.back()) {
			cout << sumR + sumW << endl;
		} else {
			int gap = r.back() - (sumR - r.back());
			vector <vector <int>> dp(n, vector <int> (gap + 1, 0));
			dp[0][0] = true;
			w.erase(w.begin() + (find(copyR.begin(), copyR.end(), r.back())-copyR.begin()));
			range (i, 1, n) {
				rep (j, gap + 1) {
					if (j - w[i - 1] < 0) continue;
					dp[i][j] |= dp[i - 1][j];
					dp[i][j] |= dp[i - 1][j - w[i-1]];
				}
			}

			int maxJ = 0;
			rep (i, n) rep (j, gap + 1) if (dp[i][j]) maxJ = max(maxJ, j);
			cout << 2*r.back() + sumW - maxJ << endl;
		}
	}
	return 0;
}
