#include using namespace std; constexpr int inf = 0x3f3f3f3f; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n; cin >> n; vector< vector > x(n, vector(n)); for (auto&& v : x) { for (auto&& e : v) { cin >> e; } } vector a(n); for (auto&& e : a) { cin >> e; } vector dp(1 << n, inf); dp[0] = 0; for (int S = 1; S != 1 << n; ++S) { for (int i = 0; i != n; ++i) { if (S >> i & 1) { int cost = 0; for (int j = 0; j != n; ++j) { if (x[i][j] and ~S >> j & 1) { bool need = true; for (int k = 0; k != n; ++k) { if (k != i and S >> k & 1) { if (x[k][j]) { need = false; break; } } } if (need) { cost += a[j]; } } } dp[S] = min(dp[S], dp[S ^ 1 << i] + cost); } } } cout << dp.back() << '\n'; }