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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <bits/stdc++.h>
using namespace std;

int t, n;
int i, j;
long long l[100003], a[100003], b[100003];
pair<long long, long long>oczekiwana[100003];
long long TempC;
long long litry;
int X, Y;

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

    cin>>t;

    while(t>0){
        vector<pair<int, int> >A;
        vector<pair<int, int> >B;
        X=0;
        Y=0;
        litry=0;
        TempC=0;
        cin>>n;
        for (i=1; i<=n; ++i){
            cin>>l[i]>>a[i]>>b[i];
            X=X+(l[i]*a[i]);
            Y=Y+(l[i]*b[i]);
            A.push_back(make_pair(a[i], l[i]));
            B.push_back(make_pair(b[i], l[i]));
        }

        sort(A.begin(), A.end());
        sort(B.begin(), B.end());

        oczekiwana[0].first=(B[0].first*B[0].second);
        oczekiwana[0].second=B[0].second;
        for (i=1; i<n; ++i){
            oczekiwana[i].first=oczekiwana[i-1].first+(B[i].first*B[i].second);
            oczekiwana[i].second=oczekiwana[i-1].second+B[i].second;
        }

        //for (i=0; i<n; ++i)
            //cout<<A[i].first<<" "<<A[i].second<<" "<<B[i].first<<" "<<B[i].second<<"\n";

        i=0;
        j=0;
        while(1>0){
            if(oczekiwana[j].second>litry+A[i].second){
                litry+=A[i].second;
                TempC+=(A[i].first*A[i].second);
                ++i;
            }
            else{
                TempC+=(A[i].first*(oczekiwana[j].second-litry));
                A[i].second-=oczekiwana[j].second-litry;
                litry=oczekiwana[j].second;
                //cout<<TempC<<" "<<oczekiwana[j].first<<"\n";
                if (TempC>oczekiwana[j].first){
                    X=1;
                    Y=2;
                    break;
                }
                ++j;
            }
            if (i==n-1&&j==n-1)
                break;
        }

        if (X!=Y)
            cout<<"NIE\n";
        else if (A[0].first>B[0].first)
            cout<<"NIE\n";
        else if (A[n-1].first<B[n-1].first)
            cout<<"NIE\n";
        else
            cout<<"TAK\n";


        --t;
    }

    return 0;
}