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

using ll = long long;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;

#define REP(i,n) for(int i=0; i<(n); i++)
#define FOR(i,a,b) for(int i=(a); i<(b); i++)
#define FORR(i,a,b) for(int i=(b)-1; i>=(a); i--)

#define ALL(v) (v).begin(), (v).end()
#define CHMAX(a,b) a=max(a,b)
#define CHMIN(a,b) a=min(a,b)

#define DEBUG(x) cerr<<#x<<": "<<(x)<<endl

int n;
int r[1252], w[1252];
bool ok[2521];

int main(){
    while(true){
        scanf("%d",&n);
        if(n==0)break;
        REP(i,n)scanf("%d%d",r+i,w+i);
        int rsum = accumulate(r,r+n,0);
        int mx = *max_element(r,r+n);
        if(2*mx <= rsum){
            int ans = rsum + accumulate(w,w+n,0);
            printf("%d\n",ans);
        }else{
            int wsum = accumulate(w,w+n,0);
            int mxid = max_element(r,r+n)-r;
            int rest = 2*mx - rsum;
            fill(ok,ok+1252,false);
            ok[0] = true;
            REP(i,n)if(i != mxid){
                FORR(j,w[i],rest+1)ok[j] = ok[j] || ok[j-w[i]];
            }
            int go = 0;
            REP(i,rest+1)if(ok[i])go=i;
            int ans = 2*mx + (wsum-go);
            printf("%d\n",ans);
        }
    }
    return 0;
}
