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

using namespace std;

int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        int n, l, a, b;
        long long suma = 0;
        vector<pair<int, int>> pocz, kon;
        scanf("%d", &n);
        for (int i = 1; i <= n; i++) {
            scanf("%d%d%d", &l, &a, &b);
            suma += (long long)l * (a-b);
            pocz.emplace_back(a, l);
            kon.emplace_back(b, l);
        }
        if (suma != 0) {
            puts("NIE");
            continue;
        }
        sort(pocz.begin(), pocz.end());
        sort(kon.begin(), kon.end());
        int suma_obj = 0;
        long long suma_temp = 0;
        while (!kon.empty()) {
            suma_obj += kon.back().second;
            suma_temp += (long long)kon.back().first * kon.back().second;
            kon.pop_back();
            while (suma_obj > 0) {
                if (suma_obj > pocz.back().second) {
                    suma_obj -= pocz.back().second;
                    suma_temp -= (long long)pocz.back().first * pocz.back().second;
                    pocz.pop_back();
                } else {
                    suma_temp -= (long long)pocz.back().first * suma_obj;
                    pocz.back().second -= suma_obj;
                    suma_obj = 0;
                }
            }
            if (suma_temp > 0) {
                puts("NIE");
                goto nowytest;
            }
        }
        puts("TAK");
        nowytest: ;
    }
}