import numpy as np
n = int(input())
f = []
p = []
for i in range(n):
    f.append(list(map(int, input().split())))
for i in range(n):
    p.append(list(map(int, input().split())))

pattern = []
tmp = []
for i in range(n):
    tmp.append(f[i][0])
pattern.append(tmp)

for i in range(1, 10):
    tmp = []
    tmp_loop = pattern[:]
    for j in range( n):
        tmp.append(f[j][i])
    for pat in tmp_loop:
        pattern.append(list(np.array(pat)+np.array(tmp)))
    pattern.append(tmp)

ans = -1000000
for pat in pattern:
    a = 0
    for i in range(n):
        a += p[i][pat[i]]
    ans = max(ans, a)
print(ans)
