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
#include <bits/stdc++.h>

using namespace std;

const int Base = 1e6 + 7;

int t, n;
int tab[Base];
bitset<3> B;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> t;
    int x, M;
    for(int z = 0; z < t; z++) {
        cin >> n;
        for(int i = 1; i <= n; i++) {
            cin >> x;
            tab[i] = x;
        }
        if(n == 1) {
            if(tab[1] == 1) {
                cout << "TAK\n";
            } else {
                cout << "NIE\n";
            }
            continue;
        }
        for(int i = 2; i <= n; i++) {
            if(tab[i - 1] == 0 or tab[i] == 0) continue;
            if(tab[i - 1] == tab[i]) {
                tab[i] = tab[i - 1] = 1;
            } else {
                M = min(tab[i - 1], tab[i]);
                if(tab[i - 2] == 1 and tab[i - 1] < tab[i]) M--;
                tab[i - 1] -= M;
                tab[i] -= M;
            }
        }
        // cerr << z << " :\n";
        B[0] = B[1] = B[2] = 0;
        for(int i = 1; i <= n; i++) {
            // cerr << tab[i] << " ";
            if(tab[i] > 1) B[2] = 1;
            else if(tab[i] == 1 and tab[i - 1] == 0 and B[0] == 0) B[0] = 1;
            else if(tab[i] == 1 and tab[i - 1] == 0 and B[0] == 1) B[2] = 1;
            else if(tab[i] == 0 and tab[i - 1] == 1) B[1] = 1;
        }
        // cerr << "\n";
        if(B[2] == 0) {
            cout << "TAK\n";
        } else {
            cout << "NIE\n";
        }
    }
}