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
#include <iostream>
#include <queue>

using namespace std;

bool solve()
{
    int n,l,a,b;
    long long sum1 = 0;
    long long sum2 = 0;
    priority_queue<int> actual;
    priority_queue<int> wanted;
    cin >> n;
    while(n--)
    {
        cin >> l >> a >> b;
        while(l--)
        {
            actual.push(a);
            wanted.push(b);
            sum1+=a;
            sum2+=b;
        }
    }
    if(sum1 != sum2)
    {
        return false;
    }
    while(!wanted.empty())
    {
        int a = actual.top(); actual.pop();
        int w = wanted.top(); wanted.pop();
        cout <<a<<" | "<<w<<endl;
        if(w > a)
        {
            return false;
        }
        else if(a > w)
        {
            wanted.push((2*w) - a);
            actual.push(w);
        }
    }
    return true;
}

int main()
{
    int z;
    cin >>z;
    while(z--)
    {
        cout << ((solve()) ? "TAK" : "NIE") <<endl;
    }
}