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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <stack>
#include <list>
#include <queue>
#include <deque>
#include <cctype>
#include <string>
#include <vector>
#include <sstream>
#include <iterator>
#include <numeric>
#include <cmath>
#include <cassert>
#include <limits.h>
using namespace std;

#define ST first
#define ND second
#define MP make_pair
#define PB push_back

typedef long long LL;
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 FOR(i,a,b) for(VAR(i,a);i<=(b);++i)
#define FORD(i,a,b) for(VAR(i,a);i>=(b);--i)
#define FORE(a,b) for(VAR(a,(b).begin());a!=(b).end();++a)
#define VAR(a,b) __typeof(b) a=(b)
#define SIZE(a) ((int)((a).size()))
#define ALL(x) (x).begin(),(x).end()
#define CLR(x,a) memset(x,a,sizeof(x))
#define ZERO(x) memset(x,0,sizeof(x))
#define S(t,i) scanf("%" ## t, &i)
#define SI(i) scanf("%d", &i)

// algorytm dinica pochodzi z http://was.zaa.mimuw.edu.pl/sites/default/files/file/s2008-w03/flow.cpp

const int INF = 1000000001;
const int N=500;
vector<int> krawedzie[N];
vector<int>::iterator poczatek[N];
int przepustowosc[N][N], odleglosc[N], q[N];

void AddEdge(int a,int b,int c){
//    fprintf(stderr, "%d %d %d\n", a, b, c);
    przepustowosc[a][b]+=c;
    krawedzie[a].push_back(b);
    krawedzie[b].push_back(a);
}

int bfs(int s,int t,int n){
    for (int i=0; i<n; ++i) odleglosc[i]=-1;
    odleglosc[s]=0;
    int beg=0,end=0;
    q[end++]=s;
    while (beg<end){
        int v=q[beg++];
        for (vector<int>::iterator it=krawedzie[v].begin(); it!=krawedzie[v].end(); ++it)
            if (przepustowosc[v][*it]>0 && odleglosc[*it]==-1){
                odleglosc[*it]=odleglosc[v]+1;
                q[end++]=*it;
            }
    }
    return odleglosc[t]!=-1;
}

int dfs(int x,int t,int minimum){
    int res=0;
    if (x==t || minimum==0) return minimum;
    //UWAGA: it jest referencja!!!
    for(vector<int>::iterator &it=poczatek[x];it!=krawedzie[x].end(); ++it){
        if (odleglosc[x]+1==odleglosc[*it] && przepustowosc[x][*it]>0){
            int y=dfs(*it,t,min(minimum,przepustowosc[x][*it]));
            //odwracamy czesc przepustowosci
            przepustowosc[x][*it]-=y;
            przepustowosc[*it][x]+=y;
            minimum-=y; res+=y;
            if (minimum==0) break;
        }
    }
    return res;
}

int Maxflow(int s,int t,int n){
    int res=0;
    while (bfs(s,t,n)){
        for (int i=0; i<n; ++i) poczatek[i]=krawedzie[i].begin();
        res+=dfs(s,t,INF);
    }
    return res;
}

const int MAXN = 100;
struct task {
    int start;
    int end;
    int length;
};
task tasks[MAXN+9];
int taskStarts[MAXN+9];
int taskEnds[MAXN+9];

int cmpTaskStart(const void *t1, const void *t2) {
    return tasks[*(int*)t1].start-tasks[*(int*)t2].start;
}
int cmpTaskEnd(const void *t1, const void *t2) {
    return tasks[*(int*)t1].end-tasks[*(int*)t2].end;
}

int main(){
    int n, m;
    SI(n);
    SI(m);
    int totalLength=0;
    if (m>=n) {
        printf("TAK\n");
        return 0;
    }
    
    REP(i, n){
        SI(tasks[i].start);
        SI(tasks[i].end);
        SI(tasks[i].length);
        taskStarts[i]=i;
        taskEnds[i]=i;
        AddEdge(0,i+2,tasks[i].length);
        totalLength+=tasks[i].length;
    }
    
    qsort(taskStarts, n, sizeof(taskStarts[0]), cmpTaskStart);
    qsort(taskEnds, n, sizeof(taskEnds[0]), cmpTaskEnd);
//    assert(tasks[taskStarts[0]].start<=tasks[taskStarts[n-1]].start);
    
    set<int> nodes;
    int si=0, ei=0;
    int lastEvent=tasks[taskStarts[0]].start;
    while (si<n && tasks[taskStarts[si]].start==lastEvent) {
        nodes.insert(taskStarts[si]);
        si++;
    }
    int eventId=-1;
    while (si<n || ei<n) {
        int currEvent=INT_MAX;
        if (si<n) {
            currEvent=tasks[taskStarts[si]].start;
        }
        if (ei<n && tasks[taskEnds[ei]].end<currEvent) {
            currEvent=tasks[taskEnds[ei]].end;
        }
        int d=currEvent-lastEvent;
//        assert(d>0);
        eventId++;
        AddEdge(n+2+eventId, 1, d*m);
        
        for (int i : nodes) {
            AddEdge(i+2, n+2+eventId, d);
        }
        
        while (ei<n && tasks[taskEnds[ei]].end==currEvent) {
            nodes.erase(nodes.find(taskEnds[ei]));
            ei++;
        }
        
        while (si<n && tasks[taskStarts[si]].start==currEvent) {
            nodes.insert(taskStarts[si]);
            si++;
        }
        lastEvent=currEvent;
    }
    
    int res=Maxflow(0,1,n+2+eventId+1);
//    fprintf(stderr, "%d\n", res);

    if (res==totalLength) {
        printf("TAK\n");
    } else {
        printf("NIE\n");
    }
    
    return 0;
}