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
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <deque>

#define getchar_custom getc_unlocked

char c;

template <typename T>
inline T read_custom() {

	c = getchar_custom(stdin);

	while (c<'0' || c>'9')
	{
		c = getchar_custom(stdin);
	}

	T returnValue = 0;
	while (c >= '0' && c <= '9') {
		returnValue = (returnValue << 3) + (returnValue << 1) + c - 48;
		c = getchar_custom(stdin);
	}

	return returnValue;
}

struct bin {
	bool opened;
	long long size;
	long long filled;
};

struct monsterCmp {
	bool operator()(bin const *a, bin const *b) {
		return a->size > b->size;
	};
};

int main() {

	int thingsNo = read_custom<int>();
	int binsNo = read_custom<int>();

	std::vector<long long> things;
	std::vector<bin*> bins;

	for (int i = 0; i < thingsNo; i++) {
		things.push_back(read_custom<long long>());
	}

	for (int i = 0; i < binsNo; i++) {
		bin *b = new bin;
		b->size = read_custom<long long>();
		b->opened = false;
		b->filled = 0;

		bins.push_back(b);
	}

	std::make_heap(bins.begin(), bins.end(), monsterCmp());
	std::sort_heap(bins.begin(), bins.end(), monsterCmp());

	int p = bins.size() - 1;
	while (p >= 0 && bins[p]->size < things[0]);
	{
		p--;
	}

	if (p == -1) {
		std::cout << "NIE" << std::endl;
		return 0;
	}

	bins[p]->opened = true;
	bins[p]->filled += things[0];

	for (int i = 1; i < thingsNo; i++) {

		bool fit = false;

		for (int j = 0; j < binsNo; j++) {

			if (bins[j]->opened == false) {
				continue;
			}
			long long diff = bins[j]->size - bins[j]->filled;
			if (diff >= things[i]) {
				fit = true;
				bins[j]->filled += things[i];
				break;
			}
		}

		if (!fit) {
			int a = bins.size() - 1;
			while (a >= 0)
			{
				if (bins[a]->opened) 
				{
					a--;
					continue;
				}
				if (bins[p]->size >= things[i])
				{
					break;
				}
				a--;
			}

			if (a == -1) {
				std::cout << "NIE" << std::endl;
				return 0;
			}

			bins[a]->opened = true;
			bins[a]->filled += things[i];
		}
	}

	int sum = 0;
	for (int i = 0; i < binsNo; i++) {
		if (bins[i]->opened) {
			sum += 1;
		}
	}

	std::cout << sum << std::endl;
}