#include <bits/stdc++.h>

using namespace std;

#define int long long
#define all(v) begin(v), end(v)
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for(int i = (int)(s); i < (int)(n); i++)

template<class T1, class T2> void chmin(T1 &a, T2 b){if(a>b)a=b;}
template<class T1, class T2> void chmax(T1 &a, T2 b){if(a<b)a=b;}

using pint = pair<int, int>;
using tint = tuple<int, int, int>;
using vint = vector<int>;

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

signed main()
{
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  int N;
  while(cin >> N, N) {
    pair<int,int> x[1001];
    int rsum = 0, wsum = 0;
    rep(i, N) {
      cin >> x[i].first >> x[i].second;
      rsum += x[i].first;
      wsum += x[i].second;
    }
    sort(x, x+N);
    int rx = x[N-1].first;
    if(rsum-rx >= rx) {
      cout << rsum+wsum << endl;
      continue;
    }
    bool dp[1001] = {};
    dp[0] = true;
    rep(i, N-1) {
      for(int j = rx-(rsum-rx); j >= x[i].second; j--) {
	if(dp[j-x[i].second]) dp[j] = true;
      }
    }
    int ans = 0;
    rep(i, rx-(rsum-rx) + 1) {
      if(dp[i]) ans = i;
    }
    cout << rx+rx+wsum-ans << endl;
  }
  return 0;
}