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
#include <iostream>
#include <vector>
using namespace std;

vector<vector<int>> zad;
int t;

bool test(vector<int> z, int start, bool zer) {
	if (zer && z[start]==0 )
		return(false);
	if (zer)
		z[start]-= 1;
	if (start == z.size()-1)
		return(z[start]==0);
	if (start == z.size()-2) 
		return(z[start]==z[start+1] || z[start]==z[start+1]-1);
	if (z[start] >= z[start+1])
		return(false);
	z[start+1] -= (z[start]);
	if (!zer)
		return(test(z, start+1, false)||test(z, start+1, true));
	return(test(z, start+1, false));
	
}

int main()
{
    ios_base::sync_with_stdio(0);
    cout.tie(0);
    cin.tie(0);
	int n,k;
	cin >> t;
	for (int i = 0; i < t; i++) { 
		vector<int> v1; 
		cin >> n;
		for (int j = 0; j < n; j++) { 
			cin >> k;
			v1.push_back(k); 
		} 
		while (v1[v1.size()-1] == 0)
			v1.pop_back(); 
		zad.push_back(v1); 
	}
	
	for (auto v: zad) {
		if (test(v,0, true) || test(v,0, false))
			cout << "TAK\n";
		else
			cout << "NIE\n";
	}
	return(0);
}