def solve(n, P): sum_b = 0 aa = bb = 0 for a, b in P: if a < b: aa += a elif a > b: bb += b else: if aa < bb: aa += a else: bb += b sum_b += b ma = max(aa, bb) dp = [0] * ma t = 1 for a, b in P: for j in range(min(t, ma - a - 1), -1, -1): dp[j + a] = max(dp[j + a], dp[j] + b) t = max(t, min(t, ma - a - 1) + a) ans = ma for i in range(ma): ans = min(ans, max(i, sum_b - dp[i])) return ans def main(): n = int(input()) P = [None] * n for i in range(n): P[i] = tuple(map(int, input().split())) ans = solve(n, P) print(ans) if __name__ == '__main__': main()