def main():
    n = int(input())
    fs = [sum(map(int, input().split())) for _ in range(n)]
    pss = [list(map(int, input().split())) for _ in range(n)]

    table = []
    for i, f in enumerate(fs):
        ps = sorted(((p, bool(j))
                     for j, p in enumerate(pss[i][:f + 1])), reverse=True)
        table.append(ps[:2])

    p = sum(row[0][0] for row in table)
    if any(row[0][1] for row in table):
        print(p)
        return

    sub_min = table[0][0][0] - table[0][1][0]
    sub_min_i = 0
    for row in table[1:]:
        sub = row[0][0] - row[1][0]
        if sub < sub_min:
            sub_min = sub
            sub_min_i = i
    print(p - sub_min)


if __name__ == '__main__':
    main()
