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
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;


public class pak {

	final static int INF = 1000;
	
	public static void main(String[] args) throws IOException {
		
		pak solver = new pak();
		solver.init();
		solver.solve();
	}
	
	private void init() {
//		int[][] cn = new int[25][25];
//		cn[0][0] = 1;
//		for (int i = 1; i < cn.length; i++) {
//			cn[i][0] = 1;
//			for (int j = 1; j < cn[i].length; j++) {
//				cn[i][j] = cn[i-1][j-1] + cn[i-1][j];
//			}
//		}
//		System.out.println(cn[24][12]);
	}
	
	Map<Integer, Integer>[] cache;
	int[] A;
	int[] C;
	int N,M;
	
	private void solve() throws IOException { 
		Reader in = new Reader(System.in);
		PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
		
		N = in.nextInt();
		M = in.nextInt();
		int[] a = new int[N];
		for (int i = 0; i < N; i++) a[i] = in.nextInt();
		int[] c = new int[M];
		for (int i = 0; i < M; i++) c[i] = in.nextInt();
		
		Arrays.sort(a);
		Arrays.sort(c);
		A = reverse(a);
		C = reverse(c);
		initCache(N);

		int r = go(0,0);
		
		if (r == INF) {
			out.println("NIE");
		} else {
			out.println(r);
		}
		
		out.flush();
		out.close();
	}
	
	private int go(int globalMask, int nr) {
		int used = Integer.bitCount(globalMask);
		if (used == N) return nr;
		if (nr >= M) return INF;
		Integer res = cache[nr].get(globalMask);
		if (res != null) return res;
		
		int r = INF;
		int[] p = countMovment(globalMask, used);
		int n = N - used;
		for (int mask = 1; mask < (1 << n); mask++) {
			if (isGoodCombination(mask, p, C[nr])) {
				r = Math.min(r, go(mergeMasks(globalMask, mask, p), nr + 1));
			}
			
		}
		
		cache[nr].put(globalMask, r);
		return r;
	}

	private boolean isGoodCombination(int mask, int[] p, long capacity) {
		//plecak ma wystarczajaca waga
		for (int i = 0; i < p.length; i++) if (((1 << i) & mask) > 0) {
			capacity -= A[p[i]];
		}
		if (capacity < 0) return false;
		
		//
		for (int i = 0; i < p.length; i++){
			if (((1 << i) & mask) > 0) {
				if (i > 0 && ((1 << (i-1)) & mask) == 0) {
					//moge zamienic pojedynczy przedmiot na ciezszy
					if (capacity >= A[p[i-1]] - A[p[i]]) {
						return false;
					}
				}
			} else {
				//cos jescze moge doladowac
				if (capacity >= A[p[i]]) {
					return false;
				}
			}
			
		}
		return true;
	}

	private int mergeMasks(int globalMask, int mask, int[] p) {
		for (int i = 0; i < p.length; i++) if (((1 << i) & mask) > 0) {
			globalMask |= (1 << p[i]);
		}
		return globalMask;
	}

	private int[] countMovment(int mask, int used) {
		int[] p = new int[N-used];
		int idx = 0;
		for (int i = 0; i < N; i++) if (((1 << i) & mask) == 0) {
			p[idx++] = i;
		}
		return p;
	}

	private void initCache(int n) {
		cache = new Map[n+1];
		for (int i = 0; i <= n; i++) cache[i] = new HashMap<Integer, Integer>();
	}

	private int[] reverse(int[] a) {
		int n = a.length;
		int[] r = new int[n];
		for (int i = 0; i < n; i++) r[i] = a[n-1 - i];
		return r;
	}

	private static class Reader {
	    BufferedReader reader;
	    StringTokenizer tokenizer;

	    /** call this method to initialize reader for InputStream */
	    Reader(InputStream input) {
	        reader = new BufferedReader(
	                     new InputStreamReader(input) );
	        tokenizer = new StringTokenizer("");
	    }

	    public void skipLine() throws IOException {
			reader.readLine();
		}

		/** get next word */
	    public String next() throws IOException {
	        while ( ! tokenizer.hasMoreTokens() ) {
	            //TODO add check for eof if necessary
	            tokenizer = new StringTokenizer(
	                   reader.readLine() );
	        }
	        return tokenizer.nextToken();
	    }

	    public int nextInt() throws IOException {
	        return Integer.parseInt( next() );
	    }
	    
	    public double nextDouble() throws IOException {
	        return Double.parseDouble( next() );
	    }
	    
	    public long nextLong() throws IOException {
	    	return Long.parseLong(next());
	    }
	}
}