N = int(input())
F = [list(map(int,input().split())) for _ in range(N)]
P = [list(map(int,input().split())) for _ in range(N)]
P0 = []
for i in range(N):
  P0.append(P[i][0])
ans = []
for i in range(N):
  c = F[i].count(1)
  m = max(P[i][1:c+1])
  ans.append(m)
ans2 = -float("inf")
if len(ans) == 1:
  print(*ans)
else:
  for i in range(N):
      ans1 = P0[i] + sum(ans[:i]) + sum(ans[i+1:])
      ans2 = max(ans2, sum(ans), ans1)
  print(ans2)