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
#include<cstdio>
#include<algorithm>
#include<cassert>
#include<complex>
#include<map>
#include<iomanip>
#include<sstream>
#include<queue>
#include<set>
#include<string>
#include<vector>
#include<iostream>
#include<cstring>
#include<stack>
#define FOR(i, a, b) for(int i =(a); i <=(b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define fup FOR
#define fdo FORD
#define REP(i, n) for(int i = 0;i <(n); ++i)
#define VAR(v, i) __typeof(i) v=(i)
#define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define siz SZ
#define CLR(x) memset((x), 0, sizeof(x))
#define PB push_back
#define MP make_pair
#define X first
#define Y second 
#define FI X
#define SE Y
#define SQR(a) ((a)*(a))
#define DEBUG 1
#define debug(x) {if (DEBUG)cerr <<#x <<" = " <<x <<endl; }
#define debugv(x) {if (DEBUG) {cerr <<#x <<" = "; FORE(it, (x)) cerr <<*it <<", "; cout <<endl; }}
using namespace std;
typedef long long LL;
typedef long double LD;
typedef pair<int, int>P;
typedef vector<int>VI;
const int INF=1E9+7;
template<class C> void mini(C&a4, C b4){a4=min(a4, b4); }
template<class C> void maxi(C&a4, C b4){a4=max(a4, b4); }

int n, m, a[30], c[110], cc[110]; int best = INF;

int main(){
	ios_base::sync_with_stdio(false);
	cout << setprecision(15) << fixed;
	cin>>n>>m;
	FOR(i,1,n) cin>>a[i]; sort(a+1,a+1+n); reverse(a+1,a+1+n);
	FOR(i,1,m) cin>>c[i]; sort(c+1,c+1+m); reverse(c+1,c+1+m);
	mini(m,n);
	FOR(i,1,1000000) {
		FOR(j,1,m) cc[j] = c[j];
		int d=1;
		if (best<INF) m = best-1;
		FOR(j,1,m) {
			while (d<=n && cc[j]>=a[d]) {
				cc[j]-=a[d++];
			}
			if (d>n) {
				mini(best, j);
				break;
			}
		}
		FOR(j,1,n-2) swap(a[j], a[j+1+rand()%(n-j)]);
		if (rand()%2) swap(a[n-1],a[n]);
	}
	if (best == INF) {cout<<"NIE"<<endl;} else {cout<<best<<endl;}
	return 0;
}