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
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

int ret = 101;

void perm(vector<int> p,vector<int> x,int k)
{
int i,a,n;
vector<int> t;
vector<int>::reverse_iterator it,it2;

	if (k == 1)
	{	
		t = p;
		n = 1;
		it2 = t.rbegin();
		it = x.rbegin();
		while (it2 != t.rend() && it != x.rend())
		{
//			printf("%d %d\n",*it,*it2);
			if (*it2 < *it) {
				++it2;
				n++;
			}
			else {
				*it2 -= *it;
				++it;
			}
		}
		if (it == x.rend() && n < ret) ret = n;
//		for (it = x.rbegin(); it != x.rend(); ++it) printf("%d ",*it);
//		printf("\n");
	}
	else 
	{
		for (i = 0; i < k; i++)
		{
			a = x[k-1];
			x[k-1] = x[i];
			x[i] = a;
			perm(p,x,k-1);
			a = x[k-1];
			x[k-1] = x[i];
			x[i] = a;
		}			
	}	
}

bool cmp(int i,int j)
{
	return i > j;
}

int main()
{
int N,M,n,m,t;
vector<int> a,c;
vector<int>::reverse_iterator it;
	
	scanf("%d %d",&N,&M);

	for (n = 0; n < N; n++)
	{
		scanf("%d",&t);
		a.push_back(t);
	}
	for (m = 0; m < M; m++)
	{
		scanf("%d",&t);
		c.push_back(t);
	}
	
	sort(c.begin(),c.end());
	sort(a.begin(),a.end(),cmp);
/*
	for (it = c.rbegin(); it != c.rend(); ++it) printf("%d ",*it);
	printf("\n");
	for (it = a.rbegin(); it != a.rend(); ++it) printf("%d ",*it);
	printf("\n");
*/	
	perm(c,a,a.size());

	if (ret < 101) printf("%d\n",ret); else printf("NIE\n");
	return 0;	
}