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

using namespace std;

bool check_path(int n, int k, vector<int>& current_path, int depth, int last_node, vector<int>& path_to_check) {
    // end
    if (depth == k) {
        if (equal(current_path.begin(), current_path.end(), path_to_check.begin())) {
            return true;
            // break;
            // return;
        }
        return false;
    }

    // start from each toy
    if (depth == 0) {
        for (int i = 0; i < n; i++) {
            vector<int> next = current_path;
            next[i]++;
            if (check_path(n, k, next, depth + 1, i, path_to_check)) {
                return true;
            }
        }
        return false;
    }

    vector<int> next = current_path;

    // left
    if (last_node > 0 && current_path[last_node - 1] < k) {
        next = current_path;
        next[last_node - 1]++;
        if (check_path(n, k, next, depth + 1, last_node - 1, path_to_check)) {
            return true;
        }
    }

    // right
    if (last_node < n - 1 && current_path[last_node + 1] < k) {
        next = current_path;
        next[last_node + 1]++;
        if (check_path(n, k, next, depth + 1, last_node + 1, path_to_check)) {
            return true;
        }
    }

    return false;
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        vector<int> path_to_check(n);

        for (int i = 0; i < n; i++) {
            cin >> path_to_check[i];
        }

        int sum = accumulate(path_to_check.begin(), path_to_check.end(), 0);

        vector<int> current_path(n, 0);
        bool found = check_path(n, sum, current_path, 0, -1, path_to_check);
        cout << (found ? "TAK" : "NIE") << endl;
    }

    return 0;
}