#include #include using namespace std; using namespace atcoder; using ll=long long; using ld=long double; ld pie=3.141592653589793; ll inf=1444999999999999; ll mod=998244353; int main(){ ll n; cin >> n; vector>x(n,vector(n)); for (ll i = 0; i < n; i++) { for (ll j = 0; j < n; j++) { cin >> x[i][j]; } } vectora(n); for (ll i = 0; i < n; i++) { cin >> a[i]; } vectortwo(30,1); for (ll i = 1; i < 30; i++) { two[i]=two[i-1]*2; } vector>dp(two[n],vector(n,inf)); for (ll i = 0; i < n; i++) { dp[0][i]=0; } for (ll i = 1; i < two[n]; i++) { for (ll j = 0; j < n; j++) { if (i&two[j]) { bool ok=true; for (ll k = 0; k < n; k++) { if (x[j][k]&&!(i&two[k])) { ok=false; break; } } if (ok) { for (ll k = 0; k < n; k++) { dp[i][j]=min(dp[i][j],dp[i-two[j]][k]); } }else{ for (ll k = 0; k < n; k++) { dp[i][j]=min(dp[i][j],dp[i-two[j]][k]+a[j]); } } } } } ll ans=inf; for (ll i = 0; i < n; i++) { ans=min(ans,dp[two[n]-1][i]); } cout << ans << endl; }