#include <bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef pair<int,int>P;
#define F first
#define S second

int dp[1111];
int a,b,n,sum,sum2;
vector<P>v;

signed main(){
  while(cin>>n,n){
    sum=sum2=0;
    v.clear();
    r(i,n){
      cin>>a>>b;
      v.push_back(P(a,b));
    }
    sort(v.begin(),v.end());
    r(i,n-1)sum+=v[i].F;
    r(i,n)sum2+=v[i].S;
    if(sum>=v[n-1].F){
      cout<<sum+sum2+v[n-1].F<<endl;
      continue;
    }
    memset(dp,0,sizeof(dp));
    dp[0]=1;
    for(int i=0;i<n-1;i++){
      for(int j=1100;j>=0;j--)if(dp[j]){
        if(j+v[i].F<1001)dp[j+v[i].F]=1;
        if(j+v[i].F+v[i].S<1001)dp[j+v[i].F+v[i].S]=1;
      }
    }
    for(int i=v[n-1].F;i>=0;i--){
      if(!dp[i])continue;
      cout<<v[n-1].F-i+v[n-1].F+sum+sum2<<endl;
      break;
    }
  }
}
