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

using namespace std;

struct Task
{
	int p, k, c;
};

int main()
{
	int n, m;
	cin >> n >> m;
	
	vector<Task> tasks(n);

	for (int i = 0; i < n; i++)
		cin >> tasks[i].p >> tasks[i].k >> tasks[i].c;
	
	sort(tasks.begin(), tasks.end(), [](Task const & a, Task const & b) { return a.p < b.p; });

	multimap<int, int> active; // limit -> work

	for (int i = 0; i < n; i++)
	{
		active.insert(make_pair(tasks[i].k, tasks[i].c));
		int time = tasks[i].p;
		int maxTime = (i+1 < n ? tasks[i+1].p : numeric_limits<int>::max()); 
		
		while (time < maxTime && !active.empty())
		{
			if (time + active.begin()->second > active.begin()->first)
			{
				cout << "NIE\n";
				return 0;
			}

			int minWork = numeric_limits<int>::max();
	
			auto it = active.begin();
			int j = 0;
			while (j < m && it != active.end())
			{
				minWork = min(minWork, it->second);
				++it;
				j++;
			}
		
			minWork = min(minWork, maxTime - time);
			
			it = active.begin();
			j = 0;
			while (j < m && it != active.end())
			{
				auto it2 = it;
				it2++;
				it->second -= minWork;
				if (it->second == 0)
					active.erase(it);
				it = it2;
				j++;
			}

			time += minWork;
		}
	}
	
	cout << "TAK\n";

	return 0;
}