import java.util.*; public class Main { static int n; static int[] keys; static int[] costs; static int[] dp; public static void main (String[] args) { Scanner sc = new Scanner(System.in); n = sc.nextInt(); keys = new int[n]; for (int i = n - 1; i >= 0; i--) { for (int j = 0; j < n; j++) { keys[i] <<= 1; keys[i] += sc.nextInt(); } } costs = new int[n]; for (int i = n - 1; i >= 0; i--) { costs[i] = sc.nextInt(); } dp = new int[1 << n]; System.out.println(dfw((1 << n) - 1)); } static int dfw(int mask) { if (mask == 0) { return 0; } if (dp[mask] == 0) { int min = Integer.MAX_VALUE; for (int i = 0; i < n; i++) { if (((1 << i) & mask) == 0) { continue; } if ((keys[i] & mask) == keys[i]) { min = Math.min(min, dfw(mask ^ (1 << i))); } else { min = Math.min(min, dfw(mask ^ (1 << i)) + costs[i]); } } dp[mask] = min; } return dp[mask]; } }