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

using namespace std;

int first_discrepancy(vector<int> a, int l, int r)
{
    for (int i = l; i < r; ++i)
    {
        if (a[i] <= 0) return i - 1;
        a[i + 1] -= a[i] - 1;
    }
    if (a[r] < 0) return r - 1;
    if (a[r] > 1) return r;
    return -1;
}

bool solve(int n)
{
    vector<int> a(n + 1);
    for (int i = 1; i <= n; i++) cin >> a[i];

    int k = 1;
    while (!a[n]) --n;
    while (!a[k]) ++k;

    int d = first_discrepancy(a, k, n);
    if (d == -1) return true;

    for (int i = k + 1; i <= d; ++i) --a[i];
    if (first_discrepancy(a, k, n) == -1) return true;
    return false;
}

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

    int t, n;
    cin >> t;

    while (t--)
    {
        cin >> n;
        if (solve(n)) cout << "TAK\n";
        else cout << "NIE\n";
    }
}