import sys input = sys.stdin.readline N=int(input()) X=[] for i in range(N): L=list(map(int,input().split())) X.append({j for j in range(N) if L[j]==1}) 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 j in SET: continue if X[j] <= SET: SET.add(j) flag=1 if flag==0: break if len(SET)==N: ANS=S break print(ANS)