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
#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<numeric>
#include<string>

typedef unsigned long long ull;

int main(int argc, char** argv)
{
  std::ios_base::sync_with_stdio(false);
  std::vector<ull> weights, capacities;
  int itemsCount, backpacksCount;
  int res(-1);
  std::cin>>itemsCount>>backpacksCount;
  for(int i = 0; i < itemsCount; ++i)
  {
    ull w;
    std::cin>>w;
    weights.push_back(w);
  }
  for(int i = 0; i < backpacksCount; ++i)
  {
    ull c;
    std::cin>>c;
    capacities.push_back(c);
  }
  std::sort(weights.begin(), weights.end());
  if(std::accumulate(weights.begin(), weights.end(), 0UL) > std::accumulate(capacities.begin(), capacities.end(), 0UL))
  {
    std::cout<<"NIE";
    return 0;
  }
  std::vector<ull> solution;
  do
  {
    solution = capacities;
    bool solutionCorrect = true;
    int tmpRes = 0;
    std::vector<bool> backpacksUsed(backpacksCount, false);
    for(int i = 0; i < itemsCount && solutionCorrect; ++i)
    {
      bool usedThisItem = false;
      for(int j = 0; j < backpacksCount; ++j)
      {
         if(solution[j] >= weights[i])
         {
           usedThisItem = true;
           solution[j] -= weights[i];
           if(!backpacksUsed[j])
           {
             backpacksUsed[j] = true;
             tmpRes++;
             if(res != -1 && tmpRes >= res)
             {
               solutionCorrect = false;
             }
           }
           break;
         }
      }
      if(!usedThisItem)
      {
        solutionCorrect = false;
      }
    }
    if(solutionCorrect)
    {
      res = res == -1?tmpRes:std::min(res, tmpRes);
    }
    if(res == 1)
    {
      break;
    }
  }while(std::next_permutation(weights.begin(), weights.end()));
  
  if(res == -1)
    std::cout<<"NIE";
  else
    std::cout<<res;
  
  
  return 0;
}