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
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

namespace {

struct Task {
  int start;
  int deadline;
  int time;

  Task(int s, int d, int t): start{s}, deadline{d}, time{t}
  {
  }
};

using ll = long long;

bool possible(vector<Task>&& tasks, int m)
{
  sort(tasks.begin(), tasks.end(), [](Task const& lhs, Task const& rhs) {
    return lhs.start < rhs.start;
  });
  auto it = tasks.begin();
  int now = it->start;
  vector<Task*> waiting;
  while (it != tasks.end() || !waiting.empty()) {
    if (waiting.empty()) now = it->start;
    while (it != tasks.end() && it->start == now) {
      waiting.emplace_back(&*it);
      ++it;
    }
    for (Task* t: waiting) {
      if (t->deadline <= now) return false;
    }
    int w = waiting.size();
    auto k = min(w, m);
    if (w > m) {
      nth_element(waiting.begin(), waiting.begin() + k, waiting.end(), [&](Task* a, Task* b) {
        return ll{a->time} * (b->deadline - now) > ll{b->time} * (a->deadline - now);
      });
    }
    for (int i = 0; i < k; ++i) {
      --waiting[i]->time;
    }
    waiting.erase(remove_if(waiting.begin(), waiting.end(), [&](Task* t) { return t->time == 0; }), waiting.end());
    ++now;
  }
  return true;
}

}

int main()
{
  iostream::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, m;
  cin >> n >> m;
  vector<Task> tasks;
  tasks.reserve(n);
  for (int i = 0; i < n; ++i) {
    int p, k, c;
    cin >> p >> k >> c;
    tasks.emplace_back(p, k, c);
  }

  cout << (possible(move(tasks), m) ? "TAK" : "NIE") << '\n';

  return 0;
}