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)

    max_comb = []
    for i in range(N):
        P[i] = P[i][:cnt_of_1[i] + 1]
        tmp_max = max(P[i])
        if tmp_max == P[i][0]:
            max_comb.append(max(P[i][1:]))
        else:
            max_comb.append(tmp_max)

    max_comb.sort()
    max_comb.reverse()
    if max_comb[0] < 0:
        ans = max_comb[-1]
    else:
        ans = 0
        for ele in max_comb:
            if ele < 0:
                break
            else:
                ans += ele

    print(ans)

    
solve()