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
86
87
88
89
90
91
92
93
94
95
96
#include <stdio.h>
#include<vector>
#include<algorithm>
#include<map>

using namespace std;

int main(){
	
	int t;
	scanf("%d", &t);
	
	for(int i=0; i<t; i++){
		int n;
		scanf("%d", &n);
		
		vector < int >init_tea;
		vector < int > target_tea;
		
		map<int, int > liter_init_tea;
		map<int, int > liter_target_tea;
		
		int sum_init = 0;
		int sum_target = 0;
		
		bool is_p = true;
		
		for(int j=0; j<n; j++){
			int l, i, t;
			scanf("%d", &l);
			scanf("%d", &i);
			scanf("%d", &t);
			
			sum_init+=l*i;
			sum_target+=l*t;
			
			if( sum_init > sum_target){
				sum_init -= sum_target;
				sum_target = 0;
			}else{
				sum_target -= sum_init;
				sum_init = 0; 
			}
			
			if(liter_init_tea[i]==0){
				init_tea.push_back(i);
				liter_init_tea[i]+=l;
			}else{
				liter_init_tea[i]+=l;
			}
			
			if(liter_target_tea[t]==0){
				target_tea.push_back(t);
				liter_target_tea[t]+=l;
			}else{
				liter_target_tea[t]+=l;
			}
		}
		
		if(sum_init!=sum_target){
			is_p=false;
			//printf("srednia \n");
		}else{
			
			sort(init_tea.begin(),init_tea.end());
			sort(target_tea.begin(),target_tea.end());
					
			int front = 0;
			while(init_tea[front]==target_tea[front]and liter_init_tea[init_tea[front]] == liter_target_tea[target_tea[front]] and front!=init_tea.size()-1 and front!=target_tea.size()-1){
				front++;
			}
			if(target_tea[front] <init_tea[front] or (target_tea[front]==init_tea[front] and liter_target_tea[target_tea[front]] > liter_init_tea[init_tea[front]])){
				is_p = false;
				
				//printf("za zimny docelowy \n");
			}else{
				int end_i = init_tea.size()-1;
				int end_t = target_tea.size()-1;
				while(init_tea[end_i]==target_tea[end_t]and liter_init_tea[init_tea[end_i]] == liter_target_tea[target_tea[end_t]] and end_i>0 and end_t>0){
					end_i--;
					end_t--;
				}
				if(target_tea[end_t]>init_tea[end_i] or (target_tea[end_t]==init_tea[end_i] and liter_target_tea[target_tea[end_t]] > liter_init_tea[init_tea[end_i]])){
					is_p = false;
					//printf("za goracy docelowy \n");
				}
			}
			
		}
		if(is_p){
			printf("TAK \n");
		}else{
			printf("NIE \n");
		}
	}
}