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
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <set>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
using namespace std;

#define pi pair<int,int>
#define mp(x,y) make_pair(x,y)
#define fi first
#define se second
#define pl pair<long long,long long>
#define pb push_back

struct zad
{
    int p,k,c;

    friend bool operator < (zad a, zad b)
    {
        return a.p<b.p;
    }
};

bool cmp(zad a, zad b)
{
    return a.k-a.c<b.k-b.c;
}

zad t[105], curr[105];

int main()
{
    int n,m,a,b,c,d,p1,p2;
    zad z;

    scanf ("%d%d", &n, &m);

    for (a=0; a<n; a++)
        scanf ("%d%d%d", &t[a].p, &t[a].k, &t[a].c);

    sort(t,t+n);
    p1=p2=0;

    for (a=0; p1<n || p2!=0; a++)
    {
        for (b=0; b<p2 && curr[b].k>=a+curr[b].c; b++);

        if (b<p2)
        {
            printf ("NIE");
            return 0;
        }

        for (; p1<n && t[p1].p==a; p1++)
        {
            z=t[p1];

            for (b=0; b<p2 && curr[b].k-curr[b].c<=z.k-z.c; b++);

            for (c=p2; c>b; c--)
                curr[c]=curr[c-1];

            curr[b]=z;
            p2++;
        }

        for (b=0; b<m && b<p2; b++)
            curr[b].c--;

        for (b=0; b+1<p2 && curr[b].k-curr[b].c<=curr[b+1].k-curr[b+1].c; b++);

        c=b+1;

        for (; b && curr[b].k-curr[b].c==curr[b-1].k-curr[b-1].c; b--);

        for (; c<p2 && curr[b].k-curr[b].c>curr[c].k-curr[c].c;)
            swap(curr[b++],curr[c++]);

        for (b=0; b<p2; b++)
            if (curr[b].c==0)
            {
                for (c=b; c+1<p2; c++)
                    curr[c]=curr[c+1];

                p2--;
                b--;
            }
    }

    printf ("TAK");

    return 0;
}