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
157
#include<bits/stdc++.h>
#define ALL(X)        X.begin(),X.end()
#define FOR(I,A,B)    for(int (I) = (A); (I) <= (B); (I)++)
#define FORW(I,A,B)   for(int (I) = (A); (I) < (B);  (I)++)
#define FORD(I,A,B)   for(int (I) = (A); (I) >= (B); (I)--)
#define CLEAR(X)      memset(X,0,sizeof(X))
#define SIZE(X)       int(X.size())
#define CONTAINS(A,X) (A.find(X) != A.end())
#define PB            push_back
#define MP            make_pair
#define X             first
#define Y             second
using namespace std;
template<typename T, typename U> ostream& operator << (ostream& os, const pair<T, U> &_p) { return os << "(" << _p.X << "," << _p.Y << ")"; }
template<typename T> ostream& operator << (ostream &os, const vector<T>& _V) { bool f = true; os << "["; for(auto v: _V) { os << (f ? "" : ",") << v; f = false; } return os << "]"; }
template<typename T> ostream& operator << (ostream &os, const set<T>& _S) { bool f = true; os << "("; for(auto s: _S) { os << (f ? "" : ",") << s; f = false; } return os << ")"; }
template<typename T, typename U> ostream& operator << (ostream &os, const map<T, U>& _M) { return os << set<pair<T, U>>(ALL(_M)); }
typedef signed long long slong;
typedef long double ldouble;
const slong INF = 1000000100;
const ldouble EPS = 1e-9;

namespace MaxFlow {
    struct edge {
        int u, p;
        slong f, c;
        edge(int _u, slong _f, slong _c, int _p) : u(_u), p(_p), f(_f), c(_c) {}
    };

    const int MAXE = 30100;
    const int MAXV = 5050;
    int D[MAXV];
    bool V[MAXV];
    int L[MAXV];
    vector<edge> G[MAXV];
    int E, S, T;

    void clear() {
        FORW(i,0,MAXV) G[i].clear();
        E = 0;
    }

    void add_edge(int a, int b, slong f) {
        ++E;
        if(a == b) return;
        G[a].PB(edge(b, 0, f, SIZE(G[b])));
        G[b].PB(edge(a, 0, 0, SIZE(G[a])-1));
    }

    bool bfs() {
        CLEAR(V);
        queue<int> Q;
        Q.push(S);
        D[S] = 0;
        V[S] = true;
        while(!Q.empty()) {
            int v = Q.front();
            Q.pop();
            for(edge &e: G[v]) if(!V[e.u] and e.c - e.f > 0) {
                V[e.u] = true;
                D[e.u] = D[v]+1;
                Q.push(e.u);
            }
        }
        return V[T];
    }

    slong dfs(int v, slong f) {
        if(v == T or f == 0) return f;
        slong r = 0;
        for(int &e_id = L[v]; e_id < SIZE(G[v]); ++e_id) {
            edge &e = G[v][e_id];
            if(D[v]+1 != D[e.u] or e.c == e.f) {
                continue;
            }
            slong c = dfs(e.u, min(f-r, e.c - e.f));
            e.f += c;
            G[e.u][e.p].f -= c;
            r += c;
            if(r == f) break;
        }
        return r;
    }

    slong flow() {
        slong result = 0;
        while(bfs()) {
            CLEAR(L);
            result += dfs(S, 10000000000000000LL);
        }
        return result;
    }
}

int N, M;
const int MAXN = 111;
int P[MAXN], K[MAXN], C[MAXN];

void read_data() {
    scanf("%d %d", &N, &M);
    FOR(i,1,N)
    {
        scanf("%d %d %d", &P[i], &K[i], &C[i]);
    }
}

void solve() {
    MaxFlow::clear();
    vector<int> ts;
    int total = 0;
    FOR(i,1,N)
    {
        ts.PB(P[i]);
        ts.PB(K[i]);
        total += C[i];
    }
    sort(ALL(ts));
    int cur_node = N + 10;
    int source = N + 1, sink = N + 2;
    MaxFlow::S = source;
    MaxFlow::T = sink;
    FOR(i,1,N)
    {
        MaxFlow::add_edge(i, sink, C[i]);
    }
    FORW(i,1,SIZE(ts))
    {
        int t = ts[i] - ts[i-1];
        if(t <= 0) continue;
        int cnt = 0; // how many processes can we run in parallel
        FOR(j,1,N)
        {
            int overlap = min(ts[i],K[j]) - max(ts[i-1],P[j]);
            if(P[j] <= ts[i-1] && ts[i] <= K[j])
            {
                MaxFlow::add_edge(cur_node, j, t);
                cnt++;
            }
        }
        MaxFlow::add_edge(source, cur_node, t*M);
        cur_node++;
    }
    int ans = MaxFlow::flow();
    if(ans == total)
    {
        printf("TAK\n");
    }
    else
    {
        printf("NIE\n");
    }
}

int main() {
    read_data();
    solve();
}