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
#include <bits/stdc++.h>

#define FOR(i,b,e) for(int i=(b); i <= (e); ++i)
#define FORD(i,b,e) for(int i=(b); i >= (e); --i)
#define SIZE(c) (int) (c).size()
#define FORE(i,c) FOR(i,0,SIZE(c)-1)
#define FORDE(i,c) FORD(i,SIZE(c)-1,0)

#define pb push_back
#define mp make_pair
#define st first
#define nd second

using namespace std;

typedef long long ll;
typedef pair <int,int> pii;
typedef pair <ll,ll> pll;

typedef vector <int> VI;
typedef vector <bool> VB;
typedef vector <pii> VP;
typedef vector <ll> VL;
typedef vector <pll> VPL;

typedef vector <VI> VVI;
typedef vector <VL> VVL;
typedef vector <VB> VVB;
typedef vector <VP> VVP;

const int INF = 1000000001;

/*************************************************************************/

struct Network {
    typedef int flow_t;

    struct edge {
        int u, v; flow_t flow, cap;
        int rev;
    };

    const flow_t FINF = INF;

    int n, src, dest;
    vector <vector<edge>> G;
    VI start, dist;

    Network(int n): n(n), G(n), start(n), dist(n) {}

    bool bfs(int s, int t, int n) {
        FOR(i,0,n-1) dist[i] = -1;
        dist[s] = 0;

        queue <int> q;
        q.push(s);

        while (!q.empty()) {
            int u = q.front(); q.pop();

            for (edge &e : G[u])
            if (e.flow < e.cap && dist[e.v] == -1) {
                dist[e.v] = dist[u] + 1;
                q.push(e.v);
            }
        }

        return dist[t] != -1;
    }

    flow_t dfs(int x, int t, flow_t mini) {
        flow_t ans = 0;

        if (x == t) return mini;
        if (mini <= 0) return 0;

        for (int &i = start[x]; i < SIZE(G[x]); ++i) {
            edge &e = G[x][i];

            if (dist[x] + 1 == dist[e.v] && e.flow < e.cap) {
                flow_t added = dfs(e.v, t, min(mini, e.cap - e.flow));

                e.flow += added;
                G[e.v][e.rev].flow -= added;
                mini -= added; ans += added;

                if (mini <= 0) break;
            }
        }

        return ans;
    }

    flow_t getFlow(int s, int t) {
        flow_t ans = 0;

        while (bfs(s, t, n)) {
            FOR(i,0,n-1) start[i] = 0;
            ans += dfs(s, t, INF);
        }

        return ans;
    }

    void addEdge(int u, int v, flow_t cap) {
        int sizeU = SIZE(G[u]), sizeV = SIZE(G[v]);
          
        G[u].pb({u, v, 0, cap, sizeV});
        G[v].pb({v, u, 0, 0, sizeU});
    }
};

/*************************************************************************/

struct task {
    int l, r, c;
};

void unify(VI &vec) {
    sort(vec.begin(), vec.end());
    vec.resize(unique(vec.begin(), vec.end()) - vec.begin());
}

/*************************************************************************/

int main() {
    ios_base::sync_with_stdio(0);
    
    int n, m;
    cin >> n >> m;
    
    int sumC = 0;
    VI times;
    vector <task> tasks(n);

    FOR(i,0,n-1) {
        cin >> tasks[i].l >> tasks[i].r >> tasks[i].c;
        
        sumC += tasks[i].c;
        
        times.pb(tasks[i].l);
        times.pb(tasks[i].r);
    }
    
    unify(times);

    int t = SIZE(times) - 1;
    
    const int flowN = n + t + 2;
    const int flowS = flowN - 2;
    const int flowT = flowN - 1;
    
    Network network(flowN);
    
    FOR(i,0,n-1) {
        network.addEdge(flowS, i, tasks[i].c);
    
        FOR(j,0,t-1) if (times[j] >= tasks[i].l && times[j+1] <= tasks[i].r) {
            network.addEdge(i, n + j, times[j+1] - times[j]);
        }
    }
    
    FOR(j,0,t-1) {
        network.addEdge(n + j, flowT, m * (times[j+1] - times[j]));
    }
    
    cout << (network.getFlow(flowS, flowT) == sumC ? "TAK" : "NIE");

    return 0;
}

/*************************************************************************/