#include using namespace std; int main() { ios::sync_with_stdio(false); int N; { cin >> N; } vector> X(N, vector(N)); { for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) { cin >> X[i][j]; } } } vector A(N); { for (int i = 0; i < N; ++i) { cin >> A[i]; } } vector masks(N); { for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) { masks[i] |= X[i][j] << j; } } } int res = accumulate(A.begin(), A.end(), 0); for (int s = 0; s < 1 << N; ++s) { int cost = 0; { for (int i = 0; i < N; ++i) if (s >> i & 1) { cost += A[i]; } } int t = s; { for (int _ = 0; _ < N; ++_) { for (int i = 0; i < N; ++i) { if ((masks[i] & t) == masks[i]) t |= 1 << i; } } } if (t == (1 << N) - 1) res = min(res, cost); } cout << res << endl; }