1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.util.ArrayList;
import java.util.Collections;

public class pak {

	public static void main(String[] args) {
		java.util.Scanner scanner = new java.util.Scanner(System.in);
		
		ArrayList <Integer> plecaki = new ArrayList <Integer> ();
		ArrayList <Integer> wagi = new ArrayList <Integer> ();
		int rzeczy = scanner.nextInt(), plecakow = scanner.nextInt();
		boolean [] uzyte = new boolean [plecakow];
		
		for(int i = 0; i < rzeczy; i++)
			wagi.add(scanner.nextInt());
		
		for(int i = 0; i < plecakow; i++){
			plecaki.add(scanner.nextInt());
			uzyte[i] = false;
		}
		
		Collections.sort(wagi);
		Collections.sort(plecaki);
		
		int j = 0;
		boolean koniec = false;
		
		for(Integer rzecz : wagi){
			while(plecaki.get(j) - rzecz < 0){
				j++;
				if(j > plecaki.size()){
					koniec = true;
					break;
				}
			}
			
			if(koniec) break;
			
			plecaki.set(j, plecaki.get(j) - rzecz);
			uzyte[j] = true;
		}
		
		scanner.close();
		
		j = 0;
		
		for(boolean jest : uzyte)
			if(jest) j++;
		
		System.out.println(j);
	}

}