import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Scanner; import java.util.Set; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); Map> map0 = new HashMap<>(); for (int i = 0; i < n; i++) { Set set = new HashSet<>(); map0.put(i, set); for (int j = 0; j < n; j++) { if (sc.nextInt() == 1) { set.add(j); } } } int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } sc.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); } }