#!/mnt/c/Users/moiki/bash/env/bin/python

# N,M = map(int, input().split())

N = int(eval(input()))

F = [list(map(int, input().split())) for _ in range(N)]

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



ans = - 10**10



for i in range(1,1<<10):

    score = 0

    for n in range(N):

        f = "".join(map(str,F[n])) 

        b = "{:010b}".format(i)

        c = [int(f[i]) & int(b[i]) for i in range(10)]

        cnt = len(list([x for x in c if x]))

        score += P[n][cnt]

    ans = max(ans, score)

print(ans)

N = int(eval(input()))

F = [ int(input().replace(" ", ""), 2) for _ in range(N)]

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

ans = - 10**10



for i in range(1,1<<10):

    ans = max(ans, sum(P[n][bin(i&F[n]).count('1')] for n in range(N)))

print(ans)

#!/mnt/c/Users/moiki/bash/env/bin/python
# N,M = map(int, input().split())
N = int(eval(input()))
F = [list(map(int, input().split())) for _ in range(N)]
P = [list(map(int, input().split())) for _ in range(N)]
ans = -(10**10)
for i in range(1, 1 << 10):
    score = 0
    for n in range(N):
        f = "".join(map(str, F[n]))
        b = "{:010b}".format(i)
        c = [int(f[i]) & int(b[i]) for i in range(10)]
        cnt = len(list([x for x in c if x]))
        score += P[n][cnt]
    ans = max(ans, score)
print(ans)

N = int(eval(input()))
F = [int(input().replace(" ", ""), 2) for _ in range(N)]
P = [list(map(int, input().split())) for _ in range(N)]
ans = -(10**10)
for i in range(1, 1 << 10):
    ans = max(ans, sum(P[n][bin(i & F[n]).count("1")] for n in range(N)))
print(ans)

-#!/mnt/c/Users/moiki/bash/env/bin/python -# N,M = map(int, input().split()) -F = [list(map(int, input().split())) for _ in range(N)] +F = [int(input().replace(" ", ""), 2) for _ in range(N)] -    score = 0 -    for n in range(N): -        f = "".join(map(str, F[n])) -        b = "{:010b}".format(i) -        c = [int(f[i]) & int(b[i]) for i in range(10)] -        cnt = len(list([x for x in c if x])) -        score += P[n][cnt] -    ans = max(ans, score) +    ans = max(ans, sum(P[n][bin(i & F[n]).count("1")] for n in range(N)))