#include int ri() { int n; scanf("%d", &n); return n; } int main() { int n = ri(); std::vector hen[n], rev[n]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (ri()) hen[i].push_back(j), rev[j].push_back(i); int a[n]; for (auto &i : a) i = ri(); int res = 1000000000; for (int i = 0; i < 1 << n; i++) { int tayo[n]; std::queue que; for (int j = 0; j < n; j++) { tayo[j] = (i >> j & 1) ? 0 : hen[j].size(); if (!tayo[j]) que.push(j); } while (que.size()) { for (auto k : rev[que.front()]) if (!--tayo[k]) que.push(k); que.pop(); } bool ok = true; for (int j = 0; j < n; j++) if (tayo[j] > 0) ok = false; if (ok) { int cost = 0; for (int j = 0; j < n; j++) if (i >> j & 1) cost += a[j]; res = std::min(res, cost); } } std::cout << res << std::endl; return 0; }