n = int(input())
f = []
p = []
for _ in range(n):
    tmp = [int(x) for x in input().split()]
    f.append(tmp)
for _ in range(n):
    tmp = [int(x) for x in input().split()]
    p.append(tmp)

ans = 0
pikked = False
tmp = []
for i in range(n):
    cnt = f[i].count(1)
    ans += max(p[i][:cnt+1])
    if max(p[i][:cnt+1]) != p[i][0]:
        pikked = True
    tmp.append(p[i][1]-p[i][0])

if not pikked:
    ans += min(tmp)
print(ans)