#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ free(int *);
scalar_t__ isArrayBalanced(int *, int);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ scanf(char *, int *);
int main() {
  int t, n, i, j, k;
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    int *arr = (int *)malloc(sizeof(int) * n);
    for (i = 0; i < n; i++) {
      scanf("%d", &arr[i]);
    }
    if (isArrayBalanced(arr, n)) {
      printf("YES\n");
    } else {
      printf("NO\n");
    }
    free(arr);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe9a(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1060 <scanf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x8(%rbp)
cmp    $0x0,%eax
je     1223 <main+0xd3>
lea    0xe76(%rip),%rdi
lea    -0xc(%rbp),%rsi
call   1060 <scanf@plt>
movslq -0xc(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11e2 <main+0x92>
mov    -0x20(%rbp),%rsi
movslq -0x10(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
lea    0xe31(%rip),%rdi
call   1060 <scanf@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11ad <main+0x5d>
mov    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1070 <isArrayBalanced@plt>
cmp    $0x0,%rax
je     1209 <main+0xb9>
lea    0xe04(%rip),%rdi
call   1040 <printf@plt>
jmp    1215 <main+0xc5>
lea    0xdf8(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
jmp    116f <main+0x1f>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe9e(%rip),%rdi
lea    0x4(%rsp),%rsi
call   1060 <scanf@plt>
subl   $0x1,0x4(%rsp)
jae    1184 <main+0x34>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe75(%rip),%r12
lea    0xe71(%rip),%r13
lea    0xe6d(%rip),%rbp
jmp    11c6 <main+0x76>
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1070 <isArrayBalanced@plt>
test   %rax,%rax
mov    %rbp,%rdi
cmove  %r13,%rdi
call   1040 <puts@plt>
mov    %r15,%rdi
call   1030 <free@plt>
subl   $0x1,0x4(%rsp)
jb     1173 <main+0x23>
mov    %r12,%rdi
mov    %rsp,%rsi
call   1060 <scanf@plt>
mov    (%rsp),%edi
shl    $0x2,%edi
call   1050 <malloc@plt>
mov    %rax,%r15
mov    (%rsp),%esi
test   %esi,%esi
jle    11a0 <main+0x50>
mov    %r15,%rbx
xor    %r14d,%r14d
nopl   0x0(%rax)
mov    %r12,%rdi
mov    %rbx,%rsi
call   1060 <scanf@plt>
add    $0x1,%r14
movslq (%rsp),%rsi
add    $0x4,%rbx
cmp    %rsi,%r14
jl     11f0 <main+0xa0>
jmp    11a0 <main+0x50>

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [5 x i8] c"YES\0A\00"
@.str.2 = [4 x i8] c"NO\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
store i32 0, i32* %1
%8 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %2)
br label %9
9: ; preds = %43, %0
%10 = load i32, i32* %2
%11 = add nsw i32 %10, -1
store i32 %11, i32* %2
%12 = icmp ne i32 %10, 0
br i1 %12, label %13, label %46
13: ; preds = %9
%14 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %3)
%15 = load i32, i32* %3
%16 = sext i32 %15 to i64
%17 = mul i64 4, %16
%18 = trunc i64 %17 to i32
%19 = call i64 @malloc(i32 %18)
%20 = inttoptr i64 %19 to i32*
store i32* %20, i32** %7
store i32 0, i32* %4
br label %21
21: ; preds = %31, %13
%22 = load i32, i32* %4
%23 = load i32, i32* %3
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %34
25: ; preds = %21
%26 = load i32*, i32** %7
%27 = load i32, i32* %4
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i32, i32* %26, i64 %28
%30 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %29)
br label %31
31: ; preds = %25
%32 = load i32, i32* %4
%33 = add nsw i32 %32, 1
store i32 %33, i32* %4
br label %21
34: ; preds = %21
%35 = load i32*, i32** %7
%36 = load i32, i32* %3
%37 = call i64 @isArrayBalanced(i32* %35, i32 %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %34
%40 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %43
41: ; preds = %34
%42 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
br label %43
43: ; preds = %41, %39
%44 = load i32*, i32** %7
%45 = call i32 @free(i32* %44)
br label %9
46: ; preds = %9
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@str = [3 x i8] c"NO\00"
@str.3 = [4 x i8] c"YES\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %1)
%6 = load i32, i32* %1
%7 = add nsw i32 %6, -1
store i32 %7, i32* %1
%8 = icmp eq i32 %6, 0
br i1 %8, label %35, label %9
9: ; preds = %0, %25
%10 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %2)
%11 = load i32, i32* %2
%12 = shl i32 %11, 2
%13 = call i64 @malloc(i32 %12)
%14 = inttoptr i64 %13 to i32*
%15 = load i32, i32* %2
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %25
17: ; preds = %9, %17
%18 = phi i64 [ %21, %17 ], [ 0, %9 ]
%19 = getelementptr inbounds i32, i32* %14, i64 %18
%20 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %19)
%21 = add nuw nsw i64 %18, 1
%22 = load i32, i32* %2
%23 = sext i32 %22 to i64
%24 = icmp slt i64 %21, %23
br i1 %24, label %17, label %25
25: ; preds = %17, %9
%26 = phi i32 [ %15, %9 ], [ %22, %17 ]
%27 = call i64 @isArrayBalanced(i32* %14, i32 %26)
%28 = icmp eq i64 %27, 0
%29 = select i1 %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @str.3, i64 0, i64 0)
%30 = call i32 @puts(i8* dereferenceable(1) %29)
%31 = call i32 @free(i32* %14)
%32 = load i32, i32* %1
%33 = add nsw i32 %32, -1
store i32 %33, i32* %1
%34 = icmp eq i32 %32, 0
br i1 %34, label %35, label %9
35: ; preds = %25, %0
ret i32 0
}