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
#include <bits/stdc++.h>
#define ll long long
#define ff first
#define ss second
using namespace std;

vector <pair <ll, ll> > j, p;     //temperatura i ilosc
ll Tj, Tp, V;

bool deb=0;

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

    long long t, n, l, jest, powinno;
    long long Em, Ej;
    bool spr=0;
    int ij=0, ip=0;
    cin >> t;
    for(int it=0;it<t;it++)
    {
        Em=0;
        Ej=0;
        Tj=0;
        Tp=0;
        spr=0;
        j.clear();
        p.clear();
        cin >> n;
        for(int in=0; in<n; in++)
        {
            cin >> l >> jest >> powinno;
                j.push_back(make_pair(jest, l));
                p.push_back(make_pair(powinno, l));
            Ej+=l*jest;
            Em+=l*powinno;
        }

        if(Em!=Ej)
        {
            cout << "NIE\n";
            spr=1;
        }
        else
        {
            sort(j.begin(), j.end());
            sort(p.begin(), p.end());

            //cout << "pierwsze elementy " << j[0].ff << " " << j[0].ss << "\t" << p[0].ff <<  " " << p[0].ss<<endl;

            ij=0; ip=0;
            while(ij<n && ip<n)
            {
                if(deb)cout << "j = " << Tj << " p = " << Tp << endl;
                if(Tp<Tj)
                {
                    cout << "NIE\n";
                    spr=1;
                    break;
                }
                else
                {

                    if(j[ij].ss<p[ip].ss)
                    {
                        if(deb)cout << "caly j dodaj\n";
                        Tp+=p[ip].ff*j[ij].ss;
                        Tj+=j[ij].ff*j[ij].ss;

                        p[ip].ss-=j[ij].ss;
                        j[ij].ss=0;
                    }
                    else if(j[ij].ss>p[ip].ss)
                    {
                        if(deb)cout << "caly p dodaj\n";
                        Tp+=p[ip].ff*p[ip].ss;
                        Tj+=j[ij].ff*p[ip].ss;

                        j[ij].ss-=p[ip].ss;
                        p[ip].ss=0;
                    }
                    else if(j[ij].ss==p[ip].ss)
                    {
                        if(deb)cout << "dodaj obydwa\n";
                        Tp+=p[ip].ff*p[ip].ss;
                        Tj+=j[ij].ff*p[ip].ss;

                        j[ij].ss=0;
                        p[ip].ss=0;
                    }

                    if(j[ij].ss==0)
                    {
                        if(deb)cout << "kolejne j " << j[ij+1].ff << endl;
                        ij++;
                    }

                    if(p[ip].ss==0)
                    {
                        if(deb)cout << "kolejne p " << p[ip+1].ff << endl;
                        ip++;
                    }
                }
            }

            if(!spr)
            {
                cout << "TAK\n";
            }
        }
    }
}