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


int main(){
        int t, n;
        scanf("%d", &t);
        for ( int i=0; i< t; i++){
            std::vector<long long> plus, minus;
            scanf("%d", &n);
            long long  a,b,l;
            for(int j=0; j < n; j++){
                scanf("%lld %lld %lld", &l,&a,&b);
                if ( a > b)
                        plus.push_back(l * (a-b));
                else if(b > a)
                        minus.push_back(l * (b-a));
            }
           int plus_index=plus.size() - 1;
           int minus_index=minus.size() - 1;
           long long bilans = 0;
           bool result = true;
           while( plus_index > -1 or minus_index > -1){
                   if(bilans < 0){
                       if(plus_index == -1){
                               result = false;
                               break;
                       }
                       bilans += plus[plus_index--];
                   }
                   if(bilans > 0){
                       if(minus_index == -1){
                           result = false;
                           break;
                       }
                       bilans += minus[minus_index--];
                   }
                   if(bilans == 0){
                       if(plus_index == -1 or minus_index==-1){
                               result = false;
                               break;
                       }
                       if(plus_index > -1)
                               bilans +=plus[plus_index--];
                       else
                               bilans +=minus[minus_index--];
                   }

          }
          if (result)
                printf("TAK\n");
          else
                printf("NIE\n");
        }
}