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
63
64
65
66
/*
 * pak.c
 * br-fo
 */
#include <stdio.h>
#include <stdlib.h>
int nprz, mpl;

int increase(int *liczba) {
	int i = nprz - 1;
	while (liczba[i] == mpl - 1) {
		liczba[i] = 0;
		i--;
		if (i < 0)
			return 0;
	}
	liczba[i]++;
	return 1;
}

int main() {
	int nonzero, min_nonzero, i, *ma, *uc, *liczbabf, *suma;

	scanf("%i %i", &nprz, &mpl);
	ma = (int *) malloc(nprz * sizeof(int));
	liczbabf = (int *) malloc(nprz * sizeof(int));
	uc = (int *) malloc(mpl * sizeof(int));
	suma = (int *) malloc(mpl * sizeof(int));

	for (i = 0; i < nprz; i++)
		scanf("%i", &ma[i]);
	for (i = 0; i < mpl; i++)
		scanf("%i", &uc[i]);
	for (i = 0; i < nprz; i++) {
		liczbabf[i] = 0;
	}
	min_nonzero = mpl + 1;
	for (;;) {
		for (i = 0; i < mpl; i++)
			suma[i] = 0;
		for (i = 0; i < nprz; i++)
			suma[liczbabf[i]] += ma[i];
		nonzero = 0;
		for (i = 0; i < mpl; i++) {
			if (suma[i] > uc[i])
				break;
			if (suma[i])
				nonzero++;
		}
		if (i==mpl && nonzero < min_nonzero)
			min_nonzero = nonzero;

		if (!increase(liczbabf))
			break;
	}
	if (min_nonzero == mpl + 1)
		printf("NIE\n");
	else
		printf("%i\n",min_nonzero);

	free(ma);
	free(liczbabf);
	free(uc);
	free(suma);
	return 0;
}