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

const int N = 24;

int n, m, a[N],  p[111];
pair<int, int> dp[1<<N];

int main() {
	cin >> n >> m;
	for (int i=0; i<n; i++) {
		cin >> a[i];
	}
	
	for (int i=1; i<=m; i++) {
		cin >> p[i];
	}
	sort(p+1, p+m+1);
	reverse(p+1, p+m+1);
	dp[0] = make_pair(1, 0);
	for (int i=1; i<(1<<n); i++) {
		dp[i] = make_pair(100, 0);
	}
	for (int i=1; i<(1<<n); i++) {
		for (int k=0; k<n; k++) {
			if (i & (1<<k)) {
				int poz = dp[i ^ (1<<k)].first;
				int zaj = dp[i ^ (1<<k)].second;
				if (p[poz] >= a[k] + zaj) {
					dp[i] = min(dp[i], make_pair(poz, a[k] + zaj));
				} else if (p[poz+1] >= a[k]) {
					dp[i] = min(dp[i], make_pair(poz+1, a[k]));
				}
			}
		}
	}
	if (dp[(1<<n)-1].first == 100) {
		cout << "NIE" << endl;
	} else {
		cout << dp[(1<<n)-1].first << endl;
	}
	return 0;
}