def main():
    N = int(input())
    F = tuple(tuple(map(int, input().split())) for _ in [0] * N)
    P = tuple(tuple(map(int, input().split())) for _ in [0] * N)

    c = [0] * N
    for i in range(N):
        c[i] = sum(F[i])

    profit = [0] * N
    for i, j in enumerate(c):
        p = -float('inf')
        for k in range(1, j + 1):
            p = max(P[i][k], p)
        profit[i] = p

    total = sum([i for i  in profit if i > 0])
    if not total:
        print(max(profit))
    else:
        print(total)

main()
