n = int(input())
f = []
for i in range(n):
    f.append(list(map(int, input().split())))
p = []
for i in range(n):
    p.append(list(map(int, input().split())))
f_count = []
for i in f:
    f_count.append(i.count(1))
flag = 1
ans = 0
for i in range(n):
    if p[i][0:f_count[i]+1].index(max(p[i][0:f_count[i]+1])) != 0:
        flag = 0
if flag:
    check = []
    for i in range(n):
        check.append(p[i][1])
        check_index = check.index(max(check))
    for i in range(n):
        if i == check_index:
            ans += p[i][1]
        else:
            ans += p[i][0]
else:
    for i in range(n):
        ans += max(p[i][0:f_count[i]+1])
print(ans)