n = int(input()) x = [list(map(int, input().split())) for i in range(n)] a = list(map(int, input().split())) bit_state = [0] * n for i in range(n): for j in range(n): if x[i][j] == 1: bit_state[i] += 2 ** j ans = 10**9 for bit_mask in range(1 << n): tmp_ans = 0 tmp = bit_mask for i in range(n): if (bit_mask >> i) & 1: tmp_ans += a[i] for _ in range(n): for i in range(n): if tmp & bit_state[i] == bit_state[i]: tmp |= (1 << i) if tmp == (1 << n) - 1: ans = min(ans, tmp_ans) print(ans)