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
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#define f first
#define s second
using namespace std;

const int size = 1<<24;
int n, m, P[25], T[101], K[25], ind[25];
const int INF = 100000001;

int result[size], place[size];

bool cmp (int a, int b) {
	return P[a] > P[b];
}

bool cmpRev (int a, int b) {
	return b<a;
}

int main() {
	scanf("%d %d", &n, &m);
	for(int i = 0; i<n; i++) scanf("%d", &P[i]);
	for(int i = 0; i<m; i++) scanf("%d", &T[i]);
	sort(T, T+m, cmpRev);
	
	for(int i = 1; i< (1<<n); i++) {
		result[i] = INF;
	}
	
	for(int i = 1; i<(1<<n); i++) {
		for(int k = 0; k<n; k++)
			if((1<<k)&i) {
				int S = i - (1<<k);
				int x = result[S];
				if(x == INF || x > n) continue;
				if(place[S] < P[k] && x == m) continue;
				if(place[S] >= P[k] && (result[i] > result[S] || place[i] < place[S]-P[k]) ) { //starczy miejsca
					result[i] = result[S];
					place[i] = place[S]-P[k];
				} else if((T[x] - P[k] >=0 && result[S]+1 < result[i]) || (result[S] + 1 == result[i] && place[i] < T[x]-P[k] && T[x] - P[k] >= 0) ){
					result[i] = result[S]+1;
					place[i] = T[x]-P[k];
				}
			}
	}
	
	/*printf("\n \n");
	for(int i = 0; i<(1<<n); i++) printf("%d %d %d\n", i, result[i], place[i]); */
	
	if(result[(1<<n)-1] == INF) printf("NIE\n");
	else printf("%d\n", result[(1<<n)-1]);
	return 0;
}