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
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <queue>
#include <set>
#include <map>
#include <numeric>
#include <utility>
#include <string>
#include <sstream>
#include <algorithm>

using namespace std;
const int N = 24;
const int M = 100;
int space[2][1<<N];
int items[N];
int bags[M];
int n, m;
int l;

bool simple() {
  return accumulate(items, items + n, 0LL) <= accumulate(bags, bags + m, 0LL);
}

int main() {
  scanf("%d%d", &n, &m);
  l = 1 << n;
  for (int i = 0; i < n; ++i) scanf("%d", items + i);
  for (int i = 0; i < m; ++i) scanf("%d", bags + i);
  sort(bags, bags + m);
  reverse(bags, bags + m);
  m = min(n, m);

  int b = m;
  if (simple()) {
    space[1][0] = 0;
    for (int s = 1; s < l; ++s) {
      space[1][s] = -1;
    }

    for (b = 0; b < m; ++b) {
      const int x = b & 1;
      const int y = 1 - x;
      for (int s = 0; s < l; ++s) {
        space[x][s] = -1;
        if (space[y][s] >= 0) {
          space[x][s] = bags[b];
          continue;
        }
        for (int si = 1, i = 0; si <= s; si *= 2, ++i) {
          if (!(s & si)) continue;
          if (space[x][s ^ si] < 0) break;
          space[x][s] = max(space[x][s], space[x][s ^ si] - items[i]);
        }
      }
      if (space[x][l - 1] >= 0) break;
    }
  }
  if (b < m) {
    printf("%d\n", b + 1);
  } else {
    printf("NIE\n");
  }
  return 0;
}