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
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>

using namespace std;

typedef pair<int, int> ii_t;
typedef vector<ii_t> vii_t;

#define MAX_N 100000

bool cmp_fst(ii_t a, ii_t b) {
    return a.first < b.first;
}

int main() {
    ios_base::sync_with_stdio(0);
    int t, n, l, a, b;
    cin >> t;
    auto akt = vii_t(MAX_N, ii_t(0, 0));
    auto target = vii_t(MAX_N, ii_t(0, 0));
    for (int i = 0; i < t; i++) {
        cin >> n;
        for (int j = 0; j < n; j++) {
            cin >> l >> a >> b;
            akt[j].first = a;
            target[j].first = b;
            akt[j].second = target[j].second = l;
        }
        sort(akt.begin(), akt.begin() + n, cmp_fst);
        sort(target.begin(), target.begin() + n, cmp_fst);

        bool ok = true;
        long long int diff = 0;
        int ai = 0, ti = 0, volume;
        while (ok and ai < n and ti < n) {
            volume = min(akt[ai].second, target[ti].second);
            akt[ai].second -= volume;
            target[ti].second -= volume;
            diff += (target[ti].first - akt[ai].first) * volume;
            ok = ok and diff >= 0;
            if (akt[ai].second == 0)
                ai += 1;
            if (target[ti].second == 0)
                ti += 1;
        }
        ok = ok and diff == 0;
        cout << (ok ? "TAK" : "NIE") << endl;
    }
    return 0;
}