There is a building with 2N floors, numbered 1, 2, \ldots, 2N from bottom to top.
The elevator in this building moved from Floor 1 to Floor 2N just once.
On the way, N persons got on and off the elevator. Each person i (1 \leq i \leq N) got on at Floor A_i and off at Floor B_i. Here, 1 \leq A_i < B_i \leq 2N, and just one person got on or off at each floor.
Additionally, because of their difficult personalities, the following condition was satisfied:
 - Let C_i (= B_i - A_i - 1) be the number of times, while Person i were on the elevator, other persons got on or off. Then, the following holds:
 - If there was a moment when both Person i and Person j were on the elevator, C_i = C_j.
We recorded the sequences A and B, but unfortunately, we have lost some of the records. If the record of A_i or B_i is lost, it will be given to you as -1.
Additionally, the remaining records may be incorrect.
Determine whether there is a pair of A and B that is consistent with the remaining records.

-----Constraints-----
 - 1 \leq N \leq 100
 - A_i = -1 or 1 \leq A_i \leq 2N.
 - B_i = -1 or 1 \leq B_i \leq 2N.
 - All values in input are integers.

-----Input-----
Input is given from Standard Input in the following format:
N
A_1 B_1
A_2 B_2
:
A_N B_N

-----Output-----
If there is a pair of A and B that is consistent with the remaining records, print Yes; otherwise, print No.

-----Sample Input-----
3
1 -1
-1 4
-1 6

-----Sample Output-----
Yes

For example, if B_1 = 3, A_2 = 2, and A_3 = 5, all the requirements are met.
In this case, there is a moment when both Person 1 and Person 2 were on the elevator, which is fine since C_1 = C_2 = 1.

Difficulty: MEDIUM_HARD

Starter code:

Solutions:
import sys
readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)

def main():
	N = int(readline())
	L = 2 * N
	floor = [[0, 0] for _ in range(L)]
	com = dict()
	for i in range(1, N + 1):
		(A, B) = map(int, readline().split())
		com[i] = [-1, -1]
		if A != -1:
			if floor[A - 1][1] == 0:
				floor[A - 1] = [i, 1]
				com[i][0] = A - 1
			else:
				return print('No')
		if B != -1:
			if floor[B - 1][1] == 0:
				floor[B - 1] = [i, 2]
				com[i][1] = B - 1
			else:
				return print('No')
		if A != -1 and B != -1:
			if A >= B:
				return print('No')
	dp = [False] * (L + 1)
	if floor[0][1] == 2:
		return print('No')
	else:
		dp[0] = True
	for i in range(L):
		if not dp[i]:
			continue
		for j in range(i + 1, L, 2):
			ok = True
			w = (j - i + 1) // 2
			for k in range(w):
				p = i + k
				q = i + w + k
				if floor[p][1] == 2 or floor[q][1] == 1:
					ok = False
				if floor[p][1] == 1 and floor[q][1] == 2:
					if floor[p][0] != floor[q][0]:
						ok = False
				if floor[p][1] == 1:
					f = floor[p][0]
					if com[f][1] != q and com[f][1] != -1:
						ok = False
				if floor[q][1] == 2:
					f = floor[q][0]
					if com[f][0] != p and com[f][0] != -1:
						ok = False
			if ok:
				dp[j + 1] = True
	print('Yes') if dp[L] else print('No')

def __starting_point():
	main()
__starting_point()


((n,), *t) = [[*map(int, t.split())] for t in open(0)]
e = [(f := 0)] * n * 4
for (a, b) in t:
	for (j, (c, d)) in enumerate(t):
		f |= d > 0 < a < c < b > b - a != d - c
		if b > c > a > 0 > d:
			t[j] = (c, c + b - a)
for (a, b) in t:
	e[a] += 1
	e[b] += 1
print('YNeos'[f + max(e[:-1]) > 1::2])


N = int(input())
A = [0 for i in range(N)]
B = [0 for i in range(N)]
used = [0 for i in range(2 * N)]
com = [-1 for i in range(2 * N)]
for i in range(N):
	(A[i], B[i]) = list(map(int, input().split()))
	if A[i] != -1:
		A[i] -= 1
		if used[A[i]]:
			print('No')
			return
		used[A[i]] = i + 1
	if B[i] != -1:
		B[i] -= 1
		if used[B[i]]:
			print('No')
			return
		used[B[i]] = -(i + 1)
	if A[i] != -1 and B[i] != -1:
		if A[i] > B[i]:
			print('No')
			return
		com[A[i]] = B[i]
		com[B[i]] = A[i]
dp = [False for i in range(2 * N + 1)]
dp[0] = True
for i in range(2 * N):
	if not dp[i]:
		continue
	for j in range(i + 1, 2 * N, 2):
		w = (j - i + 1) // 2
		ok = True
		for k in range(w):
			p = i + k
			q = i + k + w
			if com[p] != -1 and com[p] != q:
				ok = False
			if com[q] != -1 and com[q] != p:
				ok = False
			if used[p] != 0 and used[q] != 0 and (used[p] + used[q] != 0):
				ok = False
			if used[p] < 0 or used[q] > 0:
				ok = False
		if ok:
			dp[j + 1] = True
if dp[2 * N]:
	print('Yes')
else:
	print('No')


(R, P, I, J) = (range, print, input, int)
n = J(I())
(A, C, L) = ([list(map(J, I().split())) for i in R(n)], [0] * (2 * n), [-1] * (2 * n))
for i in R(n):
	(a, b) = A[i]
	a -= 1
	b -= 1
	if a != -2:
		if L[a] >= 0:
			P('No')
			return
		C[a] = 1
		L[a] = i
	if b != -2:
		if L[b] >= 0:
			P('No')
			return
		C[b] = -1
		L[b] = i

def f(l, r):
	m = (l + r) // 2
	if min(C[l:m]) == -1 or max(C[m:r]) == 1:
		return 0
	(X, Y) = (L[l:m], L[m:r])
	for i in R(len(X)):
		if X[i] == Y[i]:
			continue
		elif X[i] != -1 != Y[i]:
			return 0
		elif X[i] != -1 and X[i] in Y or (Y[i] != -1 and Y[i] in X):
			return 0
	return 1
dp = [0] * (n + 1)
dp[0] = 1
for i in R(n):
	for j in R(i + 1, n + 1):
		if dp[i] * f(i * 2, j * 2):
			dp[j] = 1
P('NYoe s'[dp[n]::2])


n = int(input())
AB = tuple((tuple(map(int, input().split())) for _ in range(n)))
C = [(-1, -1)] * (2 * n + 1)
for (i, (a, b)) in enumerate(AB):
	if a == -1 and b == -1:
		continue
	if a == -1:
		if C[b][0] != -1:
			print('No')
			return
		C[b] = (1, i)
		continue
	if b == -1:
		if C[a][0] != -1:
			print('No')
			return
		C[a] = (0, i)
		continue
	if a >= b:
		print('No')
		return
	if C[a][0] != -1 or C[b][0] != -1:
		print('No')
		return
	C[a] = (0, i)
	C[b] = (1, i)

def f(l, r):
	m = (r - l + 1) // 2
	for i in range(l, l + m):
		(left_direct, left_idx) = C[i]
		(right_direct, right_idx) = C[i + m]
		if left_direct == 1 or right_direct == 0:
			return False
		if left_direct != -1 and right_direct != -1:
			if left_idx != right_idx:
				return False
			continue
		if left_direct != -1:
			left_b = AB[left_idx][1]
			if left_b != -1 and i + m != left_b:
				return False
		if right_direct != -1:
			right_a = AB[right_idx][0]
			if right_a != -1 and i != right_a:
				return False
	return True
dp = [False] * (n + 1)
dp[0] = True
for i in range(n):
	if not dp[i]:
		continue
	for j in range(i + 1, n + 1):
		if f(2 * i + 1, 2 * j):
			dp[j] = True
if dp[n]:
	print('Yes')
else:
	print('No')


def match(l, r):
	L = r - l
	if L % 2 == 1:
		return False
	for i in range(L // 2):
		if R[P[l + i]] >= r and R[P[l + i]] != -1:
			return False
		if (P[l + i] == Q[l + L // 2 + i] or P[l + i] == 0 or Q[l + L // 2 + i] == 0) and Q[l + i] == P[l + L // 2 + i] == 0:
			continue
		return False
	return True
N = int(input())
(P, Q) = ([0] * (2 * N), [0] * (2 * N))
R = [-1] * (N + 1)
for i in range(1, N + 1):
	(A, B) = map(int, input().split())
	if A != -1 and P[A - 1] != 0 or (B != -1 and Q[B - 1] != 0):
		print('No')
		return
	if A != -1:
		P[A - 1] = i
	if B != -1:
		Q[B - 1] = i
		R[i] = B - 1
dp = [0] * (2 * N + 1)
dp[0] = 1
for i in range(2, 2 * N + 1):
	for j in range(i):
		if dp[j] == True and match(j, i):
			dp[i] = True
			break
print('Yes' if dp[-1] else 'No')


N = int(input())

def check_AB(A, B):
	if B >= N * 2:
		return False
	if A_table[B] != None:
		return False
	if B_table[A] != None:
		return False
	Ar = B_table[B]
	Br = A_table[A]
	if Ar == -1 and Br == -1:
		return False
	if not (Ar == -1 or Ar == None or Ar == A):
		return False
	if not (Br == -1 or Br == None or Br == B):
		return False
	return True

def check_A2B(A, B):
	if not check_AB(A, B):
		return False
	C = B - A - 1
	for A2 in range(A + 1, B):
		B2 = A2 + C + 1
		if not check_AB(A2, B2):
			return False
	return check(B + C + 1)

def check(A):
	if A >= N * 2:
		return True
	if B_table[A] != None:
		return False
	B = A_table[A]
	if B == None or B == -1:
		B_min = A + 1
		B_max = N * 2
		if not B_min < B_max:
			return False
	else:
		B_min = B
		B_max = B + 1
	for B in range(B_min, B_max):
		if check_A2B(A, B):
			return True
	return False
A_table = [None] * (2 * N)
B_table = [None] * (2 * N)

def read():
	free_count = 0
	for _ in range(N):
		(A, B) = list(map(int, input().split()))
		if A >= 1:
			A -= 1
		if B >= 1:
			B -= 1
		if A != -1 and B != -1 and (B <= A):
			return False
		if A == -1 and B == -1:
			free_count += 1
		if A != -1:
			if A_table[A] != None or B_table[A] != None:
				return False
		if B != -1:
			if B_table[B] != None or A_table[B] != None:
				return False
		if A != -1:
			A_table[A] = B
		if B != -1:
			B_table[B] = A
	return check(0)
if read():
	print('Yes')
else:
	print('No')


def f(l, r):
	L = r - l
	if L % 2:
		return 0
	for i in range(l, l + L // 2):
		if R[P[i]] >= r and R[P[i]] + 1:
			return 0
		if (P[i] == Q[L // 2 + i] or P[i] * Q[L // 2 + i] == 0) * (Q[i] + P[L // 2 + i] == 0):
			continue
		return 0
	return 1
N = int(input())
M = 2 * N
(P, Q, R, d) = ([0] * M, [0] * M, [-1] * (N + 1), [0] * (M + 1))
for i in range(1, N + 1):
	(A, B) = map(int, input().split())
	if A + 1 and P[A - 1] != 0 or (B + 1 and Q[B - 1]):
		print('No')
		return
	if A + 1:
		P[A - 1] = i
	if B + 1:
		Q[B - 1] = i
		R[i] = B - 1
d[0] = 1
for i in range(2, M + 1):
	for j in range(i):
		if d[j] * f(j, i):
			d[i] = 1
			break
print('NYoe s'[d[-1]::2])


import sys
sys.setrecursionlimit(10 ** 7)

def main(n, ab):
	f = [[-1, -1] for _ in range(2 * n)]
	mi = set(range(2 * n))
	for (i, (a, b)) in enumerate(ab):
		(a, b) = (a - 1, b - 1)
		if a >= 0 and a not in mi:
			return False
		if b >= 0 and b not in mi:
			return False
		if a >= 0 and b >= 0 and (a >= b):
			return False
		mi.discard(a)
		mi.discard(b)
		if a >= 0 and b >= 0:
			f[a][0] = 1
			f[a][1] = i
			f[b][0] = 0
			f[b][1] = i
		elif a >= 0:
			f[a][0] = 1
			f[a][1] = i
		elif b >= 0:
			f[b][0] = 0
			f[b][1] = i
	if f[0][0] == 0:
		return False
	if f[-1][0] == 1:
		return False
	now = 0
	memo = {}

	def chk(l, r):
		if (l, r) in memo:
			return memo[l, r]
		if (r - l) % 2 == 0:
			return False
		ret = True
		ci = (r - l) // 2
		rid = []
		for i in range(l, l + ci + 1):
			if f[i][0] == 0:
				ret = False
			rid.append(f[i][1])
		j = 0
		for i in range(l + ci + 1, r + 1):
			if f[i][0] == 1:
				ret = False
			if rid[j] != -1 and f[i][1] != -1 and (rid[j] != f[i][1]):
				ret = False
			j += 1
		memo[l, r] = ret
		return ret

	def dp(l):
		if l == 2 * n:
			return True
		for ri in range(l + 1, 2 * n):
			tmp = chk(l, ri)
			if tmp:
				if dp(ri + 1):
					return True
		return False
	return dp(0)
n = int(input())
ab = [list(map(int, input().split())) for _ in range(n)]
ret = main(n, ab)
print('Yes' if ret else 'No')


INF = float('inf')

def no():
	print('No')
	return
N = int(input())
ms = 0
arr = []
L = []
R = []
seen = [False] * (2 * N)
for i in range(N):
	(A, B) = map(int, input().split())
	if A == -1 and B == -1:
		ms += 1
	elif A == -1:
		R.append(B - 1)
	elif B == -1:
		L.append(A - 1)
	else:
		arr.append((A - 1, B - 1))
	if A != -1:
		if seen[A - 1]:
			no()
		seen[A - 1] ^= 1
	if B != -1:
		if seen[B - 1]:
			no()
		seen[B - 1] ^= 1
for i in range(len(arr)):
	for j in range(len(arr)):
		if i == j:
			continue
		(l1, r1) = arr[i]
		(l2, r2) = arr[j]
		if l1 <= l2 and l2 <= r1 and (r1 - l1 != r2 - l2):
			no()
dp = [INF] * (2 * N + 1)
dp[0] = 0
for k in range(1, 2 * N + 1):
	if k % 2:
		continue
	for j in range(k - 1):
		if dp[j] == INF:
			continue
		M = 0
		for l in range(j, (j + k) // 2):
			if (l, l + (k - j) // 2) in arr or l in L or l + (k - j) // 2 in R:
				continue
			else:
				M += 1
		if M <= ms:
			dp[k] = min(dp[k], dp[j] + M)
print('Yes' if dp[2 * N] == ms else 'No')


((n,), *t) = [[*map(int, t.split())] for t in open(0)]
c = [0] * (2 * n + 1)
for i in range(n):
	(a, b) = t[i]
	if a > -1 < b and a >= b:
		print('No')
		return
	if a > -1:
		c[a] += 1
	if b > -1:
		c[b] += 1
if max(c) > 1:
	print('No')
	return
for _ in range(n):
	for i in range(n):
		(a, b) = t[i]
		if a > -1 < b:
			for j in range(n):
				(c, d) = t[j]
				if c == d == -1:
					continue
				elif c > -1 < d:
					if d < a or b < c:
						continue
					if d - c != b - a:
						print('No')
						return
				elif c > -1:
					if a < c < b:
						t[j][1] = c + b - a
				elif a < d < b:
					t[j][0] = d - b + a
c = [0] * (2 * n + 1)
for i in range(n):
	(a, b) = t[i]
	if a > -1:
		c[a] += 1
	if b > -1:
		c[b] += 1
if max(c) > 1:
	print('No')
	return
print('Yes')


N = int(input())
AB = [tuple(map(int, input().split())) for i in range(N)]
from collections import Counter
ctr = Counter()
pairs = {}
starts = set()
ends = set()
free = 0
for (a, b) in AB:
	if a > 0 and b > 0:
		pairs[a] = b
		ctr[a] += 1
		ctr[b] += 1
	elif a > 0:
		ctr[a] += 1
		starts.add(a)
	elif b > 0:
		ctr[b] += 1
		ends.add(b)
	else:
		free += 1
if any((v > 1 for v in ctr.values())):
	print('No')
	return
dp = [False] * (N + 1)
dp[0] = True
for i in range(N):
	if not dp[i]:
		continue
	for j in range(i + 1, N + 1):
		if dp[j]:
			continue
		l = j - i
		for s in range(2 * i + 1, 2 * i + l + 1):
			t = s + l
			if s in pairs:
				if pairs[s] != t:
					break
			if s in starts and t in ends:
				break
			if s in ends:
				break
			if t in starts:
				break
		else:
			dp[j] = True
print('Yes' if dp[-1] else 'No')


((n,), *t) = [[*map(int, t.split())] for t in open(0)]
s = 'Yes'
r = range(n)
for _ in r:
	for (a, b) in t:
		if min(a, b) < 0:
			continue
		for j in r:
			(c, d) = t[j]
			if a < c < b > b - a != d - c < d > 0:
				s = 'No'
			if b > c > a > 0 > d:
				t[j] = (c, c + b - a)
			if a < d < b > 0 > c:
				t[j] = (d - b + a, d)
c = [0] * n * 4
for (a, b) in t:
	c[a] += 1
	c[b] += 1
print((s, 'No')[max(c[:n * 3]) > 1])


import sys
sys.setrecursionlimit(1919810)

def dfs(i):
	if i == 2 * n + 1:
		nonlocal ans
		ans = 'Yes'
		return
	if ans == 'No':
		if y[i] == 0:
			for k in range(1, n):
				if ok(k, i):
					dfs(i + 2 * k)
		elif ok(y[i], i):
			dfs(i + 2 * y[i])
	return

def ok(k, i):
	if k + i >= 2 * n + 1:
		return 0
	f = 1
	for l in range(k):
		if k + i + l >= 2 * n + 1:
			f = 0
			break
		if x[i + l] == 2 or x[k + i + l] == 1:
			f = 0
			break
		if not (y[i + l] == 0 or y[i + l] == k):
			f = 0
			break
		if not z[i + l] == z[k + i + l] and min(z[i + l], z[k + i + l]) >= 1:
			f = 0
			break
	return f
n = int(input())
ans = 'Yes'
x = [0] * (2 * n + 2)
y = [0] * (2 * n + 2)
z = [0] * (2 * n + 2)
for i in range(1, n + 1):
	(a, b) = map(int, input().split())
	if a >= b and (not (a == -1 or b == -1)):
		ans = 'No'
	if not (x[a] == 0 and x[b] == 0):
		ans = 'No'
	if not a == -1:
		x[a] = 1
		z[a] = i
	if not b == -1:
		x[b] = 2
		z[b] = i
	if not (a == -1 or b == -1):
		y[a] = b - a
if ans == 'No':
	print(ans)
	return
ans = 'No'
dfs(1)
print(ans)


def main():
	n = int(input())
	arr = [[0, 0] for i in range(2 * n + 1)]
	for i in range(n):
		(a, b) = map(int, input().split())
		if a == -1:
			pass
		elif arr[a] == [0, 0]:
			arr[a] = [i + 1, b]
		else:
			arr[a] = [2 ** 16, 2 ** 16]
		if b == -1:
			pass
		elif arr[b] == [0, 0]:
			arr[b] = [-i - 1, a]
		else:
			arr[b] = [2 ** 16, 2 ** 16]
	ans = [False for i in range(n + 1)]
	ans[0] = True
	for hi in range(2, 2 * n + 2, 2):
		for lo in range(1, hi + 1, 2):
			if not ans[lo // 2]:
				continue
			flag = True
			for i in range(lo, lo + (hi - lo + 1) // 2):
				(x, y) = (arr[i], arr[i + (hi - lo + 1) // 2])
				if x == [0, 0] and y == [0, 0]:
					continue
				elif x == [0, 0]:
					if y[0] > 0 or y[1] != -1:
						flag = False
						break
				elif y == [0, 0]:
					if x[0] < 0 or x[1] != -1:
						flag = False
						break
				elif x[0] != -y[0] or x[0] < 0:
					flag = False
					break
				elif x[1] != i + 1 + (hi - lo - 1) // 2 or y[1] != i:
					flag = False
					break
			if not flag:
				continue
			ans[hi // 2] = True
	if ans[-1]:
		print('Yes')
	else:
		print('No')

def __starting_point():
	main()
__starting_point()


n = int(input())
c = {}
p = {}
s = set()
e = set()
for i in range(n):
	(a, b) = list(map(int, input().split()))
	if a in c:
		c[a] += 1
	else:
		c[a] = 1
	if b in c:
		c[b] += 1
	else:
		c[b] = 1
	if a > 0 and b > 0:
		if a >= b:
			print('No')
			return
		else:
			p[a] = b
	elif a > 0:
		s.add(a)
	else:
		e.add(b)
for (k, v) in list(c.items()):
	if k < 0:
		continue
	if v > 1:
		print('No')
		return
dp = [False for _ in range(n + 1)]
dp[0] = True
for i in range(n):
	if not dp[i]:
		continue
	for j in range(i + 1, n + 1):
		if dp[j]:
			continue
		f = True
		for k in range(j - i):
			x = i * 2 + k + 1
			y = x + (j - i)
			if x in p:
				if p[x] != y:
					f = False
					break
			if x in s and y in e:
				f = False
				break
			if x in e:
				f = False
				break
			if y in s:
				f = False
				break
		if f:
			dp[j] = True
print('Yes' if dp[-1] else 'No')


(N, *AB) = map(int, open(0).read().split())
AB = list(zip(*[iter(AB)] * 2))
memo = [False] * (2 * N + 1)
for (i, (a, b)) in enumerate(AB):
	if a != -1 and memo[a] or (b != -1 and memo[b]) or (a != -1 and b != -1 and (a > b)):
		print('No')
		return
	if a != -1:
		memo[a] = True
	if b != -1:
		memo[b] = True
dp = [True] + [False] * N
for i in range(N):
	if not dp[i]:
		continue
	for j in range(1, N - i + 1):
		if dp[i + j]:
			continue
		ok = True
		for (a, b) in AB:
			if not ok:
				break
			in_a = 2 * i + 1 <= a <= 2 * i + j
			in_b = 2 * i + j + 1 <= b <= 2 * i + 2 * j
			if a != -1 and b != -1 and in_a ^ in_b or (in_a and in_b and (b - a != j)):
				ok = False
			if a != -1 and 2 * i + j + 1 <= a <= 2 * i + 2 * j or (in_a and b - a != j and memo[a + j]):
				ok = False
			if b != -1 and 2 * i + 1 <= b <= 2 * i + j or (in_b and b - a != j and memo[b - j]):
				ok = False
		dp[i + j] = ok
if dp[N]:
	print('Yes')
else:
	print('No')


def check():
	N = int(input())
	(up, down) = ([0] * (2 * N + 1), [0] * (2 * N + 1))
	(A, B) = ([0] * N, [0] * N)
	for i in range(N):
		(A[i], B[i]) = map(int, input().split())
	for (a, b) in zip(A, B):
		if 0 < b <= a or b == 1 or a == 2 * N:
			return 'No'
		if a == -1:
			pass
		elif up[a] == 0:
			up[a] = b
		else:
			return 'No'
		if b == -1:
			pass
		elif down[b] == 0:
			down[b] = a
		else:
			return 'No'
	dp = [[(i - j) % 2 for j in range(2 * N + 1)] for i in range(2 * N)]
	for l in range(1, 2 * N):
		if l % 2 == 1:
			x = (l + 1) // 2
			for left in range(1, 2 * N - l + 1):
				for y in range(left, left + x):
					if up[y] > 0 and up[y] != y + x or down[y] != 0:
						dp[left][left + l] = 0
					if up[y + x] != 0 or (down[y + x] > 0 and down[y + x] != y):
						dp[left][left + l] = 0
					if up[y] == -1 and down[y + x] == -1:
						dp[left][left + l] = 0
			if l == 1:
				continue
		for left in range(1, 2 * N + 1 - l):
			right = left + l
			for a in range(left + 1, right - 1):
				dp[left][right] |= dp[left][a] & dp[a + 1][right]
	if dp[1][-1] == 1:
		return 'Yes'
	return 'No'
print(check())


N = int(input())
kettei = []
nobori = []
kudari = []
all_ori = set()
all_nobori = set()
flag = False
for i in range(N):
	(a, b) = list(map(int, input().split()))
	if a == -1 and b == -1:
		continue
	elif a == -1:
		if b == 1 or b in all_ori:
			flag = True
		kudari.append(b)
		all_ori.add(b)
	elif b == -1:
		if a == 2 * N or a in all_nobori:
			flag = True
		nobori.append(a)
		all_nobori.add(a)
	elif a >= b:
		flag = True
	else:
		if a in all_nobori or b in all_ori:
			flag = True
		kettei.append((a, b))
		all_nobori.add(a)
		all_ori.add(b)
if flag:
	print('No')
	return
dp = [-1] * (2 * N + 1)
for (a, b) in kettei:
	for i in range(a, b):
		if dp[i] == -1:
			dp[i] = b - a
		elif dp[i] != b - a:
			print('No')
			return
nobori.sort()
kudari = sorted(kudari)[::-1]
for a in nobori:
	if dp[a] == -1:
		continue
	else:
		b = a + dp[a]
		if b > 2 * N or b in all_ori:
			print('No')
			return
		else:
			all_ori.add(b)
			for i in range(a, b):
				if dp[i] == -1:
					dp[i] = b - a
				elif dp[i] != b - a:
					print('No')
					return
for b in kudari:
	if dp[b] == -1:
		continue
	else:
		a = b - dp[b]
		if a < 1 or a in all_nobori:
			print('No')
			return
		else:
			all_nobori.add(a)
			for i in range(a, b):
				if dp[i] == -1:
					dp[i] = b - a
				elif dp[i] != b - a:
					print('No')
					return
print('Yes')


import sys
input = sys.stdin.readline

def I():
	return int(input())

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

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

def main():
	mod = 10 ** 9 + 7
	N = I()
	st = [0] * (2 * N)
	flag = 1
	looked = []
	fixed = [-1] * (2 * N)
	for i in range(N):
		(a, b) = MI()
		a -= 1
		b -= 1
		if a != -2:
			looked.append(a)
		if b != -2:
			looked.append(b)
		if a != -2 and b != -2:
			fixed[a] = b
			fixed[b] = a
			if b <= a:
				flag == 0
		else:
			if a != -2:
				st[a] = 1
			if b != -2:
				st[b] = -1
	looked.sort()
	for i in range(len(looked) - 1):
		aa = looked[i]
		bb = looked[i + 1]
		if aa == bb:
			flag = 0
	if flag == 0:
		print('No')
		return
	from collections import defaultdict
	dd = defaultdict(int)

	def check(a, d):
		if a + 2 * d - 1 >= 2 * N:
			return False
		for i in range(d):
			l = a + i
			r = l + d
			if fixed[l] != -1:
				if fixed[l] != r:
					return False
			elif fixed[r] != -1:
				if fixed[r] != l:
					return False
			else:
				if st[l] == -1:
					return False
				if st[r] == 1:
					return False
				if st[l] * st[r] != 0:
					return False
		return True
	adj = [[] for _ in range(2 * N + 1)]
	for i in range(2 * N):
		for d in range(1, N + 3):
			if i + 2 * d <= 2 * N:
				if check(i, d):
					adj[i].append(i + 2 * d)
	can = [0] * (2 * N + 1)
	can[0] = 1
	import queue
	q = queue.Queue()
	q.put(0)
	while not q.empty():
		v = q.get()
		for nv in adj[v]:
			if can[nv] == 0:
				can[nv] = 1
				q.put(nv)
	if can[-1]:
		print('Yes')
	else:
		print('No')
main()


def main():
	n = int(input())
	ab = [list(map(int, input().split())) for _ in [0] * n]
	d_left = dict()
	d_right = dict()
	s_left = set()
	s_right = set()
	s = set()
	for (a, b) in ab:
		if a == -1 and b == -1:
			continue
		elif a == -1:
			if b in s:
				print('No')
				return 0
			s_right.add(b)
			s.add(b)
		elif b == -1:
			if a in s:
				print('No')
				return 0
			s_left.add(a)
			s.add(a)
		elif a > b:
			print('No')
			return 0
		else:
			if a in s:
				print('No')
				return 0
			if b in s:
				print('No')
				return 0
			d_left[a] = b
			d_right[b] = a
			s.add(a)
			s.add(b)

	def check(x, y):
		size = (y - x) // 2
		for i in range(x, x + size):
			if i in d_left:
				if d_left[i] != i + size:
					return False
			elif i in s_right:
				return False
			elif i in d_right:
				return False
			elif i in s_left:
				if i + size in s_right:
					return False
		for i in range(x + size, x + 2 * size):
			if i in d_right:
				if d_right[i] != i - size:
					return False
			elif i in s_left:
				return False
			elif i in d_left:
				return False
			elif i in s_right:
				if i - size in s_left:
					return False
		return True
	dp = [False] * (n + 1)
	dp[0] = True
	for i in range(1, n + 1):
		for j in range(i - 1, -1, -1):
			if dp[j]:
				if check(2 * j + 1, 2 * i + 1):
					dp[i] = True
					break
	print(['No', 'Yes'][dp[-1]])
main()


import sys

def main():
	sys.setrecursionlimit(10 ** 7)
	LI = lambda : [int(x) for x in sys.stdin.readline().split()]
	NI = lambda : int(sys.stdin.readline())
	N = NI()
	c = [0] * (2 * N + 1)
	f = [0] * (2 * N + 1)
	for _ in range(N):
		(a, b) = LI()
		if a > 0:
			if c[a] != 0 or f[a] != 0:
				print('No')
				return
			c[a] = b
		if b > 0:
			if c[b] != 0 or f[b] != 0:
				print('No')
				return
			f[b] = a
		if a >= b and a > 0 and (b > 0):
			print('No')
			return
	dp = [[-1] * (2 * N + 1) for _ in range(2 * N + 1)]

	def check(i, j):
		if dp[i][j] >= 0:
			return bool(dp[i][j])
		for k in range(i, j + 1):
			if c[k] != 0:
				if k > (i + j) // 2:
					break
				if c[k] > 0 and c[k] - k != (j - i + 1) // 2:
					break
				if c[k] < 0 and f[k + (j - i + 1) // 2] != 0:
					break
			if f[k] != 0:
				if k <= (i + j) // 2:
					break
				if f[k] > 0 and k - f[k] != (j - i + 1) // 2:
					break
				if f[k] < 0 and c[k - (j - i + 1) // 2] != 0:
					break
		else:
			dp[i][j] = 1
			return bool(dp[i][j])
		for k in range(i + 2, j, 2):
			ret1 = check(i, k - 1)
			ret2 = check(k, j)
			if ret1 and ret2:
				dp[i][j] = 1
				return bool(dp[i][j])
		dp[i][j] = 0
		return bool(dp[i][j])
	if check(1, 2 * N):
		print('Yes')
	else:
		print('No')

def __starting_point():
	main()
__starting_point()


N = int(input())
AB = [0] * N
L = [[-1] * 2 for i in range(2 * N + 1)]
for i in range(N):
	(a, b) = list(map(int, input().split()))
	AB[i] = [a, b]
	if a != -1:
		if L[a] == [-1, -1]:
			L[a] = [1, i]
		else:
			print('No')
			return
	if b != -1:
		if L[b] == [-1, -1]:
			L[b] = [0, i]
		else:
			print('No')
			return
DP = [False] * (2 * N + 1)
DP[0] = True
for i in range(2, 2 * N + 1, 2):
	for j in range(1, i, 2):
		if DP[j - 1]:
			x = int((i - j + 1) / 2)
			T = True
			for k in range(j, i + 1):
				if L[k] == [-1, -1]:
					continue
				else:
					(p, q) = L[k]
					if p == 0 and k - x >= j:
						if AB[q][0] == -1:
							if L[k - x] == [-1, -1]:
								continue
							else:
								T = False
						elif AB[q][0] == k - x:
							continue
						else:
							T = False
					elif p == 1 and k + x <= i:
						if AB[q][1] == -1:
							if L[k + x] == [-1, -1]:
								continue
							else:
								T = False
						elif AB[q][1] == k + x:
							continue
						else:
							T = False
					else:
						T = False
			if T:
				DP[i] = True
if DP[-1]:
	print('Yes')
else:
	print('No')


import collections
import sys
from functools import total_ordering
from typing import List
sys.setrecursionlimit(10 ** 9)

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

def main():
	N = int(input())
	AB = [lmi() for _ in range(N)]
	result = solve(N, AB)
	print(result)

def solve(N: int, AB: List[List[int]]) -> str:
	for (a, b) in AB:
		if a != -1 and b != -1 and (not a < b):
			return 'No'
	tp = [0] * (2 * N + 1)
	for (a, b) in AB:
		if a != -1:
			if tp[a] != 0:
				return 'No'
			tp[a] = -1
		if b != -1:
			if tp[b] != 0:
				return 'No'
			tp[b] = 1
	P = [[0] * (2 * N + 1) for _ in range(2 * N + 1)]
	for (a, b) in AB:
		if a != -1 and b != -1:
			for i in range(0, 2 * N + 1):
				P[a][i] = -1
				P[i][a] = -1
				P[i][b] = -1
				P[b][i] = -1
			for i in range(0, a):
				for j in range(b + 1, 2 * N + 1):
					P[i][j] = -1
			for i in range(a + 1, b):
				for j in range(i, b):
					P[i][j] = -1
			P[a][b] = 0
		elif a != -1:
			for i in range(a + 1, 2 * N + 1):
				if tp[i] != 0:
					P[a][i] = -1
			for i in range(1, a):
				P[i][a] = -1
		elif b != -1:
			for j in range(0, b):
				if tp[j] != 0:
					P[j][b] = -1
			for j in range(b + 1, 2 * N + 1):
				P[j][b] = -1
	dp = [False] * (2 * N + 1)
	dp[0] = True
	for i in range(0, 2 * N + 1, 2):
		if dp[i]:
			for j in range(i + 2, 2 * N + 1, 2):
				ng = False
				step = (j - i) // 2
				for k in range(step):
					a = i + 1 + k
					b = a + step
					if P[a][b] == -1:
						ng = True
						break
				if not ng:
					dp[j] = True
	return 'Yes' if dp[2 * N] else 'No'

def __starting_point():
	main()
__starting_point()


printn = lambda x: print(x, end='')
inn = lambda : int(input())
inl = lambda : list(map(int, input().split()))
inm = lambda : map(int, input().split())
ins = lambda : input().strip()
DBG = True and False
BIG = 10 ** 18
R = 10 ** 9 + 7

def ddprint(x):
	if DBG:
		print(x)

def leaf(x, y):
	nonlocal hl
	if (x, y) in hl:
		return hl[x, y]
	m = (y - x) // 2
	for (aa, bb) in pairs:
		if aa < x < bb or aa < y < bb or (x <= aa < bb <= y and bb - aa != m):
			hl[x, y] = False
			return False
	for i in range(m):
		if bl[x + i] or al[x + m + i]:
			hl[x, y] = False
			return False
	hl[x, y] = True
	return True

def dfs(x, y):
	nonlocal hd
	if leaf(x, y):
		return True
	if y - x == 2:
		return False
	if (x, y) in hd:
		return hd[x, y]
	for (aa, bb) in pairs:
		if aa < x < bb or aa < y < bb:
			hd[x, y] = False
			ddprint(f'dfs x={x!r} y={y!r} F')
			return False
	for i in range(x + 2, y, 2):
		if dfs(x, i) and dfs(i, y):
			hd[x, y] = True
			ddprint(f'dfs x={x!r} y={y!r} T')
			return True
	hd[x, y] = False
	ddprint(f'dfs x={x!r} y={y!r} F2')
	return False
n = inn()
al = [False] * (2 * n)
bl = [False] * (2 * n)
aonly = []
bonly = []
pairs = []
for i in range(n):
	(a, b) = inm()
	a -= 1
	b -= 1
	if a >= 0 and (al[a] or bl[a]) or (b >= 0 and (al[b] or bl[b])):
		print('No')
		return
	if a >= 0:
		al[a] = True
		if b < 0:
			aonly.append(a)
	if b >= 0:
		bl[b] = True
		if a < 0:
			bonly.append(b)
	if a >= 0 and b >= 0:
		if a >= b:
			print('No')
			return
		pairs.append((a, b))
ddprint(al)
ddprint(bl)
ddprint(pairs)
chged = True
while chged:
	ddprint('chg loop')
	chged = False
	for aa in aonly:
		ddprint(f'aa={aa!r}')
		dela = False
		for i in range(len(pairs)):
			(ap, bp) = pairs[i]
			ddprint(f'i={i!r} ap={ap!r} bp={bp!r}')
			if ap < aa < bp:
				bb = aa + bp - ap
				if bb >= 2 * n or al[bb] or bl[bb]:
					ddprint(f'bb={bb!r}')
					print('No')
					return
				pairs.append((aa, bb))
				bl[bb] = True
				chged = True
				dela = True
				break
		if dela:
			aonly.remove(aa)
			break
	for bb in bonly:
		delb = False
		for i in range(len(pairs)):
			(ap, bp) = pairs[i]
			if ap < bb < bp:
				aa = bb - (bp - ap)
				if aa < 0 or al[aa] or bl[aa]:
					print('No')
					return
				al[aa] = True
				pairs.append((aa, bb))
				delb = True
				chged = True
				break
		if delb:
			bonly.remove(bb)
			break
hd = {}
hl = {}
print('Yes' if dfs(0, 2 * n) else 'No')


N = int(input())
AB = [list(map(int, input().split())) for _ in range(N)]
judge = True
flag = [0] * 201
for i in AB:
	if i[0] == -1:
		if i[1] != -1:
			if flag[i[1]]:
				judge = False
			else:
				flag[i[1]] = i
	elif i[1] == -1:
		if flag[i[0]]:
			judge = False
		else:
			flag[i[0]] = i
	elif flag[i[0]] or flag[i[1]]:
		judge = False
	elif i[0] >= i[1]:
		judge = False
	else:
		flag[i[0]] = i
		flag[i[1]] = i
dp = [0] * (2 * N + 1)
dp[0] = 1
for left in range(1, 2 * N, 2):
	for right in range(left + 1, 2 * N + 1, 2):
		length = (right - left + 1) // 2
		for i in range(left, left + length):
			x = flag[i]
			if x:
				if x[0] != -1 and x[1] != -1 and (x[1] - x[0] != length):
					break
				elif x[0] == -1:
					break
				elif x[1] == -1 and flag[i + length]:
					break
				elif x[1] == i:
					break
			elif flag[i + length] and flag[i + length][0] != -1 and (flag[i + length][0] != i):
				break
		else:
			if dp[left - 1]:
				dp[right] = 1
if judge and dp[-1]:
	print('Yes')
else:
	print('No')


(_, *t) = [[*map(int, t.split())] for t in open(0)]
e = [(f := 0)] * 999
for (a, b) in t:
	j = 0
	for (c, d) in t:
		f |= d > 0 < a < c < b > b - a != d - c
		t[j] = (c, (d, c + b - a)[b > c > a > d])
		j += 1
for (a, b) in t:
	e[a] += 1
	e[b] += 1
print('YNeos'[f + max(e[:-1]) > 1::2])


n = int(input())
inout = [[0, 0] for i in range(2 * n + 1)]
for i in range(n):
	(a, b) = map(int, input().split())
	if a != -1:
		if inout[a] == [0, 0]:
			inout[a] = [i + 1, b]
		else:
			print('No')
			return
	if b != -1:
		if inout[b] == [0, 0]:
			inout[b] = [-i - 1, a]
		else:
			print('No')
			return
ans = [False for i in range(n + 1)]
ans[0] = True
for OUT in range(2, 2 * n + 1, 2):
	for IN in range(1, OUT + 1, 2):
		if ans[IN // 2]:
			flg = True
			for i in range(IN, IN + (OUT - IN + 1) // 2):
				x = inout[i]
				y = inout[i + (OUT - IN + 1) // 2]
				if x == [0, 0] and y == [0, 0]:
					continue
				elif x == [0, 0]:
					if y[0] > 0 or y[1] != -1:
						flg = False
						break
				elif y == [0, 0]:
					if x[0] < 0 or x[1] != -1:
						flg = False
						break
				elif x[0] != -y[0] or x[0] < 0:
					flg = False
					break
				elif x[1] != i + (OUT - IN + 1) // 2 or y[1] != i:
					flg = False
					break
			if flg == False:
				continue
			ans[OUT // 2] = True
if ans[-1]:
	print('Yes')
else:
	print('No')


import sys

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

def list2d(a, b, c):
	return [[c for j in range(b)] for i in range(a)]

def list3d(a, b, c, d):
	return [[[d for k in range(c)] for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e for l in range(d)] for k in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

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

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 19
MOD = 10 ** 9 + 7
EPS = 10 ** (-10)
N = INT()
S = set()
S1 = set()
S2 = set()
used = [-1] * (N * 2)
for i in range(N):
	(a, b) = MAP()
	a -= 1
	b -= 1
	if a != -2 and b != -2 and (a >= b) or a in S or b in S:
		No()
		return
	if a != -2:
		S.add(a)
		S1.add(i)
		used[a] = i
	if b != -2:
		S.add(b)
		S2.add(i)
		used[b] = i
N *= 2

def check(l, r):
	d = r - l
	for i in range(d // 2):
		fr = used[l + i]
		to = used[l + d // 2 + i]
		if fr == to:
			continue
		if fr == -1 and to not in S1:
			continue
		if to == -1 and fr not in S2:
			continue
		return False
	return True
dp = list2d(N + 1, N + 1, False)
for i in range(N - 1):
	dp[i][i + 2] = check(i, i + 2)
for d in range(4, N + 1, 2):
	for l in range(N + 1 - d):
		r = l + d
		for m in range(l + 1, r):
			dp[l][r] |= dp[l][m] & dp[m][r]
		dp[l][r] |= check(l, r)
if dp[0][N]:
	Yes()
else:
	No()


n = int(input())
ab = [list(map(int, input().split())) for i in range(n)]
cnt = [0] * (2 * n)
lis = [-1] * (2 * n)
for (i, (a, b)) in enumerate(ab):
	if a != -1:
		if lis[a - 1] >= 0:
			print('No')
			return
		cnt[a - 1] = 1
		lis[a - 1] = i
	if b != -1:
		if lis[b - 1] >= 0:
			print('No')
			return
		cnt[b - 1] = -1
		lis[b - 1] = i

def ok(l, r):
	if min(cnt[l:(l + r) // 2]) == -1 or max(cnt[(l + r) // 2:r]) == 1:
		return False
	(lis_l, lis_r) = (lis[l:(l + r) // 2], lis[(l + r) // 2:r])
	for i in range(len(lis_l)):
		if lis_l[i] == lis_r[i]:
			continue
		elif lis_l[i] != -1 != lis_r[i]:
			return False
		elif lis_l[i] != -1 and lis_l[i] in lis_r or (lis_r[i] != -1 and lis_r[i] in lis_l):
			return False
	return True
dp = [False] * (n + 1)
dp[0] = True
for i in range(n):
	if dp[i]:
		for j in range(i + 1, n + 1):
			if ok(i * 2, j * 2):
				dp[j] = True
	else:
		continue
print('Yes' if dp[-1] else 'No')


import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10 ** 9))
write = lambda x: sys.stdout.write(x + '\n')
n = int(input())
ab = [tuple(map(int, input().split())) for _ in range(n)]
t = [None] * (2 * n)
ans = None
for (a, b) in ab:
	if a > 0 and b > 0:
		if t[a - 1] is not None or a >= b:
			ans = -1
			break
		if t[b - 1] is not None:
			ans = -1
			break
		t[a - 1] = (1, b - a)
		t[b - 1] = (2, b - a)
	elif a > 0:
		if t[a - 1] is not None:
			ans = -1
			break
		t[a - 1] = (1, None)
	elif b > 0:
		if t[b - 1] is not None:
			ans = -1
			break
		t[b - 1] = (2, None)
if ans == -1:
	print('No')
else:
	for i in range(len(t)):
		if t[i] is None:
			t[i] = (0, None)
	dp = [False] * (2 * n + 1)
	dp[0] = True

	def sub(j, i):
		if (i - j) % 2 == 1:
			return False
		for k in range((i - j) // 2):
			if t[j + k][0] == 2 or t[j + (i - j) // 2 + k][0] == 1:
				return False
			elif t[j + k][1] is not None and t[j + k][1] != (i - j) // 2:
				return False
			elif t[j + (i - j) // 2 + k][1] is not None and t[j + (i - j) // 2 + k][1] != (i - j) // 2:
				return False
			elif t[j + k][0] != 0 and t[j + k][1] is None and (t[j + k + (i - j) // 2][0] != 0):
				return False
			elif t[j + (i - j) // 2 + k][0] != 0 and t[j + (i - j) // 2 + k][1] is None and (t[j + k][0] != 0):
				return False
		return True
	for i in range(1, 2 * n + 1):
		val = False
		for j in range(i):
			if dp[j] and sub(j, i):
				val = True
		dp[i] = val
	if dp[2 * n]:
		print('Yes')
	else:
		print('No')


def check(l, r):
	if (r - l) % 2 == 1:
		return False
	n = (r - l) // 2
	res = True
	for j in range(l, l + n):
		k = j + n
		res = res and P[j][k]
	return res
N = int(input())
F = [(0, '未')] * (2 * N + 1)
for i in range(N):
	(a, b) = [int(x) for x in input().split()]
	if 1 <= a:
		if F[a][1] != '未':
			print('No')
			return
		F[a] = (i, '乗')
	if 1 <= b:
		if F[b][1] != '未':
			print('No')
			return
		F[b] = (i, '降')
P = [[False] * (2 * N + 1) for _ in range(2 * N + 1)]
for a in range(1, 2 * N + 1):
	for b in range(a + 1, 2 * N + 1):
		pair = (F[a][1], F[b][1])
		if pair in (('未', '未'), ('未', '降'), ('乗', '未')):
			P[a][b] = True
		if pair == ('乗', '降'):
			P[a][b] = F[a][0] == F[b][0]
dp = [False] * (N + 1)
dp[0] = True
for i in range(N + 1):
	if dp[i]:
		for j in range(i + 1, N + 1):
			if check(2 * i + 1, 2 * j + 1):
				dp[j] = True
print('Yes' if dp[N] else 'No')


import numpy as np
from numpy import int8, int32, int64

def solve_core(N, ca, cb, dp):

	def check(n, d):
		k = 2 * n
		for m in range(k, k + d):
			if cb[m] > 0:
				return 0
			if ca[m] > 0 and ca[m] != d:
				return 0
			if ca[m] == -1 and cb[m + d] > 0:
				return 0
		for m in range(k + d, k + 2 * d):
			if ca[m] != 0:
				return 0
		return 1
	for i in range(N - 1, -1, -1):
		dp[i] = 0
		for j in range(i + 1, N + 1):
			d = j - i
			if dp[j] and check(i, d):
				dp[i] = 1
				break
	return dp[0]

def solve(N, ab):
	ca = np.zeros(2 * N, dtype=int64)
	cb = np.zeros(2 * N, dtype=int64)
	for i in range(ab.shape[0]):
		a = ab[i, 0]
		b = ab[i, 1]
		if a > -1 and b > -1:
			if b - a <= 0:
				return 0
			if ca[a] != 0 or cb[a] > 0 or ca[b] != 0 or (cb[b] > 0):
				return 0
			ca[a] = b - a
			cb[b] = 1
		elif a > -1:
			if ca[a] != 0 or cb[a] > 0:
				return 0
			ca[a] = -1
		elif b > -1:
			if ca[b] != 0 or cb[b] > 0:
				return 0
			cb[b] = 1
	dp = np.empty(N + 1, dtype=int64)
	dp[N] = 1
	return solve_core(N, ca, cb, dp)

def main(in_file):
	f = open(in_file)
	N = int(f.readline())
	ab = np.fromstring(f.read(), dtype=int64, sep=' ')
	ab -= 1
	ab = ab.reshape((-1, 2))
	ans = solve(N, ab)
	return ans

def __starting_point():
	ans = main(0)
	print('Yes' if ans else 'No')
__starting_point()


import bisect
import heapq
import math
import random
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from fractions import Fraction
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations, accumulate
from operator import add, mul, sub, itemgetter, attrgetter
import sys
readline = sys.stdin.readline
INF = 1 << 60

def read_int():
	return int(readline())

def read_int_n():
	return list(map(int, readline().split()))

def read_float():
	return float(readline())

def read_float_n():
	return list(map(float, readline().split()))

def read_str():
	return readline().strip()

def read_str_n():
	return readline().strip().split()

def ep(*args):
	print(*args, file=sys.stderr)

def mt(f):
	import time

	def wrap(*args, **kwargs):
		s = time.perf_counter()
		ret = f(*args, **kwargs)
		e = time.perf_counter()
		ep(e - s, 'sec')
		return ret
	return wrap

@mt
def slv(N, AB):
	used = set()
	for (a, b) in AB:
		if a > 0 and b > 0 and (a >= b):
			return 'No'
		if a > 0 and a in used:
			return 'No'
		else:
			used.add(a)
		if b > 0 and b in used:
			return 'No'
		else:
			used.add(b)
	AB = [(a - 1, b - 1) for (a, b) in AB]

	@lru_cache(maxsize=None)
	def f(s):
		if s == 2 * N:
			return True
		for r in range(s + 2, 2 * N + 1, 2):
			l = r - s
			l2 = l // 2
			r2 = s + l2
			used = Counter()
			for (a, b) in AB:
				if a >= 0 and r2 <= a < r:
					break
				if b >= 0 and s <= b < r2:
					break
				if s <= a < r2 and r2 <= b < r and (b - a != l2):
					break
				if s <= a < r2:
					used[a] += 1
					if b < 0:
						used[a + l2] += 1
				if r2 <= b < r:
					used[b] += 1
					if a < 0:
						used[b - l2] += 1
			else:
				if used and used.most_common()[0][1] > 1:
					pass
				elif f(r):
					ep(s, True)
					return True
		ep(s, False)
		return False
	return 'Yes' if f(0) else 'No'

def main():
	N = read_int()
	AB = [read_int_n() for _ in range(N)]
	print(slv(N, AB))

def __starting_point():
	main()
__starting_point()


N = int(input())
el = []
for i in range(N):
	(a, b) = map(int, input().split())
	if a > 0 and b > 0 and (a >= b):
		print('No')
		return
	el.append((a, b))
floor = [0] * 2 * N
for i in range(N):
	if el[i][0] != -1:
		if floor[el[i][0] - 1] != 0:
			print('No')
			return
		floor[el[i][0] - 1] = -(i + 1)
	if el[i][1] != -1:
		if floor[el[i][1] - 1] != 0:
			print('No')
			return
		floor[el[i][1] - 1] = i + 1
dp = [False] * (N + 1)
dp[0] = True
for i in range(1, N + 1):
	for j in range(i + 1):
		if dp[j] == True:
			(l, r) = (j * 2, i * 2)
			high = (r - l) // 2
			for k in range(l, l + high):
				if floor[k] > 0:
					break
				if floor[k + high] < 0:
					break
				if floor[k] < 0:
					if floor[k + high] != -floor[k] and floor[k + high] != 0:
						break
					elif floor[k + high] == 0:
						if el[-floor[k] - 1][1] != -1:
							break
				if floor[k + high] > 0:
					if floor[k] != -floor[k + high] and floor[k] != 0:
						break
					elif floor[k] == 0:
						if el[floor[k + high] - 1][0] != -1:
							break
			else:
				dp[i] = True
				break

def yn(p):
	if p:
		return 'Yes'
	else:
		return 'No'
print(yn(dp[-1]))


n = int(input())
AB = [tuple(map(int, input().split())) for i in range(n)]
l = [-1] * (2 * n + 1)
m = 0
for k in range(n):
	(i, j) = AB[k]
	if i == -1 and j == -1:
		m += 1
		continue
	if i == -1:
		if l[j] >= 0:
			print('No')
			return
		l[j] = k
		continue
	if j == -1:
		if l[i] >= 0:
			print('No')
			return
		l[i] = k
		continue
	if i >= j or j - i > n:
		print('No')
		return
	if l[j] >= 0:
		print('No')
		return
	l[j] = k
	if l[i] >= 0:
		print('No')
		return
	l[i] = k
dp = [-1] * (2 * n + 1)
dp[0] = m
for i in range(1, 2 * n + 2, 2):
	if dp[i - 1] == -1:
		continue
	for j in range(i + 2, 2 * n + 2, 2):
		x = dp[i - 1]
		count = [0] * (2 * n + 1)
		dif = (j - i) // 2
		for k in range(n):
			(a, b) = AB[k]
			if a == b == -1:
				continue
			if a == -1:
				if i + dif <= b < j:
					if count[b] or count[b - dif]:
						continue
					count[b] += 1
					count[b - dif] += 1
			elif b == -1:
				if i <= a < i + dif:
					if count[a] or count[a + dif]:
						continue
					count[a] += 1
					count[a + dif] += 1
			elif i <= a < i + dif and a + dif == b:
				if count[a] or count[b]:
					continue
				count[a] += 1
				count[a + dif] += 1
		if x * 2 >= j - i - sum(count):
			dp[j - 1] = max(dp[j - 1], x - (j - i - sum(count)) // 2)
if dp[-1] >= 0:
	print('Yes')
else:
	print('No')


import sys
from functools import lru_cache
sys.setrecursionlimit(100000)

@lru_cache(None)
def dfs(f):
	if f == 2 * n + 1:
		return True
	max_up = (2 * n - f + 1) // 2
	for up in range(1, max_up + 1):
		for i in range(f, f + up):
			if floors[i] < 0:
				break
			if floors[i + up] > 0:
				break
			if floors[i] != 0 and floors[i + up] != 0 and (floors[i] != -1 * floors[i + up]):
				break
		else:
			if dfs(f + 2 * up):
				return True
			else:
				pass
	return False
n = int(input())
floors = [0] * (2 * n + 1)
for i in range(1, n + 1):
	(a, b) = map(int, input().split())
	if a != -1:
		if floors[a] != 0:
			print('No')
			return
		floors[a] = i
	if b != -1:
		if floors[b] != 0:
			print('No')
			return
		floors[b] = -i
print('Yes' if dfs(1) else 'No')


def main():
	n = int(input())
	AB = [list(map(int, input().split())) for _ in range(n)]
	YesFlag = True
	arr = [True] * (2 * n + 1)
	cnt = 0
	for (a, b) in AB:
		if a != -1 and b != -1 and (a > b):
			YesFlag = False
		if a != -1 and (not arr[a]):
			YesFlag = False
		if b != -1 and (not arr[b]):
			YesFlag = False
		if a != -1:
			arr[a] = False
		if b != -1:
			arr[b] = False
		if a == -1 and b == -1:
			cnt += 1
	dp = [-1] * (2 * n + 1)
	dp[0] = cnt
	for i in range(0, 2 * n + 1, 2):
		if dp[i] < 0:
			continue
		for j in range(i + 2, 2 * n + 1, 2):
			fs = [True] * (2 * n + 1)
			lngth = (j - i) // 2
			sf = True
			for (a, b) in AB:
				if a == -1 and b == -1 or (a != -1 and (a <= i or j < a)) or (b != -1 and (b <= i or j < b)):
					continue
				if a != -1 and b != -1:
					if b - a != lngth or a <= i or j < b:
						sf = False
						break
					elif not fs[a] or not fs[b]:
						sf = False
						break
					else:
						fs[a] = False
						fs[b] = False
				elif a == -1:
					p = b - lngth
					if p <= i or j < b:
						sf = False
						break
					if not fs[p] or not fs[b]:
						sf = False
						break
					else:
						fs[p] = False
						fs[b] = False
				else:
					q = a + lngth
					if a <= i or j < q:
						sf = False
						break
					if not fs[a] or not fs[q]:
						sf = False
						break
					else:
						fs[a] = False
						fs[q] = False
			if sf:
				c = 0
				for k in range(i + 1, j + 1):
					if fs[k]:
						c += 1
				if dp[j] < dp[i] - c // 2:
					dp[j] = dp[i] - c // 2
	if dp[-1] != 0:
		YesFlag = False
	if YesFlag:
		print('Yes')
	else:
		print('No')

def __starting_point():
	main()
__starting_point()


import sys
input = sys.stdin.readline

def solve():
	N = int(input())
	noInOuts = [None] * (2 * N + 1)
	fIns = [-1] * (N + 1)
	fOuts = [-1] * (N + 1)
	for no in range(1, N + 1):
		(A, B) = list(map(int, input().split()))
		if A != -1:
			if noInOuts[A] is not None:
				return False
			noInOuts[A] = (no, 'in')
			fIns[no] = A
		if B != -1:
			if noInOuts[B] is not None:
				return False
			noInOuts[B] = (no, 'out')
			fOuts[no] = B

	def check(fr, to):
		L = (to + 1 - fr) // 2
		for i in range(L):
			if noInOuts[fr + i] is not None:
				(no, dire) = noInOuts[fr + i]
				if dire == 'in':
					if noInOuts[fr + i + L] is not None:
						(no2, dire2) = noInOuts[fr + i + L]
						if no2 != no:
							return False
					elif fOuts[no] != -1:
						return False
				else:
					return False
		for i in range(L):
			if noInOuts[fr + i + L] is not None:
				(no, dire) = noInOuts[fr + i + L]
				if dire == 'out':
					if noInOuts[fr + i] is not None:
						(no2, dire2) = noInOuts[fr + i]
						if no2 != no:
							return False
					elif fIns[no] != -1:
						return False
				else:
					return False
		return True
	dp = [False] * (2 * N + 1)
	dp[0] = True
	for i in range(0, 2 * N, 2):
		if not dp[i]:
			continue
		for j in range(i + 2, 2 * N + 1, 2):
			if check(i + 1, j):
				dp[j] = True
	return dp[2 * N]
if solve():
	print('Yes')
else:
	print('No')


n = int(input())
d = [-1] * (2 * n)
ok = True
for _ in range(n):
	(s, t) = list(map(int, input().split()))
	s -= 1
	t -= 1
	if s == t == -2:
		continue
	if s >= 0 and d[s] != -1 or (t >= 0 and d[t] != -1) or (s >= 0 and t >= 0 and (s >= t)):
		print('No')
		return
	if s == -2:
		d[t] = -2
	elif t == -2:
		d[s] = -3
	else:
		d[s] = t
		d[t] = s
dp = [[False] * (2 * n + 1) for _ in range(2 * n + 1)]
for w in range(2, 2 * n + 1, 2):
	for i in range(0, 2 * n - w + 1, 2):
		j = i + w
		k = i + w // 2
		if all((d[a] == -1 and (d[b] == -1 or d[b] == -2) or (d[a] == -3 and d[b] == -1) or (d[a] == b and d[b] == a) for (a, b) in zip(list(range(i, k)), list(range(k, j))))) or any((dp[i][k] and dp[k][j] for k in range(i + 2, j, 2))):
			dp[i][j] = True
print('Yes' if dp[0][2 * n] else 'No')


import sys
N = int(input())
fixed = []
up_only = []
down_only = []
up_set = set()
down_set = set()
for i in range(N):
	(A, B) = map(int, input().split())
	if A == -1 and B == -1:
		continue
	elif A in up_set or B in down_set or A == 2 * N or (B == 1):
		print('No')
		return
	elif A == -1:
		down_only.append((A, B))
		down_set.add(B)
	elif B == -1:
		up_only.append((A, B))
		up_set.add(A)
	else:
		fixed.append((A, B))
		down_set.add(B)
		up_set.add(A)
dp = [-1] * (2 * N + 1)
for (a, b) in fixed:
	if a >= b:
		print('No')
		return
	else:
		c = b - a
		for i in range(a, b):
			if dp[i] == -1:
				dp[i] = c
			elif dp[i] != c:
				print('No')
				return
down_only.sort(reverse=True)
up_only.sort()
for (na, b) in down_only:
	if dp[b] != -1:
		na = b - dp[b]
		if na < 1 or na in up_set:
			print('No')
			return
		else:
			up_set.add(na)
		for i in range(na, b):
			if dp[i] == -1:
				dp[i] = dp[b]
			elif dp[i] != dp[b]:
				print('No')
				return
for (a, nb) in up_only:
	if dp[a] != -1:
		nb = a + dp[a]
		if nb > 2 * N or nb in down_set:
			print('No')
			return
		else:
			down_set.add(nb)
		for i in range(a, nb):
			if dp[i] == -1:
				dp[i] = dp[a]
			elif dp[i] != dp[a]:
				print('No')
				return
print('Yes')


N = int(input())
P = [list(map(int, input().split())) for _ in range(N)]
E = [[-1, ''] for _ in range(2 * N + 1)]
for i in range(1, N + 1):
	(a, b) = P[i - 1]
	if a != -1:
		if E[a][0] != -1:
			print('No')
			return
		else:
			E[a][0] = i
			E[a][1] = 'S'
	if b != -1:
		if E[b][0] != -1:
			print('No')
			return
		else:
			E[b][0] = i
			E[b][1] = 'G'
	if a >= b and a != -1 and (b != -1):
		print('No')
		return
dp = [False] * (2 * N + 1)
dp[0] = True
for x in range(1, 2 * N + 1):
	for k in range(1, 2 * N + 1 - x):
		if x + 2 * k - 1 > 2 * N:
			continue
		ok = True
		for i in range(k):
			if E[x + i][0] != -1 and E[x + k + i][0] != -1 and (E[x + i][0] != E[x + k + i][0]):
				ok = False
				break
			if E[x + i][1] == 'G':
				ok = False
				break
			if E[x + k + i][1] == 'S':
				ok = False
				break
		dp[x + 2 * k - 1] = dp[x + 2 * k - 1] or (dp[x - 1] and ok)
ans = 'Yes' if dp[2 * N] else 'No'
print(ans)


printn = lambda x: print(x, end='')
inn = lambda : int(input())
inl = lambda : list(map(int, input().split()))
inm = lambda : map(int, input().split())
ins = lambda : input().strip()
DBG = True and False
BIG = 10 ** 18
R = 10 ** 9 + 7

def ddprint(x):
	if DBG:
		print(x)

def leaf(x, y):
	m = (y - x) // 2
	for (aa, bb) in pairs:
		if aa < x < bb or aa < y < bb or (x <= aa < bb <= y and bb - aa != m):
			return False
	for i in range(m):
		if bl[x + i] or al[x + m + i] or (al[x + i] and (not pal[x + i]) and bl[x + m + i]):
			return False
	return True
n = inn()
pal = [False] * (2 * n)
al = [False] * (2 * n)
bl = [False] * (2 * n)
pairs = []
for i in range(n):
	(a, b) = inm()
	a -= 1
	b -= 1
	if a >= 0 and (al[a] or bl[a]) or (b >= 0 and (al[b] or bl[b])):
		print('No')
		return
	if a >= 0:
		al[a] = True
	if b >= 0:
		bl[b] = True
	if a >= 0 and b >= 0:
		if a >= b:
			print('No')
			return
		pairs.append((a, b))
		pal[a] = True
dp = [False] * (2 * n + 1)
dp[0] = True
for i in range(1, n + 1):
	for j in range(i):
		if dp[j] and leaf(2 * j, 2 * i):
			dp[i] = True
			break
print('Yes' if dp[n] else 'No')


import sys

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

def check(Elevator, takedown, left, right):
	aida = right - left + 1
	for i in range(2 * left, 2 * left + aida):
		(i, seti) = (i, i + aida)
		if Elevator[i][0] != -1:
			iperson = Elevator[i][0]
			if Elevator[i][1] != 'A':
				return False
			elif Elevator[seti][0] != -1:
				if Elevator[seti] != [iperson, 'B']:
					return False
			if takedown[iperson][1] != -1:
				if takedown[iperson][1] != seti:
					return False
		if Elevator[seti][0] != -1:
			iperson = Elevator[seti][0]
			if Elevator[seti][1] != 'B':
				return False
			if takedown[iperson][0] != -1:
				if takedown[iperson][0] != i:
					return False
	return True

def main():
	n = int(input())
	Elevator = [[-1, -1] for i in range(2 * n)]
	takedown = [[-1, -1] for i in range(n)]
	for i in range(n):
		(a, b) = list(map(int, input().split()))
		if a != -1:
			a -= 1
			if Elevator[a][0] != -1:
				print('No')
				return
			Elevator[a] = [i, 'A']
			takedown[i][0] = a
		if b != -1:
			b -= 1
			if Elevator[b][0] != -1:
				print('No')
				return
			Elevator[b] = [i, 'B']
			takedown[i][1] = b
			if a >= b:
				print('No')
				return
	dp = [False] * (n + 1)
	dp[-1] = True
	for i in range(n):
		for j in range(i + 1):
			if dp[j - 1]:
				if check(Elevator, takedown, j, i):
					dp[i] = True
					break
	print('Yes' if dp[-2] else 'No')

def __starting_point():
	main()
__starting_point()


import sys
((n,), *t) = [[*map(int, t.split())] for t in open(0)]
s = 'Yes'
for _ in t:
	for (a, b) in t:
		if min(a, b) < 0:
			continue
		for (j, (c, d)) in enumerate(t):
			if a < c < b and b - a != d - c < d > 0:
				s = 'No'
			if b > c > a > 0 > d:
				t[j] = (c, c + b - a)
			if a < d < b > 0 > c:
				t[j] = (d - b + a, d)
c = [0] * n * 4
for (a, b) in t:
	c[a] += 1
	c[b] += 1
print((s, 'No')[max(c[:n * 3]) > 1])


def checkAtomic(i, j):
	length = (j - i) // 2
	for k in range(i, (i + j) // 2):
		if A[k] == INF and A[k + length] is not None:
			return False
		if A[k] is not None and A[k] != INF and (A[k] != k + length):
			return False
	for k in range((i + j) // 2, j):
		if A[k] == -1 and A[k - length] is not None:
			return False
		if A[k] is not None and A[k] != -1 and (A[k] != k - length):
			return False
	return True
import sys
N = int(input())
floors = [[int(x) for x in input().split()] for _ in range(N)]
ON = 0
OFF = 1
INF = float('inf')
floors.sort()
ans = True
A = [None] * (2 * N + 1)
for (on, off) in floors:
	if on == -1 and off == -1:
		continue
	elif on == -1:
		if A[off] is not None:
			print('No')
			return
		A[off] = -1
		continue
	elif off == -1:
		if A[on] is not None:
			print('No')
			return
		A[on] = INF
		continue
	elif on >= off:
		print('No')
		return
	elif A[on] is not None or A[off] is not None:
		print('No')
		return
	else:
		A[on] = off
		A[off] = on
dp = [[0 for _ in range(2 * N + 2)] for _ in range(2 * N + 1)]
for length in range(2, 2 * N + 1, 2):
	for i in range(1, 2 * N + 2 - length):
		j = i + length
		dp[i][j] = sum((dp[i][k] * dp[k][j] for k in range(i + 2, j, 2))) + checkAtomic(i, j)
		res = dp[i][j]
if dp[1][2 * N + 1]:
	print('Yes')
else:
	print('No')


import sys

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

def mapint():
	return map(int, input().split())
sys.setrecursionlimit(10 ** 9)
N = int(input())
As = set()
Bs = set()
dirc = [0] * (N * 2)
pair = [-1] * (N * 2)
for _ in range(N):
	(a, b) = mapint()
	if a != -1 and b != -1:
		if dirc[a - 1] != 0 or dirc[b - 1] != 0 or b <= a:
			print('No')
			return
		dirc[a - 1] = 1
		pair[a - 1] = b - 1
		dirc[b - 1] = -1
		pair[b - 1] = a - 1
	elif a != -1:
		if dirc[a - 1] != 0:
			print('No')
			return
		As.add(a - 1)
		dirc[a - 1] = 1
	elif b != -1:
		if dirc[b - 1] != 0:
			print('No')
			return
		Bs.add(b - 1)
		dirc[b - 1] = -1
dp = [0] * (N * 2 + 1)
dp[0] = 1
for l in range(1, N * 2):
	if not dp[l - 1]:
		continue
	for r in range(l + 1, N * 2 + 1):
		if (r - l + 1) % 2 != 0:
			continue
		_range = (r - l + 1) // 2
		tmp = [0] * (N * 2 + 1)
		for i in range(l - 1, r):
			d = dirc[i]
			if d == 0:
				continue
			p = pair[i]
			if d == -1 and i - _range < l - 1 or (d == 1 and i + _range >= r):
				break
			if p >= r or (p != -1 and p < l - 1):
				break
			tmp[i] = 1
			if p == -1:
				if d == 1:
					if i + _range >= r or i + _range in Bs:
						break
					tmp[i + _range] = 1
				elif d == -1:
					if i - _range < l - 1 or i - _range in As:
						break
					tmp[i - _range] = 1
				else:
					pair_idx = i + _range if i + _range < r else i - _range
					tmp[pair_idx] = 1
			else:
				if abs(p - i) != _range or (tmp[p] != 0 and pair[p] != i):
					break
				tmp[p] = 1
		else:
			dp[r] = 1
print('Yes' if dp[-1] else 'No')


N = int(input())
X = [list(map(int, input().split())) for i in range(N)]
Y = [0] * (2 * N + 2)
for i in range(N):
	for j in range(2):
		Y[X[i][j]] += 1
if max(Y[1:2 * N + 1]) > 1:
	print('No')
	return
for i in range(N):
	if X[i][0] > X[i][1] and X[i][1] != -1:
		print('No')
		return
X.sort(key=lambda x: max(x) % (N * 3))
DP = [-1] * (N + 1)
DP[N] = 1
V = [N * 3] * (N + 1)
V[0] = 0
C = 0
Y = [-1] * (N * 2 + 1)
for i in range(N):
	for j in range(2):
		if X[i][j] != -1:
			Y[X[i][j]] = (i, j)
		else:
			X[i][j] = N * 3
	if sum(X[i]) == N * 6:
		C += 1

def f(n, x):
	if x > C:
		return 0
	V[n] = min(x, V[n])
	if DP[n] != -1:
		return DP[n]
	DP[n] = 0
	for i in range(1, N - n + 1):
		c = V[n]
		Z = []
		for j in range(2 * n + 1, 2 * (n + i) + 1):
			if Y[j] == -1:
				Z.append(-1)
				continue
			if min(X[Y[j][0]]) <= 2 * n:
				Z = -1
				break
			Z.append(Y[j][0])
		if Z == -1:
			continue
		for j in range(i):
			if min(Z[j], Z[i + j]) != -1 and Z[j] != Z[i + j]:
				Z = -1
				break
		if Z == -1:
			continue
		for j in range(i):
			if Z[j] != -1 and X[Z[j]][1] != N * 3 and (X[Z[j]][1] != 2 * n + i + j + 1):
				Z = -1
				break
			j += i
			if Z[j] != -1 and X[Z[j]][0] != N * 3 and (X[Z[j]][0] != 2 * n + j - i + 1):
				Z = -1
				break
		if Z == -1:
			continue
		for j in range(i):
			if max(Z[j], Z[i + j]) == -1:
				c += 1
		DP[n] |= f(n + i, c)
	return DP[n]
if f(0, 0):
	print('Yes')
else:
	print('No')


import collections
import heapq

def default():
	return -1
n = int(input())
arr = [list(map(int, input().split())) for _ in range(n)]
dica = [-1] * (2 * n + 1)
dicb = [-1] * (2 * n + 1)
cnta = collections.defaultdict(int)
cntb = collections.defaultdict(int)
for (a, b) in arr:
	c = b - a - 1
	if (a != -1 and b != -1) and (a >= b or n < b - a) or (cnta[a] == 1 or cntb[a] == 1 or cnta[b] == 1 or (cntb[b] == 1)):
		print('No')
		return
	if a != -1:
		cnta[a] += 1
		dica[a] = b
	if b != -1:
		cntb[b] += 1
		dicb[b] = a
flag = [True] * (2 * n + 1)
q = []
heapq.heappush(q, 1)
s = set()
s.add(1)

def judge(i, j):
	k = j - i
	for pos in range(i, j):
		a = pos
		b = pos + k
		if cnta[b] == 1 or cntb[a] == 1 or (cnta[a] == 1 and cntb[b] == 1 and (dica[a] != b)):
			return False
	return True
while len(q) != 0:
	i = heapq.heappop(q)
	if i == 2 * n + 1:
		print('Yes')
		return
	for j in range(i + 1, n + (i + 1) // 2 + 1):
		if judge(i, j):
			if 2 * j - i not in s:
				heapq.heappush(q, 2 * j - i)
				s.add(2 * j - i)
print('No')
return


N = int(input())
state = [(-1, -1) for _ in range(2 * N + 1)]
for i in range(N):
	(A, B) = map(int, input().split())
	if A != -1 and B == -1:
		if state[A] != (-1, -1):
			print('No')
			return
		state[A] = (0, -1)
	if A == -1 and B != -1:
		if state[B] != (-1, -1):
			print('No')
			return
		state[B] = (1, -1)
	if A != -1 and B != -1:
		if B <= A or state[A] != (-1, -1) or state[B] != (-1, -1):
			print('No')
			return
		state[A] = (0, B - A)
		state[B] = (1, B - A)
dp = [0] * (N + 1)
dp[0] = 1
for i in range(N):
	if dp[i]:
		for k in range(1, N - i + 1):
			flag = True
			for j in range(2 * i + 1, 2 * i + k + 1):
				if state[j][0] == 1:
					flag = False
				if state[j + k][0] == 0:
					flag = False
				if state[j][0] == 0 and state[j + k][0] == 1:
					if not (state[j][1] == k and state[j + k][1] == k):
						flag = False
				if state[j][0] == 0 and state[j][1] != -1:
					if state[j][1] != k:
						flag = False
				if state[j + k][0] == 1 and state[j + k][1] != -1:
					if state[j][1] != k:
						flag = False
			if flag:
				dp[i + k] = 1
if dp[N]:
	print('Yes')
else:
	print('No')


import sys
N = int(input())
events = [(0, False)] * 2 * N
for i in range(1, N + 1):
	(A, B) = list(map(int, input().split()))
	if A != -1:
		if events[A - 1][0] != 0:
			print('No')
			return
		events[A - 1] = (i, B != -1)
	if B != -1:
		if events[B - 1][0] != 0:
			print('No')
			return
		events[B - 1] = (-1 * i, A != -1)
	if A != -1 and B != -1 and (B <= A):
		print('No')
		return

def judge(l, r):
	if l % 2 != 0 or r % 2 != 0:
		return False
	d = (r - l) // 2
	for i in range(l, l + d):
		(l_id, l_flg) = events[i]
		(r_id, r_flg) = events[i + d]
		if l_flg or r_flg:
			if l_id != -1 * r_id:
				return False
		if l_id != 0 and r_id != 0:
			if l_id != -1 * r_id:
				return False
		if r_id > 0:
			return False
		if l_id < 0:
			return False
	return True
dp = [False] * (N + 1)
dp[0] = True
for i in range(N + 1):
	if dp[i]:
		for j in range(i + 1, N + 1):
			if judge(2 * i, 2 * j):
				dp[j] = True
if dp[-1]:
	print('Yes')
else:
	print('No')


import sys

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

def main():
	N = int(input())
	start = []
	end = []
	confirmed = set()
	confirmed_start = set()
	confirmed_end = set()
	start_set = set()
	end_set = set()
	start_only = set()
	end_only = set()
	ill_input = False
	for _ in range(N):
		(a, b) = list(map(int, input().split()))
		if a != -1:
			a -= 1
		if b != -1:
			b -= 1
		if a != -1 and b != -1 and (a >= b):
			ill_input = True
		if a != -1 and (a in start_set or a in end_set):
			ill_input = True
		if b != -1 and (b in start_set or b in end_set):
			ill_input = True
		start.append(a)
		end.append(b)
		if a != -1:
			start_set.add(a)
		if b != -1:
			end_set.add(b)
		if a != -1 and b == -1:
			start_only.add(a)
		if a == -1 and b != -1:
			end_only.add(b)
		if a != -1 and b != -1:
			confirmed.add((a, b))
			confirmed_start.add(a)
			confirmed_end.add(b)
	if ill_input:
		print('No')
		return
	state = [0] * (N * 2)
	for (s, e) in zip(start, end):
		if s != -1:
			state[s] = 1
		if e != -1:
			state[e] = -1
	ok = [[0] * (N * 2) for _ in range(N * 2)]
	for s in range(N * 2):
		for e in range(s + 1):
			ok[s][e] = -1
	for s in start_only:
		for e in range(s + 1, N * 2):
			if e in confirmed_start or e in confirmed_end or e in end_only or (e in start_set):
				ok[s][e] = -1
	for e in end_only:
		for s in range(e):
			if s in confirmed_start or s in confirmed_end or s in start_only or (s in end_set):
				ok[s][e] = -1
	for s in range(N * 2):
		if s in start_only:
			continue
		for e in range(s + 1, N * 2):
			if e in confirmed_start or e in confirmed_end or e in start_only:
				ok[s][e] = -1
	for e in range(N * 2):
		if e in end_only:
			continue
		for s in range(e):
			if s in confirmed_start or s in confirmed_end or s in end_only:
				ok[s][e] = -1
	for (s, e) in confirmed:
		ok[s][e] = 1
		for ns in range(N * 2):
			if ns != s:
				ok[ns][e] = -1
			ok[ns][s] = -1
		for ne in range(N * 2):
			if ne != e:
				ok[s][ne] = -1
			ok[e][ne] = -1
	dp = [False] * (N * 2 + 1)
	dp[0] = True
	for i in range(2, N * 2 + 1, 2):
		flag = False
		for j in range(i - 2, -1, -2):
			if not dp[j]:
				continue
			stride = (i - j) // 2
			for k in range(j, j + stride):
				if state[k] == -1 or state[k + stride] == 1 or ok[k][k + stride] == -1:
					break
			else:
				flag = True
				break
		if flag:
			dp[i] = True
	if dp[-1]:
		print('Yes')
	else:
		print('No')

def __starting_point():
	main()
__starting_point()