#include"bits/stdc++.h"
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for (int i = b - 1; i >= a; i--)
#define RREP(i, n) RFOR(i, 0, n)
#define rep(i, a, b) for (auto i = a; i < b; i++)
#define rrep(i, a, b) for (auto i = a; i > b; i--)
#define range(i, a, b) (a <= i && i < b)
#define ll long long
#define fi first
#define fs first
#define se second
#define sc second
//#define int long long
using namespace std;
using vec = vector<int>;
using mat = vector<vec>;
typedef pair<int, int> P;
 
int n;
vec dp;
 
void solve() {
    vector<P> a(n);
    int sum1 = 0, sum2 = 0;
    REP(i, n) cin >> a[i].fs >> a[i].sc, sum1 += a[i].fs, sum2 += a[i].sc;
 
    sort(a.begin(), a.end());
 
    if (sum1 >= a.back().fs * 2) {
        cout << sum1 + sum2 << endl;
        return;
    }
 
    int k = a.back().fs * 2 - sum1;
    vec dp(k + 1, 0);
    dp[0] = 1;
 
    REP(i, n - 1) {
        RREP(j, k) {
            if (j + a[i].sc <= k) dp[j + a[i].sc] |= dp[j];
        }
    }
 
    int mx = 0;
    REP(i, k + 1) if (dp[i]) mx = i;
 
    cout << a.back().fs * 2 + sum2 - mx << endl;
}
 
int main() {
    while (cin >> n, n) solve();
}
