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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<fstream>
#include<iomanip>
#include<vector>
#include<map>
#include<set>
#include<stack>

#define SC(n) scanf("%d", &n)
#define SC2(n, m) scanf("%d %d", &n, &m)
#define SCC(c) scanf(" %c", &c)
#define SCS(b) scanf("%s", b)
#define REP(i, n) for(int i = 0; i < (n); ++i)
#define FORE(i, a, b) for(int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define FORIT(i, c, typ) for(typ::iterator i = c.begin(); i != c.end(); ++i)
#define PR(n) printf("%d ", (int) (n)) 
#define PRN(n) printf("%d\n", (int) (n)) 
#define elif else if
#define pb push_back
#define mp make_pair
#define xx first
#define yy second
#define all(v) v.begin(), v.end()
#define itr iterator
#define dbg if(0)
#define prd dbg printf

using namespace std;

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<uint, int> pi;
typedef vector<pi> vpi;
typedef pair<int, pi> pii;
typedef vector<pii> vpii;
typedef vector<vpi> vvpi;
typedef map<int, vpi> mvpi;

const int NMAX = 25, MMAX = 107, SMAX = (1<<24);

int c[MMAX], s[NMAX], qq[SMAX];
char t[SMAX];
pi q[SMAX]; // (uint, int)
int n, m, k, k2, rund;

bool greate(pi const & a, pi const & b) {
	return a > b;
}

inline void dfs(int mask, int b) {
	int l0 = 1;
	q[0] = mp(b, mask);
	
	while(l0) {
		mask = q[l0 - 1].yy;
		prd("dfs %d\n", mask);
		b = q[l0 - 1].xx;
		--l0;
		
		REP(i, n) if(((1<<i) & mask) == 0 && s[i] <= b) {
			t[mask] = MMAX; // nie jest maksymalny
			int mask2 = mask + (1<<i);
		
			if(t[mask2] == 0) {
				prd("got %d -> %d\n", mask, mask2);
				t[mask2] = rund;
				q[l0++] = mp(b - s[i], mask2);
				qq[k2++] = mask2;
			}
		}
	}
}
	
inline uint getSize(int mask) {
	uint size = 0;
	REP(i, n) if((1<<i) & mask)
		size += s[i];
	return size;
}
			
int main() {
	SC2(n, m);
	
	REP(i, n)
		SC(s[i]);
	REP(i, m)
		SC(c[i]);
			
	sort(c, c+m);
	reverse(c, c+m);
	m = min(n,m);
	
	k = 1;
	
	REP(j, m) {
		prd("round %d\n", j);
		rund = j+1;
		
		k2 = k;
		REP(i, k)
			dfs(qq[i], c[j]);	
			
		if(t[(1<<n)-1]) {
			PR(j+1);
			return 0;
		}
		
		int k0 = k;
		k = 0;
		FORE(i, k0, k2 - 1) {
			int mask = qq[i];
			if(t[mask] == rund)
				q[k++] = mp(getSize(mask), mask);
		}
		sort(q, q+k, greate);
		REP(i, k)
			qq[i] = q[i].yy;		
	}
		
	printf("NIE");
	return 0;
}