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
69
70
71
#include <iostream>
#include <vector>
#include <cstdio>
#include <algorithm>

using namespace std;

typedef long long LL;

vector<pair<int, int> > S;
vector<pair<int, int> > T;

int main()
{
    int t,n;

    scanf("%d", &t);
    while(t--) {
        int v,a,b;
        LL SS = 0, TS = 0;
        scanf("%d",&n);
        S.clear();
        T.clear();
        for (int i=0;i<n;i++) {
            scanf("%d%d%d",&v,&a,&b);
            SS += (LL)v*a;
            TS += (LL)v*b;
            S.push_back(make_pair(a,v));
            T.push_back(make_pair(b,v));
        }
        if (SS != TS) {
            printf("NIE\n");
            continue;
        }
        sort(S.begin(), S.end());
        sort(T.begin(), T.end());


        int ok=1;
        int w = n-1;
        LL sps = 0, tps = 0;
        LL stw = 0, ttw = 0;
        for (int i=n-1;i>=0;i--) {
            ttw += T[i].second;
            tps += (LL)T[i].first * T[i].second;
            while(w >=0 && stw != ttw) {
                int cwa = S[w].first;
                int cwb = S[w].second;
                if (stw + cwb > ttw) {
                    LL r = ttw - stw;
                    sps +=  r*cwa;
                    S[w].second -= r;
                    stw = ttw;
                } else {
                    sps += (LL)cwa*cwb;
                    stw += cwb;
                    w--;
                }
            }
            if (sps < tps) {
                printf("NIE\n");
                ok = 0;
                break;
            }
        }
        if (ok) {
            printf("TAK\n");
        }
    }
    return 0;
}