fork download
  1. def main():
  2. T = int(input())
  3. for _ in range(T):
  4. n = int(input())
  5. a = list(map(int, input().split()))
  6.  
  7. works = True
  8. prev = 1
  9.  
  10. for x in a:
  11. if prev == 0:
  12. prev = 2 if x == 0 else 0
  13. elif prev == 1:
  14. prev = 2 if x == 0 else 1
  15. else:
  16. if x == 0:
  17. works = False
  18. else:
  19. prev = 0
  20.  
  21. if prev == 1:
  22. works = False
  23.  
  24. print("YES" if not works else "NO")
  25.  
  26. main()
Success #stdin #stdout 0.13s 14056KB
stdin
6
3
0 1 0
2
0 0
2
1 1
4
0 1 1 1
4
1 0 0 1
7
0 1 0 1 0 1 0
stdout
NO
YES
YES
NO
YES
NO