//include //------------------------------------------ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define RFOR(i, a, b) for (int i = (b)-1; i >= (a); --i) #define REP(i, n) FOR(i, 0, (n)) #define RREP(i, n) for (int i = (n) - 1; i >= 0; i--) #define FOREACH(x, arr) for (auto &(x) : (arr)) #define FORITER(x, arr) for (auto(x) = (arr).begin(); (x) != (arr).end(); ++(x)) #define SHOW_VECTOR(v) {std::cerr << #v << "\t:";for(const auto& xxx : v){std::cerr << xxx << " ";}std::cerr << "\n";} #define SHOW_MAP(v){std::cerr << #v << endl; for(const auto& xxx: v){std::cerr << xxx.first << " " << xxx.second << "\n";}} using LL = long long; //------------------------------------------ //------------------------------------------ int main() { int N; cin >> N; vector> X(N, vector(N)); for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) cin >> X[i][j]; vector D(N); for (int i = 0; i < N; i++) { int bit = 0; for (int j = 0; j < N; j++) { bit <<= 1; bit += X[i][N - j - 1]; } D[i] = bit; } vector A(N); for (int i = 0; i < N; i++) cin >> A[i]; int ans = INT_MAX; for (int bit = 0; bit < (1 << N); bit++) { int B = bit; int cost = 0; for (int i = 0; i < N; i++) if (bit & (1 << i)) cost += A[i]; while (true) { bool update = false; for (int i = 0; i < N; i++) { if (!(B & (1 << i))) { if (D[i] == (D[i] & B)) { B |= 1 << i; update = true; } } } if (!update)break; } if (B == (1 << N) - 1) ans = min(ans, cost); } cout << ans << endl; }