N=int(eval(input()))

F=[int(input().replace(' ',''),2)for _ in[0]*N]

P=[list(map(int,input().split()))for _ in[0]*N]

ans = -float('inf')

for i in range(1,2**10):

    sum = 0

    for k in range(N):

        sum += P[k][bin(F[k]&i).count('1')]

    if ans < sum:

        ans = sum

print(ans)
N=int(eval(input()))

F=[int(input().replace(' ',''),2)for _ in[0]*N]

P=[list(map(int,input().split()))for _ in[0]*N]

print((max(sum([p[bin(f&i).count('1')]for f,p in zip(F,P)])for i in range(1,2**10))))
N = int(eval(input()))
F = [int(input().replace(" ", ""), 2) for _ in [0] * N]
P = [list(map(int, input().split())) for _ in [0] * N]
ans = -float("inf")
for i in range(1, 2**10):
    sum = 0
    for k in range(N):
        sum += P[k][bin(F[k] & i).count("1")]
    if ans < sum:
        ans = sum
print(ans)

N = int(eval(input()))
F = [int(input().replace(" ", ""), 2) for _ in [0] * N]
P = [list(map(int, input().split())) for _ in [0] * N]
print(
    (
        max(
            sum([p[bin(f & i).count("1")] for f, p in zip(F, P)])
            for i in range(1, 2**10)
        )
    )
)

-ans = -float("inf") -for i in range(1, 2**10): -    sum = 0 -    for k in range(N): -        sum += P[k][bin(F[k] & i).count("1")] -    if ans < sum: -        ans = sum -print(ans) +print( +    ( +        max( +            sum([p[bin(f & i).count("1")] for f, p in zip(F, P)]) +            for i in range(1, 2**10) +        ) +    ) +)