#include using namespace std; using P = pair; const int M = 1000000007; int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; vector b(n); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { int c; cin >> c; b[i] |= c << j; } } vector a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } vector dp(1 << n, M); dp[0] = 0; for (int i = 0; i < (1 << n); ++i) { for (int j = 0; j < n; ++j) { dp[i | 1 << j] = min(dp[i | 1 << j], dp[i] + ((i & b[j]) == b[j] ? 0 : a[j])); } } cout << dp[(1 << n) - 1] << '\n'; return 0; }