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())))

ps = []
flag = 0
for i in range(n):
    s = sum(f[i])
    m = max(np.array(p[i])[:s+1])
    flag += np.argmax(np.array(p[i])[:s+1])
    ps.append(m)
if flag == 0:
    ans = 0
    for i in range(n):
        ans += p[i][f[i][0]]
    for i in range(1, 10):
        tmp = 0
        for j in range(n):
            tmp += p[j][f[j][i]]
        ans = max(ans, tmp)
    print(ans)
else:
    print(sum(ps))
