import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayDeque; import java.util.Queue; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); int[] arr0 = new int[n]; for (int i = 0; i < n; i++) { String[] sa = br.readLine().split(" "); for (int j = 0; j < n; j++) { if (Integer.parseInt(sa[j]) == 1) { arr0[i] += 1 << j; } } } String[] sa = br.readLine().split(" "); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = Integer.parseInt(sa[i]); } br.close(); int ans = Integer.MAX_VALUE; int end = 1 << n; int all = end - 1; for (int i = 0; i < end; i++) { int[] arr = new int[n]; System.arraycopy(arr0, 0, arr, 0, n); int val = 0; int x = i; int rem = all; Queue que = new ArrayDeque<>(); for (int j = 0; j < n; j++) { if ((x & 1) == 1) { val += a[j]; arr[j] = 0; rem -= (1 << j); que.add(j); } x /= 2; } for (int k = 0; k < n; k++) { if ((rem >> k & 1) == 1 && arr[k] == 0) { rem -= (1 << k); que.add(k); } } while (!que.isEmpty()) { int j = que.poll(); for (int k = 0; k < n; k++) { if ((rem >> k & 1) == 1) { arr[k] &= (all - (1 << j)); if (arr[k] == 0) { rem -= (1 << k); que.add(k); } } } } if (rem == 0) { ans = Math.min(ans, val); } } System.out.println(ans); } }