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
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

struct zad {    
    int p, k, c;
};

zad a[101];
int s[1000001];

struct lista {
    int r, poczatek = 0;
    int ak[101];
    int k[101];
    int n[101];
    int p[101];
    
    lista() : r(1) {
        n[0] = -1;
        ak[0] = 10000000;
        k[0] = 10000000;
        p[0] = -1;
    };
    
    void wstaw(int ak2, int k2) {
        int i = poczatek;
        while(ak[i] < ak2 || (ak[i] == ak2 && k[i] > k2)) {
            i = n[i];
        }
        n[r] = i;
        p[r] = p[i];
        if(p[i] != -1) {
            n[p[i]] = r;
        } else {
            poczatek = r;
        }
        p[i] = r;
        ak[r] = ak2;
        k[r] = k2;
        r ++;
    }
    
    bool popraw(int x, int time) {
        int i = poczatek;
        while(x && n[i] != -1) {
            if(k[i] == 0) {
                if(p[i] != -1) {
                    n[p[i]] = n[i];
                } else {
                    poczatek = n[i];
                }
                p[n[i]] = p[i];
            } else if(ak[i] - time < 0) {
                return false;
            } else {
                ak[i] ++;
                k[i] --;
                x --;
            }
            i = n[i];
        }
        
        int j = poczatek, l = i, nj;
        poczatek = l;
        p[l] = -1;
        while(j != i) {
            while(ak[l] < ak[j] || (ak[l] == ak[j] && k[l] > k[j])) {
                l = n[l];
            }
            nj = n[j];
            n[j] = l;
            p[j] = p[l];
            if(p[l] != -1) {
                n[p[l]] = j;
            } else {
                poczatek = j;
            }
            p[l] = j;
            j = nj;
        }
        return true;
    }
};


vector <int> v[1000001];

int main() {
    ios_base::sync_with_stdio(0);
    int n, m, k = 0;
    cin >> n >> m;
    for(int i = 0; i < n; i ++) {
        cin >> a[i].p >> a[i].k >> a[i].c;
        v[a[i].p].push_back(i);
        k = max(k, a[i].k);    
    }
    
    lista l;
    for(int i = 0; i < k; i ++) {
        for(int j = 0; j < v[i].size(); j ++) {
            l.wstaw(a[v[i][j]].k - a[v[i][j]].p - a[v[i][j]].c + i, a[v[i][j]].c);
        }
        if(!l.popraw(m, i)) {
            cout << "NIE" << endl;
            return 0;
        }
    }
    
    cout << (l.popraw(m, k) ? "TAK" : "NIE") << endl;
}