/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ class Ideone { public static void main (String[] args) throws java.lang.Exception { // your code goes here BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); ArrayList toy = new ArrayList(); int toy_sum = 0; for(String st:br.readLine().split(" ")){ int t = Integer.parseInt(st); toy.add(t); toy_sum += t; } Collections.sort(toy,(a,b)->{return b-a;}); int m = Integer.parseInt(br.readLine()); ArrayList box = new ArrayList(); for(String st:br.readLine().split(" ")){ box.add(Integer.parseInt(st)); } Collections.sort(box,(a,b)->{return b-a;}); int min = 0; int max = m; int ans = -1; while(max>=min){ int ave = (min+max)/2; if(check(new ArrayList(box.subList(0,ave)),new ArrayList(toy),toy_sum)){ ans = ave; max = ave-1; }else{ min = ave+1; } } System.out.println(ans); } public static boolean check(ArrayList box,ArrayList toy,int toy_sum){ for(int b:box){ toy_sum-=b; } if(toy_sum>0){ return false; } for(int i=0;i= 0){ toy.remove(i); i--; box.set(idx,0); } } Collections.sort(box,(a,b)->{return b-a;}); int box_size = box.size(); int toy_size = toy.size(); start:for(int j=0;j t_box = new ArrayList(box); for(int i=0;i= 0){ t_box.set(idx,0); }else if(bo>=n){ t_box.set(0,bo-n); }else{ continue start; } Collections.sort(t_box,(a,b)->{return b-a;}); } return true; } return false; } }