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
#include<iostream>
#include <algorithm>
#include <vector>
using namespace std;
#define M 100001

struct Zad { int p; int k; int c;};

bool Por_m(const Zad& Z1, const Zad& Z2) {
    if(Z1.p > Z2.p || (Z1.p==Z2.p && (Z1.k-Z1.p)-Z1.c > (Z2.k-Z2.p)-Z2.c) 
                   || (Z1.p==Z2.p && (Z1.k-Z1.p)-Z1.c == (Z2.k-Z2.p)-Z2.c && Z1.k > Z2.k) )
        return 1;
    else
        return 0;
};

int main( ) {
    int n, m, ans=1, cs=0, left=-1, ms;
    Zad Z[100];
    cin >> n >> m;
    for (int i=0; i<n; i++) {
        cin >> Z[i].p >> Z[i].k >> Z[i].c;
        cs+=Z[i].c;
    }
    vector<Zad> A(Z,Z+n);
    make_heap(A.begin(), A.end(), Por_m); 
     
    while(cs > 0) {
        if (A[0].p > left) {
            left = A[0].p;
            ms=0;
        }
        pop_heap(A.begin(), A.end(), Por_m);
        if ( (A.back().k > A.back().p ) && (A.back().k-A.back().p >= A.back().c) ) {
            A.back().p++;
            A.back().c--;
            cs--;
            ms++;
            if(A.back().c == 0)
                A.pop_back();
            else
                push_heap(A.begin(), A.end(), Por_m);
            if (ms==m) {
                left++;
                while(A[0].p < left) {
                    pop_heap(A.begin(), A.end(), Por_m);
                    A.back().p++;
                    push_heap(A.begin(), A.end(), Por_m);
                }
                ms=0;
            }
        }
        else {
            ans=0;
            break;
        }
    } 
     
    if(ans==0)
        cout<<"NIE\n";
    else
        cout<<"TAK\n";
    return 0;
}