#include using namespace std::literals::string_literals; using i64 = std::int_fast64_t; using std::cout; using std::cerr; using std::endl; using std::cin; template std::vector make_v(size_t a){return std::vector(a);} template auto make_v(size_t a,Ts... ts){ return std::vector(ts...))>(a,make_v(ts...)); } int main() { int n; scanf("%d", &n); std::vector s(n); for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { int x; scanf("%d", &x); if(!x) continue; s[i] |= (1 << j); } } std::vector a(n); for(int i = 0; i < n; i++) scanf("%d", &a[i]); // pre std::vector dp(1 << n, 0); for(int i = 0; i < n; i++) dp[s[i]] += (1 << i); for(int i = 0; i < n; i++) { for(int j = 0; j < (1 << n); j++) { if(!(j >> i & 1)) continue; dp[j] += dp[j ^ (1 << i)]; } } std::vector ppc(1 << n, 0), cost(1 << n, 0); for(int i = 0; i < (1 << n); i++) { for(int j = 0; j < n; j++) { if(!(i >> j & 1)) continue; ppc[i] += 1; cost[i] += a[j]; } } // solve int ans = 1 << 30; for(int i = 0; i < (1 << n); i++) { int bit = i; while(ppc[bit] != ppc[bit | dp[bit]]) bit = bit | dp[bit]; if(ppc[dp[bit]] != n) continue; ans = std::min(ans, cost[i]); } printf("%d\n", ans); return 0; }