#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <algorithm>

#define REP(i,n) for(int i=0;i<(int)(n);i++)

using namespace std;

int main() {
  while(1){
    int n;
    cin>>n;
    if (!n) break;
    vector<pair<int,int>> b;
    REP(i,n) {
      int r,w;
      cin>>r>>w;
      b.emplace_back(r,w);
    }
    sort(b.rbegin(),b.rend());
    int sum = 0;
    REP(i,n) sum += b[i].first;
    int sumw = 0;
    REP(i,n) sumw += b[i].second;
    int b0r = b[0].first;
    if (b0r*2 > sum) {
      int rem = b0r*2 - sum;
      vector<vector<bool>> dp(n,vector<bool>(rem+1101,false));
      dp[0][0] = true;
      REP(i,n-1)
        REP(j,rem+1) {
          dp[i+1][j] = dp[i+1][j] || dp[i][j];
          dp[i+1][j+b[i+1].second] = dp[i+1][j+b[i+1].second] || dp[i][j];
        }
      int mxb = 0;
      REP(i,rem+1)
        if (dp[n-1][i])
          mxb = i;
      //int rbmt = max(2 * b0r, b0r + mxb);
      cout << b0r*2 + (sumw - mxb) << endl;
    } else {
      cout << (sum + sumw) << endl;
    }
  }
  return 0;
}