#include <bits/stdc++.h>

// Shrotening
#define fst first
#define snd second
#define pb push_back

// Loop
#define FOR(i,a,b) for(auto i=(a);i<(b);++i)
#define RFOR(i,a,b) for(auto i=(a);i>=(b);--i)

#define REP(i,a) for(long i=0;i<(a);++i)
#define RREP(i,a) for(long i=(a);i>=0;--i)

#define EACH(i,a) for(auto (i)=(a).begin(),_END=(a).end();i!=_END;++i)
#define REACH(i,a) for(auto (i)=(a).rbegin(),_END=(a).rend();i!=_END;++i)

//Algorithm
#define ALL(a) (a).begin(), a.end()
#define RALL(a) (a).rbegin(), a.rend()
#define EXIST(a,x) ((a).find(x)!=(a).end())
#define SORT(a) std::sort((a).begin(), (a).end())
#define UNIQUE(a) std::sort((a).begin(), a.end()), a.erase(std::unique((a).begin(), a.end()), a.end());
#define SUM(a) std::accumulate((a).begin(), (a).end(), 0);

//Setting
#define OPT std::cin.tie(0);std::ios::sync_with_stdio(false);

//debug message
bool debug = true;
#define MSG(s)   if(debug){std::cout << s << std::endl;}
#define DEBUG(x) if(debug){std::cout << "debug(" << #x << "): " << x << std::endl;}

//alias
typedef long long LL;
typedef std::vector<bool> VB;
typedef std::vector<char> VC;
typedef std::vector<int>  VI;
typedef std::vector<long> VL;
typedef std::vector<long long> VLL;

typedef std::vector< VB > VB2;
typedef std::vector< VC > VC2;
typedef std::vector< VI > VI2;
typedef std::vector< VL > VL2;
typedef std::vector< VLL > VLL2;

typedef std::pair<int,int> PII;

int N;
std::vector< PII > d(1000);
VI2 dp(1003, VI(1003));

int main() {
    while(std::cin >> N, N != 0) {
        REP(i, N) {
            std::cin >> d[i].fst >> d[i].snd;
        }
        std::sort(d.begin(), d.begin() + N);

        int sum = 0;
        REP(i, N-1) {
            //1~N-1までの和
            sum += d[i].fst;
        }
        if(sum >= d[N-1].fst) {
            int ans = 0;
            REP(i, N) {
                ans += (d[i].fst + d[i].snd);
            }
            std::cout  << ans << std::endl;
        } else {
            REP(i, 1003) { 
                REP(j, 1003) { 
                    dp[i][j] = 0;
                }
            }
            dp[0][0] = 1;

            REP(i, N-1) {
                REP(j, d[N-1].fst - sum + 1) {
                    dp[i+1][j] |= dp[i][j];
                    dp[i+1][j + d[i].snd] |= dp[i][j];
                }
            }

            int maxi = 0;
            REP(i, d[N-1].fst - sum + 1) {
                if(dp[N-1][i]) {
                    maxi = std::max(maxi, (int)i);
                }
            }

            int t = d[N-1].fst - sum;
            t -= maxi;

            REP(i, N) {
                t += d[i].snd;
            }
            std::cout << d[N-1].fst + sum + t << std::endl;
        }
    }
}
