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
// Herbata [B]
// Jakub Rożek
#include <bits/stdc++.h>
using namespace std;

long long t,n,l,a,b,x,y;
bool f;
vector <pair<int,int> > vp;
vector <pair<int,int> > vk;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin>>t;
    while(t--)
    {
        cin>>n;
        f=0;
        vp.clear();
        vk.clear();
        for(int i=0; i<n; ++i)
        {
            cin>>l>>a>>b;
            vp.push_back({a,l});
            vk.push_back({b,l});
        }
        sort(vp.begin(),vp.end());
        sort(vk.begin(),vk.end());
        a=0;
        b=0;
        x=0;
        y=0;

        while(a<n && b<n)
        {
            l=min(vp[a].second, vk[b].second);

            x+=l*vp[a].first;
            y+=l*vk[b].first;
            vp[a].second-=l;
            vk[b].second-=l;
            if(vp[a].second==0)
                ++a;
            if(vk[b].second==0)
                ++b;

            if(x>y)
            {
                f=1;
                break;
            }
        }
        if(x!=y)
        {
            f=1;
        }

        if(f)
            cout<<"NIE\n";
        else
            cout<<"TAK\n";
    }
    return 0;
}