#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <functional>
#include <set>
#include <queue>
#include <cctype>
#include <climits>
#include <stack>
using namespace std;
  
 
  
int main(){
  
    int n; 
    while(scanf("%d",&n),n){
 
        vector<pair<int,int> > rw(n);
        int sum_r=0,sum_w=0;
        for(int i=0;i<n;i++){
            cin>>rw[i].first>>rw[i].second;
            sum_r+=rw[i].first;
            sum_w+=rw[i].second;
        }
        sort(rw.begin(),rw.end(),greater< pair<int,int> >());
 
        int r0=rw[0].first;
        int r=sum_r-r0;
 
        if(r0 <= r){
            cout<<sum_r+sum_w<<endl;
        }else{
 
            int res=2*r0;
            vector<vector<bool> > dp(1001,vector<bool>(1000,false));
 
            dp[1][0]=true;
            for(int i=1;i<n;i++){
                for(int j=0;j<=1000;j++){
                    if(dp[i][j]){
                        dp[i+1][j]=true; 
                        if(j+rw[i].second<=1000)
                            dp[i+1][j+rw[i].second]=true; 
                    }
                }
            }
 
            int opt=0;
            for(int i=1;i<=r0-r;i++){
                if(dp[n][i]) opt=i;
            }
 
            res+=sum_w-opt;
            printf("%d\n",res);
 
 
 
        }
         
         
    }
}