import itertools


def solve():
    N = int(input())
    F = []
    cnt_of_1 = []
    for _ in range(N):
        tmp_F = list(map(int, input().split(" ")))
        cnt_of_1.append(tmp_F.count(1))
        F.append(tmp_F)
    P = []
    zero_cmb = []
    for _ in range(N):
        tmp_P = list(map(int, input().split(" ")))
        zero_cmb.append(tmp_P[0])
        P.append(tmp_P)

    for i in range(N):
        P[i] = P[i][:cnt_of_1[i] + 1]

    cmb = list(itertools.product(*P))
    target = cmb.index(tuple(zero_cmb))
    del cmb[target]
    maximum = -1000000000
    for ele in cmb:
        maximum = max(maximum, sum(ele))

    print(maximum)
    

solve()