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
#include <cstdio>
#include <utility>
#include <algorithm>
#include <functional>

#define REP(a, n) for (int a = 0; a < (n); ++a)

#define INF 1000000000

typedef long long LL;

using namespace std;

//////////////////////////

#define MAXN 100000

int N;

pair<int, int> jest[MAXN], ma_byc[MAXN];

bool solve() {
    scanf("%d", &N);
    REP(a, N) {
        scanf("%d%d%d", &jest[a].second, &jest[a].first, &ma_byc[a].first);
        ma_byc[a].second = jest[a].second;
    }
    sort(jest, jest + N, greater<>());
    sort(ma_byc, ma_byc + N, greater<>());
    LL nadmiar = 0;
    int cur_jest = 0, cur_ma_byc = 0;
    while (cur_ma_byc < N) {
        if (jest[cur_jest].second < ma_byc[cur_ma_byc].second) {
            nadmiar += jest[cur_jest].second * (LL)(jest[cur_jest].first - jest[cur_jest + 1].first);
            jest[cur_jest + 1].second += jest[cur_jest].second;
            ++cur_jest;
            continue;
        }
        nadmiar += ma_byc[cur_ma_byc].second * (LL)(jest[cur_jest].first - ma_byc[cur_ma_byc].first);
        if (nadmiar < 0)
            return false;
//        fprintf(stderr, "Tworze kubek o objetosci %d i temperaturze %d; zostaje mi %lld energii\n", ma_byc[cur_ma_byc].second, ma_byc[cur_ma_byc].first, nadmiar);
        jest[cur_jest].second -= ma_byc[cur_ma_byc].second;
        ++cur_ma_byc;
    }
    return !nadmiar;
}

int main() {
    int TT;
    scanf("%d", &TT);
    REP(tt, TT)
        printf("%s\n", solve() ? "TAK" : "NIE");
}