import math
import numpy as np
import sys
input = sys.stdin.readline
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N = I()
F = [sum(IL()) for i in range(N)]
P = [IL() for i in range(N)]

f = 0
cnt = 0
ans = 0
mi_ans = int(-10e10)
for i in range(N):
	tmp = max(P[i][:F[i]+1])
	if tmp > 0:
		ans += tmp
		f = 1
	elif tmp == 0:
		l = [j for j in P[i][:F[i]+1] if j<0]
		l.sort()
		mi_ans = max(mi_ans,l[-1])
		cnt += 1
		f = 1
	else:
		mi_ans = max(mi_ans,tmp)

if f == 0 or cnt == N:
	print(mi_ans)
else:
	print(ans)