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
#include<iostream>
#include<map>

using namespace std;

int main()
{
	ios_base::sync_with_stdio(false);
	int t;
	cin>>t;
	for(int i=0;i<t;i++)
	{
		bool war=1;
		map<int,long long> a;
		map<int,long long> b;
		int n;
		cin>>n;
		long long sum1=0,sum2=0;
		for(int j=0;j<n;j++)
		{
			int l,s,d;
			cin>>l>>s>>d;
			a[s]+=l;
			b[d]+=l;
			sum1+=l*s;
			sum2+=l*d;
		}
		if(sum1!=sum2)
		{
			cout<<"NIE"<<endl;
			continue;	
		}
		map<int,long long>::iterator a_i=a.begin();
		int nad=0;
		for(map<int,long long>::iterator b_i=b.begin();b_i!=b.end()&&war;b_i++)
		{
			
			while(b_i->second>0&&war)
			{
				if(a_i->second>b_i->second)
				{
					nad+=b_i->second*(b_i->first-a_i->first);
					a_i->second-=b_i->second;
					b_i->second=0;
				}
				else
				{
					nad+=a_i->second*(b_i->first-a_i->first);
					b_i->second-=a_i->second;
					a_i++;
				}
				if(nad<0){
					cout<<"NIE"<<endl;
					war=0;
				}
				//cout<<nad<<" ? ";
			}
		}
		if(war)cout<<"TAK"<<endl;
	}
	
	return 0;
}