import sys input = sys.stdin.readline N=int(input()) X=[set() for i in range(N)] for i in range(N): L=list(map(int,input().split())) for j in range(N): if L[j]==1: X[i].add(j) A=list(map(int,input().split())) ANS=sum(A) for i in range(1<ANS: continue for renew in range(N): flag=0 for j in range(N): if X[j] <= SET: SET.add(j) flag=1 if flag==0: break if len(SET)==N: ANS=S break print(ANS)