n = int(input())
f = [sum(list(map(int,input().split()))) for _ in range(n)]
p = [list(map(int,input().split())) for _ in range(n)]
ans = []
zero_list = 0
for i in range(n):
  for j in range(f[i]+1):
    if j == 0:
      cnt = p[i][j]
      zero_cnt = True
    else:
      if p[i][j] > cnt:
        cnt = p[i][j]
        zero_cnt = None
      else:
        pass
  if zero_cnt:
    zero_list += 1
  ans.append(cnt)
if zero_list != n:
  print(sum(ans))
else:
  ans2 = []
  for i in range(n):
    ans2.append(sum(ans)-p[i][0]+p[i][1])
  print(max(ans2))
    