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

#define DEBUG_MODE 0 
#if DEBUG_MODE
	#include <cstdio>
	#define DEBUG(x...) printf(x);
#else
	#define DEBUG(x...)
#endif

using namespace std;

const int N = 24;
const int M = 100;

struct plecak
{
	int a;
	int f;
	bool operator<(const plecak &p) const
	{
		return a > p.a; // odwrotny porzadek
	}
};

template <class ForwardIterator, class T>
ForwardIterator lessThanOrEqual(ForwardIterator first, ForwardIterator last, const T &val)
{
	ForwardIterator it = upper_bound(first,last,val);

	if (it == first)
		return it;

	return it - 1;
}

int main(int argc, char ** argv)
{
	cin.sync_with_stdio(false);
	int n, m;
	cin >> n >> m;
	vector<int> T(n);
	vector<plecak> R(m);
	for (int i = 0; i < n; cin >> T[i++]);
	for (int i = 0; i < m; cin >> R[i].a, R[i].f = R[i++].a);
	sort(T.begin(), T.end());
	sort(R.begin(), R.end());
	
	if ((*R.begin()).a < T.back())
	{
		cout << "NIE\n";
		return 0;
	}
	vector<int>::iterator itT = T.begin();
	vector<plecak>::iterator itR = R.begin();
	while (!T.empty())
	{
		if ((*itR).f == 0 || (*itR).f < *(T.begin()))
		{
			++itR;
			if (itR == R.end())
			{
				cout << "NIE\n";
				return 0;
			}
		}
		itT = lessThanOrEqual(T.begin(),T.end(),(*itR).f);
		if ((*itR).f >= (*itT))
		{
			(*itR).f -= *itT;
			T.erase(itT);
		} else
		{
			cout << "NIE\n";
			return 0;
		}
	}
	cout << itR - R.begin() + 1 << "\n";
	return 0;
}