import math
from math import gcd,pi,sqrt
INF = float("inf")
MOD = 10**9 + 7

import sys
sys.setrecursionlimit(10**6)
import itertools
from collections import Counter,deque
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]


def main():
    n = i_input()
    f = [i_list().count(1) for i in range(n)]
    p = [i_list() for i in range(n)]

    flg = False
    ans = []
    sub = []
    for k,i in enumerate(p):
        l = sorted(i[:f[k]+1], reverse=True)
        m = l[0]
        ans.append(m)
        if i.index(m) == 0:
            sub.append(l[1])
        else:
            flg = True

    if flg == True:
        print(sum(ans))
    else:
        trial = -INF
        for i in range(len(ans)):
            trial = max(trial, sub[i] + sum(ans) - ans[i])
        print(trial)




if __name__=="__main__":
    main()
