import sys
N = int(input())

time_lis = [0] * N
profit = [0] * N
for n in range(N):
    time_lis[n] = list(map(int, input().split()))
for n in range(N):
    profit[n] = list(map(int, input().split()))

ans = 0
always_zero = True
for n in range(N):
    count_1 = len([i for i in time_lis[n] if i == 1])
    ans += max(profit[n][:count_1 + 1])
    if profit[n].index(ans) != 0:
        always_zero = False
if always_zero:
    max_prof = [item[1] for item in profit]
    print(max(max_prof))
    sys.exit()
print(ans)