N = int(input())
Fs = [len(input().replace("0", "").split()) for _ in range(N)]
Ps = []
for _ in range(N):
    Ps += [list(map(int, input().split()))[:Fs[_]+1]]

rest_max = -float('inf')
zeros = False
max_index = 0
max_sums = 0

for P in Ps:
    m = -float('inf')
    for index, p in enumerate(P):
        if (rest_max < p and index != 0):
            rest_max = p
        if (m >= p): continue
        m = p
        max_index = index
    if (max_index != 0):
        zeros = True
    max_sums += m

if not (zeros):
    ans = rest_max
else:
    ans = max_sums
print(ans)