import java.util.*; import java.io.*; public class Main { static int[] cost; static int[] trust; static int[] dp; static int n; public static void main (String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); n = Integer.parseInt(br.readLine()); trust = new int[n]; for (int i = 0; i < n; i++) { String[] line = br.readLine().split(" ", n); for (int j = 0; j < n; j++) { trust[i] += Integer.parseInt(line[j]) << j; } } cost = new int[n]; String[] line = br.readLine().split(" ", n); for (int i = 0; i < n; i++) { cost[i] = Integer.parseInt(line[i]); } dp = new int[1 << n]; Arrays.fill(dp, -1); System.out.println(dfw((1 << n) - 1)); } static int dfw(int key) { if (key == 0) { return 0; } if (dp[key] != -1) { return dp[key]; } int min = Integer.MAX_VALUE / 10; for (int i = 0; i < n; i++) { if (((1 << i) & key) == 0) { continue; } int add = 0; if ((key & trust[i]) != trust[i]) { add += cost[i]; } min = Math.min(min, dfw(key ^ (1 << i)) + add); } dp[key] = min; return min; } }