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
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

int A[144];
int C[144];
int N;

vector<vector<int>> step_forward(vector<vector<int>> settings, int V)
{
	vector<vector<int>> next;
	
	for(auto& v : settings)
	{
		for(int i=0;i<v.size();i++)
		{
			vector<int> copy(v);
			copy[i] += V;
			if(i==v.size()-1)
			{
				copy.push_back(0);
			}
			
			//sort(copy.rbegin(), copy.rend());
			for(int j=i-1;j>=0;j--)
			{
				if(copy[j]<copy[j+1])
					swap(copy[j], copy[j+1]);
			}
			
			bool ok = true;
			for(int j=0;j<copy.size();j++)
				if(copy[j]>C[j])
				{
					ok = false;
					break;
				}
				
			if(ok)
			{
				next.push_back(copy);
			}
		}
	}
	
	return next;
}

int main()
{
	int m;
	scanf("%d %d", &N, &m);
	
	for(int i=0;i<N;i++)
		scanf("%d", &A[i]);
	for(int i=0;i<m;i++)
		scanf("%d", &C[i]);
		
	sort(C, C+m, greater<int>());
	sort(A, A+N, less<int>());
	
	int a=0;
	int b=N-1;
	
	vector<int> fwd;
	fwd.push_back(0);
	vector<vector<int>> options_fwd { fwd };
	
	

	while(b>=a)
	{
		options_fwd=step_forward(options_fwd, A[b--]);		
		fprintf(stderr, "Step a=%d b=%d, settings#=%d\n", a-1, b+1, options_fwd.size());
	}
	
	int mi = 9999;
	for(auto& v : options_fwd)
	{
		mi = min(mi, (int)v.size()-1);
	}
	if(mi!=9999)
		printf("%d\n", mi);
	else
		printf("NIE\n");
}