Chef owns an icecream shop in Chefland named scoORZ. There are only three types of coins in Chefland: Rs. 5, Rs. 10 and Rs. 15. An icecream costs Rs. 5.
There are $N$ people (numbered $1$ through $N$) standing in a queue to buy icecream from scoORZ. Each person wants to buy exactly one icecream. For each valid $i$, the $i$-th person has one coin with value $a_i$. It is only possible for someone to buy an icecream when Chef can give them back their change exactly ― for example, if someone pays with a Rs. 10 coin, Chef needs to have a Rs. 5 coin that he gives to this person as change.
Initially, Chef has no money. He wants to know if he can sell icecream to everyone in the queue, in the given order. Since he is busy eating his own icecream, can you tell him if he can serve all these people?

-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single integer $N$.
- The second line contains $N$ space-separated integers $a_1, a_2, \ldots, a_N$.

-----Output-----
For each test case, print a single line containing the string "YES" if all people can be served or "NO" otherwise (without quotes).

-----Constraints-----
- $1 \le T \le 100$
- $1 \le N \le 10^3$
- $a_i \in \{5, 10, 15\}$ for each valid $i$

-----Subtasks-----
Subtask #1 (40 points): $a_i \in \{5, 10\}$ for each valid $i$
Subtask #2 (60 points): original constraints

-----Example Input-----
3
2
5 10
2
10 5
2
5 15

-----Example Output-----
YES
NO
NO

-----Explanation-----
Example case 1: The first person pays with a Rs. 5 coin. The second person pays with a Rs. 10 coin and Chef gives them back the Rs. 5 coin (which he got from the first person) as change.
Example case 2: The first person already cannot buy an icecream because Chef cannot give them back Rs. 5.
Example case 3: The first person pays with a Rs. 5 coin. The second person cannot buy the icecream because Chef has only one Rs. 5 coin, but he needs to give a total of Rs. 10 back as change.

Source: codechef | Difficulty: EASY
Tags: Mathematics


Solutions:
t = int(input())
for tc in range(t):
	n = int(input())
	l = list(map(int, input().split(' ')))
	cnt = {5: 0, 10: 0}
	flag = 0
	for i in l:
		if i == 5:
			cnt[5] += 1
		elif i == 10:
			cnt[5] -= 1
			cnt[10] += 1
		elif cnt[10] >= 1:
			cnt[10] = cnt[10] - 1
		else:
			cnt[5] -= 2
		if cnt[5] < 0 or cnt[10] < 0:
			print('NO')
			flag = 1
			break
	if flag == 0:
		print('YES')


for i in range(int(input())):
	n = int(input())
	x = list(map(int, input().split()))
	change = []
	try:
		for i in x:
			if i == 5:
				change.append(i)
			elif i == 10:
				change.append(i)
				change.remove(5)
			elif i == 15:
				change.append(i)
				try:
					change.remove(10)
				except:
					change.remove(5)
					change.remove(5)
	except:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	coins = list(map(int, input().split()))
	rem = [0, 0, 0]
	flag = True
	for c in coins:
		change = c - 5
		tens = change // 10
		change = change % 10
		fives = change // 5
		if rem[0] >= fives and rem[1] >= tens:
			rem[0] -= fives
			rem[1] -= tens
		elif rem[0] >= tens * 2 + fives:
			rem[1] -= 2
		else:
			flag = False
			break
		rem[c // 5 - 1] += 1
	print('YES' if flag else 'NO')


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	(f, t, fif) = (0, 0, 0)
	for i in l:
		if i == 5:
			f += 1
		elif i == 10:
			t += 1
			f -= 1
		else:
			fif += 1
			if t > 0:
				t -= 1
			else:
				f -= 2
		if f < 0 or t < 0 or fif < 0:
			print('NO')
			break
	else:
		print('YES')


for i in range(int(input())):
	n = int(input())
	li = list(map(int, input().split()))
	five = 0
	ten = 0
	ans = 0
	for i in li:
		if i == 5:
			five += 1
		elif i == 10:
			ten += 1
			if five >= 1:
				five -= 1
			else:
				ans += 1
				break
		elif ten >= 1:
			ten -= 1
		elif five >= 2:
			five -= 2
		else:
			ans += 1
			break
	if ans == 0:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	l1 = []
	z = 0
	for j in l:
		if j == 5:
			l1 += [j]
		elif j == 10:
			if 5 not in l1:
				z = 1
				print('NO')
				break
			else:
				l1 += [j]
				l1.remove(5)
		elif 10 not in l1:
			if l1.count(5) < 2:
				z = 1
				print('NO')
				break
			else:
				l1 += [j]
				l1.remove(5)
				l1.remove(5)
		else:
			l1 += [j]
			l1.remove(10)
	if z == 0:
		print('YES')


def f(n, arr):
	val = 0
	(c1, c2, c3) = (0, 0, 0)
	for i in range(n):
		if arr[i] == 5:
			c1 += 1
		elif arr[i] == 10:
			c2 += 1
		else:
			c3 += 1
		tbr = arr[i] - 5
		if tbr == 5:
			if c1 >= 1:
				c1 -= 1
			else:
				return False
		if tbr == 10:
			if c2 >= 1:
				c2 -= 1
			elif c1 >= 2:
				c1 -= 2
			else:
				return False
	return True
for i in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = f(n, arr)
	if ans:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	nums = [int(j) for j in input().split()]
	(yes, five, ten) = (1, 0, 0)
	for j in range(n):
		if nums[j] == 15:
			if ten > 0 and five > 0:
				ten -= 1
				five -= 1
			elif five > 1:
				five -= 2
			else:
				yes = 0
		elif nums[j] == 10:
			ten += 1
			if five > 0:
				five -= 1
			else:
				yes = 0
		else:
			five += 1
	if yes == 1:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	counter = [0, 0, 0]
	flag = False
	for i in arr:
		if i == 5:
			counter[0] += 1
		elif i == 10:
			counter[1] += 1
			counter[0] -= 1
		else:
			counter[2] += 1
			if counter[1] > 0:
				counter[1] -= 1
			else:
				counter[0] -= 2
		if counter[0] < 0 or counter[1] < 0:
			flag = True
	print('NO') if flag else print('YES')


for tc in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	(ni, d, f) = (0, 0, 0)
	ans = 'YES'
	for i in a:
		if i == 5:
			ni += 1
		elif i == 10:
			d += 1
			ni -= 1
		elif i == 15:
			f += 1
			if d > 0:
				d -= 1
			elif ni > 1:
				ni -= 2
			else:
				ans = 'NO'
		if ni < 0 or d < 0:
			ans = 'NO'
	print(ans)


a = int(input(''))
for i in range(a):
	x = int(input(''))
	t = [int(x) for x in input('').split()]
	k = list(t)
	(f, t, fi) = (0, 0, 0)
	for j in k:
		if j == 5:
			f = f + 1
		elif j == 10:
			t = t + 1
			f = f - 1
		else:
			fi = fi + 1
			if t > 0:
				t = t - 1
			else:
				f = f - 2
		if f < 0 or t < 0 or fi < 0:
			print('NO')
			break
	else:
		print('YES')


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	(a, b, c) = (0, 0, 0)
	for i in arr:
		if i == 5:
			a += 1
		elif i == 10:
			if a >= 1:
				b += 1
				a -= 1
			else:
				print('NO')
				break
		elif b >= 1:
			c += 1
			b -= 1
		elif a >= 2:
			c += 1
			a -= 2
		else:
			print('NO')
			break
	else:
		print('YES')


T = int(input())
for tc in range(T):
	N = int(input())
	L = list(map(int, input().split(' ')))
	cnt = {5: 0, 10: 0}
	flag = 0
	for i in L:
		if i == 5:
			cnt[5] += 1
		elif i == 10:
			cnt[5] -= 1
			cnt[10] += 1
		elif cnt[10] >= 1:
			cnt[10] -= 1
		else:
			cnt[5] -= 2
		if cnt[5] < 0 or cnt[10] < 0:
			print('NO')
			flag = 1
			break
	if flag == 0:
		print('YES')


t = int(input())
for tc in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	x = a[0]
	y = [a[0]]
	if a[0] != 5:
		print('NO')
		continue
	for j in a[1:]:
		if j - 5 in y and j != 5:
			y.remove(j - 5)
			y.append(j)
		elif y.count(j - 10) >= 2 and j != 5:
			y.remove(j - 10)
			y.remove(j - 10)
			y.append(j)
		elif j == 5:
			y.append(j)
		else:
			print('NO')
			break
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d = {5: 0, 10: 0}
	for i in l:
		if i == 5:
			d[5] += 1
		elif i == 10:
			d[10] += 1
			if d[5] > 0:
				d[5] -= 1
			else:
				print('NO')
				break
		elif d[10] > 0:
			d[10] -= 1
		elif d[5] > 1:
			d[5] -= 2
		else:
			print('NO')
			break
	else:
		print('YES')


n = int(input())
while n > 0:
	t = int(input())
	d = {5: 0, 10: 0, 15: 0}
	arr = list(map(int, input().split()))
	for temp in arr:
		if d[5] < 0:
			print('NO')
			break
		if temp == 5:
			d[temp] += 1
		elif temp == 10:
			if d[5] >= 1:
				d[10] += 1
				d[5] -= 1
			else:
				print('NO')
				break
		elif temp == 15:
			if d[10] >= 1:
				d[10] -= 1
			elif d[5] >= 2:
				d[5] -= 2
				d[15] += 1
			else:
				print('NO')
				break
		else:
			print('NO')
			break
	else:
		print('YES')
	n -= 1


tests = int(input())
for i in range(tests):
	length = int(input())
	arr = list(map(int, input().split()))
	fives = 0
	tens = 0
	ans = 'YES'
	for j in arr:
		if j == 5:
			fives += 1
		elif j == 10:
			tens += 1
			if fives > 0:
				fives -= 1
			else:
				ans = 'NO'
				break
		elif tens > 0:
			tens -= 1
		elif fives > 1:
			fives -= 2
		else:
			ans = 'NO'
			break
	print(ans)


for _ in range(int(input())):
	n = int(input())
	l = [int(x) for x in input().split()]
	five = 0
	ten = 0
	fifteen = 0
	flag = 1
	for i in l:
		if i == 10:
			if five > 0:
				ten += 1
				five -= 1
			else:
				flag = 0
		elif i == 15:
			fifteen += 1
			if ten > 0:
				ten -= 1
			elif five > 1:
				five -= 2
			else:
				flag = 0
		else:
			five += 1
		if flag == 0:
			print('NO')
			break
	if flag == 1:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	(x, y, z) = (0, 0, 0)
	for i in range(n):
		if a[i] == 5:
			x += 1
			y += 1
		elif a[i] == 10 and x >= 1:
			z += 1
			x = x - 1
			y += 1
		elif a[i] == 15:
			if z >= 1:
				z = z - 1
				y += 1
			elif x >= 2:
				x = x - 2
				y += 1
			else:
				break
	if y == n:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	money_with_chef = []
	ans = 'YES'
	for j in range(len(a)):
		if a[j] - 5 == 0:
			money_with_chef.append(a[j])
		elif a[j] - 5 == 5 and 5 not in money_with_chef:
			ans = 'NO'
			break
		elif a[j] - 5 == 5 and 5 in money_with_chef:
			money_with_chef.remove(5)
			money_with_chef.append(a[j])
		elif (a[j] - 5 == 10 and 10 not in money_with_chef) and (a[j] - 5 == 10 and 5 not in money_with_chef):
			ans = 'NO'
			break
		elif a[j] - 5 == 10 and 10 in money_with_chef:
			money_with_chef.remove(10)
			money_with_chef.append(a[j])
		elif (a[j] - 5 == 10 and 10 not in money_with_chef) and (a[j] - 5 == 10 and 5 in money_with_chef):
			if money_with_chef.count(5) >= 2:
				money_with_chef.remove(5)
				money_with_chef.remove(5)
				money_with_chef.append(a[j])
			else:
				ans = 'NO'
				break
	print(ans)


s = int(input())
for i in range(0, s, 1):
	n = int(input())
	arr = list(map(int, input().split()))
	ten = 0
	five = 0
	r = 1
	for j in range(0, n, 1):
		if arr[j] == 5:
			five += 1
		elif arr[j] == 10:
			ten += 1
			if five > 0:
				five = five - 1
			else:
				r = 0
				break
		elif arr[j] == 15:
			if ten > 0:
				ten = ten - 1
			elif five > 1:
				five = five - 2
			else:
				r = 0
				break
	if r == 1:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))[:n]
	c = []
	ans = True
	for i in range(n):
		if a[i] - 5 == 0:
			c.append(5)
		elif a[i] - 5 == 5:
			if c.count(5):
				c.remove(5)
				c.append(10)
			else:
				ans = False
				break
		elif c.count(5) >= 2 or c.count(10):
			if c.count(10):
				c.remove(10)
			elif c.count(5) >= 2:
				c.remove(5)
				c.remove(5)
		else:
			ans = False
			break
	if ans:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().strip().split()))
	tens = 0
	fives = 1
	if a[0] > 5:
		print('NO')
		continue
	flag = True
	for i in range(1, n):
		if a[i] == 15:
			if tens == 0:
				if fives < 2:
					flag = False
					break
				else:
					fives -= 2
			else:
				tens -= 1
		elif a[i] == 10:
			if fives >= 1:
				fives -= 1
			else:
				flag = False
				break
			tens += 1
		elif a[i] == 5:
			fives += 1
	if flag:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	total = 0
	for i in arr:
		if i == 5:
			total += i
		elif total == 0:
			print('NO')
			break
		else:
			to_return = i - 5
			if total < to_return:
				print('NO')
				break
			total += 5 - to_return
	else:
		print('YES')


for _ in range(int(input().strip())):
	N = int(input().strip())
	A = list(map(int, input().strip().split(' ')))
	coins = [0, 0, 0]
	f = 1
	for i in A:
		if i == 5:
			coins[0] += 1
		elif i == 10 and coins[0] > 0:
			coins[0] -= 1
		elif i == 15:
			if coins[1] > 1:
				coins[1] -= 1
			elif coins[1] > 1:
				coins[1] -= 2
			else:
				f = 0
				print('NO')
				break
		else:
			f = 0
			print('NO')
			break
	if f:
		print('YES')


T = int(input())
for _ in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	Fives = 0
	Flag = True
	Tens = 0
	for i in A:
		if i == 5:
			Fives = Fives + 1
		elif i == 10:
			if Fives > 0:
				Tens = Tens + 1
				Fives = Fives - 1
			else:
				Flag = False
		elif Tens > 0:
			Tens = Tens - 1
		elif Fives > 1:
			Fives = Fives - 2
		else:
			Flag = False
			break
	if Flag == True:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	s = []
	a = 0
	if l[0] == 5:
		for i in range(len(l)):
			if l[i] == 5:
				s.append(5)
				a = 1
			elif l[i] == 10:
				if 5 in s:
					s.remove(5)
					s.append(10)
					a = 1
				else:
					a = 0
					break
			elif 10 in s:
				s.remove(10)
				a = 1
			elif s.count(5) >= 2:
				s.remove(5)
				s.remove(5)
				a = 1
			else:
				a = 0
				break
	else:
		a = 0
	if a == 1:
		print('YES')
	else:
		print('NO')


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	k = []
	if a[0] == 5:
		a.pop(0)
		k = [5]
		for i in a:
			if i == 5:
				k.append(i)
			elif i == 10:
				k.append(i)
				if 5 in k:
					k.remove(5)
				else:
					print('NO')
					break
			elif i == 15:
				if 10 in k:
					k.remove(10)
				elif k.count(5) >= 2:
					k.remove(5)
					k.remove(5)
				else:
					print('NO')
					break
		else:
			print('YES')
	else:
		print('NO')


import sys

def hasKey(dic, key):
	return key in dic.keys()
cases = int(sys.stdin.readline().rstrip())
for case in range(cases):
	N = int(sys.stdin.readline().rstrip())
	p = list(map(int, sys.stdin.readline().rstrip().split(' ')))
	change = 0
	error = False
	for pi in p:
		if pi - 5 <= change:
			change = change - (pi - 5)
			change += 5
		else:
			error = True
			break
	if error:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	(c_5, c_10, c_15) = (0, 0, 0)
	for i in a:
		if i == 5:
			c_5 += 1
		elif i == 10:
			c_10 += 1
			c_5 -= 1
		else:
			c_15 += 1
			if c_10 > 0:
				c_10 -= 1
			else:
				c_5 -= 2
		if c_5 < 0 or c_10 < 0 or c_15 < 0:
			print('NO')
			break
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	coins = list(map(int, input().split()))
	cashier = {5: 0, 10: 0}
	boo = False
	for coin in coins:
		if coin == 5:
			cashier[5] += 1
		elif coin == 10:
			if cashier[5] > 0:
				cashier[5] -= 1
				cashier[10] += 1
			else:
				boo = True
				break
		elif cashier[10] > 0:
			cashier[10] -= 1
		elif cashier[5] >= 2:
			cashier[5] -= 2
		else:
			boo = True
			break
	if boo:
		print('NO')
	else:
		print('YES')


t = int(input())
for i in range(0, t):
	cnt5 = 0
	cnt10 = 0
	found = 0
	n = int(input())
	li = list(map(int, input().split()))
	for k in li:
		if k == 5:
			cnt5 += 1
		elif k == 10:
			cnt5 -= 1
			cnt10 += 1
		elif k == 15:
			if cnt10 == 0:
				cnt5 -= 2
			else:
				cnt10 -= 1
		if cnt5 < 0 or cnt10 < 0:
			print('NO')
			found = 1
			break
	if found != 1:
		print('YES')


for _ in range(int(input())):
	N = int(input())
	inp_arr = tuple((int(i) for i in input().strip().split()))
	chefCoins = {5: 0, 10: 0}
	printStr = 'YES'
	for i in inp_arr:
		if i == 5:
			chefCoins[5] += 1
		elif i == 10:
			chefCoins[10] += 1
			if chefCoins[5] > 0:
				chefCoins[5] -= 1
			else:
				printStr = 'NO'
				break
		elif i == 15:
			if chefCoins[10] > 0:
				chefCoins[10] -= 1
			elif chefCoins[5] > 1:
				chefCoins[5] -= 2
			else:
				printStr = 'NO'
				break
	print(printStr)


T = int(input())
for _ in range(T):
	N = input()
	a = map(int, input().split())
	five_count = 0
	ten_count = 0
	done = False
	for amount in a:
		if amount == 5:
			five_count += 1
		elif amount == 10:
			ten_count += 1
			if five_count > 0:
				five_count -= 1
			else:
				done = True
				break
		elif amount == 15:
			if ten_count > 0:
				ten_count -= 1
			elif five_count > 1:
				five_count -= 2
			else:
				done = True
				break
	if not done:
		print('YES')
	else:
		print('NO')


t = int(input())
for l in range(t):
	m = int(input())
	x = list(map(int, input().split()))
	y = 0
	z = 0
	count = 0
	for i in range(len(x)):
		if x[i] == 5:
			y += 1
			count += 1
		elif x[i] == 10 and y >= 1:
			z += 1
			y = y - 1
			count += 1
		elif x[i] == 15:
			if z >= 1:
				z = z - 1
				count += 1
			elif y >= 2:
				y = y - 2
				z += 1
				count += 1
			else:
				break
	if count == m:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	has = 1
	get = []
	tens = 0
	flag = 1
	N = int(input())
	a = list(map(int, input().split()))
	for i in range(N):
		get.append(a[i] - 5)
	if a[0] != 5 or a[1] == 15:
		flag = 0
	else:
		for i in range(1, N):
			if get[i] == 0:
				has = has + 1
			elif get[i] == 5:
				if has >= 1:
					has = has - 1
				else:
					flag = 0
					break
				tens = tens + 1
			elif tens != 0:
				tens = tens - 1
			elif has >= 2:
				has = has - 2
			else:
				flag = 0
				break
	if flag == 0:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	coins = list(map(int, input().split()))
	res = True
	fives = 0
	tens = 0
	for i in coins:
		if i == 5:
			fives += 1
		elif i == 10:
			if fives > 0:
				fives -= 1
				tens += 1
			else:
				res = False
				break
		elif tens > 0:
			tens -= 1
		elif fives > 1:
			fives -= 2
		else:
			res = False
			break
	if res == True:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	f = 0
	t = 0
	p = 1
	for i in lst:
		if i == 5:
			f += 1
		elif i == 10:
			if f > 0:
				f -= 1
				t += 1
			else:
				p = 0
				break
		elif t > 0:
			t -= 1
		elif f > 1:
			f -= 2
		else:
			p = 0
			break
	if p == 1:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	flag = 0
	s = {'5': 0, '10': 0}
	for i in a:
		if i == 5:
			s['5'] += 1
		elif i == 10:
			if s['5'] >= 1:
				s['5'] -= 1
				s['10'] += 1
				continue
			else:
				flag = -1
				break
		elif s['10'] >= 1:
			s['10'] -= 1
		elif s['5'] >= 2:
			s['5'] -= 2
		else:
			flag = -1
			break
	print('YES' if flag == 0 else 'NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	sum5 = 0
	sum10 = 0
	f = 1
	if a[0] != 5:
		f = 0
	else:
		for i in range(n):
			if a[i] == 5:
				sum5 += 1
			elif a[i] == 10:
				if sum5 < 1:
					f = 0
				else:
					sum5 -= 1
					sum10 += 1
			elif a[i] == 15:
				if sum5 < 2 and sum10 < 1:
					f = 0
				elif sum5 < 2 and sum10 >= 1:
					sum10 -= 1
				elif sum5 >= 2 and sum10 < 1:
					sum5 -= 2
				else:
					sum10 -= 1
	if f == 1:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	coin5 = 0
	coin10 = 0
	if a[0] == 10 or a[0] == 15:
		print('NO')
	else:
		flag = 1
		coin5 += 1
		for i in range(1, n):
			if a[i] == 10:
				if coin5 > 0:
					coin10 += 1
					coin5 -= 1
				else:
					flag = 0
					break
			elif a[i] == 15:
				if coin10 > 0:
					coin10 -= 1
				elif coin5 // 2 > 0:
					coin5 -= 2
				else:
					flag = 0
					break
			else:
				coin5 += 1
		if flag == 0:
			print('NO')
		else:
			print('YES')


j = int(input())
kl = []
for mm in range(0, j):
	jk = int(input())
	ki = str(input())
	bb = ki.split(' ')
	nmu = []
	for pp in bb:
		nmu.append(int(pp))
	kl.append(nmu)
for hh in kl:
	dus = 0
	ck = 0
	fif = 0
	lpr = 'YES'
	for inv in range(0, len(hh)):
		if inv == 0:
			if hh[inv] == 5:
				ck += 1
				continue
			else:
				lpr = 'NO'
				break
		elif hh[inv] > 5:
			klp = hh[inv] - 5
			if klp == 10:
				if 10 * dus >= 10:
					fif += 1
					dus -= 1
				elif ck * 5 >= 10:
					ck -= 2
					fif += 1
				else:
					lpr = 'NO'
					break
			elif ck * 5 >= klp:
				ck -= 1
				dus += 1
			else:
				lpr = 'NO'
				break
		else:
			ck += 1
	print(lpr)


for _ in range(int(input())):
	N = int(input())
	a = list(map(int, input().split()))
	d = {5: 0, 10: 0, 15: 0}
	flag = 0
	for i in a:
		if i == 5:
			d[5] += 1
		elif i == 10:
			if d[5] > 0:
				d[5] -= 1
				d[10] += 1
			else:
				flag = -1
				break
		elif d[10] > 0:
			d[10] -= 1
			d[15] += 1
		elif d[5] > 1:
			d[5] -= 2
			d[15] += 1
		else:
			flag = -1
			break
	if flag == 0:
		print('YES')
	else:
		print('NO')


T = int(input())
for _ in range(T):
	N = int(input())
	a = list(map(int, input().split()))
	ct_five = 0
	ct_ten = 0
	ct_no = 0
	for i in range(N):
		if a[i] == 5:
			ct_five = ct_five + 5
		elif a[i] == 10:
			if ct_five > 0:
				ct_five = ct_five - 5
				ct_ten += 10
			else:
				ct_no += 1
				break
		elif ct_ten > 0:
			ct_ten = ct_ten - 10
		elif ct_five > 9:
			ct_five = ct_five - 10
		else:
			ct_no += 1
			break
	if ct_no >= 1:
		print('NO')
	else:
		print('YES')


t = int(input())
for ni in range(t):
	n = int(input())
	l = [int(i) for i in input().split()]
	(c5, c10, c15) = (0, 0, 0)
	x = 0
	if l[0] != 5:
		x = 1
	for i in l:
		if i == 5:
			c5 = c5 + 1
		elif i == 10:
			c10 = c10 + 1
			c5 = c5 - 1
		else:
			c15 = c15 + 1
			if c10 >= 1:
				c10 = c10 - 1
			else:
				c5 = c5 - 2
		if c5 < 0:
			x = 1
			print('NO')
			break
	if x == 0:
		print('YES')


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	a = [0, 0, 0]
	check = 0
	for i in range(n):
		if l[i] == 5:
			a[0] = a[0] + 1
		elif l[i] == 10:
			if a[0] >= 1:
				a[1] = a[1] + 1
				a[0] = a[0] - 1
			else:
				check = check + 1
		elif a[1] >= 1:
			a[2] = a[2] + 1
			a[1] = a[1] - 1
		elif a[0] >= 2:
			a[2] = a[2] + 1
			a[0] = a[0] - 2
		else:
			check = check + 1
	if check > 0:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	count = {5: 0, 10: 0}
	c = 0
	for i in l:
		if i == 5:
			count[5] += 1
		elif i == 10:
			count[5] -= 1
			count[10] += 1
		elif count[10] >= 1:
			count[10] -= 1
		else:
			count[5] -= 2
		if count[5] < 0 or count[10] < 0:
			print('NO')
			c = 'any'
			break
	if c == 0:
		print('YES')


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	x = a[0]
	l = [a[0]]
	if a[0] != 5:
		print('NO')
		continue
	for i in a[1:]:
		if i - 5 in l and i != 5:
			l.remove(i - 5)
			l.append(i)
		elif l.count(i - 10) >= 2 and i != 5:
			l.remove(i - 10)
			l.remove(i - 10)
			l.append(i)
		elif i == 5:
			l.append(i)
		else:
			print('NO')
			break
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	n5 = 0
	n10 = 0
	n15 = 0
	temp = 1
	for i in range(len(l)):
		if l[i] == 5:
			n5 = n5 + 1
		elif l[i] == 10:
			if n5 == 0:
				print('NO')
				temp = 0
				break
			else:
				n10 = n10 + 1
				n5 = n5 - 1
		elif n5 == 1 and n10 == 0 or (n5 == 0 and n10 == 0):
			print('NO')
			temp = 0
			break
		elif n10 != 0:
			n15 = n15 + 1
			n10 = n10 - 1
		elif n5 >= 2:
			n15 = n15 + 1
			n5 = n5 - 2
	if temp != 0:
		print('YES')


n = int(input())
while n > 0:
	t = int(input())
	total = 0
	arr = list(map(int, input().split()))
	for i in arr:
		if i == 5:
			total += 5
		else:
			total -= i - 5
			if total < 0:
				print('NO')
				break
	else:
		print('YES')
	n -= 1


for _ in range(int(input())):
	coin_count = {5: 0, 10: 0}
	n = int(input())
	a = [int(x) for x in input().split()]
	ans = 'YES'
	for pay in a:
		if pay == 5:
			coin_count[5] += 1
		elif pay == 10:
			if coin_count[5]:
				coin_count[5] -= 1
				coin_count[10] += 1
			else:
				ans = 'NO'
				break
		elif coin_count[10]:
			coin_count[10] -= 1
		elif coin_count[5] > 1:
			coin_count[5] -= 2
		else:
			ans = 'NO'
			break
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	tens = 0
	fives = 0
	ans = 'YES'
	for v in a:
		if v == 5:
			fives += 1
		elif v == 10:
			if fives > 0:
				fives -= 1
				tens += 1
			else:
				ans = 'NO'
				break
		elif tens > 0:
			tens -= 1
		elif fives > 1:
			fives -= 2
		else:
			ans = 'NO'
			break
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	(coin_5, coin_10, coin_15) = (0, 0, 0)
	flag = True
	if a[0] > 5:
		print('NO')
	else:
		for i in range(n):
			if a[i] == 5:
				coin_5 += 1
			elif a[i] == 10:
				if coin_5 >= 1:
					coin_5 -= 1
					coin_10 += 1
				else:
					flag = False
					break
			elif a[i] == 15:
				if coin_10 >= 1:
					coin_10 -= 1
					coin_15 += 1
				elif coin_5 >= 2:
					coin_5 -= 2
					coin_15 += 1
				else:
					flag = False
					break
		if flag:
			print('YES')
		else:
			print('NO')


for tc in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	(nickles, dimes, fifs) = (0, 0, 0)
	ans = 'YES'
	for i in a:
		if i == 5:
			nickles += 1
		elif i == 10:
			dimes += 1
			nickles -= 1
		elif i == 15:
			fifs += 1
			if dimes > 0:
				dimes -= 1
			elif nickles > 1:
				nickles = nickles - 2
			else:
				ans = 'NO'
		if nickles < 0 or dimes < 0:
			ans = 'NO'
	print(ans)


t = int(input())
for j in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	(s, r) = (0, 0)
	cnt = 0
	for i in range(len(a)):
		if a[i] == 5:
			s += 1
			cnt += 1
		elif a[i] == 10 and s >= 1:
			r += 1
			s = s - 1
			cnt += 1
		elif a[i] == 15:
			if r >= 1:
				r = r - 1
				cnt += 1
			elif s >= 2:
				s = s - 2
				r += 1
				cnt += 1
			else:
				break
	if cnt == n:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	c5 = 0
	c10 = 0
	c15 = 0
	for value in a:
		if value == 5:
			c5 += 1
		elif value == 10:
			if c5 > 0:
				c5 -= 1
				c10 += 1
			else:
				print('NO')
				break
		elif c10 > 0:
			c10 -= 1
			c15 += 1
		elif c5 >= 2:
			c5 -= 2
			c15 += 1
		else:
			print('NO')
			break
	else:
		print('YES')


def Solution(n, l):
	p5 = 0
	p10 = 0
	for i in l:
		if i == 5:
			p5 = p5 + 1
		elif i == 10:
			if p5 > 0:
				p5 = p5 - 1
			else:
				return 'NO'
			p10 = p10 + 1
		elif p10 > 0:
			p10 = p10 - 1
		elif p5 >= 2:
			p5 = p5 - 2
		else:
			return 'NO'
	return 'YES'
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	print(Solution(n, l))


for i in range(int(input())):
	n = int(input())
	x = list(map(int, input().split()))
	i = 0
	flag = 1
	(a, b, c) = (0, 0, 0)
	while i < n:
		if x[i] == 5:
			a += 1
		elif x[i] == 10:
			b += 1
			if a > 0:
				flag = 1
				a -= 1
			else:
				flag = 0
				break
		else:
			c += 1
			if b > 0:
				b -= 1
				flag = 1
			elif a > 1:
				a -= 2
				flag = 1
			else:
				flag = 0
				break
		i += 1
	if flag == 1:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	ice = int(input())
	cream = list(map(int, input().split()))[:ice]
	(x, y, z) = (0, 0, 0)
	camp = 0
	for i in range(len(cream)):
		if cream[i] == 5:
			x += 1
			y += 1
		elif cream[i] == 10 and x >= 1:
			z += 1
			x = x - 1
			y += 1
		elif cream[i] == 15:
			if z >= 1:
				z = z - 1
				y += 1
			elif x >= 2:
				x = x - 2
				z += 1
				y += 1
			else:
				break
	if y == ice:
		print('YES')
	else:
		print('NO')