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
#include <cstdlib>
#include <cstdio>
#include <algorithm>
using namespace std;

struct Task{
	int p, k, c;
	bool works;
	int freeTimeLeft;
	
	Task(){}
	Task(int pp, int kk, int cc):p(pp),k(kk),c(cc),works(false){}
};

Task tasks[101];
Task* (sortedTasks[101]);
int nTask, nProc;


void updateTasks(){
	for(int i=0; i<nTask; i++)
		if(tasks[i].works){
			tasks[i].c--;
			
			if(tasks[i].c <= 0)
				tasks[i].works = false;
		}
}

bool sortFunc(Task* t1, Task* t2){
	return t1->freeTimeLeft < t2->freeTimeLeft;
}

void onRangeStart(int t){
	int toSort = 0;
	
	for(int i=0; i<nTask; i++){
		tasks[i].works = false;
		
		if(t >= tasks[i].p && t < tasks[i].k && tasks[i].c > 0){
			tasks[i].freeTimeLeft = tasks[i].k - t - tasks[i].c;
			sortedTasks[toSort++] = &tasks[i];
		}
	}
	
	if(toSort > 1 && nProc < toSort)
		sort(sortedTasks, sortedTasks + toSort, sortFunc);
	
	for(int p=0; p<nProc && p<toSort; p++)
		sortedTasks[p]->works = true;
}

bool onRangeEnd(int t){
	for(int i=0; i<nTask; i++){
		if(tasks[i].k == t)
			if(tasks[i].c > 0)
				return false;
	}
	
	return true;
}

int main(){
	scanf("%d %d", &nTask, &nProc);
	
	int timeMax = 1;
	
	for(int i=0; i<nTask; i++){
		int p, k, c;
		scanf("%d %d %d", &p, &k, &c);
		tasks[i] = Task(p, k, c);
		timeMax = max(timeMax, k);
	}
	
	for(int t=0; t<=timeMax; t++){
		updateTasks();
		
		if(!onRangeEnd(t)){
			printf("NIE\n");				
			return 0;
		}

		onRangeStart(t);
	}
	
	printf("TAK\n");
	return 0;
}