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

std::pair<int, int> a[100001];
std::pair<int, int> b[100001];

int main() {
	int t;
	scanf("%d", &t);

	while (t--) {
		int n;
		scanf("%d", &n);
		for (int i = 0; i < n; i++) {
			int li, ai, bi;
			scanf("%d%d%d", &li, &ai, &bi);
			a[i] = std::make_pair(ai, li);
			b[i] = std::make_pair(bi, li);
		}

		std::sort(a, a+n);
		std::sort(b, b+n);

		int i = 0;
		int j = 0;
		bool fail = false;
		long long reserve = 0;

		while (i < n) {
			int l = std::min(a[i].second, b[j].second);
			a[i].second -= l;
			b[j].second -= l;
			
			reserve += (long long) l * (b[j].first - a[i].first);
			if (reserve < 0) {
				fail = true;
				break;
			}

			if (a[i].second == 0) i++;
			if (b[j].second == 0) j++;
		}

		if (fail || reserve != 0)
			puts("NIE");
		else
			puts("TAK");
	}
}