import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;

void main() {
    auto N = readln.chomp.to!int;
    auto X = iota(N).map!(_ => readln.split.map!(to!int)).array;
    auto A = new int[](N);
    foreach (i; 0..N) foreach (j; 0..N) if (X[i][j]) A[i] |= 1 << j;
    auto B = readln.split.map!(to!int).array;
    int ans = 1 << 29;

    foreach (i; 0..(1<<N)) {
        int mask = i;
        bool ok = false;
        while (true) {
            int premask = mask;
            foreach (j; 0..N) if (!(mask & (1 << j))) {
                if ((A[j] & mask) == A[j]) {
                    mask |= (1 << j);
                }
            }
            if (mask == (1 << N) - 1) {
                ok = true;
                break;
            }
            if (mask == premask) {
                break;
            }
        }
        if (ok) {
            ans = min(ans, iota(N).filter!(j => (1 << j) & i).map!(j => B[j]).sum);
        }
    }

    ans.writeln;
}