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
import java.util.*;

public class sze {

    private static final int MAX_TIME = 1000000;
    static class Task implements Comparable<Task> {
        private int p;
        private int k;
        private int c;

        @Override
        public int compareTo(Task other) {
            int max_time_start = k - c;
            int max_time_start_other = other.k - other.c;
            int time_start_diff = max_time_start - max_time_start_other;
            return time_start_diff != 0 ? time_start_diff : p - other.p;
        }

        Task(int id, int p, int k, int c) {
            this.p = p;
            this.k = k;
            this.c = c;
        }

    }

    public static void main(String[] args) {

        PriorityQueue<Task> queue = new PriorityQueue<>();
        LinkedList<Task> nextIter = new LinkedList<>();

        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int m = input.nextInt();
        int first_start = Integer.MAX_VALUE;
        Task task;
        for (int i = 0; i < n; ++i) {
            task = new Task(i + 1, input.nextInt(), input.nextInt(), input.nextInt());
            first_start = Math.min(first_start, task.p);
            queue.add(task);
        }

        Iterator<Task> it;
        int workers;
        for (int t = first_start; t < MAX_TIME; ++t) {
            workers = m;
            it = queue.iterator();
            while (it.hasNext() && workers > 0) {
                task = it.next();
                if (task.k - t < task.c) {
                    System.out.println("NIE");
                    return;
                }
                if (task.p <= t) {
                    //we can do work for this task
                    //System.err.println("T=" + t + " P=" + workers + " J=" + task.hashCode());
                    it.remove();
                    --task.c;
                    --workers;
                    if (task.c != 0) {
                        //proccesed but not ended
                        nextIter.add(task);
                    }
                }
            }

            while (!nextIter.isEmpty()) {
                queue.add(nextIter.remove());
            }

            if (queue.isEmpty()) {
                System.out.println("TAK");
                return;
            }
        }

        if (!queue.isEmpty()) {
            System.out.println("NIE");
        }
    }
}