n = int(eval(input()))

f = [list(map(int,input().split())) for i in range(n)]

p = [list(map(int,input().split())) for i in range(n)]

ans = -10**12

for i in range(1,1024):

    #print("i=",i)

    temp = 0

    for j in range(n): #j = 0,1,2,..,n-1で店番号

        fi = f[j]

        ci = sum([1 for k in range(10) if (i>>k)&1==1 and fi[9-k]==1])

        temp += p[j][ci]

    #print(temp)

    ans = max(ans,temp)

print(ans)

n = int(eval(input()))

f = [int(input().replace(" ",""),2) for i in range(n)] #数にしてしまう

p = [list(map(int,input().split())) for i in range(n)]

ans = -10**12

for i in range(1,1024):

    #print("i=",i)

    temp = 0

    for j in range(n): #j = 0,1,2,..,n-1で店番号

        fi = f[j]

        ci = bin(fi&i).count("1") #bin()は2進法表記のstrになる。

        #print("ci=",ci)

        temp += p[j][ci]

    #print(temp)

    #print(temp)

    ans = max(ans,temp)

print(ans)
n = int(eval(input()))
f = [list(map(int, input().split())) for i in range(n)]
p = [list(map(int, input().split())) for i in range(n)]
ans = -(10**12)
for i in range(1, 1024):
    # print("i=",i)
    temp = 0
    for j in range(n):  # j = 0,1,2,..,n-1で店番号
        fi = f[j]
        ci = sum([1 for k in range(10) if (i >> k) & 1 == 1 and fi[9 - k] == 1])
        temp += p[j][ci]
    # print(temp)
    ans = max(ans, temp)
print(ans)

n = int(eval(input()))
f = [int(input().replace(" ", ""), 2) for i in range(n)]  # 数にしてしまう
p = [list(map(int, input().split())) for i in range(n)]
ans = -(10**12)
for i in range(1, 1024):
    # print("i=",i)
    temp = 0
    for j in range(n):  # j = 0,1,2,..,n-1で店番号
        fi = f[j]
        ci = bin(fi & i).count("1")  # bin()は2進法表記のstrになる。
        # print("ci=",ci)
        temp += p[j][ci]
    # print(temp)
    # print(temp)
    ans = max(ans, temp)
print(ans)

-f = [list(map(int, input().split())) for i in range(n)] +f = [int(input().replace(" ", ""), 2) for i in range(n)]  # 数にしてしまう -        ci = sum([1 for k in range(10) if (i >> k) & 1 == 1 and fi[9 - k] == 1]) +        ci = bin(fi & i).count("1")  # bin()は2進法表記のstrになる。 +        # print("ci=",ci) +    # print(temp)