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
54
55
56
57
58
59
60
61
62
#include <cstdio>
#include <algorithm>
using namespace std;

int pack[123];
int object[25];
unsigned int sum[25];

bool B[26];
int n, m, inUse;

void put(int o, int p, int packed);

void next(int o, int p, int packed) {
	for(int i = o; i < n; ++i)
		if(!B[i]) {
			B[i] = true;
			put(i, p, packed);
			B[i] = false;
		}
}

void put(int o, int p, int packed) {
	if(p == inUse)
		return;
	if(pack[p] < object[o]) {
		B[o] = false;
		next(0, p + 1, packed);
		return;
	}
	/*if(pack[p] >= sum[o]) {
		for(int i = o + 1; i < n; ++i)
			B[i] = true;
		packed += n - o;
	}*/
	if(packed == n - 1) {
		inUse = inUse < p ? inUse : p;
		return;
	}
	pack[p] -= object[o];
	next(o + 1, p, packed + 1);
	pack[p] += object[o];
}

int main() {
	scanf("%d%d", &n, &m);
	inUse = min(n, m);
	for(int i = 0; i < n; ++i)
		scanf("%d", object + i);
	for(int i = 0; i < m; ++i)
		scanf("%d", pack + i);
	sort(pack, pack + m, [](int a, int b) { return a > b; });
	sort(object, object + n);
	for(int i = n; i; --i)
		sum[i - 1] = object[i - 1] + sum[i];
	next(0, 0, 0);
	if(inUse == min(n, m))
		printf("NIE\n");
	else
		printf("%d\n", inUse + 1);
	return 0;
}