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
#include <cstdio>
#include <algorithm>
#define T first
#define L second
using namespace std;
using LL = long long;
using PLL = pair<LL, LL>;

const int N = 1e5;

int n;
PLL a[N], b[N];

int main() {
  int t; scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
      scanf("%lld%lld%lld", &a[i].L, &a[i].T, &b[i].T);
      b[i].L = a[i].L;
    }
    sort(a, a+n);
    sort(b, b+n);

    LL l = 0, e = 0;
    int i = 0, j = 0;
    while (j < n) {
      while (i < n && e + a[i].L * a[i].T <= (l + a[i].L) * b[j].T) {
        l += a[i].L;
        e += a[i].L * a[i].T;
        ++i;
      }
      // printf("%lld %lld %d\n", l, e, i);
      if ((i < n && l * a[i].T - e >= b[j].L * (a[i].T - b[j].T)) || (i == n && e == l * b[j].T)) {
        l -= b[j].L;
        e -= b[j].L * b[j].T;
        ++j;
      } else break;
    }

    puts(j == n ? "TAK" : "NIE");
  }
  return 0;
}