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
133
134
135
136
/*	Arkadiusz Wróbel
 *
 *	Konkurs: Potyczki Algorytmiczne 2016, runda 5B
 *	Zadanie: Szeregowanie zadań
 */
#include <cstdio>
#include <iostream>

#include <algorithm>
#include <cmath>
#include <cstring>

#include <vector>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <deque>

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;

typedef vector<int> VI;
typedef vector<LL> VLL;
typedef vector<double> VD;
typedef vector<string> VS;
typedef vector<PII> VPII;
typedef vector<VI> VVI;

#define REP(I, N) for(int I = 0; I < (N); ++I)
#define FOR(I, M, N) for(int I = (M); I <= (N); ++I)
#define FORD(I, M, N) for(int I = (M); I >= (N); --I)
//#define FOREACH(IT, CON) for(__typeof((CON).begin()) IT = (CON).begin(); IT != (CON).end(); ++IT)

#define ST first
#define ND second
#define MP make_pair
#define PB push_back
#define SIZE(CON) ((int)(CON).size())
#define ALL(CON) (CON).begin(), (CON).end()

const int INF = 1000000000;
const LL INFLL = 1000000000000000000LL;

//######################################################################


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

int n, m;

Task tasks[200];

vector<Task*> poczatki[2000007];
vector<Task*> konce[2000007];

vector<Task*> running;

struct Setcmp {
	inline bool operator() (const Task* a, const Task* b) {
		return (a->start == b->start) ? (a < b) : (a->start < b->start);
	}
};
set<Task*, Setcmp> starty;

inline bool make() {
	// REP(i, 9) {
	// 	printf("%d:\n", i);
	// 	for (Task* task : poczatki[i]) {printf(" (%d %d (%d))", task->p, task->k, task->start);} printf("\n");
	// 	for (Task* task : konce[i]) {printf(" (%d %d (%d))", task->p, task->k, task->start);} printf("\n");
	// }
	
	FOR(czas, 0, 1000000) {
		for (Task* task : poczatki[czas]) {
			starty.insert(task);
		}
		
		// if (czas < 20) {
		// 	printf("(%d)\n", czas);
		// 	for (Task* task : starty) {printf(" (%d %d (%d))", task->p, task->k, task->start);} printf("\n");
		// }
		
		for (Task* task : konce[czas]) {
			if (task->start < czas) {
				return false;
			}
		}
		
		REP(rep, m) {
			if (starty.empty()) {
				break;
			}
			Task* task = *starty.begin();
			starty.erase(starty.begin());
			if (task->start < czas) {
				// już i tak nie zdąży
				return false;
			}
			task->start++;
			if (task->start < task->k) {
				running.PB(task);
			}
		}
		
		for (Task* task : running) {
			starty.insert(task);
		}
		running.clear();
	}
	
	return true;
}

int main() {
	
	scanf("%d%d", &n, &m);
	REP(i, n) {
		int p, k, c;
		scanf("%d%d%d", &p, &k, &c);
		tasks[i].p = p;
		tasks[i].k = k;
		tasks[i].start = k - c;
		poczatki[p].PB(tasks+i);
		konce[k].PB(tasks+i);
	}
	
	printf(make() ? "TAK\n" : "NIE\n");
	
	return 0;
}