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
#include <bits/stdc++.h>
using namespace std;

const long long inf=1e18+2137;

vector<pair<long long,long long>> x,y;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    long long r,l,a,b,mx,mn,mx2,mn2,z,n,p,p2,len;
    bool fail;
    cin>>z;
    while(z--)
    {
        cin>>n;
        r=0;
        mx=-inf;
        mn=inf;
        mx2=-inf;
        mn2=inf;
        x.clear();
        y.clear();
        while(n--)
        {
            cin>>l>>a>>b;
            r+=(a-b)*l;
            mx=max(mx,a);
            mn=min(mn,a);
            mx2=max(mx2,b);
            mn2=min(mn2,b);
            x.emplace_back(make_pair(a,l));
            y.emplace_back(make_pair(b,l));
        }
        if(r==0&&mn2>=mn&&mx2<=mx)
        {
            sort(x.begin(),x.end());
            sort(y.begin(),y.end());
            fail=false;
            p=x.size()-1;
            for(int i=x.size()-1;i>=0;i--)
            {
                p2=x[i].second;
                while(p2)
                {
                    len=min(y[p].second,p2);
                    y[p].second-=len;
                    p2-=len;
                    r+=(x[i].first-y[p].first)*len;
                    if(r<0)
                        fail=true;
                    if(!y[p].second)
                        p--;
                }
            }
            if(!fail)
                cout<<"TAK\n";
            else
                cout<<"NIE\n";
        }
        else
            cout<<"NIE\n";
    }
}