N = int(input())

F = []
for i in range(N):
    F.append(sum(map(int, input().split())))
P = []
for i in range(N):
    P.append(list(map(int, input().split())))

ans = 0
holiday_profits = []
holiday = 0
for i in range(N):
    profits = P[i][:F[i]+1]
    ans += max(profits)
    # 稼働しないのが一番儲かるか
    if len([x for x in profits if x > profits[0]]) == 0:
        holiday += 1
        holiday_profits.extend(profits[1:])

if holiday == N:
    ans += sorted(holiday_profits)[-1]
print(ans)
