n=int(input())
f=[[int(i)for i in input().split()]for i in range(n)]
p=[[int(i)for i in input().split()]for i in range(n)]
p2=[]
l=[]
for i in range(n):
    p2.append([p[i][j] for j in range(f[i].count(1))])
    l.append(max(p2[i]))
#print(l)
print(sum(l))