Boris thinks that chess is a tedious game. So he left his tournament early and went to a barber shop as his hair was a bit messy.

His current hair can be described by an array $a_1,a_2,\ldots, a_n$, where $a_i$ is the height of the hair standing at position $i$. His desired haircut can be described by an array $b_1,b_2,\ldots, b_n$ in a similar fashion.

The barber has $m$ razors. Each has its own size and can be used at most once. In one operation, he chooses a razor and cuts a segment of Boris's hair. More formally, an operation is:

Choose any razor which hasn't been used before, let its size be $x$;

Choose a segment $[l,r]$ ($1\leq l \leq r \leq n$);

Set $a_i := \min (a_i,x)$ for each $l\leq i \leq r$;

Notice that some razors might have equal sizes — the barber can choose some size $x$ only as many times as the number of razors with size $x$.

He may perform as many operations as he wants, as long as any razor is used at most once and $a_i = b_i$ for each $1 \leq i \leq n$ at the end. He does not have to use all razors.

Can you determine whether the barber can give Boris his desired haircut?


-----Input-----

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \leq t \leq 20000$). The description of the test cases follows.

The first line of each test case contains a positive integer $n$ ($3\leq n\leq 2\cdot 10^5$) — the length of arrays $a$ and $b$.

The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$)  — Boris's current hair.

The third line of each test case contains $n$ positive integers $b_1, b_2, \ldots, b_n$ ($1 \leq b_i \leq 10^9$)  — Boris's desired hair.

The fourth line of each test case contains a positive integer $m$ ($1 \leq m \leq 2\cdot 10^5$)  — the number of razors.

The fifth line of each test case contains $m$ positive integers $x_1,x_2, \ldots, x_m$ ($1 \leq x_i \leq 10^9$)  — the sizes of the razors.

It is guaranteed that the sum of $n$ and the sum of $m$ over all test cases do not exceed $2\cdot 10^5$.


-----Output-----

For each test case, print "YES" if the barber can cut Boris's hair as desired. Otherwise, print "NO".

You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.


-----Examples-----

Input
7
3
3 3 3
2 1 2
2
1 2
6
3 4 4 6 3 4
3 1 2 3 2 3
3
3 2 3
10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
10
1 2 3 4 5 6 7 8 9 10
3
1 1 1
1 1 2
12
4 2 4 3 1 5 6 3 5 6 2 1
13
7 9 4 5 3 3 3 6 8 10 3 2 5
5 3 1 5 3 2 2 5 8 5 1 1 5
8
1 5 3 5 4 2 3 1
13
7 9 4 5 3 3 3 6 8 10 3 2 5
5 3 1 5 3 2 2 5 8 5 1 1 5
7
1 5 3 4 2 3 1
3
19747843 2736467 938578397
2039844 2039844 2039844
1
2039844
Output
YES
NO
YES
NO
YES
NO
YES


-----Note-----

In the first test case, Boris's hair is initially $[3,3,3]$. Let us describe a sequence of $2$ operations the barber can perform:

The barber uses the razor with size $1$ on the segment $[2,2]$; hence Boris's hair becomes $[3,1,3]$.

The barber uses the razor with size $2$ on the segment $[1,3]$; hence Boris's hair becomes $[2,1,2]$ which is the desired haircut.

In the third test case, no operation has to be done since Boris's hair is already as desired.

In the fourth test case, no cuts will be able to increase the third element in $[1,1,1]$ in a way that the array becomes $[1,1,2]$.

Source: codeforces | Difficulty: MEDIUM_HARD
Tags: Constructive algorithms

Sorting

Greedy algorithms

Dynamic programming

Spanning trees

Data structures


Solutions:
import sys

def solve():
	inp = sys.stdin.readline
	n = int(inp())
	a = list(map(int, inp().split()))
	b = list(map(int, inp().split()))
	st = [(int(1000000000.0 + 7), n)]
	next = [None] * n
	for i in range(n - 1, -1, -1):
		v = b[i]
		while st[-1][0] <= v:
			st.pop()
		next[i] = st[-1][1]
		st.append((v, i))
	m = int(inp())
	have = dict()
	for v in map(int, inp().split()):
		have[v] = have.get(v, 0) + 1
	need = dict()
	last = dict()
	for i in range(n):
		if a[i] < b[i]:
			print('NO')
			return
		if a[i] == b[i]:
			continue
		v = b[i]
		val = last.get(v, None)
		if val is None:
			need[v] = 1
		elif next[val] < i:
			need[v] += 1
		last[v] = i
	for (k, v) in need.items():
		if have.get(k, 0) < v:
			print('NO')
			return
	print('YES')

def main():
	for i in range(int(sys.stdin.readline())):
		solve()
main()


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	have = list(map(int, input().split(' ')))
	req = list(map(int, input().split(' ')))
	m = int(input())
	razors = list(map(int, input().split(' ')))
	razors = Counter(razors)

	def solve(n):
		for i in range(n):
			if req[i] > have[i]:
				return False
		stack = []
		for i in range(n):
			while stack and stack[-1] < req[i]:
				stack.pop()
			if req[i] == have[i]:
				continue
			if stack and stack[-1] == req[i]:
				continue
			if req[i] not in razors or razors[req[i]] == 0:
				return False
			razors[req[i]] -= 1
			stack.append(req[i])
		return True
	ans = solve(n)
	if ans:
		print('YES')
	else:
		print('NO')


import heapq
T = int(input())
import collections
while T > 0:
	n = int(input())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	m = int(input())
	X = list(map(int, input().split()))
	counter = collections.Counter(X)
	sign = 1
	heap = []
	for i in range(n):
		if B[i] > A[i]:
			print('NO')
			sign = 0
			break
		while heap and heap[0] < B[i]:
			heapq.heappop(heap)
		if B[i] < A[i] and (not heap or heap[0] > B[i]):
			if counter[B[i]] == 0:
				print('NO')
				sign = 0
				break
			else:
				counter[B[i]] -= 1
				heapq.heappush(heap, B[i])
	if sign:
		print('YES')
	T -= 1


from collections import deque
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().strip().split()]
	b = [int(i) for i in input().strip().split()]
	m = int(input())
	x = [int(i) for i in input().strip().split()]
	d = {}
	for xi in x:
		d[xi] = d.get(xi, 0) + 1
	del x
	check = True
	for i in range(n):
		if b[i] > a[i] or (b[i] not in d and a[i] != b[i]):
			check = False
			break
	if not check:
		print('NO')
	else:
		right_max = [n] * n
		s = deque()
		for i in range(n):
			while s and b[s[-1]] < b[i]:
				right_max[s.pop()] = i
			s.append(i)
		done = {}
		check = True
		for i in range(n):
			if a[i] != b[i]:
				if b[i] in done and done[b[i]] > i:
					continue
				if d[b[i]] == 0:
					check = False
					break
				done[b[i]] = right_max[i]
				d[b[i]] -= 1
		if not check:
			print('NO')
		else:
			print('YES')


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	b = [int(i) for i in input().split()]
	m = int(input())
	x = Counter([int(i) for i in input().split()])
	stack = list()
	flag = True
	for i in range(n):
		if a[i] < b[i]:
			flag = False
			break
		for j in range(len(stack) - 1, -1, -1):
			if b[i] > stack[j]:
				stack.pop()
			else:
				break
		if b[i] == a[i]:
			continue
		if len(stack) == 0 or b[i] != stack[len(stack) - 1]:
			if x[b[i]] == 0:
				flag = False
				break
			x[b[i]] -= 1
			stack.append(b[i])
	if flag:
		print('YES')
	else:
		print('NO')


import heapq
iter = int(input())
for i in range(iter):
	n = int(input())
	a = list(map(int, input().split(' ')))
	b = list(map(int, input().split(' ')))
	m = input()
	x = list(map(int, input().split(' ')))
	razor_sizes = {}
	for i in x:
		razor_sizes[i] = razor_sizes.get(i, 0) + 1
	heap = []
	heapq.heapify(heap)
	ans = 1
	for i in range(n):
		ex = 0
		while len(heap):
			val = heapq.heappop(heap)
			if val < b[i]:
				if razor_sizes.get(val, 0) == 0:
					ans = 0
					break
				else:
					razor_sizes[val] -= 1
			if val == b[i]:
				ex = 1
				heapq.heappush(heap, val)
				break
			if val > b[i]:
				heapq.heappush(heap, val)
				break
		if ans == 0:
			break
		if a[i] < b[i]:
			ans = 0
		elif a[i] > b[i] and (not ex):
			heapq.heappush(heap, b[i])
	while len(heap):
		val = heapq.heappop(heap)
		if razor_sizes.get(val, 0) == 0:
			ans = 0
			break
		else:
			razor_sizes[val] -= 1
	if ans == 0:
		print('NO')
	else:
		print('YES')


t = int(input(''))
for _ in range(t):
	n = int(input(''))
	a = [int(x) for x in input('').split(' ')]
	b = [int(x) for x in input('').split(' ')]
	m = int(input(''))
	x = [int(x) for x in input('').split(' ')]
	bad = False
	for i in range(n):
		if a[i] < b[i]:
			bad = True
			break
	if bad:
		print('NO')
		continue
	stack = []
	right = [-1] * n
	for i in range(n):
		while stack and b[stack[-1]] < b[i]:
			stack.pop()
		if stack and b[stack[-1]] == b[i]:
			right[stack[-1]] = i
			stack[-1] = i
		stack.append(i)
	h = {}
	for i in range(m):
		if x[i] in h:
			h[x[i]] += 1
		else:
			h[x[i]] = 1
	v = [a[i] != b[i] for i in range(n)]
	out = 'YES'
	for i in range(n):
		if v[i]:
			cur = i
			u = b[cur]
			if u not in h or h[u] == 0:
				out = 'NO'
				break
			h[u] -= 1
			while cur != -1:
				v[cur] = False
				cur = right[cur]
	print(out)