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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include<algorithm>
//#include<stdio.h>
#include<iostream>
#include<list>
#include<map>
#include<queue>
#define FOR(i,n) for(int i = 0;i<n;++i)
#define FORI(i,b,n) for(int i = b;i<n;++i)
#define FORD(i,n) for(int i = n;i>=0;--i)
#define FORIT(it, m) for((it) = (m).begin(); (it) != (m).end(); ++(it))
#define ZERO 0.000001
#define MAX ((1<<31)-1)
#define qprintf debug && printf
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define ull long long int
using namespace std;
//int debug=1;
#define dcout debug && cout
//ull ccc=0;
int findMax(int*t,int n){
	int m=0;
	FOR(i,n)if(t[i]>m)m=t[i];
	return m;
}
ull sum(int*t,int n){
	ull s=0;
	while(n--)s+=t[n];
	return s;
}
class N
{
	public:
	int*takenItems;
	int n;

	int*availableItems;
	int a;

	int*rejectedItems;
	int r;

	int sumTaken;
	ull sumAvailable;
	int capacity;
		N(){}
		N(N*node){
			n = 0;
			a = node->a + node->r;
			r = 0;
			sumTaken = 0;
			capacity = 0;
			int all = n+a+r+1;
			takenItems=new int[all];
			availableItems=new int[all];
			rejectedItems=new int[all];
			FOR(i,node->a)availableItems[i]=node->availableItems[i];
			FOR(i,node->r)availableItems[i+node->a]=node->rejectedItems[i];
			sumAvailable = sum(availableItems,a);
		}
		~N(){
			delete []takenItems;
			delete []availableItems;
			delete []rejectedItems;
		}
		int mask(map<int,int>*m){
			int ret=0;
			FOR(i,a)ret+=(*m)[availableItems[i]];
			return ret;
		}
};
/*ostream& operator<<(ostream& os, const N& r)
{
	os<<"["<<r.n<<","<<r.a<<","<<r.r<<", sum:"<<r.sumTaken<<", cap:"<<r.capacity<<", ava:"<<r.sumAvailable<<"]";
	os<<" {";
	FOR(i, r.n)os<<r.takenItems[i]<<",";
	os<<"}";
	os<<" {";
	FOR(i, r.a)os<<r.availableItems[i]<<",";
	os<<"}";
	os<<" {";
	FOR(i, r.r)os<<r.rejectedItems[i]<<",";
	os<<"}";
	return os;
}*/
int isDom(const N*node){
	//dcout<<"Checking isDom: "<<(*node)<<endl;
	int r=node->a + node->r;
	if(r==0){
		//dcout<<"F YEAH!"<<endl;
		return 1;
	}
	//dcout<<r;
	int rest[r];
	FOR(i,node->a)rest[i]=node->availableItems[i];
	FOR(i,node->r)rest[node->a+i]=node->rejectedItems[i];

	map<int,int> m;
	m[0]=0;
	map<int,int>::iterator it;
	FOR(i,node->n){
		map<int,int> mt;
		FORIT(it,m){//++ccc;
			int nw=it->first+node->takenItems[i];
			if(nw<=node->capacity)
				mt[nw] = max(it->second+1, mt[nw]);
		}
		FORIT(it,mt){
			m[it->first] = max(it->second,m[it->first]);
		}
		//cout<<m.size()<<"ś ";
	}
	sort(rest,rest+r);
	int ir=0;
	int left = node->capacity - node->sumTaken;
	FORIT(it,m){
		//czy można podnieść wielkość elementu spoza setu
		while(ir+1<r && rest[ir+1]<=it->first+left){
			++ir;
		}
		//czy jest jeszcze szansa
		//if(rest[ir]<it->first){
		//	//return 1;
		//}
		if(rest[ir] > it->first){//czyli lepsze
			if(left+it->first>=rest[ir]){//i mieści się
				//cout<<"Można wyrzucić sumę "<<it->first<<" ("<<it->second<<" elementów) i dodać pojedynczą "<<rest[ir]<<endl;
				//cout<<"Można wyrzucić sumę "<<it->first<<" ("<<it->second<<" elementów) i dodać pojedynczą "<<rest[ir]<<endl;
				return 0;
			}
		}
		if(rest[ir] == it->first){//czyli równe
			if( it->second > 1 ){//ale tu jest suma kilku
				//dcout<<"Można wyrzucić sumę "<<it->first<<" ("<<it->second<<" elementów) i dodać pojedynczą "<<rest[ir]<<endl;
				return 0;
			}
		}
	}
	return 1;
}
void generateSubs(N*node, vector<N*> *solutions){
	//cout<<"dupa"<<*node;
	//dcout<<"Generating for: "<<(*node)<<endl;
	//jeśli coś poszło źle
	if(node->capacity < node->sumTaken){
		//dcout<<"Cos poszlo zle !!!!!!!!!!"<<endl;
		return;
	}
	//jeśli nie ma co brać
	if(node->a == 0){
		//dcout<<"Spotkalem liscia !!!!!!!!!!("<<(node->n+node->r)<<")"<<endl;
		if(isDom(node)){
			//to się skopiuj i dodaj do rozwiązania
			//dcout<<" We've got it!! "<<*node<<endl;
			solutions->push_back(new N(node));
		}
		return;
	}
	//jeśli mogę wziąć wszystkie
	if(node->sumAvailable<=node->capacity-node->sumTaken){
		//to weż

		//przepisz dostępne do wziętych
		int node_a = node->a;
		int node_sumAvailable = node->sumAvailable;
		FOR(i,node->a)node->takenItems[node->n+i]=node->availableItems[i];
		node->n += node->a;
		node->sumTaken += node->sumAvailable;
		node->sumAvailable = 0;
		node->a=0;

		//to go skopiuj i dodaj do rozwiązania
		generateSubs(node,solutions );

		//wycofaj zmiany
		node->a=node_a;
		node->sumAvailable=node_sumAvailable;
		node->n -= node->a;
		node->sumTaken-=node_sumAvailable;
		//i skończ
		return;
	}//if(ccc++)return;
	//skoro nie, to odrzućmy coś...
	int nowTaken = 0;
	FOR(i,node->a){
		//weź i początkowych z dostępnych do wziętych
		//dcout<<"Przed rejectem: "<<(*node)<<endl;
		//FOR(j,i){nowTaken+=(node->takenItems[node->n+j]=node->availableItems[j]);}
		if(i)nowTaken+=(node->takenItems[node->n+i-1]=node->availableItems[i-1]);
		node->sumTaken+=nowTaken;
		//będziemy odrzucać ten z available[i]
		node->rejectedItems[node->r]=node->availableItems[i];
		node->r+=1;
		node->n+=i;//wziąłem i
		int nowRejected = node->availableItems[i];
		node->sumAvailable-=nowTaken + nowRejected;
		node->availableItems+=i+1;
		node->a-=i+1;
		//dcout<<"Przed rejectem: "<<(*node)<<endl;
		
		int stop=0;
		if(node->sumTaken<=node->capacity){
			generateSubs(node, solutions);
		}else{
			stop=1;
		}
		//dcout<<"Przed rejectem: "<<(*node)<<endl;


		node->a+=i+1;
		node->availableItems-=i+1;
		node->sumAvailable+=nowTaken + nowRejected;
		node->n-=i;
		node->r-=1;
		node->sumTaken-=nowTaken;
		//if(i)nowTaken-=node->takenItems[node->n+i-1];
		//cout<<" ";
		//cerr<<"Po wycofaniu zm:"<<(*node)<<endl;
		if(stop)break;
	}
	

	return;
}
int main(){
	ios_base::sync_with_stdio(0);
	int n,k;
	cin>>n>>k;
	int items[n];
	FOR(i,n)cin>>items[i];
	int bp[k];
	FOR(i,k)cin>>bp[i];

	sort(items,items+n);
	map<int,int> mask;
	FORD(i,n)mask[items[i]]=1<<i;

	sort(bp, bp+k);
	//FOR(i,n/2+1)swap(items[i],items[n-1-i]);

	N*root=new N();
	root->takenItems=new int[n];
	root->n=0;
	root->availableItems=items;
	root->a=n;
	root->rejectedItems=new int[n];
	root->r=0;
	root->sumTaken=0;
	root->sumAvailable=sum(items,n);
	root->capacity=bp[k-1];
	
	vector<N*>*subs=new vector<N*>();
	subs->push_back(root);
	int mx = 0;
	int currentBackpack=k-1;
	vector<N*>::iterator it;
	while(mx<n){
		//cout<<ccc<<endl;
		//cout<<currentBackpack;
		if(currentBackpack<0){
			//cout<<currentBackpack;
			//dcout<<"       LOST!"<<endl;
			cout<<"NIE"<<endl;
			return 0;
		}
		vector<N*>*loopSubs=new vector<N*>();
		map<int,int> occured;
		FORIT(it,*subs){
			if((*it)->r+(*it)->a==0){
				//dcout<<"      SUCCESS!!"<<endl;
				cout<<(k-currentBackpack-1)<<endl;
				return 0;
			}
			N*next =*it;//= new N(*it,bp[currentBackpack]);
			next->capacity = bp[currentBackpack];
			int su=next->mask(&mask);
			if(occured.count(su)){
				//dcout<<"skipping ("<<su<<") "<<*next<<endl;
				//dcout<<"-";
				continue;
			}
			int maks=findMax(next->availableItems,next->a);
			if(maks>next->capacity)continue;
			//dcout<<"executing ("<<su<<") "<<*next<<endl;
			occured[su]=1;
			generateSubs(next,loopSubs);
		}
		//dcout<<"This iteration brought us: "<<loopSubs->size()<<" nodes to fill backpack no "<<k-currentBackpack<<endl;
		//while(!subs->empty()) delete subs->back(), subs->pop_back();
		//dcout<<"WYNIKI:"<<endl;
		//if(currentBackpack==99){
		//	FORIT(it,*loopSubs){
				//dcout<<(*(*it))<<endl;
		//	}
			//return 0;
		//}
		//return 0;
		subs = loopSubs;
		--currentBackpack;
	}

	return 0;
}