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
# include <cstdio>
# include <algorithm>
# define OVER (10000*10000+3)
using namespace std;
bool poss[1<<24][2];
int val[1<<24];
int przedmioty[24];
int plecaki[100];
int n, m;
void pass(int to, int x, int mask, int mask2)//add to all mask2|(subset of mask)
{
	if (mask==0)
	{
		if (poss[mask2][1-to])
			poss[mask2|x][to]=true;
		return;
	}
	int bit=mask&-mask;
	pass(to, x, mask&~bit, mask2);
	pass(to, x, mask&~bit, mask2|bit);
}

int main()
{
	scanf("%d%d", &n, &m);
	for (int i=0; i<n; ++i)
		scanf("%d", przedmioty+i);
	for (int mask=0; mask<(1<<n); ++mask)
	{
		for (int i=0; i<n; ++i)
			if (mask&(1<<i))
			{
				val[mask]+=przedmioty[i];
				if (val[mask]>OVER)
					val[mask]=OVER;
			}
	}
	for (int i=0; i<m; ++i)
	{
		scanf("%d", plecaki+i);
		plecaki[i]*=-1;
	}
	sort(plecaki, plecaki+m);
	poss[0][1]=true;
	for (int i=0; i<n && i<m; ++i)
	{
		for (int j=0; j<(1<<n); ++j)
			poss[j][i%2]=false;
		for (int j=0; j<(1<<n); ++j)
			if (val[j]<=-plecaki[i])
			{
				pass(i%2, j, ((1<<n)-1)&~j, 0);
			}
		if (poss[(1<<n)-1][i%2])
		{
			printf("%d\n", i+1);
			return 0;
		}
	}
	printf("NIE\n");
}