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
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <bits/stdc++.h>

using namespace std;
typedef long long int64;
#define DEBUG(x) cerr << #x << " = " << x << endl;
#define REP(x, n) for(__typeof(n) x = 0; x < (n); ++x)
#define FOR(x, b, e) for(__typeof(b) x = (b); x != (e); x += 1 - 2 * ((b) > (e)))
const int INF = 1000000001;
const double EPS = 10e-9;

#define DEBUGT(x) cerr << #x << " = "; for (auto it : x) cerr << "{<" << it.begin << ", " << it.end << ">, " << it.time << "} "; cerr << endl; 

int m;

struct Task {
    int begin, end, time;
};

const Task EMPTY = { -1, -1, -1 };

int nextSet(int x) {
    int smallest, ripple, new_smallest, ones;
    smallest     = (x & -x);
    ripple       = x + smallest;
    new_smallest = (ripple & -ripple);
    ones         = ((new_smallest/smallest) >> 1) - 1;
    return ripple | ones;
}

bool solve(vector<Task>& tasks) {
    for (auto t : tasks) {
        if (t.begin + t.time > t.end) {
            return false;
        }
    }
    if (tasks.size() <= m) {
        return true;
    }
    int time = tasks.back().begin;

    int count = 0;
    for (int i = tasks.size() - 1; i >= 0; --i) {
        if (tasks[i].begin != time) {
            break;
        }
        ++count;
    }

    int maxi = 1 << count;
    int set = (1 << min(m, count)) - 1;
    do {
        vector<Task> taken, notTaken;
        for (int i = 0, x = (1 << (tasks.size() - 1)); i < tasks.size(); ++i, x >>= 1) {
            if (x & set) {
                taken.push_back(tasks[i]);
            } else {
                notTaken.push_back(tasks[i]);
            }
        }

        int newTime = INF;
        if (count < tasks.size()) {
            newTime = tasks[tasks.size() - count - 1].begin;
        }
        for (auto t : taken) {
            newTime = min(newTime, time + t.time);
        }

        for (auto& t : notTaken) {
            if (t.begin < newTime) {
                t.begin = newTime;
            }
        }
        int deltaTime = newTime - time;
        for (auto& t : taken) {
            t.time -= deltaTime;
            if (t.time > 0) {
                t.begin = newTime;
                notTaken.push_back(t);
            }
        }
        if (solve(notTaken)) {
            return true;
        }
        set = nextSet(set);
    } while (set < maxi);
    return false;
}

struct Computer {
    int m;
    int time;
    vector<Task> processors;
    vector<Task> tasks;
    set<int> begins;

    Computer(int n) : m(n), processors(n, EMPTY) { }

    bool simulate(int newTime) {
        int deltaTime = newTime - time;
        for (auto& p : processors) {
            if (p.time == -1) {
                continue;
            }
            if (p.end < newTime) {
                return false;
            }
            p.time -= deltaTime;
            if (p.time > 0) {
                tasks.push_back(p);
            }
            p = EMPTY;
        }
        time = newTime;
        for (auto& t : tasks) {
            if (t.begin < newTime) {
                t.begin = newTime;
            }
        }
        return true;
    }

    int closestEndTime() {
        int end = INF;
        for (auto it : processors) {
            if (it.end != -1) {
                end = min(end, time + it.time);
            }
        }
        return end;
    }
};

#ifndef CATCH_TEST
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

    int n;
    vector<Task> tasks;
    cin >> n >> m;
    REP(x, n) {
        int p, k, c;
        cin >> p >> k >> c;
        tasks.push_back({ p, k, c });
    }

    sort(tasks.begin(), tasks.end(), [](const Task& first, const Task& second) { 
        return first.begin > second.begin; 
    });
    cout << (solve(tasks) ? "TAK" : "NIE") << endl;

	return 0;
}
#endif