from collections import defaultdict INF = 1 << 30 n = int(input()) items = [tuple(map(int, input().split())) for _ in range(n)] s = 0 dp = defaultdict(bool) dp[0] = True for i in range(n): ai, bi = items[i] s += ai ndp = defaultdict(bool) for x, _ in dp.items(): ndp[x] = True ndp[x + bi - ai] = True dp = ndp ans = s + min(list(dp.keys())) print(ans)