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
#include <iostream>
#include <vector>
#include <queue>

struct State {
    int pos;                     // Pozycja zabawki
    std::vector<int> visitCount;  // Liczba odwiedzin każdej zabawki

    State(int p, const std::vector<int>& vc) : pos(p), visitCount(vc) {}
};

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int t;
    std::cin >> t;

    while (t--) {
        int n;
        std::cin >> n;
        std::vector<int> toys(n);

        for (int &toy : toys) {
            std::cin >> toy;
        }

        std::queue<State> q;

        for (int i = 0; i < n; i++) {
            if (toys[i] > 0) {
                std::vector<int> visited(n, 0);
                visited[i] = 1;
                q.emplace(i, visited);
            }
        }

        bool found = false;
        while (!q.empty()) {
            State current = q.front();
            q.pop();

            int pos = current.pos;

            bool valid = true;
            for (int j = 0; j < n; j++) {
                if (current.visitCount[j] != toys[j]) {
                    valid = false;
                    break;
                }
            }

            if (valid) {
                std::cout << "TAK\n";
                found = true;
                break;
            }

            if (pos + 1 < n) {
                State nextState = current;
                nextState.pos = pos + 1;
                nextState.visitCount[pos + 1]++;

                if (nextState.visitCount[pos + 1] <= toys[pos + 1]) {
                    q.push(nextState);
                }
            }

            if (pos - 1 >= 0) {
                State prevState = current;
                prevState.pos = pos - 1;
                prevState.visitCount[pos - 1]++;

                if (prevState.visitCount[pos - 1] <= toys[pos - 1]) {
                    q.push(prevState);
                }
            }
        }

        if (!found) {
            std::cout << "NIE\n";
        }
    }

    return 0;
}