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)
l = []
for i in zip(P0, ans):
  l.append([i,i[0]-i[1]])
l = sorted(l, key=lambda x:-x[1])
ans1 = 0
cnt = 0
if len(ans) == 1:
  print(*ans)
else:
  for i in range(N):
    if l[i][0][0] > l[i][0][1]:
      cnt += 1
      if cnt == len(P0):
        ans1 += l[i][0][1]
        break
      ans1 += l[i][0][0]
    else:
      ans1 += l[i][0][1]
  print(ans1)