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
#include<bits/stdc++.h>
#define mk make_pair
#define ff first
#define sc second

using namespace std;

int T,n;
vector<pair<int,int> > v1,v2;

int main()
{
	scanf("%d", &T);
	while(T--)
	{
		scanf("%d", &n);
		for(int i = 0;i<n;++i)
		{
			int a,b,c;
			scanf("%d%d%d", &a, &b, &c);
			v1.push_back(mk(b,a));
			v2.push_back(mk(c,a));
		}
		sort(v1.begin(),v1.end());
		sort(v2.begin(),v2.end());
		long long tmp2 = 0;
		long long sm1 = 0;
		long long sm2 = 0;
		long long tmp1 = 0;
		int j = 0;
		bool wyn = 1;
		for(int i = 0;i<n;++i)
		{
			tmp2 += (long long)v2[i].ff*v2[i].sc;
			sm2 += v2[i].sc;
			while(sm1<sm2)
			{
				if(sm1+v1[j].sc<sm2)
				{
					sm1 += v1[j].sc;
					tmp1 += (long long)v1[j].ff*v1[j].sc;
					v1[j].sc = 0;
					++j;
				}
				else
				{
					v1[j].sc -= sm2-sm1;
					tmp1 += (long long)v1[j].ff*(sm2-sm1);
					sm1 = sm2;
				}
			}
			if(tmp1>tmp2)
			{
				//printf("%d %d %lld %lld\n", i, j, tmp1, tmp2);
				wyn = 0;
				break;
			}
		}
		if(tmp1!=tmp2) wyn = 0;
		if(wyn)
		{
			printf("TAK\n");
		}
		else
		{
			printf("NIE\n");
		}
		v1.clear();v2.clear();
	}
}