#include <bits/stdc++.h>
using namespace std;

typedef ostringstream OSS;
typedef istringstream ISS;

typedef long long LL;
typedef pair<int, int> PII;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<double> VD;
typedef vector<VD> VVD;
typedef vector<string> VS;
typedef vector<VS> VVS;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<PII> VPII;

#define fst first
#define snd second
// #define Y first
// #define X second
#define MP make_pair
#define PB push_back
#define EB emplace_back 
#define ALL(x) (x).begin(),(x).end()
#define RANGE(x,y,maxX,maxY) (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))
#define DUMP( x ) cerr << #x << " = " << ( x ) << endl
#define rep(i, N) for (int i = 0; i < (N); i++)
#define rep2(i, j, N, M) rep(i, N) rep(j, M)
#define rep3(i, j, k, N, M, L) rep(i, N) rep(j, M) rep(k, L)
#define REP(i, init, N) for (int i = (init); i < (N); i++)

template < typename T > inline T fromString(const string &s) { T res; ISS iss(s); iss >> res; return res; };
template < typename T > inline string toString(const T &a) { OSS oss; oss << a; return oss.str(); };

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const double DINF = 0x3f3f3f3f;
const int DX[]={1,0,-1,0},DY[]={0,-1,0,1};

int N;
VI R, W;

int solve() {
    // ?????§r???idx
    int max_r_i;
    {
        auto it = max_element(ALL(R));
        max_r_i = it - R.begin();
    }

    // ?????§r????????????sum
    int other_sum = accumulate(ALL(R), 0) - R[max_r_i];

    // calc time
    int r_time;
    int w_time;
    if (other_sum >= R[max_r_i]) {
        r_time = other_sum + R[max_r_i];
        w_time = accumulate(ALL(W), 0);
    } else {
        r_time = R[max_r_i] * 2;

        int T = R[max_r_i] - other_sum;

        VB dp(T + 1);
        dp[0] = true;

        rep(i, N) {
            if (i == max_r_i) continue;
            for (int j = T; j >= 0; --j) {
                if (dp[j]) {
                    int t = j + W[i];
                    if (t <= T) {
                        dp[t] = true;
                    }
                }
            }
        }

        int t = 0;
        rep(i, T + 1) {
            if (dp[i]) {
                t = i;
            }
        }

        w_time = accumulate(ALL(W), 0) - t;
    }

    return r_time + w_time;
}

int main(void) {
    while (cin >> N, N) {
        R = VI(N);
        W = VI(N);

        rep(i, N) {
            cin >> R[i] >> W[i];
        }

        cout << solve() << endl;
    }

	return 0;
}