#include #include #include constexpr int INF = 1 << 30; void solve() { int n; std::cin >> n; std::vector ss(n, 0); for (auto& s : ss) { for (int i = 0; i < n; ++i) { int x; std::cin >> x; s |= (x << i); } } std::vector xs(n); for (auto& x : xs) std::cin >> x; int ans = INF; std::vector ok(n); for (int b = 0; b < (1 << n); ++b) { int t = b; std::fill(ok.begin(), ok.end(), false); bool update = true; while (update) { update = false; for (int i = 0; i < n; ++i) { if (ok[i]) continue; auto s = ss[i]; if ((t & s) == s) { ok[i] = true; update = true; t |= (1 << i); } } } if (std::any_of(ok.begin(), ok.end(), [](auto x) { return !x; })) continue; int cost = 0; for (int i = 0; i < n; ++i) { if ((b >> i) & 1) cost += xs[i]; } ans = std::min(ans, cost); } std::cout << ans << std::endl; } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); solve(); return 0; }