import numpy as np
from collections import Counter
N = int(input())
F = [list(map(int, input().split())) for _ in range(N)]
P = np.array([list(map(int, input().split())) for _ in range(N)])
benefit = 0
open = 0

for i in range(len(F)):
    count = F[i].count(1)
    m = -10^(8)
    for j in range(count+1):
        if m < P[i][j]:
            m = P[i][j]
            open += j
    benefit += m
if open == 0:
    benefit = max(P[:,1])
print(benefit)
