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
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class sze {

    static int[] tasksInSecond = new int[1000001];

    static class task implements Comparable<task> {
        int p;
        int k;
        int c;
        int lastExecutionSecond;

        public task(int p, int k, int c) {
            this.p = p;
            this.k = k;
            this.c = c;
            for (int i = 0; i < c; i++) {
                tasksInSecond[p + i]++;
            }
            this.lastExecutionSecond = p + c - 1;
        }

        boolean canBeExecutedAt(int second) {
            return p <= second && second < k;
        }

        boolean canBeShifted() {
            return lastExecutionSecond < k - 1;
        }

        int secondsThatCanBeShifted() {
            return k - 1 - lastExecutionSecond;
        }

        void shift(int second) {
            tasksInSecond[second]--;
            lastExecutionSecond++;
            tasksInSecond[lastExecutionSecond]++;
        }

        boolean alreadyScheduled(int second) {
            return lastExecutionSecond < second;
        }

        @Override
        public int compareTo(task t) {
            return t.secondsThatCanBeShifted() - secondsThatCanBeShifted();
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int maxSecond = 0;
        task[] tasks = new task[n];
        for (int i = 0; i < n; i++) {
            int p = sc.nextInt();
            int k = sc.nextInt();
            maxSecond = Math.max(maxSecond, k);
            int c = sc.nextInt();
            tasks[i] = new task(p, k, c);
        }

        for (int second = 0; second < maxSecond; second++) {
            if (tasksInSecond[second] > m) {
                List<task> tasksThatCanBeShifted = new ArrayList<>();
                for (task t : tasks) {
                    if (t.canBeExecutedAt(second) && !t.alreadyScheduled(second) && t.canBeShifted()) {
                        tasksThatCanBeShifted.add(t);
                    }
                }
                if (tasksThatCanBeShifted.size() < tasksInSecond[second] - m) {
                    System.out.println("NIE");
                    return;
                }
                Collections.sort(tasksThatCanBeShifted);
                for (int i = 0; tasksInSecond[second] > m; i++) {
                    tasksThatCanBeShifted.get(i).shift(second);
                }
            }
        }

        System.out.println("TAK");
    }
}