N = int(input())
F = []
P = []
for i in range(N):
    F.append(list(map(int, input().split())))
for j in range(N):
    P.append(list(map(int, input().split())))
even_list = []
odd_list = []
for i in range(N):
    even_sum = 0
    odd_sum = 0
    for j in range(5):
        even_sum += F[i][0::2][j]*P[i][2*j]
        odd_sum += F[i][1::2][j]*P[i][2*j+1]
    even_list.append(even_sum)
    odd_list.append(odd_sum)
print(max(sum(even_list), sum(odd_list)))
