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

using namespace std;

bool srt(unsigned short i, unsigned short j) { return i > j; }

int main(void)
{
	cin.sync_with_stdio(false);
	unsigned short n, m, j, x;
	cin >> n; cin >> m;
	vector<unsigned int> ns, ms;
	ns.reserve(n); ms.reserve(m);
	unsigned int t;
	for (unsigned short i = 0; i < n; ++i)
	{
		cin >> t;
		ns.push_back(t);
	}
	for (unsigned short i = 0; i < m; ++i)
	{
		cin >> t;
		ms.push_back(t);
	}
	sort(ns.begin(), ns.end(), srt);
	sort(ms.begin(), ms.end(), srt);
	x = 0;
	for (unsigned short i = 0; i < n; ++i)
	{
		cout << ns[i] << endl;
		for (j = 0; j <= m; ++j)
		{
			if (j < m && ms[j] > ns[i])
			{
				ms[j] -= ns[i];
				break;
			}
		}
		if (j == m)
		{
			cout << "NIE" << endl;
			return 0;
		}
		if (j > x) x = j;
	}
	cout << x << endl;
}