local mmi, mma = math.min, math.max local bls, brs = bit.lshift, bit.rshift local function bdp_prepare(n, t, a) local tot = bls(1, n) local alltask = {} local stagetask = {} for i = 1, n do stagetask[i] = {} end for i = 1, tot - 1 do alltask[i] = 1000000007 end for i = 1, tot - 1 do local ti = i local cnt = 0 for j = 1, n do if ti % 2 == 1 then cnt = cnt + 1 end ti = brs(ti, 1) end table.insert(stagetask[cnt], i) end local tmp = 1 for i = 1, n do if #t[i] == 0 then alltask[tmp] = 0 else alltask[tmp] = a[i] end tmp = tmp * 2 end return tot, alltask, stagetask end local function bdp_doall(n, alltask, stagetask, t, a) for stage = 1, n - 1 do local stlen = #stagetask[stage] for i_stagetask = 1, stlen do local used = stagetask[stage][i_stagetask] local mul = 1 local tmp = used local b = {} local c = {} for j = 1, n do if tmp % 2 == 0 then table.insert(c, j) else b[j] = true end tmp = brs(tmp, 1) mul = mul * 2 end for j = 1, #c do local tgt = c[j] local cost = 0 for k = 1, #t[tgt] do if not b[ t[tgt][k] ] then cost = a[tgt] break end end local dst = used + bls(1, tgt - 1) alltask[dst] = mmi(alltask[dst], alltask[used] + cost) end end end end local n = io.read("*n") local t = {} for i = 1, n do t[i] = {} for j = 1, n do local tij = io.read("*n") if tij == 1 then table.insert(t[i], j) end end end local a = {} for i = 1, n do a[i] = io.read("*n") end local tot, alltask, stagetask = bdp_prepare(n, t, a) bdp_doall(n, alltask, stagetask, t, a) print(alltask[tot - 1])