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

#define int long long
#define pi pair<long long, long long>
#define a3 array<long long, 3>

#define meow main

using namespace std;

void display_res(bool res) { cout << (res ? "TAK" : "NIE") << '\n'; }

void solve() {
  int n;
  cin >> n;
  vector<int> A(n);
  for (auto &a : A) {
    cin >> a;
  }

  int left = 0;
  for (; A[left] == 0; left++);
  
  auto check = [=](int i, vector<int> V) {
    int sum = 0;
    for(auto v : V) sum += v;

    while(i+1 < n) {
      V[i+1] -= V[i];
      sum -= 2*V[i];
      V[i] = 0;
      if(sum == 0 && V[i+1] == 0) return true;
      V[i+1]--;
      sum--;
      if(V[i+1] < 0) return false;
      i++;
    }
    if(sum != 0) return false;
    for(auto v : V) if(v != 0) return false;
    return true;
  };

  A[left]--;
  auto copy = A;
  if(check(left, copy)) {
    display_res(true);
    return;
  }
  if(left + 1 < n && A[left+1] > 0) {
    A[left+1]--;
    display_res(check(left, A));
  } else display_res(false);
}

signed meow() {
  cin.tie((ostream *)!ios::sync_with_stdio(false));

  int satori_moment;
  cin >> satori_moment;
  while (satori_moment--)
    solve();

  return 0;
}