#include <iostream>
#include <algorithm>
#include <map>

using namespace std;

const int N = 1000;

int n;
pair<int, int> data[N];

int calc(int emp){
  bool dp[N][N+1];
  fill(dp[0], dp[N], false);
  dp[0][0] = true;
  for(int i=0;i<n-1;i++){
    for(int j=0;j<=emp;j++){
      dp[i+1][j] |= dp[i][j];
      if(j+data[i].second <= emp) dp[i+1][j+data[i].second] |= dp[i][j];
    }
  }
  int tmp = 0;
  for(int i=0;i<=emp;i++) if(dp[n-1][i]) tmp = i;
  int res = data[n-1].first * 2;
  for(int i=0;i<n;i++) res += data[i].second;
  return res - tmp;
}

int main(){
  while(cin >> n && n){
    for(int i=0;i<n;i++){
      int a, b;
      cin >> a >> b;
      data[i] = make_pair(a, b);
    }
    sort(data, data+n);
    int sum = 0;
    for(int i=0;i<n-1;i++) sum += data[i].first;
    int ans = 0;
    if(sum >= data[n-1].first){
      for(int i=0;i<n;i++) ans += data[i].first + data[i].second;
    }else ans = calc(data[n-1].first - sum);
    cout << ans << endl;
  }
}