#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	int N;
	
	while (scanf("%d", &N) && N){
		pair<int, int> a[1024];
		for (int i = 0; i < N; i++){
			scanf("%d %d", &a[i].first, &a[i].second);
		}
		
		sort(a, a + N);
		
		int sum = 0, ans = 0;
		for (int i = 0; i < N - 1; i++) sum += a[i].first;
		for (int i = 0; i < N; i++) ans += a[i].first + a[i].second;
		
		if (sum >= a[N - 1].first){
			printf("%d\n", ans);
		}
		else {
			bool dp[1001] = {false};
			dp[0] = true;
			int spend = 0;
			for (int i = 0; i < N - 1; i++){
				for (int j = a[N - 1].first - sum; j >= a[i].second; j--){
					dp[j] |= dp[j - a[i].second];
					if (dp[j]) spend = max(spend, j);
				}
			}
			printf("%d\n", ans + a[N - 1].first - sum - spend);
		}
	}
	
	return (0);
}