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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include<bits/stdc++.h>
using namespace std;
struct kubek{
	int t,v;
};
kubek a[100000+69];
kubek b[100000+69];
bool cmp(kubek x,kubek y){
	return x.t<y.t;
}
int main(){
	int t,n,i,j,x,y;
	long long l,e;
	bool result;
	scanf("%d",&t);
	for(j=0;j<t;j++){
		scanf("%d",&n);
		for(i=0;i<n;i++){
			scanf("%d%d%d",&a[i].v,&a[i].t,&b[i].t);
			b[i].v=a[i].v;
		}
		sort(a,a+n,cmp);
		sort(b,b+n,cmp);
		l=0;
		for(i=0;i<n;i++)
			l+=a[i].v-b[i].v;
		result=(l==0);
		e=0;
		x=0;
		for(y=0;y<n;y++){
			while(l<b[y].v && x<n){
				l+=a[x].v;
				e+=(long long)a[x].v*a[x].t;
				x++;
			}
			x--;
			l-=a[x].v;
			e-=(long long)a[x].v*a[x].t;
			if((e+(long long)(b[y].v-l)*b[y].t) > (long long)b[y].t*b[y].v){
				result=false;
				break;
			}
			l+=a[x].v;
			e+=(long long)a[x].v*a[x].t;
			x++;
			l-=b[y].v;
			e-=(long long)b[y].t*b[y].v;
			if(l<0){
				result=false;
				break;
			}
		}
		l=0;
		e=0;
		x=n-1;
		for(y=n-1;y>=0;y--){
			while(l<b[y].v && x>=0){
				l+=a[x].v;
				e+=(long long)a[x].v*a[x].t;
				x--;
			}
			x++;
			l-=a[x].v;
			e-=(long long)a[x].v*a[x].t;
			if((e+(long long)(b[y].v-l)*b[y].t) < (long long)b[y].t*b[y].v){
				result=false;
				break;
			}
			l+=a[x].v;
			e+=(long long)a[x].v*a[x].t;
			x--;
			l-=b[y].v;
			e-=(long long)b[y].t*b[y].v;
			if(l<0){
				result=false;
				break;
			}
		}
		if(result)
			printf("TAK\n");
		else
			printf("NIE\n");
	}
	return 0;
}