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
#include <algorithm>
#include <bitset>
#include <iostream>

using namespace std;

unsigned rseed = 380265931;

unsigned myr() {
  rseed = (16807 * rseed + 1) % 2147483647;
  return rseed;
}

int n;
int m;
vector<int> ga;
vector<int> gc;

int l1;
int m1;

bool calc_l1() {
  int sum = 0;
  for (int i = 0; i < n; i++)
    sum += ga[i];
  int sumbins = 0;
  for (int i = 0; i < m; i++) {
    sumbins += gc[i];
    if (sumbins >= sum) {
      l1 = i + 1;
      return true;
    }
  }
  return false;
}


void slurp() {
  ios::sync_with_stdio(false);
  cin >> n >> m;
  ga.resize(n);
  gc.resize(m);
  for (int i = 0; i < n; i++) {
    cin >> ga[i];
    rseed ^= ga[i];
  }
  sort(ga.begin(), ga.end(), greater<int>());
  for (int i = 0; i < m; i++) {
    cin >> gc[i];
    rseed ^= gc[i];
  }
  sort(gc.begin(), gc.end(), greater<int>());
  m = min(m, n);
  gc.resize(m);
}


void go(int k, vector<int>& c, int nbins, const bitset<32>& bins) {

  if (m1 == l1) {
    return;
  }
  if (nbins >= m1) {
    return;
  }
  if (k == n) {
    m1 = min(m1, nbins);
    return;
  }

  int ak = ga[k];
  for (int i = 0; i < m; i++) {
    if (c[i] >= ak) {
      c[i] -= ak;
      bitset<32> binsn = bins;
      int nbinsn = nbins;
      if (!bins[i]) {
        binsn[i] = true;
        nbinsn++;
      }
      if (nbinsn < m1)
        go(k+1, c, nbinsn, binsn);
      c[i] += ak;
    }
  }
}

int randomestim(vector<int> a, vector<int> c, int pbest, int pfirst, int pworst) {
  int ptot = pbest + pfirst + pworst;
  bitset<32> bins;
  reverse(a.begin(), a.end());
  while (a.size() > 0) {
    int h = myr() % ptot;
    int a0 = a[a.size()-1];
    a.pop_back();
    int besti = -1;
    int bestr = 1000000007;
    if (h >= pbest + pfirst)
      bestr *= -1;
    for (int i = 0; i < c.size(); i++) {
      int r = c[i] - a0;
      if (r < 0)
        continue;
      if (h < pbest && (r < bestr)) {
        bestr = r;
        besti = i;
      } else if (h < pbest+pfirst && true) {
        besti = i;
        break;
      } else if (true && (r > bestr)) {
        bestr = r;
        besti = i;
      }
    }
    if (besti < 0)
      return 1000000007;
    bins[besti] = true;
    c[besti] -= a0;
  }
  return bins.count();
}

int main() {
  slurp();
  if (!calc_l1())
    goto no;
  m1 = m+1;
  m1 = min(m1, randomestim(ga, gc, 1, 0, 0));
  m1 = min(m1, randomestim(ga, gc, 0, 1, 0));
  m1 = min(m1, randomestim(ga, gc, 0, 0, 1));
  for (int i = 0; i < 1000; i++) {
    for (int j = 0; j < 100; j++)
      m1 = min(m1, randomestim(ga, gc, 1, 1, 0));
    for (int j = 2; j <= 25; j++) {
      m1 = min(m1, randomestim(ga, gc, j, 1, 0));
      m1 = min(m1, randomestim(ga, gc, 1, j, 0));
    }
    for (int bf=1; bf <= 5; bf++)
      for (int ff=1; ff <= 5; ff++)
        for (int wf=1; wf <= 5; wf++)
          m1 = min(m1, randomestim(ga, gc, bf, ff, wf));
  }
  if (l1 == m1) {
    cout << m1 << endl;
    goto out;
  }
  go(0, gc, 0, bitset<32>());
  if (m1 > m)
    goto no;
  cout << m1 << endl;
  goto out;
 no:
  cout << "NIE" << endl;
 out:
  return 0;
}