n = int(input())
f_list = []
p_list = []
max_list = []
semi_max_list= []
for i in range(n):
    _list = list(map(int, input().split()))
    f_list.append(_list)
for i in range(n):
    _list = list(map(int, input().split()))
    p_list.append(_list)
ans = 0
fine = 0
for i in range(n):
    count = 0
    for j in range(10):
        if f_list[i][j] == 1:
            count += 1
    max_ = -1000000000000000000
    del p_list[i][count+1:]
    max_ = max(p_list[i])
    if max_ == p_list[i][0]:
        fine += 1
    max_list.append(max_)
    p_list[i].remove(max_)
    semi_max_ = max(p_list[i])
    semi_max_list.append(semi_max_)
for i in max_list:
    ans += i
if fine != n:
    print(ans)
else:
    m_list = []
    for i in range(n):
        m_list.append(max_list[i] - semi_max_list[i])
    print(ans-min(m_list))