#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
  int N;
  while(cin>>N,N) {
    vector<int> r(N),w(N);
    int maxi=0,rsum=0,wsum=0;
    for(int i=0; i<N; i++) {
      cin >> r[i] >> w[i];
      maxi = ((r[i]>r[0])?i:0);
      rsum += r[i];
      wsum += w[i];
    }
    swap(r[0],r[maxi]);
    swap(w[0],w[maxi]);
    if(r[0]*2<=rsum){
      cout << rsum+wsum << endl;
    }else{
      int ds = r[0]*2-rsum;
      vector<vector<int> > dp(N, vector<int>(ds+1,0));
      for(int n=1; n<N; n++)
	for(int s=1; s<=ds; s++)
	  dp[n][s] = max(dp[n-1][s],(s<w[n]?0:dp[n-1][s-w[n]]+w[n]));
      cout << r[0]*2+wsum-dp[N-1][ds] << endl;
    }
  }
}