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
#include <cstdio>
#include <vector>
#include <algorithm>

struct zadanie
{
int P,K,C;

  friend bool operator< (const zadanie& l, const zadanie& r)
  { 
    if (l.P+l.C == l.K) return false;
    if (r.P+r.C == r.K) return false;
    if (l.P != r.P) return l.P > r.P;
    if (l.C != r.C) return l.C > r.C;
    return l.K > r.K;
  }
};

using namespace std;

int main() 
{
int N,M,n,m,T_min,T,T_max;
struct zadanie z;
vector<struct zadanie> Q;
vector<struct zadanie>::iterator it_Q;
vector<struct zadanie>::reverse_iterator rit_Q;
  
    T_min = 1000000;
    T_max = -1;
    scanf("%d %d",&N,&M);
    for (n = 0; n < N; n++)
    {
      scanf("%d %d %d",&z.P,&z.K,&z.C); 
      Q.push_back(z);
      if (T_min > z.P) T_min = z.P;
      if (T_max < z.K) T_max = z.K;      
    }

//printf("%d %d %d\n",T_min,T_max,Q.size());
  for (T = T_min; T <= T_max; T++)
  {
      sort(Q.begin(),Q.end());
      m = 0;
      for (rit_Q = Q.rbegin(); rit_Q != Q.rend(); ++rit_Q)
      {
	if (rit_Q->P < T) rit_Q->P = T;
	if (rit_Q->P == T && rit_Q->K - rit_Q->P < rit_Q->C)
	{
	  printf("NIE\n");
	  return 0;
	}
	if (rit_Q->P == T && rit_Q->K - rit_Q->P == rit_Q->C)
	{
//	 printf("must! %d %d %d\n",rit_Q->P,rit_Q->K,rit_Q->C);
	 m++;
	 rit_Q->C--;
	 if (rit_Q->C == 0) Q.pop_back();
	}
	else if (rit_Q->P == T && m < M)
	{
//	 printf("may! %d %d %d\n",rit_Q->P,rit_Q->K,rit_Q->C);
	 m++;
	 rit_Q->C--;
	 if (rit_Q->C == 0) Q.pop_back();
	}
      }
      if (m > M)
      {
	printf("NIE\n");
	return 0;
      }
    }
    
  printf("TAK\n");
  
  return 0;
}