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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
uses
  Math;

var
  a:Array[1..202] of Int64;
  b:Array[1..202] of Longint;
  p:Array[1..101] of Int64;
  k:Array[1..101] of Longint;
  c:Array[1..101] of Longint;
  aa:Array[0..1000002] of Longint;

procedure QuickSort(iLow, iHigh : Integer);
var
  iLo, iHi : Integer;
  x, Temp : Int64;
begin
  iLo := iLow;
  iHi := iHigh;
  X := a[(iLow + iHigh) div 2];
  repeat
    while a[iLo] < X do Inc(iLo);
    while a[iHi] > X do Dec(iHi);
    if (iLo <= iHi) then
    begin
      Temp := a[iLo];
      a[iLo] := a[iHi];
      a[iHi] := Temp;
      Temp := b[iLo];
      b[iLo] := b[iHi];
      b[iHi] := Temp;
      Inc(iLo);
      Dec(iHi);
    end;
  until iLo > iHi;
  if (iHi > iLow) then QuickSort(iLow, iHi);
  if (iLo < iHigh) then QuickSort(iLo, iHigh);
end;
var
  i, ii, j, m, n, t, u, v, mi, pi, ki, d, pn: Longint;
  w: Int64;
  dalej: boolean;
begin
  Read(n, m);

  if m >= n then
  begin
    Writeln('TAK');
    Exit;
  end;

  for i:=1 to n do
    Read(p[i], k[i], c[i]);

  pn := 1;
  repeat
    j := 0;
    dalej := true;
    for i:=pn to n do
    begin
      Inc(j);
      a[j] := (p[i] shl 1) or 1;
      b[j] := i;

      Inc(j);
      a[j] := (k[i] shl 1);
      b[j] := i;
    end;
    a[j+1] := 1000001 shl 20;
    QuickSort(1, j);

    v := 0;
    mi := 0;
    for ii:=1 to j do
    begin
      if (a[ii] and 1) = 1 then
      begin
        d := (a[ii] shr 1);
        if mi = m then
          for i:=pn to n do
          begin
            if p[i] <= v then
            begin
              if (k[i] >= d) then
              begin
                Dec(c[i], d - v);
              end
              else
              begin
                if (k[i] > v) then
                begin
                  Dec(c[i], k[i] - v);
                  k[i] := d;
                end
                else
                  Inc(k[i], d - v);
              end;
              Inc(p[i], d - v);
            end
            else
            begin
              if p[i] < d then
              begin
                if (k[i] >= d) then
                begin
                  Dec(c[i], d - p[i]);
                end
                else
                begin
                  Dec(c[i], k[i] - p[i]);
                  k[i] := d;
                end;
                p[i] := d;
              end;
            end;
          end;
        Inc(mi);
      end
      else
      begin
        Dec(mi);
        if mi = m then
          v := a[ii] shr 1;
      end;
    end;
    for i:=pn to n do
    begin
      if (k[i] > v) then
      begin
        Dec(c[i], k[i] - Max(p[i], v));
        k[i] := v;
      end;
      if (c[i] <= 0) then
      begin
        dalej := false;
        if (i > pn) then
        begin
          t := p[i];
          p[i] := p[pn];
          p[pn] := t;
          t := k[i];
          k[i] := k[pn];
          k[pn] := t;
          t := c[i];
          c[i] := c[pn];
          c[pn] := t;
        end;
        Inc(pn);
      end;
    end;
  until dalej or (pn > n);

  if pn > n then
  begin
    Writeln('TAK');
    Exit;
  end;

  j := 0;
  for i:=pn to n do
  begin
    Inc(j);
    Inc(aa[p[i]]);
    a[j] := (p[i] shl  20) or (k[i]-p[i]-c[i]);
    b[j] := i;
  end;
  a[j+1] := 1000001 shl 20;
  QuickSort(1, j);

  i := 1;
  repeat
    pi := a[i] shr 20;
    t := aa[pi];
    ki := a[i+t] shr 20;
    u := aa[ki];
    v := ki - pi;
    mi := 0;
    w := v;
    for ii:=i to i+t-1 do
    begin
      if w > c[b[ii]] then
         w := c[b[ii]];
      if ii-i = m then
      begin
        if w > (a[ii] and 1048575) - (a[ii-1] and 1048575) then
          w := (a[ii] and 1048575) - (a[ii-1] and 1048575);
        if w = 0 then
          w := 1;
        if (a[ii] and 1048575) < w then
        begin
          Writeln('NIE');
          Exit;
        end
        else
          break;
      end;
    end;
    for ii:=i to i+t-1 do
    begin
      Inc(a[ii], w shl 20);
      if ii-i < m then
      begin
        if (c[b[ii]] = w) then
        begin
          a[ii] := 0;
          Inc(mi);
        end
        else
        begin
          Dec(c[b[ii]], w);
        end;
      end
      else
      begin
        if (a[ii] and 1048575) < w then
        begin
          Writeln('NIE');
          Exit;
        end;
        Dec(a[ii], w);
      end;
    end;
    Inc(aa[pi + w], t-mi);
    if (w < v) then
      QuickSort(i, i+t-1)
    else
      QuickSort(i, i+t+u-1);

    Inc(i, mi);
  until i>j;
  Writeln('TAK');
end.