import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; 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()); Map> map0 = new HashMap<>(); for (int i = 0; i < n; i++) { String[] sa = br.readLine().split(" "); Set set = new HashSet<>(); map0.put(i, set); for (int j = 0; j < n; j++) { if (Integer.parseInt(sa[j]) == 1) { set.add(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; for (int i = 0; i < end; i++) { Map> map = new HashMap<>(); for (int j = 0; j < n; j++) { Set set = new HashSet<>(); set.addAll(map0.get(j)); map.put(j, set); } int val = 0; for (int j = 0; j < n; j++) { if ((i >> j & 1) == 1) { val += a[j]; map.remove(j); for (Integer k : map.keySet()) { map.get(k).remove(j); } } } boolean flg = true; for (Set set : map.values()) { if (!set.isEmpty()) { flg = false; break; } } if (flg) { ans = Math.min(ans, val); } } System.out.println(ans); } }