from collections import deque from copy import deepcopy N = int(input()) X = [list(map(int, input().split())) for i in range(N)] A = [int(x) for x in input().split()] INK = [0] * N for i in range(N): for j in range(N): if X[i][j]: INK[i] += 1 def isGood(bit): ink = deepcopy(INK) expanded = [False] * N Q = deque() for i in range(N): if bit & (1 << i): ink[i] = 0 if ink[i] == 0: Q.append(i) while Q: 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 and not expanded[i]: 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)