from collections import deque N = int(input()) X = [list(map(int, input().split())) for i in range(N)] A = [int(x) for x in input().split()] def isGood(bit): ink = [0] * N expanded = [False] * N for i in range(N): for j in range(N): if X[i][j]: ink[i] += 1 Q = deque() for i in range(N): if bit & (1 << i): ink[i] = 0 if ink[i] == 0: Q.append(i) while len(Q) > 0: d = Q.popleft() if expanded[d]: continue expanded[d] = True for i in range(N): if X[i][d] and (not expanded[i]): ink[i] -= 1 if ink[i] == 0: Q.append(i) return expanded.count(True) == N ans = 1e20 for bit in range(1 << N): cost = 0 for i in range(N): if bit & (1 << i): cost += A[i] if isGood(bit): ans = min(ans, cost) print(ans)