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
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//created by Albert Mosialek
#include<bits/stdc++.h>
using namespace std;


struct A{long long weight; long long temperature(){return moment/weight;}; long long moment; long long init_temp;};

A mugs[100005];
A mugsWanted[100005];
int n,t;
long long a,b,c,sumb, sumc;

bool isCoolerThanSecond(const A& firstElem, A& secondElem) {
  return firstElem.moment*secondElem.weight < secondElem.moment*firstElem.weight;
}

bool isCoolerOrEqualThanSecond(const A& firstElem, A& secondElem) {
  return firstElem.moment*secondElem.weight <= secondElem.moment*firstElem.weight;
}
A hugeBucket;

A sumMugs(A a1, A a2)
{
    A a;
    a.weight=a1.weight+a2.weight;
    //cerr<<"repair temperature division"<<endl;
    a.moment = a1.moment + a2.moment;
    return a;
}

void clearMugs(int n)
{
for(int i=0;i<n;i++){
    mugs[i].init_temp = mugs[i].moment = mugs[i].weight = 0;
    mugsWanted[i].init_temp = mugsWanted[i].moment = mugsWanted[i].weight = 0;
};
};

// bool sumNeeded(A& a1, A& a2, int temp)
// {
//     double toBeSubtracted = (a1.moment-temp*a1.weight)/(temp-???a2.temperature);
//     a1.weight += toBeSubtracted;
//     a1.moment = a1.weight*??a1.temperature;
//     a2.weight -= toBeSubtracted;
//     a2.moment = a2.weight*??a2.temperature;
//     return a2.weight>=0;
// }

// bool trySubtract(A& a1, A a2)
// {
//     a1.moment-=a2.moment;
//     a1.weight-=a2.weight;
//     return a1.weight>=0;

// }

int main()
{
    ios_base::sync_with_stdio(0);
    bool broken=0;
    int torg;
    cin>>torg;
    for(t=torg;t>0;t--)
    {
        //cerr<<torg+1-t<<endl;
        hugeBucket.init_temp=0;
        hugeBucket.moment=0;
        hugeBucket.weight=0;
        broken=0;
        sumb=0;
        sumc=0;
        cin>>n;
        //cerr<<t<<" "<<n<<endl;
        for(int n2=n-1;n2>=0;n2--)
        {
            //cerr<<"x";
            cin>>a>>b>>c;
            mugs[n2].moment = a * b;
            mugs[n2].init_temp = b;
            mugsWanted[n2].moment = a * c;
            mugsWanted[n2].init_temp = c;
            sumb += mugs[n2].moment;
            sumc += mugsWanted[n2].moment;
            //cerr<<sumb<<" "<<sumc<<endl;
            mugs[n2].weight=a;
            mugsWanted[n2].weight = a;
            //cerr<<"d";
        }
        if (sumb!=sumc)
        {
            broken=1;
            cout<<"NIE\n";
            //cerr<<"incorrect sums\n";
        }
        else
        {
            int mugsIterator = 0;
            sort(mugs, mugs+n, isCoolerThanSecond);
            sort(mugsWanted, mugsWanted+n, isCoolerThanSecond);
            // for(int i = 0; i<n;i++)
            //     {
            //         //cerr<<mugs[i].init_temp<<" "<<mugs[i].weight<<endl;
            //     }
            // //cerr<<"----------------------------------------\n";
            // for(int i = 0; i<n;i++)
            //     {
            //         //cerr<<mugsWanted[i].init_temp<<" "<<mugsWanted[i].weight<<endl;
            //     }
            //cerr<<mugs[0].init_temp<<" "<<mugsWanted[0].init_temp<<" "<<mugsWanted[n-1].init_temp<<" "<<mugs[n-1].init_temp<<endl;
            if (isCoolerThanSecond(mugsWanted[0], mugs[0]) or isCoolerThanSecond(mugs[n-1], mugsWanted[n-1]))
            {
                broken=1;
                cout<<"NIE\n";
                //cerr<<"REASON "<<isCoolerOrEqualThanSecond(mugs[0], mugsWanted[0])<<" "<<isCoolerThanSecond(mugs[n-1], mugsWanted[n-1])<<"\n";
            }
            else
            {
                for(int i=0;i<n;i++)
                {
                    A tempMug = sumMugs(hugeBucket,mugs[mugsIterator]);
                    //cerr<<"temp mug: "<<tempMug.weight<<" "<<tempMug.moment<<" "<<mugs[mugsIterator].weight<<" "<<mugs[mugsIterator].moment<<" "<<mugsIterator<<endl;
                    //cerr<<"mug wanted: "<<mugsWanted[i].weight<<" "<<mugsWanted[i].moment<<endl;
                    while(isCoolerOrEqualThanSecond(tempMug,mugsWanted[i]))
                    {
                        hugeBucket = tempMug;
                        mugsIterator++;
                        //cerr<<"a";
                        tempMug = sumMugs(hugeBucket,mugs[mugsIterator]);
                        //cerr<<"b";
                    }
                    if(hugeBucket.weight<mugsWanted[i].weight)
                    {
                        long long toBeSubstracted = mugsWanted[i].weight - hugeBucket.weight;
                        //cerr<<"I would like to subtract "<<toBeSubstracted<<" from "<<mugsIterator<<" for mug nr "<<i<<endl;
                        long long toBeSubstractedMoment = toBeSubstracted*mugs[mugsIterator].init_temp;
                        //cerr<<"I would like to subtract "<<toBeSubstractedMoment<<" from "<<mugsIterator<<endl;
                        //cerr<<"And mug "<<mugsIterator<<" is "<<mugs[mugsIterator].weight<<endl;
                        if(mugs[mugsIterator].weight<toBeSubstracted || toBeSubstractedMoment+hugeBucket.moment>mugsWanted[i].moment)
                        {
                            //cerr<<"cannot because "<< (mugs[mugsIterator].weight<toBeSubstracted) <<" "<<(toBeSubstractedMoment+hugeBucket.moment>mugsWanted[i].moment)<<"\n";
                            cout<<"NIE\n";
                            broken=1;
                            break;
                        }
                        else
                        {
                            hugeBucket.weight+=toBeSubstracted;
                            hugeBucket.moment+=toBeSubstractedMoment;
                            mugs[mugsIterator].weight-=toBeSubstracted;
                            mugs[mugsIterator].moment-=toBeSubstractedMoment;
                            if(mugs[mugsIterator].weight==0)
                                mugsIterator++;
                        }
                        
                    }    
                    hugeBucket.moment-=mugsWanted[i].moment;
                    hugeBucket.weight-=mugsWanted[i].weight;
                }
            }
            // for(int i=0;i<n;i++)
            // {
            //     A tempMug = sumMugs(hugeBucket,mugs[mugsIterator]);
            //     while(isCoolerThanSecond(tempMug,mugsWanted[i]))
            //     {
            //         hugeBucket = tempMug;
            //         mugsIterator++;
            //         tempMug = sumMugs(hugeBucket,mugs[mugsIterator]);
            //     }
            //     if(isCoolerThanSecond(hugeBucket,mugsWanted[i]))
            //     {
            //         if( true
            //         and sumNeeded(hugeBucket,mugs[mugsIterator], mugsWanted[i].temperature())
            //         and trySubtract(hugeBucket, mugsWanted[i]))
            //         {

            //         }
            //         else
            //         {
            //             cout<<"NIE\n";
            //             break;
            //         }
            //     }
            //     else
            //     {
            //         if(!trySubtract(hugeBucket, mugsWanted[i]))
            //         {
            //             cout<<"NIE\n";
            //             break;
            //         }
            //     }
                
            // }
            if(!broken)
                cout<<"TAK\n";
            clearMugs(n);
        }
    }
    return 0;
}