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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class sze {

	public static class Task {
		
		private int p;
		private int k;
		private int c;
		private int id;
		private int priority;
		private int startTime;
		
		public Task(int p, int k, int c, int id) {
			this.p = p;
			this.k = k;
			this.c = c;
			this.id = id;
			this.priority = k - c;
			this.startTime = -1;
		}

		public int getP() {
			return p;
		}
		
		public int getK() {
			return k;
		}
		
		public int getC() {
			return c;
		}
		
		public int getId() {
			return id;
		}
		
		public int getPriority() {
			return priority;
		}
		
		public void start(int currentTime){
			this.startTime = currentTime;
		}
		
		public void stop(){
			this.startTime = -1;
		}
		
		private void update(int currentTime){
			if(startTime < 0){
				this.priority = k - (currentTime + c);
			}else{
				this.c -= currentTime - startTime;
				//this.priority = k - (currentTime + c);
				this.startTime = currentTime;
			}
		}
		
		@Override
		public boolean equals(Object obj){
			return this.id == ((Task)obj).id ? true : false;
		}
		
		@Override
		public String toString() {
			return "[" + p + ", " + k + ", " + c + ", " + priority + ", " + startTime + ", " + id +"]";
		}
		
	}
	
	public static class TaskLaunchComparator implements Comparator<Task> {
		public int compare(Task t0, Task t1) {
			return Integer.compare(t0.getP(), t1.getP());
		}
	}
	
	public static class TaskPriorityComparator implements Comparator<Task> {
		public int compare(Task t0, Task t1) {
			return Integer.compare(t0.getPriority(), t1.getPriority());
		}
	}
	
	public static class TaskPreemptionComparator implements Comparator<Task> {
		public int compare(Task t0, Task t1) {
			return -Integer.compare(t0.getPriority(), t1.getPriority());
		}
	}
	
	public static final int MAX_TIMESPAN = 10000001;
	
	public static void main(String[] args) {
		try {
			BufferedReader buffReader = new BufferedReader(new InputStreamReader(System.in));
			StringTokenizer tokenzier = new StringTokenizer(buffReader.readLine());
			
			int n = Integer.parseInt(tokenzier.nextToken());
			int m = Integer.parseInt(tokenzier.nextToken());
			/*
			Task taskTab[] = new Task[n];
			PriorityQueue<Task> tasksLaunchQueue = new PriorityQueue<Task>(n, new TaskLaunchComparator());
			for(int i = 0; i < n; i++){
				tokenzier = new StringTokenizer(buffReader.readLine());
				Task task = new Task(Integer.parseInt(tokenzier.nextToken()), Integer.parseInt(tokenzier.nextToken()), Integer.parseInt(tokenzier.nextToken()), i);
				taskTab[i] = task;
				tasksLaunchQueue.add(task);
			}
			*/
			PriorityQueue<Task> tasksLaunchQueue = new PriorityQueue<Task>(n, new TaskLaunchComparator());
			for(int i = 0; i < n; i++){
				tokenzier = new StringTokenizer(buffReader.readLine());
				Task task = new Task(Integer.parseInt(tokenzier.nextToken()), Integer.parseInt(tokenzier.nextToken()), Integer.parseInt(tokenzier.nextToken()), i);
				tasksLaunchQueue.add(task);
			}
			
			PriorityQueue<Task> activeTasksQueue = new PriorityQueue<Task>(n, new TaskPriorityComparator());
			PriorityQueue<Task> runningTasksQueue = new PriorityQueue<Task>(n, new TaskPreemptionComparator());
			LinkedList<Task> completedTasks = new LinkedList<Task>();
			
			boolean result = true;
			
			int currentTime = tasksLaunchQueue.peek().getP();

			while(currentTime < MAX_TIMESPAN){
				/*
				System.out.println(currentTime);
				*/
				updateQueues(currentTime, tasksLaunchQueue, activeTasksQueue, runningTasksQueue, completedTasks);
			
				while(!tasksLaunchQueue.isEmpty() && tasksLaunchQueue.peek().getP() == currentTime)
					activeTasksQueue.add(tasksLaunchQueue.poll());
				while(!activeTasksQueue.isEmpty() && runningTasksQueue.size() < m){
					activeTasksQueue.peek().start(currentTime);
					runningTasksQueue.add(activeTasksQueue.poll());
				}
				while(!activeTasksQueue.isEmpty() && activeTasksQueue.peek().getPriority() < runningTasksQueue.peek().getPriority()){
					Task active = activeTasksQueue.poll();
					Task running = runningTasksQueue.poll();
					running.stop();
					activeTasksQueue.add(running);
					active.start(currentTime);
					runningTasksQueue.add(active);
				}
				/*
				System.out.println(tasksLaunchQueue);
				System.out.println(activeTasksQueue);
				System.out.println(runningTasksQueue);
				System.out.println(completedTasks);
				*/
				if(activeTasksQueue.isEmpty()){
					if(tasksLaunchQueue.isEmpty()) 
						break;
				}else if(activeTasksQueue.peek().getPriority() <= 0){
					result = false;
					break;
				}
				
				int nextTimeStep = MAX_TIMESPAN;
				if(!tasksLaunchQueue.isEmpty())
					nextTimeStep = Math.min(tasksLaunchQueue.peek().getP(), nextTimeStep);
				if(!activeTasksQueue.isEmpty() && !runningTasksQueue.isEmpty())
					nextTimeStep = Math.min(currentTime + activeTasksQueue.peek().getPriority() - runningTasksQueue.peek().getPriority() + 1, nextTimeStep);
				int firstCompletion = MAX_TIMESPAN;
				for(Task task : runningTasksQueue)
					firstCompletion = Math.min(task.getC(), firstCompletion);
				nextTimeStep = Math.min(currentTime + firstCompletion, nextTimeStep);
				currentTime = nextTimeStep;
			}
			
			System.out.println(result ? "TAK" : "NIE");
			
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private static void updateQueues(int currentTime, PriorityQueue<Task> tasksLaunchQueue,
			PriorityQueue<Task> activeTasksQueue, PriorityQueue<Task> runningTasksQueue, LinkedList<Task> completedTasks) {
		for(Task task : tasksLaunchQueue)
			task.update(currentTime);
		for(Task task : activeTasksQueue)
			task.update(currentTime);
		LinkedList<Task> newCompletedTasks = new LinkedList<Task>();
		for(Task task : runningTasksQueue){
			task.update(currentTime);
			if(task.getC() <= 0)
				newCompletedTasks.add(task);
		}
		if(!newCompletedTasks.isEmpty()){
			completedTasks.addAll(newCompletedTasks);
			Task[] runningTasks = runningTasksQueue.toArray(new Task[0]);
			runningTasksQueue.clear();
			for(Task task : runningTasks){
				Iterator<Task> completedTasksIterator = newCompletedTasks.iterator();
				boolean contains = false;
				while(completedTasksIterator.hasNext()){
					Task completedTask = completedTasksIterator.next();
					if(task.equals(completedTask)){
						completedTasksIterator.remove();
						contains = true;
						break;
					}
				}
				if(!contains)
					runningTasksQueue.add(task);
			}
		}
	}
	/*
	private static int tasksStartTime(Task[] taskTab){
		int startTime = Integer.MAX_VALUE;
		for(int i = 0; i < taskTab.length; i++)
			startTime = Math.min(taskTab[i].getP(), startTime);
		return startTime;
	}
	*/
}