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
#include <bits/stdc++.h>

using namespace std;

#define int long long

const bool debug = false;

#define cerr if (!debug) {} else cout 
#define pisz(x) cerr << #x << ": " << x << endl;

const int INF = numeric_limits<int>::max() - 10;

struct Algo {
    int n;
    map<int, int> mapa;
    vector<pair<int, int> > v;
    
    Algo() {
        cin >> n;
        
        for (int l, a, b, i = 0; i < n; i++) {
            cin >> l >> a >> b;
            mapa[a] += l;
            v.push_back({b, l});
        }
        
        sort(v.begin(), v.end());
        int pocz = 0, kon = n - 1;
        
        while (pocz <= kon) {
            while (mapa.begin()->second == 0) {
                mapa.erase(mapa.begin());
            }
            
            while (mapa.rbegin()->second == 0) {
                auto it = mapa.end();
                mapa.erase(--it);
            }
            
            int pp = mapa.begin()->first;
            int kk = mapa.rbegin()->first;
            int p = v[pocz].first - pp;
            int k = kk - v[kon].first;
            
            if (p < 0 || k < 0) {
                cout << "NIE\n";
                return;
            } else if (p == 0) {
                mapa[pp] -= v[pocz].second;
                if (mapa.begin()->second < 0) {
                    cout << "NIE\n";
                    return;
                }
                pocz++;
            } else if (k == 0) {
                mapa[kk] -= v[kon].second;
                if (mapa.rbegin()->second < 0) {
                    cout << "NIE\n";
                    return;
                }
                kon--;
            } else {
                int x = min(p, k);
                int y = min(mapa.begin()->second, mapa.rbegin()->second);
                mapa[pp + x] += y;
                mapa[kk - x] += y;
                mapa[pp] -= y;
                mapa[kk] -= y;
            }
        }
        
        cout << "TAK\n";
    }
};

int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    
    int t;
    cin >> t;
    while (t--) {
        Algo p;
    }
    
    return 0;
}
/*

 */