N = int(input()) X = [list(map(int, input().split())) for i in range(N)] A = list(map(int, input().split())) trust = [0] * N for i in range(N): for j in range(N): if X[i][j] == 1: trust[i] |= 1 << j dp = [10**100] * (1 << N) dp[0] = 0 for i in range(1 << N): for j in range(N): if ~i >> j & 1: mask = 0 for k in range(N): if i >> k & 1: mask |= 1 << k if (mask & trust[j]) == trust[j]: dp[i | 1 << j] = min(dp[i | 1 << j], dp[i]) dp[i | 1 << j] = min(dp[i | 1 << j], dp[i] + A[j]) print(dp[(1 << N) - 1])