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
#include <bits/stdc++.h>
#define s second
#define f first
#define mp make_pair

typedef long long ll;

using namespace std;

int zestawy, n;
ll a, b, l;
pair<ll, ll> tc[100005], tw[100005];

bool spr()
{
    ll sumac = 0, sumaw = 0;
    int cliter = 0, it = 1, wliter = 0;
    for(int i = 1; i<=n; i++)
    {
        sumaw += tw[i].s * tw[i].f;
        wliter += tw[i].s;
        while(cliter < wliter){
            if(it > n) return false;
            if(cliter + tc[it].s <= wliter)
            {
                cliter += tc[it].s;
                sumac += tc[it].s * tc[it].f;
                it++;
            }
            else
            {  
                int temp = wliter - cliter;
                sumac += temp * tc[it].f;
                tc[it].s -= temp;
                cliter = wliter;
            }
            
        }
        if(sumac > sumaw) return false;
        //cout << sumaw << " " << sumac << "\n";
     }
     if(sumac != sumaw || it <= n) return false;
     return true;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> zestawy;
    while(zestawy--){
        cin >> n;
        for(int i = 1; i<=n; i++)
        {
            cin >> l >> a >> b;
            tc[i] = mp(a, l);
            tw[i] = mp(b, l);
        }
        sort(tc + 1, tc + n + 1);
        sort(tw + 1, tw + n + 1);
        /*for(int i = 1; i<=n; i++)
        {
            cout << "cur: " << tc[i].f << " " << tc[i].s << " --- res: " << tw[i].f << " " << tw[i].s << "\n";
        }*/
        if(spr()) cout << "TAK\n"; else cout << "NIE\n";
    }
}