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 <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

int t, n, l, a, b;
long long suma;

vector < pair <int, int> > F, S;

int main ()
{
	scanf("%d", &t);
	while (t--)
	{
		F.clear();
		S.clear();
		suma = 0;
		scanf("%d", &n);
		for (int i = 0; i < n; ++i)
		{
			scanf("%d%d%d", &l, &a, &b);
			F.push_back(make_pair(a, l));
			S.push_back(make_pair(b, l));
		}
		sort(F.begin(), F.end());
		sort(S.begin(), S.end());
		for (int i = 0, j = 0; i < n; )
		{
	 	 	l = min(F[i].second, S[j].second);
	 	 	suma += (long long)(l) * (long long)(S[j].first - F[i].first);
	 	 	if (suma < 0) break;
	 	 	if (F[i].second == l) i++;
	 	 	else F[i].second -= l;
	 	 	if (S[j].second == l) j++;
	 	 	else S[j].second -= l;
		}
		if (suma == 0) printf("TAK\n");
		else printf("NIE\n");
	}
	return 0;
}