#include using namespace std; #define rep(i, n) for(int i = 0; i < (int)n; ++i) #define FOR(i, a, b) for(int i = a; i < (int)b; ++i) #define rrep(i, n) for(int i = ((int)n - 1); i >= 0; --i) typedef long long ll; typedef long double ld; const int Inf = 1e9; const double EPS = 1e-9; const int MOD = 1e9 + 7; int main() { cin.tie(nullptr); ios::sync_with_stdio(0); int n, res = Inf; cin >> n; vector > x(n, vector(n)); vector a(n), trust(n); rep (i, n) { rep (j, n) { int v; cin >> v; if (v) x[i][j] = true; } } rep (i, n) cin >> a[i]; rep (i, n) rep (j, n) if (x[i][j]) trust[i]++; rep (bit, 1 << n) { int tmp = 0; queue s; vector > y(n, vector(n)); vector checked(n); vector cnt(n); rep (i, n) cnt[i] = trust[i]; rep (i, n) { if (bit & (1 << i)) { cnt[i] = 0; tmp += a[i]; } if (!cnt[i]) { s.push(i); } } if (tmp >= res) continue; while (!s.empty()) { int f = s.front(); s.pop(); if (checked[f]) continue; checked[f] = true; rep (i, n) { if (x[i][f] && !checked[i]) { cnt[i]--; if (!cnt[i]) s.push(i); } } } bool ok = true; rep (i, n) if (!checked[i]) ok = false; if (ok) res = min(res, tmp); } cout << res << endl; return 0; }