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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
//#define READ_NUM_TEST_CASES
//#define ASSERTS		// force asserts on online judge
//#define OLD_COMPILER

// (SOLUTION IS AT THE END OF THIS FILE)

////////////////////////// PROTOTYPING TEMPLATE /////////////////////////////////
//
// Implementation: Adam Blaszkiewicz (atablash.pl), 2012-2014
/*
    This program is licensed under GNU General Public License version 3.
    See <http://www.gnu.org/licenses/>.
*/
//
//

// enable c++ iostream; only if no fastio
//#define IOSTREAM

//#define NO_UNLOCKED	// fread_unlocked, fwrite_unlocked



#include<cstdint>
#include<utility>

namespace ab{

typedef int8_t i8;
typedef uint8_t ui8;
typedef int16_t i16;
typedef uint16_t ui16;
typedef int32_t i32;
typedef uint32_t ui32;
typedef int64_t i64;
typedef uint64_t ui64;


// old:
typedef i64 ll;
typedef ui64 ull;

typedef std::pair<i32,i32> pii;

typedef unsigned int uint;
typedef unsigned short ushort;
typedef unsigned char uchar;

struct VOID {};


template<typename I>struct FloatFrom{typedef VOID R;};
template<>struct FloatFrom<i32>{typedef double R;};
template<>struct FloatFrom<ui32>{typedef double R;};
template<>struct FloatFrom<i64>{typedef long double R;};
template<>struct FloatFrom<ui64>{typedef long double R;};
template<>struct FloatFrom<i16>{typedef float R;};
template<>struct FloatFrom<ui16>{typedef float R;};
template<>struct FloatFrom<i8>{typedef float R;};
template<>struct FloatFrom<ui8>{typedef float R;};
template<>struct FloatFrom<float>{typedef float R;};
template<>struct FloatFrom<double>{typedef double R;};
template<>struct FloatFrom<long double>{typedef long double R;};

template<typename I>struct Double{typedef VOID R;};
template<>struct Double<i8>{typedef i16 R;};
template<>struct Double<ui8>{typedef ui16 R;};
template<>struct Double<i16>{typedef i32 R;};
template<>struct Double<ui16>{typedef ui32 R;};
template<>struct Double<i32>{typedef i64 R;};
template<>struct Double<ui32>{typedef ui64 R;};

template<>struct Double<float>{typedef float R;};
template<>struct Double<double>{typedef double R;};
template<>struct Double<long double>{typedef long double R;};


#if defined __GNUC__ and ( __WORDSIZE == 64 )
typedef int128_t i128;
typedef uint128_t ui128;
template<>struct Double<i64>{typedef i128 R;};
template<>struct Double<ui64>{typedef ui128 R;};
#endif


}

#define FASTIO

#ifdef NO_FASTIO
#undef FASTIO
#endif

#ifdef DEBUG
#undef NDEBUG
#pragma message "Compiling in DEBUG mode."
#undef FASTIO

#ifdef _WIN32
#include<windows.h>
inline void ec(int a){static HANDLE h=GetStdHandle(-12);SetConsoleTextAttribute(h,a?12:0xf);}
inline void oc(int a){static HANDLE h=GetStdHandle(-11);SetConsoleTextAttribute(h,a?10:0xf);}
#else
// *nix includes for debug
#endif

#else
#define NDEBUG
#define ec(x)
#define oc(x)
#endif



#include <cstdio>
#include <cfloat>



#include<type_traits>

#define _CAT(a,b) a##b
#define CAT(a,b) _CAT(a,b)
#define _10TH(a,b,c,d,e,f,g,h,i,CNT,...) CNT
#define VA_CNT(...) _10TH(__VA_ARGS__,9,8,7,6,5,4,3,2,1)
#define VA_SEL(a,...) CAT(a,VA_CNT(__VA_ARGS__))(__VA_ARGS__)

#define _VA_GTONE(...) _10TH(__VA_ARGS__,1,1,1,1,1,1,1,1,0)
#define VA_GTONE(a,...) CAT(a,_VA_GTONE(__VA_ARGS__))(__VA_ARGS__)

#define foreach(e,c) for(auto&e:c)

#define  L0(str) 			fprintf(stderr,"%s:%d - " str "\n",__FILE__,__LINE__)
#define  L1(str,...)		fprintf(stderr,"%s:%d - " str "\n",__FILE__,__LINE__,__VA_ARGS__)

#define L(...) VA_GTONE(L,__VA_ARGS__)

#ifdef NDEBUG

#define D if(0)
#define A(x)
#define TEST_FLOAT(x)
#define ASSERT_FLOAT(x)

#else

#define A(x) assert(x);

#define _TEST_FLOAT(x) ((x) <= LDBL_MAX && (x) >= -LDBL_MAX)
#define ASSERT_FLOAT(f) A(_TEST_FLOAT(f))

#define D

#endif

#define V(x) (E<<#x<<": "<<(x)<<N)
#define XX {E<<"XX LINE "<<__LINE__<<N;}

#if defined DEBUG or defined ASSERTS
#undef NDEBUG
#else
#define NDEBUG
#endif

#ifdef _WIN32

// TODO
#ifndef NO_UNLOCKED
#define NO_UNLOCKED
#endif

//#define fread_unlocked _fread_nolock
//#define fwrite_unlocked _fwrite_nolock

#endif



#include<bits/stdc++.h>

#define mp make_pair
#define it iterator
#define be begin()
#define en end()
#define sz size()
#define fi first
#define se second
#define pub push_back
#define puf push_front
#define pob pop_back()
#define pof pop_front()
#define all(x) (x).begin(),(x).end()
#define bs binary_search
#define lb lower_bound
#define ub upper_bound
#define type(x) __typeof(x)

#define fo3(i,a,b) for(int i=(int)(a); i<(int)(b); ++i)
#define of3(i,a,b) for(int i=(int)((b)-1); i>=(int)(a); --i)

#define fo2(i,n) fo3(i,0,n)
#define of2(i,n) of3(i,0,n)

#define fo(...) VA_SEL(fo,__VA_ARGS__)
#define of(...) VA_SEL(of,__VA_ARGS__)

#define inl		inline __attribute__ ((always_inline))
#define ninl	__attribute__ ((noinline))

const char N='\n';
const char S=' ';

using namespace std;
using namespace __gnu_cxx;

namespace ab{}
using namespace ab;



template<class T>inline T cbitl(T x,int i){return (x<<i) ^ (x>>(sizeof(T)*8-i));}

namespace std{
// 1,2,last (!!!!!)
template<>template<class A,class B>struct hash<map<A,B>>{
    size_t operator()(const map<A,B>& m) const{
        size_t r = m.size();
        if(!r)return r;
        auto i=m.be;
        r^=hash<type(i)>()(i);
        ++i;
        r^=cbitl(hash<type(i)>()(*i),sizeof(r)*8/4);
        i=m.end();
        --i;
        r^=cbitl(hash<type(i)>()(*i),sizeof(r)*8/2);
        return r;
    }
};
template<>template<class A, class B>struct hash<pair<A,B>>{
    size_t operator()(const pair<A,B>& p) const{
        return hash<A>()(p.first) ^ cbitl(hash<B>()(p.second),sizeof(size_t)*8/2);
    }
};
}

#define rint _rint()
#define ruint _ruint()
#define rll _rll()
#define rull _rull()
#define rchar _rchar()

#ifndef FASTIO
inline void fin(int){}inline void fout(int){}
#ifdef IOSTREAM
inline int _rint(){int r;cin>>r;return r;}
inline uint _ruint(){uint r;cin>>r;return r;}
inline ull _rull(){ull r;cin>>r;return r;}
inline char _rchar(){return cin.get();}
template<typename T>inline void W(const T& i){cout<<i;}
inline void W(){W('\n');}
#else
inline int _rint(){int r;scanf("%d",&r);return r;}
inline uint _ruint(){uint r;scanf("%u",&r);return r;}
inline ull _rull(){ull r;scanf("%llu",&r);return r;}
inline ull _rll(){ll r;scanf("%lld",&r);return r;}
inline char _rchar(){return getchar();}
inline void W(char a='\n'){printf("%c",a);}
inline void W(int a){printf("%d",a);}
inline void W(uint a){printf("%u",a);}
inline void W(const ull& a){printf("%llu",a);}
inline void W(const ll& a){printf("%lld",a);}
inline void W(const char *str){fputs(str,stdout);}
inline void W(const double& d){printf("%.20f",d);}
inline void W(const long double& d){printf("%.50Lf",d);}
#endif
#else

#ifndef NO_UNLOCKED
#define FREAD fread_unlocked
#define FWRITE fwrite_unlocked
#else
#define FREAD fread
#define FWRITE fwrite
#endif

// todo: test on pointers
const int _IB=32768;const int _OB=32768;
char _iB[_IB];int _iP = _IB;char _oB[_OB];int _oP = 0;
inline void fin(int mc){int r=_IB-_iP;if(r>mc)return;fo(i,r)_iB[i]=_iB[i+_iP];	// TODO: memcpy
    FREAD(_iB+r,1,_iP,stdin);_iP = 0;}
inline void fout(int mc){if(_OB-_oP>mc)return;FWRITE(_oB,1,_oP,stdout);_oP = 0;}
inline void W(char c='\n'){_oB[_oP++]=c;}
inline char _rchar(){return _iB[_iP++];}
template<class T>inline T _rur(){
    T r=0;char c=rchar;
    do c-='0',r=r*10+c,c=rchar;while(c>='0');
    return r;}
template<class T>inline T _rsi(){
	char c=rchar;
	while(c<'-')c=rchar;
	if(c=='-')return -_rur<T>();
	else{--_iP;return _rur<T>();}}
template<class T>inline T _ru(){while(rchar<'0');--_iP;return _rur<T>();}

inline int _rint(){fin(15);return _rsi<int>();}
inline uint _ruint(){fin(15);return _ru<uint>();}
inline ll _rll(){fin(25);return _rsi<ll>();}
inline ull _rull(){fin(25);return _ru<ull>();}

const int _rs = 20;char _r[_rs];
template<class T>inline void _w(T i){
	if(numeric_limits<T>::is_signed && i<0)W('-'),i=-i;
	else if(i==0){W('0');return;}
	int pos = _rs;
	do{_r[--pos]='0'+i%10;i/=10;}while(i);
	do W(_r[pos]),++pos;while(pos<_rs);}
inline void W(const int32_t& i){fout(15);_w(i);}
inline void W(const uint32_t& i){fout(15);_w(i);}
inline void W(const int64_t& i){fout(25);_w(i);}
inline void W(const uint64_t& i){fout(25);_w(i);}
inline void W(const char*str){int l=strlen(str);fout(l);memcpy(&_oB[_oP],str,l);_oP+=l;}
inline void W(double d){fout(50);_oP+=sprintf(&_oB[_oP],"%.20f",d);}	// todo
#endif
template<class A,class B>inline void W(const A&a,const B&b){W(a);W(b);}
template<class A,class B,class C>
inline void W(const A&a,const B&b,const C&c){W(a);W(b);W(c);}
template<class A,class B,class C,class DD>
inline void W(const A&a,const B&b,const C&c,const DD&dd){W(a);W(b);W(c);W(dd);}
template<class T>inline void sortall(T&t){sort(t.be,t.en);}
template<class T>inline void uniqueall(T&t){t.resize(unique(t.be,t.en));}

inline int count1(uint i){return __builtin_popcount(i);}
inline int count1(ull i){return __builtin_popcountll(i);}
inline int parity(uint i){return __builtin_parity(i);}
inline int parity(ull i){return __builtin_parityll(i);}
inline int ilog(uint i){return 31-__builtin_clz(i);} // int log; floor(log2(i))
inline int ilog(ull i){return 63-__builtin_clzll(i);}
#define powerof2(x) !(x&(x-1))
#define subset(a,b) (a&b==a)
#define kbegin(k) ((1<<k)-1)
#define kend(k,n) ((kbegin(k)<<n-k)+1)
inline uint knext(uint a){uint b=(a|(a-1))+1;return b|((a^b)>>(__builtin_ctz(a)+2));}
#define fors(x,k,n) for(uint x=kbegin(k);x<kend(k,n);x=knext(x))


template<class A,class B>inline ostream&operator<<(ostream&o,const pair<A,B>&p){o<<'<'<<p.fi<<", "<<p.se<<'>';return o;}
template<class A,class B>inline ostream&operator<<(ostream&o,const map<A,B>&m){o<<N;for(auto&k:m)o<<"map["<<k.fi<<"] -> "<<k.se<<N;return o;}
template<class A>inline ostream&operator<<(ostream&o,const vector<A>&v){o<<N;fo(i,v.sz)o<<"vector["<<i<<"] = "<<v[i]<<N;return o;}

struct _O{template<class T>_O&operator<<(const T&t){oc(1);W(t);oc(0);return*this;}}O;
struct _I{
	_I&operator>>(int32_t&t){t=rint;return*this;}
	_I&operator>>(uint32_t&t){t=ruint;return*this;}
	_I&operator>>(int64_t&t){t=rll;return*this;}
	_I&operator>>(uint64_t&t){t=rull;return*this;}
}I;

struct _E{template<class T>_E&operator<<(const T&t){D{ec(1);cerr<<t;ec(0);}return*this;}}E;



inline void tc();
int main()
{
#ifdef FASTIO
    setvbuf(stdin,0,_IONBF,0);setvbuf(stdout,0,_IONBF,0);
#elif defined IOSTREAM
	ios_base::sync_with_stdio(0);cin.tie(0);cout.precision(99);cout.setf(ios::fixed,ios::floatfield);
#endif

#ifdef DEBUG
#ifdef _WIN32
	ec(0);
	oc(0);
#else
// TODO: *nix
#endif
#endif
	
#ifdef READ_NUM_TEST_CASES
	int nz=rint;fo(i,nz)
#endif
	tc();
#ifdef FASTIO
    FWRITE(_oB,1,_oP,stdout);
#endif
	return 0;
}
// END OF TEMPLATE








// first greater or equal
template<class FUN, class ARG, class VAL, class=typename std::enable_if<std::is_integral<ARG>::value>::type>
inline ARG funlb(const ARG&_a, const ARG&_b, const VAL&val, const FUN&f)
{
	ARG a = _a;
	ARG b = _b;
	A(a<b);
	while(a+1!=b){
		ARG s = (a+b)/2;
		auto nval = f(s-1);
		if(nval >= val)b=s;
		else a=s;
	}
	return a;
}

int rsum=0;

int greedy(vector<int> rzeczy, vector<int> plecaki)
{
	while(!rzeczy.empty()){
		int r = rzeczy.back();
		uint pmax = 0;
		while(pmax<plecaki.sz && plecaki[pmax] >= r)++pmax;
		if(pmax==0)return 0;
		
		uint rnd = rand()%pmax;
		plecaki[rnd]-=r;
		rzeczy.pob;
		A(plecaki[rnd]>=0);
		if(plecaki[rnd]==0) plecaki.erase(plecaki.be+rnd);
		else while(rnd<plecaki.sz-1 && plecaki[rnd] < plecaki[rnd+1]) swap(plecaki[rnd],plecaki[rnd+1]), ++rnd;
	}
	return 1;
}


int solve(const vector<int>& _rzeczy, const vector<int>& _plecaki, int ple, int tries)
{
	A(ple <= (int)_plecaki.sz);
	vector<int> rzeczy(_rzeczy);
	vector<int> plecaki(_plecaki.be,_plecaki.be+ple);
	
	int psum=0; fo(i,plecaki.sz)psum+=plecaki[i];
	int nadmiar = psum-rsum;
	if(nadmiar < 0){
		//E << "psum " << psum << "   rsum " << rsum << N;
		return 0;
	}
	
	//++level;
	//V(rzeczy);
	//V(plecaki);
	if(rzeczy.back() > plecaki[0]){
		//--level;
		return 0;
	}
	
	fo(i,tries){
		int g = greedy(rzeczy,plecaki);
		if(g){
			//--level;
			return 1;
		}
	}
	//--level;
	return -1;
}

inline void tc()
{
	//srand(time(NULL));
	//auto t = clock();
	vector<int> rzeczy(ruint),plecaki(ruint);
	for(auto&e:rzeczy)I>>e;
	for(auto&e:plecaki)I>>e;
	sort(all(rzeczy));
	sort(all(plecaki),[](int a,int b){return a>b;});
	
	if(plecaki.sz > rzeczy.sz)plecaki.resize(rzeczy.sz);
	
	rsum=0; fo(i,rzeczy.sz)rsum+=rzeczy[i];
	
	int result = plecaki.size()+1;
	
	fo(ple,plecaki.sz+1){
		int ret = solve(rzeczy,plecaki,ple,1);
		if(ret<=0) continue;
		result = ple;
		break;
	}
	
	int t = 0;
	
	for(;;){
		++t;
		//auto nt = clock();
		//double time = 1.0*(nt-t)/CLOCKS_PER_SEC;
		//E<<time<<N;
		
		double time = t*0.0015;
		
		/*if(rzeczy.sz <= 19){
			if(time > 0.4)break;
		}
		else if(rzeczy.sz <= 22){
			if(time > 0.9)break;
		}
		else {*/
			if(time > 1.9)break;
		//}
		
		if(result==1)break;
		//E<<result<<N;
		int ret = solve(rzeczy,plecaki,result-1,1000);
		//E<<"ret "<<ret<<N;
		if(ret==0)break;
		if(ret==1)--result;
	}
	
	
	if(result>(int)plecaki.sz)O<<"NIE"<<N;
	else O<<result<<N;
}