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

using namespace std;

int main() {
	int T;
	scanf("%d", &T);
	for (int t = 0; t < T; ++t) {
		int n;
		scanf("%d", &n);
		vector< pair<int, int> > A;
		vector< pair<int, int> > B;
		long long int sA = 0, sB = 0;
		for (int i = 0; i < n; ++i) {
			int l, a, b;
			scanf("%d %d %d", &l, &a, &b);
			A.push_back(make_pair(a, l));
			B.push_back(make_pair(b, l));
			sA += 1LL * a * l;
			sB += 1LL * b * l;
		}
		if (sA != sB) {
			printf("NIE\n");
			continue;
		}
		sort(A.begin(), A.end());
		sort(B.begin(), B.end());
		int i = 0, j = 0;
		sA = 0; sB = 0;
		long long int lA = 0, lB = 0;
		bool r = 1;
		while (i < n && j < n) {
			// lA == lB
			lB += B[j].second;
			sB += 1LL * B[j].first * B[j].second;
			while (lA + A[i].second < lB) {
				lA += A[i].second;
				sA += 1LL * A[i].first * A[i].second;
				++i;
			}
			// lB - lA <= A[i].second
			A[i].second -= lB - lA;
			sA += 1LL * A[i].first * (lB - lA);
			if (A[i].second == 0) ++i;
			lA = lB;
			++j;
			
			if (sA > sB) {
				printf("NIE\n");
				r = 0;
				break;
			}
		}
		if (r == 1)	printf("TAK\n");
	}
	return 0;
}