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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <cstdio>
#include <algorithm>
#include <cstdint>
using namespace std;

typedef int_fast32_t fas;
//typedef int_fast8_t f_8;
typedef int f_8;

//printf("%lu");

/*rekurencja
bool brut(int x, int n, int m)
{
	if (x==n) return 1;
	
	for (int i=0; i<m; i++)
	{
		if (p[i]>=t[x])
		{
			p[i]-=t[x];
			if (brut(x+1, n, m)) return 1;
			p[i]+=t[x];
		}
	}
	
	return 0;
}*/

int main()
{
	fas t[30];
	fas p[105];
	fas id[30];

	fas n, m, x, sm;
	f_8 vic;
	scanf("%ld%ld", &n, &m);
	
	for (f_8 i=0; i<n; i++) 
	{
		scanf("%ld", &sm);
		t[i]=-sm;
	}
	for (f_8 i=0; i<m; i++) 
	{
		scanf("%ld", &sm);
		p[i]=-sm;
	}
	
	sort(p, p+m);
	sort(t, t+n);
	if (m>n) m=n;
	for (f_8 i=0; i<n; i++) t[i]=-t[i];
	for (f_8 i=0; i<m; i++) p[i]=-p[i];
	
	//fill(id, id+m, 0);
	
	for (int i=1; i<=m; i++)
	{
		fill(id, id+n, -1);
		x=0;
		vic=0;
		
		begin:
		//printf("b %d %d\n", x, id[x]);
		if (x==-1) goto end;
		if (x==n) {vic=1; goto end;}
		
		if (id[x]!=-1) p[id[x]]+=t[x];
		
		loop:
		//printf("l %d %d\n", x, id[x]);
		id[x]++;
		if (id[x]==i) {id[x]=-1; x--; goto begin;}
		if (p[id[x]]<t[x]) goto loop;
		p[id[x]]-=t[x];
		x++;
		goto begin;
		
		end:
		//printf("e\n");
		if (vic)
		{
			printf("%d\n", i);
			return 0;
		}
	}
	
	printf("NIE\n");
	
	return 0;
	//ADG
}