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
#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>
using namespace std;

unsigned count_bits(unsigned x) {
  x = x - ((x >> 1) & 0x55555555);
  x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
  x = x + (x >> 8);
  x = x + (x >> 16);
  return x & 0x0000003F;
}

vector<unsigned> cost;
vector<unsigned> sum;
vector<unsigned> setcost;
vector<unsigned> bucket;

void maximals(unsigned used, unsigned state, unsigned price, unsigned pos, unsigned min, unsigned max, vector<pair<unsigned, unsigned>>& res) {
  if(pos >= cost.size()) {
    if (min==0 and state>0)
      res.push_back(make_pair(price, state));
    return;
  }
  if(min > (sum.back() - ((pos>0)?sum[pos-1]:0)))
    return;
  if((used & (1<<pos)) != 0)
    return maximals(used, state, price, pos+1, min, max, res);
  if(cost[pos] > max)
    return maximals(used, state, price, pos+1, min, max, res);
  maximals(used, state, price, pos+1, std::max(min, max-cost[pos]), max, res);
  maximals(used, state + (1<<pos), price+cost[pos], pos+1, (min>cost[pos])?min-cost[pos]:0, max-cost[pos], res);
}

bool better(const vector<unsigned>& cost, unsigned a, unsigned b) {
  if (count_bits(a)==1 and count_bits(b)==1 and setcost[a] >= setcost[b])
    return true;
  /*
  if (count_bits(a) <= count_bits(b)) {
    vector<unsigned> A,B;
    for(size_t i=0;i<cost.size();i++) {
      if((1u<<i) & a)
        A.push_back(cost[i]);
      if((1u<<i) & b)
        B.push_back(cost[i]);

    }
  }
  */
  return false;
}

void moves(unsigned buc, unsigned used, unsigned min, unsigned max, vector<unsigned>& res) {
  vector<pair<unsigned, unsigned>> r;
  maximals(used, 0, 0, 0, min, std::min(max, bucket[buc]), r);
  //cerr << "MOVES " << buc << " " << used << " " << min << " " << max << ":"; for (auto a : r) cerr << " " << a.second; cerr << endl;
  sort(r.begin(), r.end());
  res.resize(0);
  for(size_t i=0;i<r.size();i++) {
    bool ok=true;
    for(size_t j=0;j<res.size();j++)
      if(better(cost, res[j], r[i].second)) {
        ok=false;
        break;
      }
    if(ok)
      res.push_back(r[i].second);
  }
}

size_t solve(unsigned buc, unsigned used, unsigned max) {
  size_t res = 0;
  unsigned tgt = (1u<<cost.size())-1;
  if (used == tgt)
    return buc;
  if (buc >= bucket.size())
    return 0;
  vector<unsigned> m;
  moves(buc, used, 0, max, m);
  for(size_t i=0;i<cost.size();i++)
    if(((1u<<i) & used) == 0) {
      if(cost[i]>bucket[buc])
        return 0;
      break;
    }
  unsigned space=0;
  for(size_t i=buc;i<bucket.size();i++)
    space += bucket[i];
  unsigned rest=0;
  for(size_t i=0;i<cost.size();i++)
    if(((1u<<i) & used) == 0)
      rest += cost[i];
  if (rest > space)
    return 0;


  for(auto move : m) {
    //cerr << buc << " " << used << " " << move << endl;
    size_t r = solve(buc+1, used | move, setcost[move]);
    if (r>0) {
      if (res==0)
        res = r;
      else
        res = min(res, r);
    }
  }
  return res;
}
size_t solve() {
  sort(cost.rbegin(), cost.rend());
  sort(bucket.rbegin(), bucket.rend());
  bucket.resize(min(bucket.size(), cost.size()));

  sum.resize(cost.size()); sum[0]=cost[0]; for(size_t i=1;i<sum.size();i++) sum[i]=sum[i-1]+cost[i];
  setcost.resize(1u<<cost.size());
  for(size_t i=0;i<cost.size();i++)
    for(size_t j=0;j<(1u<<i);j++)
      setcost[(1u<<i)+j] = cost[i]+setcost[j];

  return solve(0, 0, bucket[0]);
}

void onecase() {
  size_t N, P;
  cin >> N >> P;
  cost.resize(N);
  bucket.resize(P);
  for(size_t i=0;i<N;i++)
    cin >> cost[i];
  for(size_t i=0;i<P;i++)
    cin >> bucket[i];
  size_t s = solve();
  if(s)
    cout << s << endl;
  else
    cout << "NIE" << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  onecase();
/*
  size_t Z;
  cin >> Z;
  while(Z--)
    onecase();
*/
  return 0;
}