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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Krzysztof Małysa
#include <bits/stdc++.h>
using namespace std;

#define FOR(i,a,n) for (int i = (a), i##__ = (n); i <= i##__; ++i)
#define REP(i,n) FOR(i,0,n-1)
#define FORD(i,a,n) for (int i = (a), i##__ = (n); i >= i##__; --i)
#define ALL(x) x.begin(), x.end()
#define EB emplace_back
#define ST first
#define ND second
#define OO(A) template<class... T> ostream& operator<<(ostream& os, const A<T...>& x) { return __o(os, ALL(x)); }
#define SZ(x) ((int)x.size())

typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<PII> VPII;

template<class A, class B> ostream& operator<<(ostream&, const pair<A, B>&);
template<class I> ostream& __o(ostream&, I, I);
template<class T, size_t N> ostream& operator<<(ostream& os, const array<T, N>& x) { return __o(os, ALL(x)); }
OO(vector) OO(deque) OO(set) OO(multiset) OO(map) OO(multimap)
template<class A, class B> ostream& operator<<(ostream& os, const pair<A, B>& p) {
	return os << "(" << p.ST << ", " << p.ND << ")";
}
template<class I> ostream& __o(ostream& os, I a, I b) {
	os << "{";
	for (; a != b;)
		os << *a++, cerr << (a == b ? "" : " ");
	return os << "}";
}
template<class I> ostream& __d(ostream& os, I a, I b) {
	os << "{\n";
	for (I c = a; a != b; ++a)
		os << "  " << distance(c, a) << ": " << *a << endl;
	return os << "}";
}
template<class... T> void __e(T&&... a) {
	int t[] = {(cerr << forward<T>(a), 0)...}; (void)t;
	cerr << endl;
}

template<class A, class B> void mini(A& a, B&& b) { if (b < a) a = b; }
template<class A, class B> void maxi(A& a, B&& b) { if (b > a) a = b; }
int ceil2(int x) { return 1 << (sizeof(x) * 8 - __builtin_clz(x - 1)); }

#ifdef DEBUG
# define D(...) __VA_ARGS__
#else
# define D(...)
#endif

#define LOG(x) D(cerr << #x ": " << x)
#define LOGN(x) D(LOG(x) << endl)
#define DUMP(x) D(cerr << #x ": ", __d(cerr, ALL(x)) << endl)
#define E(...) D(__e(__VA_ARGS__))
#define endl '\n'
constexpr char nl = '\n';
// End of templates

struct Task {
	int a, b; // seconds needed, seconds left

	Task(int x, int y) : a(x), b(y) {}
};

ostream& operator <<(ostream& os, const Task& x) {
	return os << "(" << x.a << " / " << x.b << ")";
}

auto cmp = [](const Task& a, const Task& b) {
	return a.b < b.b;
};

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int n, m;
	cin >> n >> m;
	multiset<Task, decltype(cmp)> tasks(cmp); // (seconds needed, seconds left)
	VI obligatory_tasks; // val - how many seconds a task needs
	vector<pair<int, Task>> events; // (beginning, (seconds needed, seconds left))
	REP (i, n) {
		int a, b, c;
		cin >> a >> b >> c;
		events.EB(a, Task{c, b - a});
	}

	sort(ALL(events), [](const pair<int, Task>& a, const pair<int, Task>& b) { return a.ST < b.ST; });

	int t = 0, j = 0; // t - # of seconds passed, j - pointer to next event
	while (tasks.size() || obligatory_tasks.size() || j < SZ(events)) {
		// Add new tasks
		while (j < SZ(events) && events[j].ST == t) {
			if (events[j].ND.a == events[j].ND.b)
				obligatory_tasks.EB(events[j].ND.a);
			else
				tasks.insert(events[j].ND);
			++j;
		}

		int k = m - SZ(obligatory_tasks);
		if (k < 0)
			return puts("NIE"), 0;

		// Do obligatory tasks
		FORD (i, SZ(obligatory_tasks) - 1, 0)
			if (--obligatory_tasks[i] == 0) {
				obligatory_tasks[i] = obligatory_tasks.back();
				obligatory_tasks.pop_back();
			}

		// Do other tasks
		vector<decltype(tasks.begin())> to_remove;
		for (auto it = tasks.begin(); it != tasks.end() && k--; ++it)
			if (--const_cast<int&>(it->a) == 0) // Task is done
				to_remove.EB(it);
		for (auto&& it : to_remove)
			tasks.erase(it);
		to_remove.clear();

		// Update time left for each task
		for (auto it = tasks.begin(); it != tasks.end(); ++it) {
			if (--const_cast<int&>(it->b) == 0)
				return puts("NIE"), 0; // Task won't be finished, because it->a > 0
			else if (it->a == it->b) {
				to_remove.EB(it);
				obligatory_tasks.EB(it->a);

			}
		}

		for (auto&& it : to_remove)
			tasks.erase(it);

		++t;
		LOGN(t);
		LOGN(tasks);
		LOGN(obligatory_tasks);
	}

	puts("TAK");
	return 0;
}

/*
3 1
0 5 4
1 2 1
0 30 25
TAK

4 3
0 4 3
0 4 3
0 4 3
0 4 3
TAK
*/