#include <bits/stdc++.h>
using namespace std;

int main()
{
  int N, r[1000], w[1000];
  bool dp[1000];
  
  while(cin >> N, N) {
    for(int i = 0; i < N; i++) {
      cin >> r[i] >> w[i];
    }
    int sum = accumulate(r, r + N, 0);
    int* big = max_element(r, r + N);
    int add = 0;
  
    if(*big * 2 > sum) {
      int space = *big * 2 - sum;
      swap(w[big - r], w[N - 1]);
      memset(dp, false, sizeof(bool) * (space + 1));
      dp[0] = true;
      for(int i = 0; i < N - 1; i++) {
        for(int j = space; j >= w[i]; j--) {
          dp[j] |= dp[j - w[i]];
        }
      }
      for(int i = space; i >= 0; i--) {
        if(dp[i]) {
          add = space - i;
          break;
        }
      }
    }
    cout << sum + accumulate(w, w + N, 0) + add << endl;
  }
}