#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cctype>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define LINF (ll)INF*INF
#define MOD 1000000007
#define rep(i,n) for(int i=0;i<(n);i++)
#define loop(i,a,n) for(int i=a;i<(n);i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

#define int ll //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;
typedef vector<pii> vp;

int gcd(int a, int b){
    if(b==0) return a;
    return gcd(b,a%b);
}
int lcm(int a, int b){
    return a/gcd(a,b)*b;
}

int dp[1010][3010] = {};

signed main(void) {
    int n;
    while(cin >> n,n){
        vp v(n);
        rep(i,n)cin >> v[i].first >> v[i].second;
        sort(all(v));
        int sumr = 0, sumw = 0;
        rep(i,n){
            sumr += v[i].first;
            sumw += v[i].second;
        }
        int ma = v.back().first;
        if(ma <= sumr - ma){
            cout << sumr + sumw << endl;
        }else{
            rep(i,1010)rep(j,3010)dp[i][j] = 0;
            dp[0][0] = 1;
            int MAX = 2*ma-sumr+1;
            rep(i,n)rep(j,MAX){
                dp[i+1][j] += dp[i][j];
                if(j+v[i].second < MAX && i < n-1){
                    dp[i+1][j+v[i].second] += dp[i][j];
                }
            }
            int tmp = -1;
            rep(i,MAX)if(dp[n][i])tmp = max(tmp, i);
            cout << ma*2 + sumw - tmp << endl;
        }
    }
}

