You have an array a consisting of n integers. Each integer from 1 to n appears exactly once in this array.

For some indices i (1 ≤ i ≤ n - 1) it is possible to swap i-th element with (i + 1)-th, for other indices it is not possible. You may perform any number of swapping operations any order. There is no limit on the number of times you swap i-th element with (i + 1)-th (if the position is not forbidden).

Can you make this array sorted in ascending order performing some sequence of swapping operations?


-----Input-----

The first line contains one integer n (2 ≤ n ≤ 200000) — the number of elements in the array.

The second line contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 200000) — the elements of the array. Each integer from 1 to n appears exactly once.

The third line contains a string of n - 1 characters, each character is either 0 or 1. If i-th character is 1, then you can swap i-th element with (i + 1)-th any number of times, otherwise it is forbidden to swap i-th element with (i + 1)-th.


-----Output-----

If it is possible to sort the array in ascending order using any sequence of swaps you are allowed to make, print YES. Otherwise, print NO.


-----Examples-----
Input
6
1 2 5 3 4 6
01110

Output
YES

Input
6
1 2 5 3 4 6
01010

Output
NO



-----Note-----

In the first example you may swap a_3 and a_4, and then swap a_4 and a_5.

Source: codeforces | Difficulty: MEDIUM
Tags: Graph traversal

Sorting

Amortized analysis

Greedy algorithms

Mathematics


Solutions:
import sys
n = int(input())
a = list(map(int, input().split()))
s = input()
b = [10 ** 8] * (n + 1)
for i in range(len(s) - 1, -1, -1):
	b[i] = i if s[i] == '0' else b[i + 1]
for i in range(len(a)):
	if a[i] > i + 1:
		if b[i] < a[i] - 1:
			print('NO')
			sys.exit()
print('YES')


import sys
from math import *
from collections import Counter as cc, defaultdict as df

def counter(l):
	return dict(cc(l))

def lcm(a, b):
	return int(a * b / gcd(a, b))

def input():
	return sys.stdin.readline().strip()

def ini():
	return int(input())

def inp():
	return map(int, input().split())

def li():
	return list(map(int, input().split()))
n = ini()
l = li()
s = input()
m = -1
c = 1
for i in range(n - 1):
	m = max(m, l[i])
	if s[i] == '0':
		if i + 1 < m:
			print('NO')
			c = 0
			break
if c:
	print('YES')


def main():
	n = int(input())
	a = [int(x) for x in input().split()]
	s = input()
	xx = sorted(((a[i], i) for i in range(n)))
	oo = [-1] * n
	for i in range(n):
		oo[xx[i][1]] = i
	nn = -1
	for i in range(n):
		nn = max(nn, oo[i])
		if i < nn and s[i] == '0':
			print('NO')
			break
	else:
		print('YES')

def __starting_point():
	main()
__starting_point()


n = int(input())
s = input().split()
arr = []
for l in s:
	arr.append(int(l))
s = input()
k = len(s)
pos = []
for l in s:
	pos.append(int(l))
i = 0
indexset = set()
valueset = set()
while i < k:
	if pos[i] == 0:
		if arr[i] != i + 1:
			print('NO')
			return
	if pos[i] == 1:
		while i < k:
			if pos[i] == 1:
				indexset.add(i + 1)
				valueset.add(arr[i])
				i += 1
			else:
				break
		indexset.add(i + 1)
		valueset.add(arr[i])
		if len(indexset ^ valueset) > 0:
			print('NO')
			return
		indexset.clear()
		valueset.clear()
	i += 1
print('YES')


import sys
import os.path
if os.path.exists('input_file.txt'):
	sys.stdin = open('input_file.txt', 'r')
	sys.stdout = open('output_file.txt', 'w')
n = int(input())
a = list(map(int, input().split()))
s = input()
m = 0
f = 1
for i in range(n):
	m = max(m, a[i])
	if m > i + 1:
		if s[i] != '1':
			f = 0
			break
if f:
	print('YES')
else:
	print('NO')


num = int(input())
array = [0] + [num for num in map(int, input().strip().split())]
valid = input()
group = [0] * (num + 1)
groupID = 0
group[array[1]] = groupID
for i in range(2, num + 1):
	if valid[i - 2] == '0':
		groupID += 1
	group[array[i]] = groupID
for i in range(1, num + 1):
	if array[i] != i and group[array[i]] != group[i]:
		print('NO')
		break
else:
	print('YES')


n = int(input())
a = list(map(int, input().split()))
s = input()
maximum = -1
for i in range(n - 1):
	maximum = max(a[i], maximum)
	if i + 1 < maximum and s[i] == '0':
		print('NO')
		break
else:
	print('YES')


from itertools import groupby
n = int(input())
nums = [int(i) for i in input().split()]
copy = list(nums)
pos = input()
pos = [''.join(g) for (k, g) in groupby(pos) if k != '#']
cur_pos = 0
for i in pos:
	if i[0] == '1':
		nums[cur_pos:cur_pos + len(i) + 1] = sorted(nums[cur_pos:cur_pos + len(i) + 1])
	cur_pos += len(i)
if sorted(copy) == nums:
	print('YES')
else:
	print('NO')


N = int(input())
T = [0] * (N + 1)
for (i, j) in enumerate(map(int, input().split())):
	j -= 1
	if i > j:
		(i, j) = (j, i)
	T[i] += 1
	T[j] -= 1
from itertools import accumulate
T = list(accumulate(T))
T.pop()
for (i, j) in zip(map(int, input()), T):
	if j != 0 and i == 0:
		print('NO')
		exit()
print('YES')


n = int(input())
a = list(map(int, input().split()))
s = input()
m = 0
for i in range(n - 1):
	m = max(a[i], m)
	if s[i] == '0' and m > i + 1:
		print('NO')
		exit()
print('YES')


from __future__ import print_function
from collections import Counter, OrderedDict
from itertools import permutations as perm
from fractions import Fraction
from collections import deque
from sys import stdin
from bisect import *
from heapq import *
import math
g = lambda : stdin.readline().strip()
gl = lambda : g().split()
gil = lambda : [int(var) for var in gl()]
gfl = lambda : [float(var) for var in gl()]
gcl = lambda : list(g())
gbs = lambda : [int(var) for var in g()]
mod = int(1000000000.0) + 7
inf = float('inf')
(n,) = gil()
a = gil()
s = g()
h = []
i = 0
while i < n - 1:
	if s[i] == '0':
		i += 1
		continue
	h.append(a[i])
	j = i
	while j + 1 < n - 1 and s[j + 1] == '1':
		j += 1
		heappush(h, a[j])
	if j + 1 < n:
		heappush(h, a[j + 1])
	while h:
		a[i] = heappop(h)
		i += 1
for i in range(1, n):
	if a[i - 1] > a[i]:
		print('NO')
		exit()
print('YES')


n = int(input())
A = list(map(int, input().split()))
s = input()
ans = []
st = A[:1]
for (i, a) in enumerate(A[1:]):
	if s[i] == '1':
		st += [a]
	else:
		ans += sorted(st)
		st = [a]
ans += sorted(st)
print('YES' if sorted(ans) == ans else 'NO')


import sys, math, os.path
FILE_INPUT = 'c.in'
DEBUG = os.path.isfile(FILE_INPUT)
if DEBUG:
	sys.stdin = open(FILE_INPUT)

def ni():
	return map(int, input().split(' '))

def nia():
	return list(map(int, input().split()))

def log(x):
	if DEBUG:
		print(x)
(n,) = ni()
a = nia()
en = list(map(lambda x: x == '1', input()))
i = 0
while i < n - 1:
	if en[i]:
		j = i
		amin = a[i]
		amax = a[i]
		while j < n - 1 and en[j]:
			j += 1
			amin = min(a[j], amin)
			amax = max(a[j], amax)
		if amin == i + 1 and amax == j + 1:
			i = j + 1
		else:
			print('NO')
			sys.exit()
	else:
		if a[i] != i + 1:
			print('NO')
			sys.exit()
		i += 1
print('YES')


n = int(input())
l = list(map(int, input().split()))
s = list(map(int, list(input())))
for i in range(1, n - 1):
	s[i] += s[i - 1]

def query(l, r):
	ans = s[r]
	if l > 0:
		ans -= s[l - 1]
	return ans
ans = True
for i in range(n):
	index = l[i] - 1
	if index > i:
		ans = ans and query(i, index - 1) == index - i
	elif index < i:
		ans = ans and query(index, i - 1) == i - index
if ans:
	print('YES')
else:
	print('NO')


def main():
	input()
	aa = list(map(int, input().split()))
	ff = list(map({'0': False, '1': True}.get, input()))
	flag = True
	for (i, f) in enumerate(ff):
		if flag and f:
			(start, flag) = (i, False)
		elif not (flag or f):
			flag = True
			aa[start:i + 1] = sorted(aa[start:i + 1])
	if ff[-1]:
		aa[start:] = sorted(aa[start:])
	print(('NO', 'YES')[all((i == a for (i, a) in enumerate(aa, 1)))])
main()


n = int(input())
a = list(map(int, input().split()))
s = input() + '0'
i = 0
while i < n:
	if s[i] == '0' and a[i] != i + 1:
		print('NO')
		quit()
	if s[i] == '0':
		i += 1
		continue
	if s[i] == '1':
		j = i
		while j < n:
			if s[j] == '1':
				j += 1
			else:
				break
		a[i:j + 1] = sorted(a[i:j + 1])
		for k in range(i, j):
			if a[k] != k + 1:
				print('NO')
				quit()
		i = j + 1
print('YES')


n = int(input())
elements = list(map(int, input().split()))
swapping = input()
start = reach = 0
for (index, swap) in enumerate(swapping):
	swap = swap == '1'
	element = elements[index] - 1
	if element < index:
		if element < start:
			print('NO')
			break
	elif element > index:
		reach = max(reach, element)
	if not swap:
		if reach > index:
			print('NO')
			break
		start = index + 1
else:
	print('YES')


length = int(input())
nums = input().split(' ', length - 1)
for i in range(length):
	nums[i] = int(nums[i])
swaps = []
inp = input()
for i in range(length - 1):
	swaps.append(int(inp[i]))
res = 1
currmin = 1
currmax = 1
currnums = [nums[0]]
for i in range(length - 1):
	if swaps[i] == 0:
		currmax = i + 1
		if min(currnums) < currmin or max(currnums) > currmax:
			res = 0
		currmin = i + 2
		currmax = i + 2
		currnums = [nums[i + 1]]
	else:
		currnums.append(nums[i + 1])
		currmax = i + 1
if res == 1:
	print('YES')
else:
	print('NO')


import sys
n = int(input())
a = [i - 1 for i in list(map(int, input().split()))]
b = [int(i) for i in list(input())]
sm = sum(b)
l = [0] * (n - 1)
r = [0] * (n - 1)
l[0] = b[0]
r[-1] = b[-1]
for i in range(1, len(b)):
	l[i] += l[i - 1] + b[i]
for i in range(len(b) - 2, -1, -1):
	r[i] += r[i + 1] + b[i]
for i in range(len(a)):
	if a[i] == i:
		continue
	(start, end) = (min(a[i], i), max(a[i], i) - 1)
	if start == 0 and end == n - 2:
		if sm != n - 1:
			print('NO')
			sys.exit(0)
	elif start == 0:
		if end + 1 != sm - r[end + 1]:
			print('NO')
			sys.exit(0)
	elif end == n - 2:
		if end - start + 1 != sm - l[start - 1]:
			print('NO')
			sys.exit(0)
	elif sm - (l[start - 1] + r[end + 1]) != end - start + 1:
		print('NO')
		sys.exit(0)
print('YES')


from os import path
import sys
from heapq import heappush, heappop
from functools import cmp_to_key as ctk
from collections import deque, defaultdict as dd
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from itertools import permutations
from datetime import datetime
from math import ceil, sqrt, log, gcd

def ii():
	return int(input())

def si():
	return input().rstrip()

def mi():
	return map(int, input().split())

def li():
	return list(mi())
abc = 'abcdefghijklmnopqrstuvwxyz'
mod = 1000000007
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def bo(i):
	return ord(i) - ord('a')
file = 1

def solve():
	n = ii()
	a = li()
	s = si()
	b = []
	pre = [0] * n
	for i in range(1, n):
		pre[i] += pre[i - 1]
		if s[i - 1] == '1':
			pre[i] += 1
	for (i, x) in enumerate(a):
		b.append([x, i])
	b.sort()
	f = 0
	for i in range(n):
		l = b[i][1]
		r = i
		if l > r:
			(l, r) = (r, l)
		if pre[r] - pre[l] != r - l:
			f = 1
	print('NO' if f == 1 else 'YES')
if file:
	if path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	else:
		input = sys.stdin.readline
solve()


n = int(input())
sp = list(map(int, input().split()))
zn = list(map(int, list(input()))) + [0]
u = []
sr = []
h = 0
for i in range(len(zn)):
	if zn[i] == 0:
		u.append((h, sp[i]))
		h += 1
	else:
		u.append((h, sp[i]))
u.sort()
for el in u:
	sr.append(el[1])
if sr == sorted(sp):
	print('YES')
else:
	print('NO')


n = int(input())
a = [int(i) for i in input().split()]
s = [i == '1' for i in input()]
(l, r) = ([0] * len(a), [0] * len(a))
(l[0], r[-1]) = (0, len(a) - 1)
for i in range(1, len(a)):
	l[i] = l[i - 1] if s[i - 1] else i
for i in range(len(a) - 2, -1, -1):
	r[i] = r[i + 1] if s[i] else i
x = [(a[i], i) for i in range(len(a))]
x.sort()
res = True
for i in range(len(x)):
	p = x[i]
	if p[1] > i:
		res = l[p[1]] <= i
	else:
		res = r[p[1]] >= i
	if res == False:
		break
print('YES' if res else 'NO')


def can_order(arr, ok):
	i = 0
	while i < len(ok):
		if not ok[i]:
			if i + 1 != arr[i + 1] and (i + 1 >= len(ok) or not ok[i + 1]):
				return False
			i += 1
		else:
			j = i
			while j < len(ok) and ok[j]:
				j += 1
			if sum(arr[i:j + 1]) != sum(range(i, j + 1)):
				return False
			i = j
	return True

def main():
	N = int(input())
	arr = [int(x) - 1 for x in input().split()]
	ok = [c == '1' for c in input()]
	print('YES' if can_order(arr, ok) else 'NO')

def __starting_point():
	main()
__starting_point()


n = int(input())
a = [int(i) for i in input().split()]
s = input()
s += '0'

def get_ans():
	last = float('-inf')
	i = 0
	while i < len(s):
		if s[i] == '0':
			if a[i] < last:
				return 'NO'
			last = a[i]
		else:
			temp = []
			while i < len(s) and s[i] == '1':
				temp.append(a[i])
				i += 1
			temp.append(a[i])
			mn = min(temp)
			mx = max(temp)
			if mn < last:
				return 'NO'
			last = mx
		i += 1
	return 'YES'
ans = get_ans()
print(ans)


n = int(input())
a = [int(x) for x in input().split()]
rot = input()
stack = []
pairs = []
for i in range(len(rot)):
	if rot[i] == '1' and stack == []:
		stack.append(i)
	if rot[i] == '0' and len(stack) > 0:
		stack.append(i - 1)
		pairs.append((stack[-2], stack[-1] + 1))
		stack.clear()
if len(stack) > 0:
	pairs.append((stack[-1], n))
for (l, r) in pairs:
	a[l:r + 1] = sorted(a[l:r + 1])
print('YNEOS'[not a == sorted(a)::2])


n = int(input())
a = list(map(int, input().split()))
s = input()
prev = -1
for i in range(n - 1):
	if s[i] == '1' and prev == -1:
		prev = i
	elif s[i] == '0' and prev != -1:
		a[prev:i + 1] = sorted(a[prev:i + 1])
		prev = -1
a[prev:n] = sorted(a[prev:n])
k = []
for i in a:
	k.append(i)
k.sort()
f = 0
for i in range(n):
	if a[i] != k[i]:
		f = 1
if f:
	print('NO')
else:
	print('YES')


import os, sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import ceil
mod = 10 ** 9 + 7
for _ in range(1):
	n = int(input())
	a = list(map(int, input().split()))
	values = input()
	actual_value = 0
	for i in range(n - 1):
		actual_value = max(actual_value, a[i])
		if actual_value > i + 1 and values[i] == '0':
			print('NO')
			exit(0)
	print('YES')


n = int(input())
a = list(map(int, input().split()))
st = input().strip('\r\n')
flag = True
t = 0
for i in range(1, n):
	t = max(t, a[i - 1])
	if st[i - 1] == '0' and t != i:
		flag = False
if flag:
	print('YES')
else:
	print('NO')


import sys, math, os.path
FILE_INPUT = 'c.in'
DEBUG = os.path.isfile(FILE_INPUT)
if DEBUG:
	sys.stdin = open(FILE_INPUT)

def ni():
	return map(int, input().split(' '))

def nia():
	return list(map(int, input().split()))

def log(x):
	if DEBUG:
		print(x)
(n,) = ni()
a = nia()
en = list(map(lambda x: x == '1', input()))
count = 1
i = 0
while i < n - 1:
	if en[i]:
		j = i
		b = [a[i]]
		while j < n - 1 and en[j]:
			j += 1
			b.append(a[j])
		b.sort()
		for j in b:
			if j != count:
				print('NO')
				sys.exit()
			else:
				count += 1
		i = j
	else:
		if a[i] == count:
			count += 1
		else:
			print('NO')
			sys.exit()
		i += 1
print('YES')


import sys
from itertools import groupby
n = int(input())
a = list(map(int, input().split()))
sorted_a = sorted(a)
i = 0
for (k, v) in groupby(input()):
	w = len(list(v))
	if k == '1':
		a[i:i + w + 1] = sorted(a[i:i + w + 1])
	i += w
print('YES' if a == sorted_a else 'NO')


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
n = int(input())
l = list(map(int, input().split()))
s = input()
l1 = [0]
c = 0
for i in s:
	if i == '1':
		c += 1
	l1.append(c)
l2 = [0] * (n + 1)
ans = 'YES'
for i in range(n):
	l2[l[i]] = i
for i in range(n):
	if l[i] != i + 1:
		x = l2[i + 1]
		if l1[x] - l1[i] != x - i:
			ans = 'NO'
			break
print(ans)


v = input()
num = list(map(int, input().split()))
s = input()
k = 0
maxx = 0
for i in range(len(num) - 1):
	maxx = max(maxx, num[i])
	if s[i] == '0' and maxx > i + 1:
		k = 1
if k:
	print('NO')
else:
	print('YES')


num = int(input())
array = [int(n) for n in map(int, input().split())]
valid = input()
maximum = -1
for i in range(num - 1):
	maximum = max(array[i], maximum)
	if i + 1 < maximum and valid[i] == '0':
		print('NO')
		break
else:
	print('YES')


from collections import defaultdict
import math
from sys import stdin
N = int(stdin.readline())
string1 = stdin.readline().strip().split()
string2 = stdin.readline().strip()
indexlist = []
numberlist = []
for x in string2:
	indexlist.append(int(x))
for x in string1:
	numberlist.append(int(x))
slicelist0 = []
slicelist1 = []
x = 0
while x < len(indexlist):
	if indexlist[x] == 0:
		if x != 0 and indexlist[x - 1] == 0:
			slicelist0.append(x)
		elif x == 0:
			slicelist0.append(x)
		x += 1
	else:
		slist = []
		slist.append(x)
		while indexlist[x]:
			slist.append(x + 1)
			x += 1
			if x >= len(indexlist):
				break
		slicelist1.append(slist)
condition = True
if indexlist[-1] == 0 and numberlist[-1] != N:
	condition = False
for item in slicelist0:
	if item == numberlist[item] - 1:
		condition = condition and True
	else:
		condition = condition and False
for k in range(len(slicelist1)):
	maximum = -1
	for j in slicelist1[k]:
		if numberlist[j] > maximum:
			maximum = numberlist[j]
	condition = condition if maximum == slicelist1[k][-1] + 1 else False
if condition == False:
	print('NO')
elif condition == True:
	print('YES')


n = int(input())
a = list(map(int, input().split()))
mask = input()
i = 0
while i < n - 1:
	k = j = i
	found = False
	while i < n - 1 and mask[i] == '1':
		found = True
		i += 1
	if found:
		while j <= i and k + 1 <= a[j] and (a[j] <= i + 1):
			j += 1
		if j <= i:
			print('NO')
			exit(0)
	elif a[i] != i + 1:
		print('NO')
		exit(0)
	i += 1
print('YES')


n = int(input())
arrstr = input()
string = input()
array = [int(i) for i in arrstr.rsplit(' ')]
m = 0
for i in range(n - 1):
	m = max(m, array[i])
	if string[i] == '0':
		if m > i + 1:
			print('NO')
			exit()
print('YES')


n = int(input())
x = list(map(int, input().split()))
z = x.copy()
z2 = z.copy()
m = x[-1]
for i in range(n - 1, 0, -1):
	m = min(x[i], m)
	z[i - 1] = m
m = x[0]
for i in range(1, n):
	m = max(m, x[i])
	z2[i] = m
y = input()
f = 'YES'
for i in range(n - 1):
	if z[i] < z2[i] and y[i] == '0':
		f = 'NO'
print(f)


n = int(input())
a = list(map(int, input().split()))
s = input()
prev = False
ans = True
i = 0
temp = []
b = []
while i < n:
	ini = i
	temp = [a[i]]
	while i < n - 1 and s[i] == '1':
		ini += 1
		i += 1
		temp.append(a[i])
	temp.sort()
	b += temp
	i += 1
if b == list(range(1, n + 1)):
	print('YES')
else:
	print('NO')


n = int(input())
l = [int(i) for i in input().split()]
canswap = input()
maxi = l[0]
for i in range(0, n - 1):
	maxi = max(maxi, l[i])
	if canswap[i] == '0' and i + 1 < maxi:
		print('NO')
		exit()
print('YES')


n = int(input())
raw_input = list(map(int, input().split()))
raw_string = input()
supposed_sum = 0
actual_sum = 0
for i in range(1, len(raw_string) + 1):
	supposed_sum += i
	actual_sum += raw_input[i - 1]
	if raw_string[i - 1] == '0':
		if actual_sum != supposed_sum:
			print('NO')
			return
		actual_sum = 0
		supposed_sum = 0
print('YES')


n = int(input())
a = list(map(int, input().split(' ')))
s = input()
p1 = [0]
p2 = [0]
c = 0
flag = 1
for k in range(0, n - 1):
	if s[k] == '0':
		c = 0
	else:
		c += 1
	p1.append(c)
for k in range(n - 2, -1, -1):
	if s[k] == '0':
		c = 0
	else:
		c += 1
	p2.append(c)
p2.reverse()
for k in range(0, n):
	if a[k] < k + 1 - p1[k] or a[k] > k + 1 + p2[k]:
		flag = 0
		break
if flag == 0:
	print('NO')
else:
	print('YES')


n = int(input())
a = list(map(int, input().split()))
s = list(input())
s.append(1)
(rec, q) = ([], 0)
for i in range(n):
	rec.append(q + a[i])
	q = rec[i]
flag = True
for i in range(n):
	if s[i] == '0' and rec[i] > (i + 1) * (i + 2) // 2:
		flag = False
		break
if flag:
	print('YES')
else:
	print('NO')


import atexit
import io
import sys
_OUTPUT_BUFFER = io.StringIO()
sys.stdout = _OUTPUT_BUFFER

@atexit.register
def write():
	sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())

def array_to_string(a):
	s = ''
	for i in a:
		s += str(i)
	return s
n = int(input())
a = list(map(int, input().split()))
b = [int(i) for i in input()]
b.append(1)
temp = sorted(a)
c = 0
s1 = 0
s2 = 0
for i in range(1, n + 1):
	s1 += i
	s2 += a[i - 1]
	if b[i - 1] == 0 and s1 != s2:
		c = 1
		break
if c == 0:
	print('YES')
else:
	print('NO')


n = int(input())
a = list(map(int, input().split()))
s = input()
set1 = set()
t = len(set1)
max1 = a[0]
min1 = a[0]
flag = 0
set1.add(a[0])
for i in range(n - 1):
	if s[i] == '1':
		set1.add(a[i])
		set1.add(a[i + 1])
		min1 = min(min1, a[i + 1], a[i])
		max1 = max(max1, a[i + 1], a[i])
	elif s[i] == '0':
		if max1 - min1 + 1 == len(set1) and i + 1 == max1:
			max1 = a[i + 1]
			min1 = a[i + 1]
			pass
		else:
			flag = 1
			break
		set1 = set()
		set1.add(a[i + 1])
if flag == 1:
	print('NO')
else:
	print('YES')


n = int(input())
a = list(map(lambda x: int(x) - 1, input().split()))
s = input() + '0'
i = 0
while True:
	b = []
	q = False
	while s[i] == '1':
		if not q:
			l = i
		b.append(a[i])
		i += 1
		q = True
	if q:
		b.append(a[i])
		for j in b:
			if j > i or j < l:
				print('NO')
				exit()
		i += 1
		if i >= n:
			break
		continue
	if a[i] != i:
		print('NO')
		exit()
	i += 1
	if i >= n:
		break
print('YES')


n = int(input())
c = list(map(int, input().split()))
s = input()
x = 0
y = 0
p = 0
for i in range(n - 1):
	if s[i] == '1':
		if x < c[i]:
			y = max(y, c[i])
			p = 1
		else:
			print('NO')
			break
	elif p == 1:
		if x < c[i]:
			y = max(y, c[i])
			x = y
			p = 0
		else:
			print('NO')
			break
	elif x > c[i]:
		print('NO')
		break
	else:
		x = c[i]
else:
	if c[-1] > x:
		print('YES')
	else:
		print('NO')


from collections import defaultdict
al = defaultdict(int)
a = int(input())
z = list(map(int, input().split()))
s = input()
flag = 0
from collections import defaultdict
ans = [0 for i in range(len(z))]
for i in range(len(z) - 2, -1, -1):
	if s[i] == '0':
		ans[i] = 0
	else:
		ans[i] = ans[i + 1] + 1
for i in range(len(z)):
	al[z[i]] = i + 1
for i in range(len(z)):
	if z[i] == i + 1 or al[i + 1] < i + 1:
		continue
	elif al[i + 1] - (i + 1) > ans[i]:
		flag = 1
		break
if flag == 1:
	print('NO')
else:
	print('YES')


import math
import sys
import bisect
from heapq import heapify, heappop, heappush
from itertools import *
from collections import deque, OrderedDict
from pprint import pprint
sys.setrecursionlimit(10 ** 6)
II = lambda : sys.stdin.readline()
inp = lambda : int(II())
inpm = lambda : map(int, II().split())
inpl = lambda : list(inpm())
DEBUG = False

def set_debug(deb: bool=True):
	global DEBUG
	DEBUG = deb

def debug(**kwargs):
	if DEBUG:
		arr = []
		for (k, w) in kwargs.items():
			arr.append(f'{k}: {w}')
		print(*arr, sep=', ')
(WHITE, GREY, BLACK, RED) = (0, 1, 2, 3)
EPS = 1e-09
INF = int(1e+18)
MOD = int(1000000000.0) + 7
N = 2000009

def solve():
	n = inp()
	a = inpl()
	s = input() + '0'
	b = []
	c = []
	for i in range(n):
		if s[i] == '1':
			c.append(a[i])
		else:
			if len(c) == 0:
				b.append(a[i])
				continue
			c.append(a[i])
			c.sort()
			b += c
			c = []
	a.sort()
	if a == b:
		print('YES')
	else:
		print('NO')

def main():
	t = 1
	for i in range(t):
		solve()
main()


n = int(input())
f = list(map(int, input().split()))
df = list(input())
a = 0
for i in range(n - 1):
	if df[i] == '1':
		a += 1
	if df[i] == '0':
		if a != 0:
			o = i - a
			f[o:i + 1] = sorted(f[o:i + 1])
		a = 0
	if i == n - 2:
		if a != 0:
			o = i - a
			f[o + 1:i + 2] = sorted(f[o + 1:i + 2])
for i in range(1, n):
	if f[i] < f[i - 1]:
		print('NO')
		break
else:
	print('YES')


n = int(input())
s = list(map(int, input().split()))
st = input()
mas = []
dop = [s[0]]
for i in range(n - 1):
	if st[i] == '0':
		dop.sort()
		mas += dop
		dop = [s[i + 1]]
	else:
		dop += [s[i + 1]]
dop.sort()
mas += dop
t = 0
for i in range(len(mas) - 1):
	if mas[i] > mas[i + 1]:
		t = 1
		break
if t == 1:
	print('NO')
else:
	print('YES')


n = int(input())
a = list(map(int, input().split()))
s = input()
d = []
cl = 0
mi = 0
i = 0
while i < len(s):
	while i < len(s) and s[i] == '1':
		i += 1
		cl += 1
	while i < len(s) and s[i] == '0':
		i += 1
	d.append([mi, cl])
	cl = 0
	mi = i
d.append([n - 1, 1])
if s[-1] != '1':
	d.append([n - 1, 0])
z = []
for i in range(len(d) - 1):
	z += sorted(a[d[i][0]:d[i][1] + d[i][0] + 1])
	z += a[d[i][1] + d[i][0] + 1:d[i + 1][0]]
print('YES' if z == list(range(1, n + 1)) else 'NO')


from collections import defaultdict
n = int(input())
li = [int(x) for x in input().split()]
string = input()
parent = defaultdict(int)
rank = defaultdict(int)
for i in range(n + 1):
	parent[i] = i
	rank[i] = 0

def find(k):
	if parent[k] != k:
		parent[k] = find(parent[k])
	return parent[k]

def union(a, b):
	x = find(a)
	y = find(b)
	if x == y:
		return
	elif rank[x] < rank[y]:
		parent[x] = y
	elif rank[x] > rank[y]:
		parent[y] = x
	else:
		parent[x] = y
		rank[y] += 1
for (i, j) in enumerate(string):
	if j == '1':
		union(li[i], li[i + 1])
for (i, j) in enumerate(li):
	if j != i + 1:
		if find(j) != find(i + 1):
			print('NO')
			exit()
print('YES')


import sys
import math
import collections
import heapq
input = sys.stdin.readline
n = int(input())
l = [int(i) for i in input().split()]
l1 = list(input())
l2 = []
l3 = []
if n == 1:
	print('YES')
elif n == 2:
	if l1[0] == '1':
		print('YES')
	elif l == [1, 2]:
		print('YES')
	else:
		print('NO')
else:
	for i in range(n - 1):
		if l1[i] == '0':
			if i == 0:
				l2.append(l[i])
			elif l1[i - 1] == '1':
				l3.append(l[i])
				l3.sort()
				l2 += l3
				l3 = []
			else:
				l2.append(l[i])
		else:
			l3.append(l[i])
	if l1[n - 2] == '1':
		l3.append(l[n - 1])
		l3.sort()
		l2 += l3
	else:
		l2.append(l[n - 1])
	c = 1
	for i in range(n):
		if l2[i] != i + 1:
			c = 0
			break
	if c == 1:
		print('YES')
	else:
		print('NO')


n = int(input())
a = list(map(int, input().split()))
s = input()
temp = []
mini = n + 1
maxi = -1
for i in range(n - 1):
	if s[i] == '1':
		mini = min(mini, a[i])
		maxi = max(maxi, a[i])
	elif i != 0 and s[i - 1] == '1':
		mini = min(mini, a[i])
		maxi = max(maxi, a[i])
		temp.append(mini)
		temp.append(maxi)
		mini = n + 1
		maxi = -1
	else:
		temp.append(a[i])
if mini != n + 1:
	mini = min(mini, a[-1])
	maxi = max(maxi, a[-1])
	temp.append(mini)
	temp.append(maxi)
else:
	temp.append(a[-1])
for i in range(1, len(temp)):
	if temp[i] < temp[i - 1]:
		print('NO')
		break
else:
	print('YES')


(n, a, s, t, q) = (int(input()), list(map(int, input().split())), input(), 0, 0)
for i in range(n - 1):
	q = max(q, a[i])
	if s[i] == '0' and q > i + 1:
		t = 1
		break
print('YNEOS'[t::2])


n = int(input())
lst = input().split()
st = str(input())
ans = 1
m = -1
for i in range(0, n - 1):
	m = max(m, int(lst[i]))
	if st[i] == '0':
		if m > i + 1:
			ans = 0
if ans == 1:
	print('YES')
else:
	print('NO')


from math import *
from collections import *
import queue
n = int(input())
l = list(map(int, input().split()))
s = input()
i = 0
while i < n - 1:
	st = i
	while i < n - 1 and s[i] == '1':
		i += 1
	l[st:i + 1] = sorted(l[st:i + 1])
	i += 1
if l == sorted(l):
	print('YES')
else:
	print('NO')


n = int(input())
l = list(map(int, input().split()))
s = input()
x = 0
m = l[0]
for i in range(n - 1):
	m = max(l[i], m)
	if s[i] == '0' and m > i + 1:
		x += 1
		print('NO')
		break
if x == 0:
	print('YES')


import os
from io import BytesIO, IOBase
import sys

def main():
	n = int(input())
	a = list(map(int, input().split()))
	b = input()
	mx = [0 for i in range(n)]
	mn = [0 for i in range(n)]
	mx[0] = a[0]
	for i in range(1, n):
		mx[i] = max(mx[i - 1], a[i])
	mn[-1] = a[-1]
	for i in range(n - 2, -1, -1):
		mn[i] = min(mn[i + 1], a[i])
	ans = True
	for i in range(0, n - 1):
		if mx[i] > mn[i + 1] and b[i] == '0':
			ans = False
			break
	print('YES' if ans else 'NO')
	return
BUFSIZE = 1048576

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')
main()


R = lambda : list(map(int, input().split()))
n = int(input())
arr = R()
s = input()
i = 0
while i < len(s):
	j = i
	while j < len(s) and s[j] == '1':
		j += 1
	if j > i:
		arr[i:j + 1] = sorted(arr[i:j + 1])
	i = j + 1
ok = True
val = 1
for i in range(n):
	if arr[i] == val:
		val += 1
	else:
		ok = False
		break
if ok:
	print('YES')
else:
	print('NO')


n = int(input())
a_s = input()
a = [int(x) for x in a_s.split(' ')]
s = input()
vmax = 0
for i in range(n):
	if a[i] != i + 1:
		vmax = max(vmax, a[i])
	if i < n - 1 and s[i] == '0' and (i < vmax - 1):
		print('NO')
		exit(0)
print('YES')


n = int(input())
a = list(map(int, input().split()))
s = input()
sa = sorted(a)
h = {}
for i in range(n):
	h[sa[i]] = i
st = 0
for i in range(n):
	p = i
	ap = h[a[i]]
	if p - ap > st:
		print('NO')
		return
	if i < n - 1 and s[i] == '1':
		st += 1
	else:
		st = 0
st = 0
for i in range(n - 1, -1, -1):
	if i < n - 1 and s[i] == '1':
		st += 1
	else:
		st = 0
	p = i
	ap = h[a[i]]
	if ap - p > st + 1:
		print('NO')
		return
print('YES')


def solve(n, a, s):
	indices = [0 for i in range(n)]
	for (i, elem) in enumerate(a):
		indices[elem - 1] = i
	ranges = [-1 for i in range(n)]
	i = 0
	r = -1
	n_r = 0
	while i < n - 1:
		if s[i] == '0':
			r = -1
		else:
			if r == -1:
				r = n_r
				n_r += 1
			ranges[i] = r
			ranges[i + 1] = r
		i += 1
	for j in range(n):
		if indices[j] != j:
			dest = j
			target = j + 1
			t_ind = indices[j]
			if ranges[dest] == -1 or ranges[t_ind] == -1 or ranges[dest] != ranges[t_ind]:
				return False
			num_at_index = a[j]
			swap(indices, j, num_at_index - 1)
			a[j] = target
			a[indices[num_at_index - 1]] = num_at_index
	return True

def swap(a, i, j):
	temp = a[i]
	a[i] = a[j]
	a[j] = temp
n = int(input())
a = [int(i) for i in input().split()]
s = input()
out = 'YES' if solve(n, a, s) else 'NO'
print(out)


n = int(input())
l = list(map(int, input().split()))
st = input()
maxi = 0
for i in range(n - 1):
	maxi = max(maxi, l[i])
	if st[i] == '0' and maxi > i + 1:
		print('NO')
		break
else:
	print('YES')


n = int(input())
m = 0
a = [int(x) for x in input().split()]
s = input()
m = 0
for x in range(n - 1):
	m = max(a[x], m)
	if s[x] == '0' and m > x + 1:
		print('NO')
		quit()
print('YES')


import getpass
import sys
import math

def ria():
	return [int(i) for i in input().split()]
files = True
if getpass.getuser() == 'frohenk' and files:
	sys.stdin = open('test.in')
n = ria()[0]
ar = ria()
st = input()
zer = True
(l, r) = (0, 0)
mx = 0
mn = 200020
changable = [False] * n
for i in range(len(st)):
	if st[i] == '0':
		if not zer:
			if l + 1 > mn or r + 1 < mx:
				print('NO')
				exit(0)
		mx = 0
		mn = 200020
		zer = True
		continue
	if zer:
		l = i
	r = i + 1
	mx = max(mx, ar[i])
	mx = max(mx, ar[i + 1])
	mn = min(mn, ar[i])
	mn = min(mn, ar[i + 1])
	changable[i] = True
	changable[i + 1] = True
	zer = False
for (n, i) in enumerate(ar):
	if n + 1 != i and (not changable[n]):
		print('NO')
		exit(0)
print('YES')


from itertools import accumulate
n = int(input())
a = list(map(int, input().split()))
s = input()
t = [0]
t.extend(list(accumulate(map(int, s))))
for (i, el) in enumerate(a, 1):
	if el != i:
		if el > i:
			if t[el - 1] - t[i - 1] != el - i:
				print('NO')
				exit()
		elif t[i - 1] - t[el - 1] != i - el:
			print('NO')
			exit()
print('YES')


n = int(input())
a = list(map(int, input().split()))
s = list(map(lambda x: True if x == '1' else False, list(input())))
i = 0
while i < n:
	if i == n - 1:
		if a[-1] != n:
			print('NO')
	elif not s[i]:
		if a[i] - 1 != i:
			print('NO')
			exit()
	else:
		start = i
		i += 1
		while i < len(s) and s[i]:
			i += 1
		end = i
		test = a[start:end + 1]
		if not (max(test) == end + 1 and min(test) == start + 1):
			print('NO')
			exit()
	i += 1
print('YES')


n = int(input())
a = list(map(int, input().split()))
s = input() + '0'
sol = True
l = 1
for i in range(n):
	if s[i] == '0':
		r = i + 1
		cnt = 0
		for j in range(l, r + 1):
			if l <= a[j - 1] and a[j - 1] <= r:
				cnt += 1
		if cnt != r - l + 1:
			sol = False
		l = r + 1
if sol:
	print('YES')
else:
	print('NO')


n = int(input())
a = [int(x) for x in input().split()]
ch = input()
ch = ch + '0'
cur_m = 0
end = False
i = 0
while not end and i < n:
	cur_m = max(cur_m, a[i])
	if ch[i] == '0' and cur_m > i + 1:
		end = True
		break
	i = i + 1
if end:
	print('NO')
else:
	print('YES')


cond = True
n = int(input())
a = list(map(int, input().split()))
s = input()
act_l = list()
last_l = list()
for i in range(n):
	act_l.append(a[i])
	if i + 1 < n and s[i] == '0':
		if len(last_l) > 0 and max(last_l) > min(act_l):
			cond = False
			break
		else:
			last_l = act_l.copy()
			act_l.clear()
if len(last_l) > 0 and max(last_l) > min(act_l):
	cond = False
if cond:
	print('YES')
else:
	print('NO')


import sys, math, itertools
from collections import Counter, deque, defaultdict
from bisect import bisect_left, bisect_right
from heapq import heappop, heappush, heapify, nlargest
from copy import deepcopy
mod = 10 ** 9 + 7
INF = float('inf')

def inp():
	return int(sys.stdin.readline())

def inpl():
	return list(map(int, sys.stdin.readline().split()))

def inpl_1():
	return list(map(lambda x: int(x) - 1, sys.stdin.readline().split()))

def inps():
	return sys.stdin.readline()

def inpsl(x):
	tmp = sys.stdin.readline()
	return list(tmp[:x])

def err(x):
	print(x)
	exit()
n = inp()
a = inpl()
s = inpsl(n - 1)
segs = []
now = [a[0]]
for i in range(n - 1):
	if s[i] == '1':
		now.append(a[i + 1])
	else:
		segs.append(now)
		now = []
		now.append(a[i + 1])
segs.append(now)
mx = 0
for seg in segs:
	if min(seg) <= mx:
		print('NO')
		break
	mx = max(seg)
else:
	print('YES')


n = int(input())
a = list(map(int, input().split()))
b = []
s = input()
s = s[:] + '0'
k = 0
for i in range(n):
	if s[i] == '0':
		t = a[k:i + 1]
		k = i + 1
		t.sort()
		b += t
a.sort()
ans = True
for i in range(len(a)):
	if a[i] != b[i]:
		ans = False
		break
if ans:
	print('YES')
else:
	print('NO')


import sys, math, queue, bisect
MOD = 10 ** 9 + 7
sys.setrecursionlimit(1000000)
n = int(input())
a = list(map(int, input().split()))
s = input()
cycles = [set()]
for i in range(len(s)):
	if s[i] == '0':
		if len(cycles[-1]) != 0:
			cycles.append(set())
	else:
		cycles[-1].add((a[i] - 1, i))
		cycles[-1].add((a[i + 1] - 1, i + 1))
if len(cycles[-1]) == 0:
	cycles.pop()
for cycle in cycles:
	(x, y) = (set(), set())
	for (val, indx) in cycle:
		x.add(val)
		y.add(indx)
	if x == y:
		for (val, indx) in cycle:
			a[indx] = indx
print('YES' if a == sorted(a) else 'NO')


def solve():
	n = int(input())
	a = [int(x) - 1 for x in input().split(' ')]
	s = input()
	pref = [0]
	for x in s:
		pref.append(pref[-1] + int(x))
	v = set()
	for i in range(n):
		if i not in v:
			v.add(i)
			j = i
			p = [j]
			if a[j] != j:
				while a[j] not in v:
					j = a[j]
					v.add(j)
					p.append(j)
			if max(p) - min(p) != pref[max(p)] - pref[min(p)]:
				return 'NO'
	return 'YES'
print(solve())


from sys import stdin
n = int(stdin.readline())
a = list(map(int, stdin.readline().split()))
s = stdin.readline().rstrip()
segs = []
i = 0
while i < n - 1:
	if s[i] == '1':
		left = i
		while i < n - 1 and s[i] == '1':
			i += 1
		segs.append((left, i))
	i += 1
for (left, right) in segs:
	a[left:right + 1] = sorted(a[left:right + 1])
print('YES' if a == list(range(1, n + 1)) else 'NO')


n = int(input())
a = list(map(int, input().split()))
t = input()
mx = [0 for i in range(n + 1)]
mn = [float('inf') for i in range(n + 2)]
total = 0
for i in range(1, n + 1):
	mx[i] = max(mx[i - 1], a[i - 1])
for i in range(n, 0, -1):
	mn[i] = min(mn[i + 1], a[i - 1])
for i in range(n - 1):
	if t[i] == '1':
		continue
	elif mx[i + 1] > mn[i + 2]:
		print('NO')
		exit()
print('YES')


n = int(input())
arr = list(map(int, input().split(' ')))
s = list(map(int, list(input())))
pre = [0]
flag = True
for i in s:
	pre.append(pre[-1] + i)
for i in range(len(arr)):
	if arr[i] > i + 1:
		if pre[arr[i] - 1] - pre[i] != arr[i] - (i + 1):
			flag = False
			print('NO')
			break
if flag:
	print('YES')


def main():
	n = int(input())
	arr = list(map(int, input().split()))
	s = input()
	final = []
	curr = set()
	for i in range(n - 1):
		if s[i] == '1':
			curr.add(i)
			curr.add(i + 1)
		else:
			curr = list(curr)
			for j in range(len(curr)):
				curr[j] = arr[curr[j]]
			curr.sort()
			final.extend(curr)
			curr = set()
			if i - 1 >= 0 and s[i - 1] == '0':
				final.append(arr[i])
			elif i - 1 < 0:
				final.append(arr[i])
	if curr:
		curr = list(curr)
		for j in range(len(curr)):
			curr[j] = arr[curr[j]]
		curr.sort()
		final.extend(curr)
	if len(final) != n:
		final.append(arr[-1])
	good = True
	for i in range(n):
		if final[i] != i + 1:
			good = False
			break
	if good:
		print('YES')
		return
	print('NO')
main()


n = int(input())
a = list(map(int, input().split()))
s = input() + '0'
f = 0
max_so_far = 0
bad = False
for i in range(len(a)):
	if a[i] > max_so_far:
		max_so_far = a[i]
	if s[i] == '0':
		if max_so_far != i + 1:
			print('NO')
			bad = True
			break
if not bad:
	print('YES')


def solve(n, ar, s):
	maxi = 0
	for i in range(n - 1):
		maxi = max(maxi, ar[i])
		if s[i] == '0' and maxi > i + 1:
			print('NO')
			return
	print('YES')
n = int(input())
ar = list(map(int, input().split()))
s = input()
solve(n, ar, s)


n = int(input())
a = list(map(int, input().split()))
s = input()
i = 0
j = 0
c = []
while i < n - 1:
	if s[i] == '0':
		c.append([j, i])
		j = i + 1
	i += 1
c.append([j, n - 1])
j = 0
f = 1
for i in range(n):
	if i > c[j][1]:
		j += 1
	if a[i] < c[j][0] + 1 or a[i] > c[j][1] + 1:
		f = 0
		break
if f == 1:
	print('YES')
else:
	print('NO')


n = int(input())
l = [int(x) for x in input().split()]
s = input() + '0'
i = minn = maxx = 0
flag = 0
while i < n:
	minn = maxx = l[i]
	start = i + 1
	while s[i] != '0':
		minn = min(minn, l[i])
		maxx = max(maxx, l[i])
		i += 1
	minn = min(minn, l[i])
	maxx = max(maxx, l[i])
	end = i + 1
	if minn >= start and maxx <= end:
		i += 1
	else:
		flag = 1
		break
if flag:
	print('NO')
else:
	print('YES')


def fun(a, ind):
	maximum = a[0]
	for i in range(n - 1):
		maximum = max(a[i], maximum)
		if ind[i] == '0' and maximum > i + 1:
			return 'NO'
	return 'YES'
n = int(input())
a = list(map(int, input().split()))
ind = input()
print(fun(a, ind))


n = int(input())
alist = list(map(int, input().split()))
binary = input() + '0'
previous = '0'
maxx = 0
minx = 200001
pointer = 0
flag = True
for i in range(n):
	if binary[i] == '0' and previous == '0':
		if alist[i] != i + 1:
			flag = False
			break
	elif binary[i] == '1' and previous == '0':
		maxx = alist[i]
		minx = alist[i]
		pointer = i
	elif binary[i] == '1' and previous == '1':
		maxx = max(alist[i], maxx)
		minx = min(alist[i], minx)
	elif binary[i] == '0' and previous == '1':
		maxx = max(alist[i], maxx)
		minx = min(alist[i], minx)
		if maxx != i + 1 or minx != pointer + 1:
			flag = False
			break
	previous = binary[i]
print(['NO', 'YES'][flag])


n = int(input())
a = list(map(int, input().split()))
s = input()
if '0' not in s:
	print('YES')
	exit()
end = -1
for i in range(n - 1, -1, -1):
	if a[i] != i + 1:
		end = i
		break
for i in range(n):
	dif = a[i] - 1 - i
	if dif > 0:
		if '0' in s[i:dif + i]:
			print('NO')
			exit()
		if dif + i == end:
			break
print('YES')


from sys import stdin, stdout

def find(num):
	x = []
	while req[num] > 0:
		x.append(num)
		num = req[num]
	for i in x:
		req[i] = num
	return num

def union(a, b):
	(A, B) = (find(a), find(b))
	if A != B:
		if req[A] > req[B]:
			(A, B) = (B, A)
		req[A] += req[B]
		req[B] = A
n = int(stdin.readline().strip())
req = [-1] * (n + 1)
a = [int(num) for num in stdin.readline().strip().split()]
b = [int(num) for num in stdin.readline().strip()]
req = [b[0]]
index = [-1] * (n + 1)
for i in range(1, n - 1):
	req.append(req[-1] + b[i])
req.append(req[-1])
dupli_a = sorted(a)
condn = True
for i in range(n):
	index[a[i]] = i
for i in range(n):
	if a[i] != dupli_a[i]:
		l = min(index[a[i]], index[dupli_a[i]])
		r = max(index[a[i]], index[dupli_a[i]])
		if b[l] == 1:
			cur_sum = req[r] if l == 0 else req[r] - req[l - 1]
			if cur_sum < r - l:
				condn = False
				break
			elif cur_sum == r - l and (r + 1 != n and b[r] != 0):
				condn = False
				break
		else:
			condn = False
			break
if condn:
	stdout.write('YES')
else:
	stdout.write('NO')


n = int(input())
A = list(map(int, input().split()))
flag = input()
zero_flag = []
for i in range(n - 1):
	if flag[i] == '0':
		zero_flag.append(i)
for i in range(len(zero_flag)):
	if i == len(zero_flag) - 1:
		if set(A[zero_flag[i] + 1:]) != set(range(zero_flag[i] + 2, n + 1)):
			print('NO')
			quit()
	elif i == 0:
		if set(A[:zero_flag[i] + 1]) != set(range(1, zero_flag[i] + 2)):
			print('NO')
			quit()
	elif set(A[zero_flag[i - 1] + 1:zero_flag[i] + 1]) != set(range(zero_flag[i - 1] + 2, zero_flag[i] + 2)):
		print('NO')
		quit()
print('YES')


def fail():
	print('NO')
	exit(0)
n = int(input())
a = [int(i) for i in input().split()]
assert len(a) == n
s = input()
assert len(s) == n - 1
max_val = 0
i = 0
for j in s:
	j = int(j)
	if j == 0:
		if max_val:
			if i + 1 != max(max_val, a[i]):
				fail()
			max_val = 0
		elif a[i] != i + 1:
			fail()
	elif max_val < a[i]:
		max_val = a[i]
	i += 1
if max_val:
	if n != max(max_val, a[-1]):
		fail()
elif a[-1] != n:
	fail()
print('YES')


n = int(input())
l = [int(x) for x in input().split()]
a = input()
s = set()
res = []
for i in range(n - 1):
	if a[i] == '1':
		s.add(l[i])
		s.add(l[i + 1])
	elif s != set():
		res.append(min(s))
		res.append(max(s))
		s = set()
	else:
		res.append(l[i])
else:
	if a[i] == '0':
		res.append(l[i + 1])
	else:
		res.append(min(s))
for i in range(len(res) - 1):
	if res[i] > res[i + 1]:
		print('NO')
		exit()
print('YES')


import math
import sys
input = sys.stdin.readline

def main():
	n = int(input())
	a = [int(x) - 1 for x in input().split(' ')]
	s = [int(x) for x in input().strip()]
	stack = []
	prev = [-1]
	for i in range(n - 1):
		stack.append(a[i])
		if s[i] == 0:
			while len(stack) > 0:
				num = stack.pop()
				if prev[-1] < num <= i:
					continue
				else:
					print('NO')
					return
			prev.append(i)
	print('YES')
main()


n = int(input())
a = list(map(int, input().split()))
p = input()
m = 0
suc = True
for i in range(n - 1):
	m = max(m, a[i])
	if p[i] == '0' and m > i + 1:
		suc = False
		break
if suc:
	print('YES')
else:
	print('NO')


n = int(input())
a = [int(i) for i in input().split()]
ind = [int(i) for i in input()]
ind.append(0)
b = []
curr = 0
for i in range(n):
	if not ind[i]:
		tmp = a[curr:i + 1]
		tmp.sort()
		b.extend(tmp)
		curr = i + 1
if all((b[i] <= b[i + 1] for i in range(n - 1))):
	print('YES')
else:
	print('NO')


n = int(input())
a = input().split()
b = input() + '1'
c = []
k = 0
for u in b:
	c.append(k)
	if u == '0':
		k += 1
for i in range(0, n):
	if c[i] != c[int(a[i]) - 1]:
		print('NO')
		exit()
print('YES')
exit()


import sys
input = sys.stdin.readline

def get_pairs(n, swap):
	n -= 1
	pairs = []
	i = 0
	while i < n:
		if swap[i]:
			start = i
			while i + 1 < n and swap[i + 1]:
				i += 1
			end = i + 1
			pairs.append((start, end))
			i += 1
		else:
			i += 1
	return pairs
n = int(input())
a = list(map(int, input().split()))
goal = sorted(a)
swap = [int(i) for i in input().rstrip()]
pairs = get_pairs(n, swap)
for (x, y) in pairs:
	a[x:y + 1] = sorted(a[x:y + 1])
if a == goal:
	print('YES')
else:
	print('NO')


n = int(input())
a = [int(i) for i in input().split()]
c = input()
x = []
hashm = [0 for i in range(n)]
for i in range(n):
	hashm[a[i] - 1] = i
max1 = 0
c1 = 0
for i in range(n - 1):
	if c[i] == '1':
		max1 = max(max1, a[i] - 1)
	elif c[i] == '0' and max1 <= i and (a[i] - 1 <= i):
		continue
	else:
		c1 = 1
if c1 == 0:
	print('YES')
else:
	print('NO')


n = int(input())
a = list(map(int, input().split(' ')))
s = list(map(int, input()))
right = [-1] * n
i = n - 2
right[n - 1] = n - 1
if s[i] == 1:
	right[i] = i + 1
else:
	right[i] = i
i -= 1
while i >= 0:
	if s[i] == 0:
		right[i] = i
	elif s[i + 1] == 0:
		right[i] = i + 1
	else:
		right[i] = right[i + 1]
	i -= 1
ans = True
i = 0
for ai in a:
	if ai - 1 > right[i]:
		ans = False
	i += 1
print('YES' if ans else 'NO')


import bisect
import os
import gc
import sys
from io import BytesIO, IOBase
from collections import Counter
from collections import deque
import heapq
import math
import statistics

def sin():
	return input()

def ain():
	return list(map(int, sin().split()))

def sain():
	return input().split()

def iin():
	return int(sin())
MAX = float('inf')
MIN = float('-inf')
MOD = 1000000007

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	s = set()
	for p in range(2, n + 1):
		if prime[p]:
			s.add(p)
	return s

def readTree(n, m):
	adj = [deque([]) for _ in range(n + 1)]
	for _ in range(m):
		(u, v) = ain()
		adj[u].append(v)
		adj[v].append(u)
	return adj

def main():
	n = iin()
	l = ain()
	s = sin()
	d = deque([])
	c = 0
	for i in s[::-1]:
		if i == '1':
			c += 1
			d.appendleft(c)
		else:
			d.appendleft(0)
			c = 0
	flag = 0
	for i in range(n):
		if i + 1 < l[i]:
			x = l[i] - i - 1
			if d[i] < x:
				flag = 1
				break
		elif i + 1 > l[i]:
			if d[l[i] - 1] + l[i] - 2 < i - 1:
				flag = 1
				break
	if flag == 1:
		print('NO')
	else:
		print('YES')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
if os.getcwd() == 'D:\\code':
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
	input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import sys, os, io, time, copy, math
from collections import deque
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

def main():
	n = int(input())
	arr = list(map(int, input().split()))
	string = input()
	parent = {}
	for i in range(1, n + 1):
		parent[i] = i
	for i in range(n - 1):
		if string[i] == '1':
			(u, v) = (arr[i], arr[i + 1])
			parent[v] = parent[u]
	flag = 0
	for i in range(n):
		if arr[i] != i + 1:
			if parent[i + 1] != parent[arr[i]]:
				flag = 1
				break
	if flag == 0:
		print('YES')
	else:
		print('NO')
main()


from sys import stdin, exit
from typing import List, Tuple, Dict
from itertools import accumulate
stdin.readline()
array = [int(v) for v in stdin.readline().rstrip().split()]
rules = [int(v) for v in stdin.readline().rstrip()]
rules_zeroscnt = [0] * len(array)
for i in range(1, len(array)):
	rules_zeroscnt[i] = rules_zeroscnt[i - 1] + (1 if rules[i - 1] == 0 else 0)
for i in range(len(array)):
	if array[i] - 1 != i:
		req_position = array[i] - 1
		if rules_zeroscnt[req_position] != rules_zeroscnt[i]:
			print('NO')
			exit(0)
print('YES')


from sys import stdin, stdout

def find(num):
	x = []
	while req[num] > 0:
		x.append(num)
		num = req[num]
	for i in x:
		req[i] = num
	return num

def union(a, b):
	(A, B) = (find(a), find(b))
	if A != B:
		if req[A] > req[B]:
			(A, B) = (B, A)
		req[A] += req[B]
		req[B] = A
n = int(stdin.readline().strip())
req = [-1] * (n + 1)
a = [int(num) for num in stdin.readline().strip().split()]
b = [int(num) for num in stdin.readline().strip()]
for i in range(n - 1):
	if b[i] == 1:
		union(a[i], a[i + 1])
	else:
		continue
dupli_a = sorted(a)
condn = True
for i in range(n):
	if a[i] != dupli_a[i]:
		if find(a[i]) != find(dupli_a[i]):
			condn = False
			break
if condn:
	stdout.write('YES')
else:
	stdout.write('NO')


n = int(input())
l = list(map(int, input().split()))
s = input()
p = 0
for i in range(1, n):
	if s[i - 1] == '0':
		p = i + 1
	if l[i] < p:
		print('NO')
		exit()
print('YES')


from itertools import *
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input()))
c = list(accumulate(b))
y = 1
for i in range(n):
	j = a[i] - 1
	if i != j:
		(l, r) = sorted([i, j])
		cl = c[l] - b[l]
		cr = c[r - 1]
		if cr - cl < r - l:
			y = 0
print('YES' if y else 'NO')


def aaa():
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input()))
	b = [0] + b + [0]
	for ii in range(n):
		aa = a[ii]
		bl = b[ii]
		br = b[ii + 1]
		if bl == 0:
			lo = hi = aa
			l1 = ii + 1
		else:
			lo = min(lo, aa)
			hi = max(hi, aa)
		if br == 0:
			r1 = ii + 1
			if lo != l1 or hi != r1:
				return 1
aaa = aaa()
print('NO' if aaa else 'YES')


def check(vals, allowed):
	groups = []
	group = []
	for (v, b) in zip(vals, allowed):
		group.append(v)
		if b == '0':
			group.sort()
			groups.append(group)
			group = []
	flat = [v for group in groups for v in group]
	for i in range(1, len(flat)):
		if flat[i] < flat[i - 1]:
			return False
	return True
n = int(input())
vals = [int(v) for v in input().split()]
allowed = input().strip() + '0'
print(['NO', 'YES'][check(vals, allowed)])


n = int(input())
arr = list(map(int, input().split()))
s = input() + '0'
pref = []
for i in range(n):
	val = int(s[i])
	if i == 0:
		pref.append(val)
	else:
		pref.append(val + pref[-1])
pref += [0]
sort_arr = sorted(arr)
d = {}
for i in range(n):
	d[sort_arr[i]] = i
flag = True
for i in range(n):
	if arr[i] != sort_arr[i]:
		val = arr[i]
		idx = d[val]
		if d[val] > i:
			c = pref[idx] - pref[i - 1] - int(s[idx])
			if idx - i <= c:
				continue
			else:
				flag = False
				break
		else:
			c = pref[i] - pref[idx - 1] - int(s[i])
			if i - idx <= c:
				continue
			else:
				flag = False
				break
if flag:
	print('YES')
else:
	print('NO')