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
156
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <cmath>
#include <cstring>
#include <string>
#include <iostream>
#include <complex>
#include <sstream>
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef vector<int> VI;
typedef pair<int,int> PII;
 
#define REP(i,n) for(int i=0;i<(n);++i)
#define SIZE(c) ((int)((c).size()))
#define FOR(i,a,b) for (int i=(a); i<(b); ++i)
#define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i)
#define FORD(i,a,b) for (int i=(a)-1; i>=(b); --i)
#define ALL(v) (v).begin(), (v).end()
 
#define pb push_back
#define mp make_pair
#define st first
#define nd second

const int MAXN = 1000;

// MAKSYMALNY PRZEPLYW O(V^3) (algorytm Push-Relabel)
// Adam Polak

struct Edge {
    int v,cap,flow;
    int back_ind;
    Edge *back;
    Edge(int vi, int ci):v(vi),cap(ci){}
};

/* Usage:
   1) n=...; s=...; t=...;
   2) REP(i,n) g[i].clear();
   3) add_edge(...);
   4) compute_flow();
*/

int n,s,t;
int e[MAXN],h[MAXN];
vector<Edge> g[MAXN];
vector<Edge>::iterator cur[MAXN];

void bfs(int start, int start_h) {
    queue<int> q;
    h[start] = start_h;
    for(q.push(start);!q.empty();q.pop()) {
        int u = q.front();
        FOREACH(i,g[u])
            if (i->back->flow < i->back->cap && h[i->v]>h[u]+1) {
                    h[i->v] = h[u] + 1;
                    q.push(i->v);
            }
    }
}

int compute_flow() {
    queue<int> q;
    REP(i,n) {
        FOREACH(j,g[i]) {
            j->flow = 0;
            j->back = &g[j->v][j->back_ind];
        }
        cur[i] = g[i].begin();
        h[i] = e[i] = 0;
    }
    FOREACH(i,g[s]) {
        i->flow = i->cap;
        i->back->flow = -i->flow;
        if (e[i->v]==0 && i->v!=t) q.push(i->v);
        e[i->v] += i->flow;
    }
    h[s] = n;
    int relabel_counter = 0;
    for(;!q.empty();q.pop()) {
        int u = q.front();
        while (e[u]>0) {
            if (cur[u]==g[u].end()) { // relabel
                relabel_counter++;
                h[u] = 2*n+1;
                FOREACH(i,g[u]) if(i->flow < i->cap) h[u]=min(h[u],1+h[i->v]);
                cur[u] = g[u].begin(); 
                continue; 
            }
            if (cur[u]->flow < cur[u]->cap && h[u]==h[cur[u]->v]+1) { // push
                int d = min(e[u], cur[u]->cap - cur[u]->flow);
                cur[u]->flow += d;
                cur[u]->back->flow -= d;
                e[u] -= d;
                e[cur[u]->v] += d;
                if (e[cur[u]->v]==d && cur[u]->v!=t && cur[u]->v!=s) q.push(cur[u]->v);
            } else cur[u]++; 
        }
        if (relabel_counter >= n) { 
            REP(i,n) h[i]=2*n+1;
            bfs(t,0);
            bfs(s,n);
            relabel_counter = 0;
        }
    }
    return e[t];
}

void add_edge(int a, int b, int c, int c_back=0) {
    g[a].push_back(Edge(b,c));
    g[b].push_back(Edge(a,c_back));
    g[a].back().back_ind = g[b].size()-1;
    g[b].back().back_ind = g[a].size()-1;
}


int p[MAXN], k[MAXN], c[MAXN];
int main() {
    int N, M;
    scanf("%d%d", &N, &M);
    REP(i,N) scanf("%d%d%d", &p[i], &k[i], &c[i]);

    vector<int> ti;
    int C = 0;
    REP(i,N) {
        ti.pb(p[i]);
        ti.pb(k[i]);
        C += c[i];
    }
    sort(ti.begin(), ti.end());
    ti.erase(unique(ti.begin(), ti.end()), ti.end());
    
    int D = ti.size() - 1;
    int S = N + D;
    int T = S + 1;
    
    n = T + 1, s = S, t = T;
    REP(i,N) add_edge(S, i, c[i]);
    REP(j,D) add_edge(N+j, T, M * (ti[j+1] - ti[j]));
    REP(i,N)REP(j,D) {
        if (p[i] <= ti[j] && ti[j+1] <= k[i]) add_edge(i, N+j, ti[j+1] - ti[j]);
    }
    int f = compute_flow();
    printf(f == C ? "TAK\n" : "NIE\n");
}