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

int n;
int r[1010], w[1010];
int dp[1000010];

int main(){
    while(cin >> n && n){
        int sum_r = 0, sum_w = 0, max_r = 0;
        for(int i = 0; i < n; ++i){
            cin >> r[i] >> w[i];
            if(r[max_r] < r[i]){
                max_r = i;
            }
            sum_r += r[i];
            sum_w += w[i];
        }

        if(r[max_r] * 2 <= sum_r){
            cout << sum_r + sum_w << endl;
        } else {
            swap(r[n-1], r[max_r]);
            swap(w[n-1], w[max_r]);
            int rem = r[n-1]*2 - sum_r;
            for(int i = 0; i <= rem; ++i) dp[i] = false;
            dp[0] = true;
            for(int i = 0; i < n-1; ++i){
                for(int j = rem; j >= w[i]; --j){
                    dp[j] |= dp[j-w[i]];
                }
            }
            int dead = -1;
            for(int i = rem; i >= 0; --i){
                if(dp[i]){
                    dead = rem - i;
                    break;
                }
            }
            cout << sum_r + sum_w + dead << endl; 
        }
    }
}