MIN = -(10**7)-1

def solve():
    cnt = 0
    i = 0
    for lst in f_lst:
        chk_range = sum(lst)
        cnt += max(p_lst[i][1:chk_range])
        i += 1
    if cnt < 0:
        cnt = min(min(p_lst))
    return cnt


n = int(input())
f_lst = [[] for _ in range(n)]
p_lst = [[] for _ in range(n)]
for i in range(n):
    f_lst[i].extend(map(int,input().split()))
for i in range(n):
    p_lst[i].extend(map(int,input().split()))

ans = solve()
print(ans)
