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
/*
 * Korzystalem z:
 * http://stackoverflow.com/questions/757059/position-of-least-significant-bit-that-is-set
 * http://stackoverflow.com/questions/8898807/pythonic-way-to-iterate-over-bits-of-integer
 * http://stackoverflow.com/questions/15932237/iterating-over-all-subsets-of-a-given-size
 */
#include <cstdio>
#include <algorithm>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <cmath>
#include <map>
#include <string>
#include <set>
#include <cstring>
#include <iostream>
#include <sstream>
#include <cassert>
#include <bitset>
using namespace std;

#define PB push_back
#define FORE(i,t) for(typeof(t.begin())i=t.begin();i!=t.end();++i)
#define SZ(x) int((x).size())
#define REP(i,n) for(int i=0,_=(n);i<_;++i)
#define FOR(i,a,b) for(int i=(a),_=(b);i<=_;++i)
#define FORD(i,a,b) for(int i=(a),_=(b);i>=_;--i)

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;

#define BITS 8

int n, m;
int weight[26];
int capacity[102];
int results[1 << 24];
bool was[1 << 24] = {};

const int MultiplyDeBruijnBitPosition[32] = {
  0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
  31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
};

int lowest_bit_position(unsigned v) {
  return MultiplyDeBruijnBitPosition[((unsigned) ((v & -v) * 0x077CB531U)) >> 27];
}

vi *old_q = new vi(), *current_q = new vi();

void solve_subset(int pos, int subset) {
  if (pos > 0 && (results[subset] < 0 || was[subset])) {
    results[subset] = max(results[subset], capacity[pos]);
  }
//  cout << "ss pos=" << pos << " subset=" << bitset<BITS>(subset) << " res=" << results[subset] << "\n";
  if (results[subset] >= 0) {
    int left = (1 << n) - 1 - subset;
    bool any = false;
    while (left) {
      int added_bit = left & (~left + 1);
      int added_bit_position = lowest_bit_position(added_bit);
      int new_value = results[subset] - weight[added_bit_position];
      if (new_value > results[subset | added_bit]) {
        if (pos > 0) {
//          current_q->PB(subset | added_bit);
          if (results[subset | added_bit] < 0) {
            old_q->PB(subset | added_bit);
          } else {
            
          }
//          cout << "q.add2(" << bitset<BITS>(subset | added_bit) << ") = " << new_value << "\n";
        }
        results[subset | added_bit] = new_value;
      }
      if (new_value >= 0) {
        any = true;
      }
      left ^= added_bit;
    }
    if (pos == 0 || !any) {
      if (pos > 0 && results[subset] == weight[pos]) {
        
      } else {
        current_q->PB(subset);
      }
//      cout << "q.add1(" << bitset<BITS>(subset) << ")\n";
    }
  }
}

int solve() {

  REP(pos, min(m, n)) {
//    printf("==========pos=%d===========\n", pos);

    REP(i, 1 << n) {
      results[i] = -1;
    }
//    memset(results, -1, sizeof(results[0]) * (1 << n));
//    cout << results[0] << "\n";
    results[0] = capacity[pos];

    if (pos == 0) {
      REP(k, n + 1) {
//        printf("k=%d\n", k);
        int subset = (1 << k) - 1;
        while (subset < (1 << n)) {
          solve_subset(pos, subset);
          if (subset == 0) {
            break;
          }
          int a = subset&-subset, b = subset + a;
          subset = (subset^b) / 4 / a | b;
        }
      }
      swap(old_q, current_q);
      current_q->clear();
    } else {
      FORE (it, (*old_q)) {
        int subset = *it;
        solve_subset(pos, subset);
      }
//      old_q = current_q;
      swap(old_q, current_q);
      current_q->clear();
    }
    if (results[(1 << n) - 1] >= 0) {
      return pos + 1;
    }
    REP (i, 1 << n) {
      was[i] = (results[i] >= 0);
    }
  }
  return -1;
}

void inline jeden() {
  scanf("%d%d", &n, &m);

  REP(i, n) {
    scanf("%d", weight + i);
  }
  sort(weight, weight + n);
  reverse(weight, weight + n);

  REP(j, m) {
    scanf("%d", capacity + j);
  }
  capacity[m++] = 0;
  sort(capacity, capacity + m);
  reverse(capacity, capacity + m);
  int result = solve();
  
  if (result == -1) {
    puts("NIE");
  } else {
    printf("%d\n", result);
  }
}

int main() {
  old_q->reserve(1 << 24);
  current_q->reserve(1 << 24);
  
  jeden();
}