# -*- coding: utf-8 -*-

# AtCoder Beginner Contest
# Problem C

if __name__ == '__main__':
    shop_count = int(input())
    shop_working_table = [list(map(int, input().split())) for _ in range(shop_count)]
    working_days = [shop_working_table[i].count(1) for i in range(shop_count)]
    benefit_table = [list(map(int, input().split())) for _ in range(shop_count)]

    benefit_total = 0
    including_closed = [max(benefit_table[j][:working_days[j] + 1]) for j in range(shop_count)]
    open_only = [max(benefit_table[j][1:working_days[j] + 1]) for j in range(shop_count)]

    for k in range(shop_count):
        if k == open_only.index(max(open_only)):
            benefit_total += max(open_only)
        else:
            benefit_total += max(including_closed[k], open_only[k])

    print(benefit_total)
