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
#include <cstdio>
#include <iostream>
#include <sstream>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <bitset>
#include <algorithm>
#include <iterator>
#include <cstring>
#include <string>
#include <cmath>
#include <numeric>
#include <ctype.h>
#include <cctype>
#include <complex>
#include <cassert>
#include <cstdlib>
#include <functional>
#include <utility>
#include <ctime>
//#include <ext/hash_set>
//#include <ext/hash_map>
//struct hasher{ size_t operator()(const string& s) const{ 
//		return hash<const char*>()(s.c_str());} };
using namespace std;
using namespace __gnu_cxx;
#define PB push_back
#define PF push_front
#define MP make_pair
#define X first
#define Y second
#define ST first
#define ND second
#define MT(x,y,z) MP(MP(x,y),z)
#define XT X.X
#define YT X.Y
#define ZT Y
#define REP(i,n) for(int i=0;i<(n);i++)
#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 FORE(i,x) for(VAR(i,(x).begin());i!=(x).end();++i)
#define FORDE(i,x) for(VAR(i,(x).rbegin());i!=(x).rend();++i)
#define FOREACH(i,x) FORE(i,x)
#define VAR(v,n) __typeof(n) v = (n)
#define IT(x) __typeof((x).begin())
#define present(c,x) ((c).find(x) != (c).end()) 
#define cpresent(c,x) (find(ALL(c),x) != (c).end())
#define SIZE(x) (int)((x).size())
#define ALL(x) (x).begin(),(x).end()
#define CLR(x) memset((x),0,sizeof (x))
#define CPY(to,from) memcpy(to,from,sizeof (from))
#define IND(t,n,v) lower_bound(t,t+n,v) - t
#define sqr(a) ((a)*(a))
#define db(x) { if (DEBU) cerr << "line " << __LINE__ << " " << #x \
					 << " = " << x << "\n"; }
#define dbn(x) { if (DEBU) cerr << "\n"; }
#define dba(x) { if (DEBU) { cerr << "line " << __LINE__ << " " << #x \
					<< " = {"; FORE(qwe,x) {if(qwe!=(x).begin()) cerr << ","; \
					 cerr << *qwe; } cerr << "}\n"; } }
#define dbt(t,x) { if (DEBU) { REP(qwe,x) cerr << #t << "[" << qwe << "]=" \
					 << t[qwe] << " "; cerr << "\n"; }}
#define dbtt(t,x,y) { if (DEBU) { REP(qwe,x) { REP(rty,y) cerr << #t \
					 << "[" << qwe << "][" << rty << "]=" << t[qwe][rty] \
					 << " "; cerr << "\n"; }}}
#define dbb(x,r) { if (DEBU) { bitset<r> qwe(x.to_ulong()); \
				 cerr << "line " << __LINE__ << " " << #x \
				 << " = " << qwe << "\n"; } }
#define dbbt(t,x,r) { if (DEBU) REP(rty,x) dbb(t[rty],r); }

typedef long double ld;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef long long int lli, LL;
typedef pair<int, int> pii, PII;
typedef pair<pii, int> piii;
typedef pair<lli, lli> pll;
typedef pair<pll, lli> plll;
typedef vector<int> vi, VI;
typedef vector<pii> vii;
typedef vector<piii> viii;
typedef vector<lli> vl;
typedef vector<pll> vll;
typedef vector<plll> vlll;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<vi> vvi;

template<class T,class U>ostream & operator<<(ostream&os,const pair<T,U>&qwe){
	    return os << "(" << qwe.X << "," << qwe.Y << ")";
}
template<class T> ostream& operator << (ostream &os, const vector<T>& qwe){
        REP(rty,(int)qwe.size()) os << "vec[" << rty << "]=" << qwe[rty] << " "; 
		return os << "\n";
}
template<class T> ostream& operator << (ostream &os, const set<T>& qwe){
        os << "set:{"; FORE(rty,qwe) {if(rty!=qwe.begin()) os<<","; os << *rty;} 
		return os << "}\n";
}
template<class T> ostream& operator << (ostream &os, const list<T>& qwe){
        os << "list:{"; FORE(rty,qwe) {if(rty!=qwe.begin()) os<<","; os << *rty;}
		return os << "}\n";
}

const double E = 2.718281828459045235360287471351;
const double PI = 3.1415926535897932384626433832795;

#define IsZero(x) (x>=-EPS && x<=EPS)
#define sgn(x) (IsZero(x)?0:((x<0)?-1:1))

lli los(lli l, lli r) { 
	return l + (lli) ((double)(r-l+1) * (rand() / (RAND_MAX + 1.0))); }
string cts(char c) { string s = ""; s+=c; return s; }

template<class T> void minX(T &x, T y){ if(x>y)x=y; }
template<class T> void maxX(T &x, T y){ if(x<y)x=y; }
template<class T> void min(T x, T y, T z){ return min(min(x,y),z); }
template<class T> void max(T x, T y, T z){ return max(max(x,y),z); }

template <class Ty, class Tx>
Ty cast(const Tx &x) {
	    Ty y; stringstream ss(""); ss<<x; ss.seekg(0); ss>>y; return y;
}
int toInt(string s){int r=0;istringstream sin(s);sin>>r;return r;}

// #pragma comment (linker, "/STACK:256000000")
// #x zwraca nazwe zmiennej    a ## b laczy napisy i daje zmienna ab

// iostream:cin,getline(cin,string) 
// stdio:scanf,gets(char*),fgets(char*),getchar()
// fgets nie wczytuje bialych znakow z nastepnej linii, dla stringa iostream
// iostream: cout;   stdio: putchar(char) printf
// ios_base::sync_with_stdio(0); przyspiesza iostreama, nie mieszac z stdio 
// freopen ("in.txt","r",stdin); freopen ("out.txt","w",stdout); ... 
// fclose (stdin); fclose (stdout);
// while(scanf("%d",&n)!=EOF)  '\n' == 10
// %c %s %d %u %hd short int %lld %f float %lf double %Lf long double 
// %x unsigned hex small %X unsigned hex big %o octal %llu unsigned lli
// (%5d, 2)    2  (%05d,2)00002  (%x,100) 64  (%#x,100) 0x64  (%#o,100) 0144 
// (%6.2lf, 2)   2.00  (%+d,5) +5   (%*d,5,10)   10 (%.*f,3,2) 2.000 (0*d,3,1) 003
//
// int -> char* : int a; char b[N]; sprintf(b, "%d", a);
// char* -> int : int a; char b[N]; sscanf(b, "%d", &a);
// int -> char* podstawa base : char a[N]; itoa(int, a, base)
// char* -> int podstawa base : char a[N]; strtol(a, NULL, base)
// atoi(char*) to int, atof(char*) to double
// string s; stringstream ss(s); while(ss >> buf) res.PB(buf)
// 
// string -> char* : string a; a.c_str();
// char* -> string : char a[N]; string(a);
// porowananie char* : strcmp(char*,char*) np return strcmp(&c[a],&c[b])<0;
// strlen strcpy strncpy strdup strcat strcmp strncmp 
// strchr strcspn strpbrk strstr strrev
// isupper(int) islower(int) toupper(int) tolower(int) 
// isspace isalpha isalnum isdigit isxdigit ispunct
//
// sort(ALL(v),less<int>()) rosnaca, sort((v),greater<int>()) malejaca
// bool func(pii a,pii b){return a.X*a.X+a.Y*a.Y < b.X*b.X+b.Y*b.Y;}
// struct T {string name; lli X,Y;};
// bool operator<(T a,T b){return a.X*a.X+b.Y*b.Y < a.X*a.X+b.Y*b.Y;}
//
// bitset<N> bs(lli); 
// set() filp() reset() size() count() any() none() [] to_ulong() to_string() 
// __builtin_popcount(int) lli nie dziala,  __ gcd(int,int),  x<<32 does nothing
// __builtin_clz(int) __builtin_ctz(int) for 0 undefined
// REP(s,(1<<N)) - wszystkie podzbiory  for(s=X;s!=0;s=(s-1)&X) - podzbiory X
// !x&(x-1) pow 2, x&(-x)  x&~(x-1) najmniej znaczac bit, ffs(int) jego indeks
// memset(t,0/1/-1,sizeof(t))  1l 1ll 1u 1f
//
// set.size() liniowo, set.end() O(1), set.erase(it++), multiset.erase(x) wszystkie x 
// priority_queue: push,top,pop     queue:push,pop,front,back  
// set/map:insert,erase,find,set.lower_bound 	list:insert,erase,reverse
// deque(jak vector) / list:back,front,push_back,push_front,pop_back,pop_front
// hash_set<string,hasher> hs; hash_map<string,int,hasher> hm; hm.bucket_count()
//
// exit(0) M_PI fabsl abs labs llabs sqrtl llround round(0.5)=1 
// lower_bound - pierwszy nie mniejszy  upper_bound - pierwszy wiekszy
// min_element(All(x)) max_element(ALL(x)) 
// sort(ALL(v)); v.erase(v.unique(),v.end())
// set_union, set_intersection, set_differenece, set_symmetric_difference
// random_shuffle RAND_MAX int rand()=random() srand(int seed) 
// complex<double> x; real,imag,abs,arg,norm,conj,polar,cos,exp,sin,sqrt
// LR :: ++ -- () [] . -> RL ++ -- (unary)+- ! ~ (type) * (address)& 
// LR .* ->* * / % + - << >> < <= > >= == != & ^ | && || ?: = += -= itd
const double EPS = 1e-9;
#define INF 1000000005
#define MAXN 100005
#define MAXM 100005 
#define DEBU 1
#define MOD 1000000009
#define N 105
#define NPOW 17000000

int a[N], c[N];
queue<int> q;
pii zaj[NPOW];
bool vis[NPOW];


int main(){
	int n,m; scanf("%d%d",&n,&m);
	REP(i,n) scanf("%d", a+i); REP(i,m) scanf("%d",c+i);
	sort(c, c+m);
	reverse(c, c+m);
	REP(i,NPOW) zaj[i] = MP(INF, INF);
	q.push(0); zaj[0] = MP(0,0);

	while(!q.empty()){
		int zb = q.front(); q.pop();
		if(zaj[zb].X == INF) break;	
		//printf("%x %d %d \n", zb, zaj[zb].X, zaj[zb].Y);
		REP(i,n){
			if(zb & 1<<i) continue;
			int zb2 = zb | 1<<i;
			//printf("inner %x %d %d \n", zb2, a[i], c[i]);
			if(zaj[zb].Y + a[i] <= c[zaj[zb].X]){
				//if(zaj[zb2] < MP(zaj[zb].X, zaj[zb].Y + a[i])) break;
				minX(zaj[zb2], MP(zaj[zb].X, zaj[zb].Y + a[i]));
			}
			else if(a[i] <= c[zaj[zb].X + 1]){
				//if(zaj[zb2] < MP(zaj[zb].X + 1, a[i])) break;
				minX(zaj[zb2], MP(zaj[zb].X + 1, a[i]));
			}
			if(!vis[zb2]) { vis[zb2] = 1; q.push(zb2); }
		}
	}

	if(zaj[(1<<n) - 1].X != INF) printf("%d\n", zaj[(1<<n) - 1].X + 1);
	else printf("NIE\n");
	
	return 0;
}