N = int(input())
F= [list(map(int, input().split())) for _ in range(N)]
P= [list(map(int, input().split())) for _ in range(N)]

num = 0
store = []
second = []
pos = []
ans = 0
min_z = 10**7+1
n = 0
for i in range(N):
    num = F[i].count(1)
    if(max(P[i][:(num+1)]) != P[i][0]):
        ans += max(P[i][:(num+1)])
    else:
        store.append(P[i][0])
        second.append(max(P[i][1:(num+1)]))
        pos.append(i)
if ans:
    for j in pos:
        ans += store[j]
    print(ans)
else:
    for k in range(len(pos)):
        if(store[k]-second[k] < min_z):
            min_z = store[k]-second[k]
            n = k
    ans += second[n]
    for k in range(len(store)):
        if(k == n):
            continue
        ans += store[k]
    print(ans)