import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
def main():
    N = I()
    F = []
    P = []
    n_open_time = [0] * N
    selected_ix = set()
    least_ans = -inf
    cur_ans = 0
    for i in range(N):
        f = LI()
        F.append(f)
        n_open_time[i] = sum(f)
    for _ in range(N):
        p = LI()
        P.append(p)
    for i in range(N):
        n_open = n_open_time[i]
        i_max_prof = -1
        ix_max_prof = -1
        for j in range(n_open+1):
            if i_max_prof <= P[i][j]:
                i_max_prof = P[i][j]
                ix_max_prof = j
        cur_ans += i_max_prof
        selected_ix.add(ix_max_prof)
        if n_open > 0:
            least_ans = max(least_ans, max(P[i][1:n_open+1]))
    if len(selected_ix) == 1 and 0 in selected_ix:
        print(least_ans)
    else:
        print(cur_ans)

main()

