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
#include <iostream>

using namespace std;
int main()
{
    int t,n,l,a,mint,maxt;
    long long int cw,cwc;
    bool f;
    cin >> t;
    for (;t>0;t--){
        cin >> n;
        long long int tab[100001]={};
        cw=0,cwc=0,mint=1000001,maxt=0;
        for (int i=1; n>=i;i++){
            cin >> l;
            cin >> a;
            tab[a]+=l;
            if (a>maxt) maxt=a;
            if (a<mint) mint=a;
            cw+=a*l;
            cin >> a;
            if (a>maxt) maxt=a;
            if (a<mint) mint=a;
            tab[a]-=l;
            cwc+=a*l;
        }
        f=0;
        if (cw!=cwc) {
            cout << "NIE \n";
            f=1;
        }
        else for (int i=maxt; i>mint+1; i--){
            if (tab[i]>0){
                tab[i-1]+=2*tab[i];
                tab[i-2]-=tab[i];
                tab[i]=0;
            }
            else if (tab[i]<0){
                cout << "NIE \n";
                f=1;
                break;
            }
        }
        if (!f) cout << "TAK \n";
    }
    return 0;
}