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
#include <cstdio>
#include <vector>
#include <algorithm>


struct task {
    int available_delay;
    int work_left;

    bool operator<(const task& rhs) const {
        if (available_delay < rhs.available_delay) {
            return true;
        }

        return false;
    }
};


int main() {
    int n, m;
    scanf("%d%d", &n, &m);
    int p, k, c;

    std::vector<task> active_tasks;
    std::vector<std::pair<int, task> > all_tasks;
    int begin = 1000 * 1000;
    int end = 0;
    for (int i = 0; i < n; i++) {
        task currentTask;
        scanf("%d%d%d", &p, &k, &c);
        currentTask.available_delay = k - p - c;
        currentTask.work_left = c;
        all_tasks.push_back(std::make_pair(p, currentTask));
        begin = std::min(begin, p);
        end = std::max(end, k);
    }
    std::sort(all_tasks.begin(), all_tasks.end());

    int next_task_position = 0;
    for (int i = begin; i <= end; i++) {
        while (next_task_position < all_tasks.size()
                && all_tasks[next_task_position].first == i) {
            active_tasks.push_back(all_tasks[next_task_position].second);
            next_task_position++;
        }

        std::sort(active_tasks.begin(), active_tasks.end());

        for (int j = 0; j < std::min(m, (int) active_tasks.size()); j++) {
            active_tasks[j].work_left--;
        }

        if (active_tasks.size() > m) {
            for (int j = m; j < active_tasks.size(); j++) {
                active_tasks[j].available_delay--;
                if (active_tasks[j].available_delay < 0) {
                    printf("NIE");
                    return 0;
                }
            }
        }

        for (int j = 0; j < active_tasks.size(); j++) {
            if (active_tasks[j].work_left == 0) {
                active_tasks[j] = active_tasks.back();
                active_tasks.pop_back();
                j--;
            }
        }
    }

    printf("TAK");
    return 0;
}