#include using namespace std; using ll = long long; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, v; cin >> n; vector X(n), A(n); for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ cin >> v; X[i] |= v << j; } } for(int i = 0; i < n; i++)cin >> A[i]; int ans = 1 << 30; auto f = [&](int S){ for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ if((S & X[j]) == X[j]) S |= 1 << j; } } return (S == ((1 << n) - 1)); }; for(int S = 0; S < (1 << n); S++){ int s = 0; for(int j = 0; j < n; j++) if(S >> j & 1) s += A[j]; if(f(S)) ans = min(ans, s); } cout << ans << '\n'; }