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)] 

ans = 0
for i in range(N):
    sum_open = sum(F[i])
    ans += max(P[i][:sum_open+1])
print(ans)
