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
//BRUTE
#include <cstdio>
#include <algorithm>
#include <functional>

using std::sort;
using std::greater;

int objects[24];
int packs[100];
int objectsSize;
int packsSize;
int realPacks[30] = { 0 };

bool pack(int object, int currentPacksSize)
{
  if (object == objectsSize)
    return true;

  int weight = objects[object];
  for (int p = 0; p < currentPacksSize; p++)
  {
    if (realPacks[p] + weight <= packs[p])
    {
      realPacks[p] += weight;
      bool success = pack(object + 1, currentPacksSize);
      if (success)
        return true;
      realPacks[p] -= weight;
    }
  }
  return false;
}

int main()
{
  scanf("%d%d", &objectsSize, &packsSize);
  for (int i = 0; i < objectsSize; i++)
    scanf("%d", objects + i);
  for (int i = 0; i < packsSize; i++)
    scanf("%d", packs + i);

  sort(objects, objects + objectsSize, greater<int>());
  sort(packs, packs + packsSize, greater<int>());

  int objectsSum = 0;
  for (int i = 0; i < objectsSize; i++)
    objectsSum += objects[i];

  int packsSum = 0;
  for (int p = 0; p < packsSize && p < objectsSize; p++)
  {
    packsSum += packs[p];
    if (packsSum < objectsSum)
      continue;

    bool success = pack(0, p + 1);
    if (success)
    {
      printf("%d\n", p + 1);
      return 0;
    }
  }

  printf("NIE\n");

  return 0;
}