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

using namespace std;

typedef vector<size_t> MS;

bool fit(size_t capacity, MS &items)
{
  MS result;
  result.reserve(items.size());

  bool used = false;
  for(size_t i = 0; i < items.size(); ++i)
  {
    if(capacity >= items[i])
    {
      capacity -= items[i];
      used = true;
    }
    else
      result.push_back(items[i]);
  }

  items.swap(result);
  return used;
}

int main()
{
  cin.sync_with_stdio(false);
  cout.sync_with_stdio(false);


  MS backpacks;
  MS items;

  size_t m, n;
  cin >> n >> m;

  size_t temp;
  items.reserve(n);
  while(n --> 0)
  {
    cin >> temp;
    items.push_back(temp);
  }
  backpacks.reserve(m);
  while(m --> 0)
  {
    cin >> temp;
    backpacks.push_back(temp);
  }

  sort(items.begin(), items.end(), greater<size_t>());
  sort(backpacks.begin(), backpacks.end(), greater<size_t>());

  size_t counter(0);
  for(MS::iterator bp = backpacks.begin(); bp != backpacks.end(); ++bp)
    counter += fit(*bp, items);

  if(items.empty())
    cout << counter << endl;
  else
    cout << "NIE" << endl;

  return 0;
}