import sys sys.setrecursionlimit(1000000) def read_data(): N = int(input()) VT = [] for n in range(N): v, t = map(int, input().split()) VT.append((v + t, v, t)) return N, VT def solve(N, VT): VT.sort(reverse=True) upper = min(VT[0][0], sum(v for vt, v, t in VT) + 1) lower = 1 mid = upper - 1 while lower + 1 < upper: if is_valid(mid, VT): lower = mid else: upper = mid mid = (upper + lower) // 2 return lower def is_valid(val, VT): head = 0 nexts = list(range(1, len(VT) + 1)) prevs = list(range(-1, len(VT) - 1)) lower = val upper = float('inf') return dfs(lower, upper, VT, head, nexts, prevs) def dfs(lower, upper, VT, head, nexts, prevs): '''VTのうち、used でないものを使って、lower 以上、upper 未満の選び方をできるかを返す。 ''' if lower <= 0: return True i = head end = len(VT) while i != end: val, v, t = VT[i] if v >= upper: i = nexts[i] continue if val < lower: return False if i == head: nhead = nexts[i] else: nhead = head nexts[prevs[i]] = nexts[i] if nexts[i] != end: prevs[nexts[i]] = prevs[i] result = dfs(lower - v, min(upper, t), VT, nhead, nexts, prevs) if i != head: nexts[prevs[i]] = i if nexts[i] != end: prevs[nexts[i]] = i if result: return True i = nexts[i] return False N, VT = read_data() print(solve(N, VT))