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
#include <unordered_map>
#include <iostream>
#include <vector>
#include <utility>
#include <cassert>
#include <algorithm>
using namespace std;
typedef unsigned int U;
typedef unordered_map<U,int> Cache;
typedef pair<U, U> PUU;
typedef vector<PUU> VUU;
typedef vector<U> VU;
const int MAXN = 24;
const int MAXM = 127;

short cache[(1<<MAXN)+1];
U a[MAXN+1];
U c[MAXM];
int n, m;

inline short make_value(int idx, int res) {
  return short((idx << 8) + res);
}

inline int get_idx(short value) {
  return (value >> 8);
}
inline int get_res(short value) {
  return (value & ((1 << 8) - 1));
}

inline U get_sum(U mask) {
  U res = 0;
  for (int i = 0; i < n; ++i) {
    if ((1<<i) & mask) {
      continue;
    }
    res += a[i];
  }
  return res;
}

int pack(int idx, U mask);

int gather_packs(int idx, U mask, int bit, U cap, U sum, U last_two) {
  U all_ones = (1 << (bit + 1)) - 1;
  if (bit < 0 or ((mask & all_ones) == all_ones)) {
    return pack(idx + 1, mask);
  }
  if (cap >= sum) {
    return pack(idx + 1, ((mask >> (bit + 1)) << (bit + 1)) | all_ones);
  }
  while (mask & (1 << bit)) {
    bit--;
  }
  U w = a[bit];
  int res = m + 1;
  if (w <= cap) {
    res = min(res, gather_packs(idx, mask | (1 << bit), bit - 1, cap - w, sum - w, last_two));
  }
  if (w > cap or cap >= last_two) {
    res = min(res, gather_packs(idx, mask, bit - 1, cap, sum - w, last_two));
  }
  return res;
}

inline int first_free(U mask) {
  for (int i = n-1; i >= 0; i--) {
    if (0 == (mask & (1 << i))) {
      return i;
    }
  }
  return n;
}

inline int last_free(U mask) {
  for (int i = 0; i < n; i++) {
    if (0 == (mask & (1 << i))) {
      return i;
    }
  }
  return n;
}

inline int second_last_free(U mask) {
  bool was = false;
  for (int i = 0; i < n; i++) {
    if (0 == (mask & (1 << i))) {
      if (was) {
        return i;
      } else {
        was = true;
      }
    }
  }
  return n;
}

int pack(int idx, U mask) {
  int value = cache[mask];
  if (value != 0 and get_idx(value) <= idx) {
    return get_res(value);
  }
  int res = m + 1;
  U sum = get_sum(mask);
  if (sum == 0) {
    res = idx;
  } else {
    res = m + 1;
    int f = first_free(mask);
    if (a[f] <= c[idx]) {
      U last_two = a[second_last_free(mask)] + a[last_free(mask)];
      res = min(res, gather_packs(idx, mask, n - 1, c[idx], sum, last_two));
    }
  }
  cache[mask] = make_value(idx, res);
  return res;
}

void solve() {
  assert(2 == scanf("%d%d", &n, &m));
  for (int i = 0; i < n; ++i) {
    assert(1 == scanf("%u", &a[i]));
  }
  for (int i = 0; i < m; ++i) {
    assert(1 == scanf("%u", &c[i]));
  }
  c[m] = 0;
  sort(a, a + n, less<int>());
  sort(c, c + m, greater<int>());
  int res = pack(0, 0u);
  if (res > m) {
    printf("NIE\n");
  } else {
    printf("%d\n", res);
  }
}

int main() {
  solve();
  return 0;
}