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
#include <bits/stdc++.h>
#pragma GCC optimize "03"

using namespace std;

int gh, n, a, b, c;
pair <int, int> t[100001];
pair <int, int> o[100001];
long long nd, x, y;

int main()
{
	scanf("%d", &gh);
	for(int hg=0; hg<gh; ++hg)
	{
		scanf("%d", &n);
		x=0;
		y=0;
		for(int i=1; i<=n; ++i)
		{
			scanf("%d%d%d", &a, &b, &c);
			t[i]={b, a};
			o[i]={c, a};
			x+=(long long)a*b;
			y+=(long long)a*c;
		}
		if(x!=y)
		{
			printf("NIE\n");
			continue;
		}
		sort(t+1, t+1+n);
		sort(o+1, o+1+n);
		nd=0;
		b=n;
		for(int i=n; i; --i)
		{
			a=o[i].second;
			while(a)
			{
				if(a>=t[b].second)
				{
					a-=t[b].second;
					nd+=(long long)t[b].first*t[b].second;
					t[b].second=0;
					--b;
				}
				else
				{
					nd+=(long long)a*t[b].first;
					t[b].second-=a;
					a=0;
				}
			}
			if(nd<(long long)o[i].first*o[i].second)
			{
				printf("NIE\n");
				a=-1;
				break;
			}
			nd-=(long long)o[i].first*o[i].second;
		}
		if(a!=-1)
		{
			printf("TAK\n");
		}
	}
}