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

using namespace std;

using ll = long long;

struct Cup {
  int l, a, b, s = 0;
  Cup(int _l, int _a, int _b) : l(_l), a(_a), b(_b) {}
};

int main() {
  ios_base::sync_with_stdio(0);
  int t;
  cin >> t;
  auto test = []() {
    int n;
    cin >> n;
    vector <Cup> cups;
    for (int i=0; i<n; i++) {
      int l, a, b;
      cin >> l >> a >> b;
      cups.emplace_back(l, a, b);
    }
    vector <int> pa, pb;
    for (int i=0; i<n; i++) {
      pa.push_back(i);
      pb.push_back(i);
    }
    sort(pa.begin(), pa.end(), [&](int p, int q){
      return cups[p].a < cups[q].a;
    });
    sort(pb.begin(), pb.end(), [&](int p, int q){
      return cups[p].b < cups[q].b;
    });
    ll volume = 0, temp = 0;
    for (int p1=0, p2=0; p1<n; p1++) {
      volume += cups[pb[p1]].l;
      temp += 1LL * cups[pb[p1]].l * cups[pb[p1]].b;
      while(p2 < n) {
        ll change = min(volume, (ll)(cups[pa[p2]].l - cups[pa[p2]].s));
        cups[pa[p2]].s += change;
        volume -= change;
        temp -= change * cups[pa[p2]].a;
        if(cups[pa[p2]].l == cups[pa[p2]].s) p2++;
        if(volume == 0) break;
      }
      if (temp < 0) return false;
    }
    return (temp == 0);
  };
  while (t--) puts(test() ? "TAK" : "NIE");
}