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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Comparator;
import java.util.PriorityQueue;

public class sze {

	static PriorityQueue<X> queue;
	static int[] slots = new int[1000*1000 + 1];
	
	static class X implements Comparable<X> {
		int l, p, pmax;
		public X(int l, int p, int pmax) { this.l = l;  this.p = p; this.pmax = pmax; }
		public String toString() { return l+","+p+","+pmax; }
		public boolean before(X x) {
			if (l < x.l) return true;
			if (l > x.l) return false;
			return pmax-p  <  x.pmax - x.p;
		}
		public static int compare(X x, X xx) { 
			if (x.before(xx)) return -1;
			if (xx.before(x)) return  1;
			return 0;
		}
		@Override public int compareTo(X x) { return compare(this, x); }
	}
	
	static void printQueue() {
		PriorityQueue<X> copy = new PriorityQueue<X>(queue);
		while (! copy.isEmpty()) {
			X x = copy.poll();
			System.out.print(x+"  ");
		}
		System.out.println();
	}
	
	static void job() {
		while (! queue.isEmpty()) {
//			printQueue();
			X x = queue.poll();
			int actualSlot = x.l;
			if (slots[actualSlot] > 0) {
				slots[actualSlot]--;
			} else {
				x.p++;
				if (x.p > x.pmax) { System.out.println("NIE"); return; } 
			}
			x.l++;
			if (x.l <= x.p) {
				queue.add(x);
			}
		}
		System.out.println("TAK");
	}
	
	public static void main(String[] args) throws Exception {
		io();
		job();
	}

	public static void io() {
		int[] nm = IO.getInts();
		int zadan      = nm[0];
		int procesorow = nm[1];
		for (int i = 0; i<slots.length; i++) { slots[i] = procesorow; }
		queue = new PriorityQueue<X>(zadan, new Comparator<X>(){
			public int compare(X x, X xx) { return X.compare(x, xx); }
		});
		for (int i = 0; i<zadan; i++) {
			int t[] = IO.getInts();
			int p=t[0], k=t[1], c=t[2]; 
			queue.add(new X(p, p+c-1, k-1));
		}
	}

	public static class IO {
		static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		public static int[]	getInts() {
			try {
				String[] ss = br.readLine().split(" ");
				int[] ret = new int[ss.length];
				for (int i = 0; i<ss.length; i++) {
					ret[i] = Integer.valueOf(ss[i]);
				}
				return ret;
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}
		public static int getInt() {
			return getInts()[0];
		}
	}

}