n=int(input())
F=[sum(list(map(int,input().split()))) for i in range(n)]
P=[list(map(int,input().split())) for i in range(n)]
ans=0
for i in range(n):
  ans+=max(P[i][:F[i]+1])
if ans==0:
  for i in range(n):
    tmp=P[i][:F[i]+1]
    ans=max(tmp)
    if ans ==0:
      ans=max(ans,sorted(tmp)[-2])
print(ans)
