#include using namespace std; using ll = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); ll N; cin >> N; vector v(N); for(auto &i : v) { for(ll j = 0; j < N; j++) { ll x; cin >> x; i |= x << j; } } vector A(N); for(auto &i : A) { cin >> i; } vector dp(1 << N, 1e18); dp[0] = 0; for(ll i = 0; i < 1 << N; i++) { for(ll j = 0; j < N; j++) { if(i >> j & 1) { continue; } ll nx = i | 1 << j; if((i & v[j]) == v[j]) { dp[nx] = min(dp[nx], dp[i]); } else { dp[nx] = min(dp[nx], dp[i] + A[j]); } } } cout << (dp.back() == 1e18 ? -1 : dp.back()) << "\n"; }