n = int(input())
f = [[int(i) for i in input().split()] for _ in range(n)]
p = [[int(i) for i in input().split()] for _ in range(n)]

f_list = [sum(i) for i in f]

p_list = []

for l, i in zip(p, f_list):
    p_list.append(l[:i+1])

false_ans = 0
list1 = []
best_ans = 0

for p in p_list:
    false_ans += p[0]
    list1.append(p[1])
    best_ans = max(p)

if best_ans == false_ans:
    print(max(list1))
else:
    print(best_ans)
