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
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <cstdio>
#include <algorithm>

using namespace std;

int n, m;
int a[30];
int w[111];
int s[1 << 24];

struct T {
	int sum, mask;
	T (int _sum = 0, int _mask = 0)
		: sum(_sum), mask(_mask) {}
} d[1 << 24];

char used[30];

bool operator< (T a, T b) {
	return a.sum > b.sum || a.sum == b.sum && a.mask < b.mask;
}

int nd = 0;
int l = 0;
int curmask = 0;

bool comp(int a, int b) {
	return a > b;
}

bool check(int i) {
	if (i > l) {
		// printf("BIN %d\n", curmask);
		return (curmask == (1 << n) - 1);
	}
	int lpos = 0, rpos = nd;
	while (rpos - lpos >= 2) {
		int mid = (lpos + rpos) / 2;
		if (d[mid].sum > w[i])
			lpos = mid;
		else
			rpos = mid;
	}
	if (lpos >= nd - 1) return false;
	for (int j = lpos + 1; j < nd; j++) {
		if (d[j].sum > w[i]) continue;
		int mask = d[j].mask;
		// printf("%d %d %d\n", i, d[j].sum, d[j].mask);
		int k;
		for (k = 0; k < n; k++)
			if ((mask & (1 << k)) && used[k])
				break;
		if (k < n) continue;
		curmask ^= mask;
		for (k = 0; k < n; k++)
			if (mask & (1 << k))
				used[k] = 1;
		if (check(i + 1))
			return true;
		curmask ^= mask;
		for (k = 0; k < n; k++)
			if (mask & (1 << k))
				used[k] = 0;
		// for (int i = 0; i < n; i++)
		// 	printf("%d", used[i]);
		// printf("\n");
	}
	return false;
}

int main() {
	// freopen("input.txt", "r", stdin);
	// freopen("output.txt", "w", stdout);
	scanf("%d%d", &n, &m);
	for (int i = 0; i < n; i++)
		scanf("%d", &a[i]);
	int ss = 0;
	for (int i = 0; i < n; i++)
		ss += a[i];
	for (int j = 0; j < m; j++)
		scanf("%d", &w[j]);
	sort(w, w + m, comp);
	for (int i = 0; i < (1 << n); i++) {
		int sum = 0;
		for (int j = 0; j < n; j++)
			if (i & (1 << j))
				sum += a[j];
		s[i] = sum;
		d[nd++] = T(sum, i);
	}
	sort(d, d + nd);
	// for (int i = 0; i < nd; i++)
	// 	printf("%d %d\n", d[i].sum, d[i].mask);
	int sum = 0;
	for (; l < m; l++) {
		sum += w[l];
		if (sum >= ss)
			break;
	}
	// printf(">>%d\n", l);
	if (l == m) {
		printf("NIE\n");
		return 0;
	}
	for (; l < m; l++) {
		// check
		if (check(0)) {
			printf("%d\n", l + 1);
			return 0;
		}
	}
	printf("NIE\n");
	return 0;
}