CodeEff/ECCO huggingface dataset:
# Original solution
n = int(eval(input()))
h = [int(i) for i in input().split()] # input:"1 2 3 4 5"
ans = 0
for i in range(n-1):
  for j in range(i,n-1):
    delta = h[j]-h[i]
    if h[j+1]+1<h[i]:
        ans = 1
        break
    elif h[j+1]+1==h[i]:
        h[i]-=1
        break
if ans == 1:
  print('No')
elif sorted(h)==h:
  print('Yes')
else:
  print('No')


# Optimized solution
n = int(eval(input()))
h = [int(i) for i in input().split()] # input:"1 2 3 4 5"

#2019/08/08とき直し
#次のマスの高さを１高くすることができるようにする
ans = 'Yes'
for i in range(n-1):
  if h[i]>h[i+1]+1:
    ans = 'No'
    break
  elif h[i]==h[i+1]+1:
    h[i+1]+=1
print(ans)
# Original solution
def resolve():
    n = int(eval(input()))
    h = list(map(int, input().split()))
    flag = 0
    for i in range(n-1):
        if h[i] == h[i+1]:
            continue
        else:
            h[i+1] -= 1
            if h[i] <= h[i+1]:
                continue
            else:
                print("No")
                exit()
    print("Yes")
resolve()

# Optimized solution
def resolve():
    n = int(eval(input()))
    h = list(map(int, input().split()))
    OK = True
    for i in range(1, len(h)):
        if h[i] == h[i-1]:
            continue
        elif h[i] > h[i-1]:
            h[i] -= 1
        else:
            OK = False
            break
    if OK:
        print("Yes")
    else:
        print("No")
resolve()
# Original solution
def main():
    H = list(map(int, open(0).read().split()[:1:-1]))
    pre = None
    for h in H:
        if pre is None:
            pre = h
        if pre + 1 < h:
            print("No")
            return
        if pre + 1 == h:
            pre = h - 1
        else:
            pre = h

    print("Yes")
    return


main()


# Optimized solution
def main():
    H = list(map(int, open(0).read().split()[:1:-1]))
    pre = None
    for h in H:
        if pre is None:
            pre = h
        elif h <= pre:
            pre = h
        elif pre + 1 == h:
            continue
        else:
            # pre + 1 < h
            print("No")
            return

    print("Yes")
    return


main()
# Original solution
def main():
    H = list(map(int, open(0).read().split()[:1:-1]))
    pre = None
    for h in H:
        if pre is None:
            pre = h
        elif h <= pre:
            pre = h
        elif pre + 1 == h:
            continue
        else:
            # pre + 1 < h
            print("No")
            return

    print("Yes")
    return


main()


# Optimized solution
def main():
    H = list(map(int, open(0).read().split()[:0:-1]))
    pre = None
    for h in H:
        if pre is None:
            pre = h
        elif h <= pre:
            pre = h
        elif pre + 1 == h:
            continue
        else:
            # pre + 1 < h
            print("No")
            return

    print("Yes")
    return


main()
# Original solution
N = int(eval(input()))
h = list(map(int, input().split()))[::-1]
f = [0] * N
ans = 'Yes'
last = 10 ** 10
for hi in h:
    if last >= hi:
        last = hi
    elif last >= hi - 1:
        last = hi - 1
    else:
        ans = 'No'
print(ans)


# Optimized solution
N = int(eval(input()))
H = list(map(int, input().split()))[::-1]
last = 10 ** 10
ans = 'Yes'
for hi in H:
    if last >= hi:
        last = hi
    elif last >= hi - 1:
        last = hi - 1
    else:
        ans = 'No'
print(ans)
# Original solution
n = int(eval(input()))
h = list(map(int, input().split()))
isYes = True
for i in range(n-1):
    if h[i] - min(h[i+1:]) >= 2:
        isYes = False
        break
if isYes:
    print('Yes')
else:
    print('No')

# Optimized solution
n = int(eval(input()))
h = list(map(int, input().split()))
isYes = True
tmpmin = 10**9+1
for i in range(1, n):
    if h[n-i] <= tmpmin:
        tmpmin = h[n-i]
    if h[n-(i+1)]-h[n-i]>= 2 or h[n-(i+1)]-tmpmin >= 2:
        isYes = False
        break
if isYes:
    print('Yes')
else:
    print('No')
# Original solution
import sys
n = int(eval(input()))
l = list(map(int, (input().split())))

for i in range(n):
    if len([ a for a in l if  l[0] -1 > a]) > 0:
        print('No')
        sys.exit()
    else:
        l = l[1::]
print('Yes')


# Optimized solution
import sys
n = int(eval(input()))
l = list(map(int, (input().split())))

max = l[0]
for i in range(n):
    if max - 1 > l[i]:
        print('No')
        sys.exit()
    elif l[i] > max:
        max = l[i]
print('Yes')
# Original solution
n = int(eval(input()))
h = list(map(int, input().split()))

up = 0
flag = 1
for i in range(n-1):
    ma = h[i]+up
    if ma<=h[i+1]:
        up = 0
    elif ma<=h[i+1]+1:
        up = 1
    else:
        flag = 0
        break

if flag:
    print("Yes")
else:
    print("No")

# Optimized solution
N = int(eval(input()))
H = list(map(int, input().split()))

if N == 1:
    print("Yes")
else:
    f = 1
    for i in range(1, N-1):
        if H[-i-1] <= H[-i]:
            continue
        elif H[-i-1]-1 == H[-i]:
            H[-i-1] -= 1
        else:
            f = 0
            break
    if f:
        print("Yes")
    else:
        print("No")
# Original solution
n = int(eval(input()))
h = list(map(int, input().split()))
reverse_h = list(reversed(h))

for i in range(n-1):
    if reverse_h[i] < reverse_h[i+1]:
        reverse_h[i+1] -= 1
        if reverse_h[i] < reverse_h[i+1]:
            print("No")
            exit()
else:
    print("Yes")

# Optimized solution
n = int(eval(input()))
h = list(map(int, input().split()))

for i in range(n-1):
    if h[i] == h[i+1]:
        pass
    elif h[i] <= h[i+1]-1:
        h[i+1] -= 1
    else:
        print("No")
        exit()
print("Yes")
# Original solution
n = int(eval(input()))
h = list(map(int, input().split()))
a = 0
for i in range(n-1):
    for j in h[i+1:]:
        if h[i]-j >= 2:
            print('No')
            exit()

print('Yes')


# Optimized solution
n = int(eval(input()))
h = list(map(int, input().split()))

c = 0
for i in range(n-1):
    if h[i] <= h[i+1]-1:
        h[i+1] = h[i+1]-1
    elif h[i] == h[i+1]:
        continue
    else:
        c = 1
if c == 1:
    print('No')
else:
    print('Yes')
# Original solution
n=int(eval(input()))
h=list(map(int,input().split()))
h=h[::-1]
count=0
for i in range(1,n):
    if h[i]==h[i-1]+1:
        h[i]+=-1
        count=1
    elif h[i]<=h[i-1]:
        count=1
    
    elif h[i]>=h[i-1]+2:
        count=0
        break
    
print(('Yes' if count==1 or n==1 else 'No'))

# Optimized solution
n = int(eval(input()))
h = list(map(int, input().split()))
h = h[::-1]
count = 0
for i in range(1, n):
    if h[i]-h[i-1]==1:
        h[i]+=-1
    elif h[i]-h[i-1]>=2:
        print('No')
        exit()
print('Yes')
# Original solution
n = int(eval(input()))
h = list(map(int,input().split()))
a = 0
b = 0
x = [0]*n

def sakana(p, q):
    for i in range(n-1):
        if p[i]>p[i+1] and q[i]==0:
            p[i]=p[i]-1
            q[i]=1

if n==1:
    print("Yes")
else:
    while b<100000:
        sakana(h, x)
        b = b+1

    for j in range(n-1):
        if h[j]>h[j+1]:
            print("No")
            break
        a = a+1
    if a==n-1:
        print("Yes")

# Optimized solution
n = int(eval(input()))
h = list(map(int,input().split()))
a = 0
b = 0
x = [0]*n

if n==1:
    print("Yes")
else:
    for i in range(n-1):
        if h[i]>h[i+1]:
            h[i]=h[i]-1
            x[i]=1

    for k in range(n-1):
        if h[n-k-1]<h[n-k-2] and x[n-k-2]==0:
            h[n-k-2]=h[n-k-2]-1


    for j in range(n-1):
        if h[j]>h[j+1]:
            print("No")
            break
        a = a+1
    if a==n-1:
        print("Yes")
# Original solution
N = int(eval(input()))
H = list(map(int,input().split()))

for i in range(N-1, 1, -1):
  if H[i] >= H[i-1]:
    continue
  if H[i] < H[i-1]:
    if H[i - 1] - 1 == H[i]:
      H[i - 1] -= 1
    else:
      print("No")
      exit()
print("Yes")

# Optimized solution
import sys

stdin = sys.stdin

ns = lambda : stdin.readline().rstrip()
ni = lambda : int(ns())
na = lambda : list(map(int, stdin.readline().split()))

def main():
  n = ni()
  a = na()

  for i in range(n - 1, 1, -1):
    if a[i] > a[i - 1]:
      continue
    if a[i] < a[i - 1]:
      if a[i] == a[i - 1] - 1:
        a[i - 1] -= 1
        continue
    if a[i] == a[i - 1]:
      continue
    print("No")
    return
  print("Yes")
main()
# Original solution
n = int(eval(input()))
l = list(map(int, input().split()))
if n == 1:
    print("Yes")
else:
    l1 = l[1:]+[0]
    d = [i-j for (i,j) in zip(l1, l)][:-1]
    t = [i for i in d if i <= -2]
    if len(t) >= 1:
        print("No")
    else:
        if -1 not in d:
            print("Yes")
        else:
            num = d.count(-1)
            p = d.index(-1)
            if num == 1:
                print("Yes")
            else:
                d = d[p+1:]
                for i in range(num-1):
                    p = d.index(-1)
                    d1 = d[:p]
                    if sum(d1) <= 0:
                        pass
                    else:
                        d = d[p+1:]
                if len(d) == 0 or -1 not in d:
                    print("Yes")
                else:
                    print("No")

# Optimized solution
n = int(eval(input()))
l = list(map(int, input().split()))
if n == 1:
    print("Yes")
else:
    l1 = l[1:]+[0]
    d = [i-j for (i,j) in zip(l1, l)][:-1]
    t = [i for i in d if i <= -2]
    if len(t) >= 1:
        print("No")
    else:
        if -1 not in d or d.count(-1)==1:
            print("Yes")
        else:
            p = d.index(-1)
            d = d[p+1:]
            s = ''.join(map(str, d))
            spl = s.split("-1") 
            if "" in spl[:-1]:
                print("No")
            else:
                if spl[-1] == "":
                    spl = [s for s in spl if s != '']
                    y = [int(i) for i in spl]
                    if 0 not in y:
                        print("Yes")
                    else:
                        print("No")
                else:
                    y = [int(i) for i in spl]
                    if 0 in y[:-1]:
                        print("No")
                    else:
                        print("Yes")
# Original solution
n=int(eval(input()))
h=list(map(int,input().split()))
if n>1:
  for i in range(n-1):
    for j in range(i+i,n):
      if h[i]-h[j]>1:
        print("No")
        break
    else:
      continue
    break
  else:print("Yes")
else:
  print("Yes")

# Optimized solution
n=int(eval(input()))
h=list(map(int,input().split()))
max=h[0]
flag=0
for i in h:
  if i>=max:
    max=i
  elif max-i>=2:
    flag=1
    break
if flag==0:
  print("Yes")
else:
  print("No")
# Original solution
# -*- coding: utf-8 -*-

# 1つ目の入力
num = int(eval(input()))

# スペース区切りの整数の入力
num_list = list(map(int,input().split()))

def judge(num_list, num):
    if num == 1:
        return('Yes')
    for i in range(0,len(num_list)-1):
        for j in range(i+1,len(num_list)):
          if num_list[i] -1 <= num_list[j]:
            if i+1 == num-1:
                return('Yes')
          else:
            return('No')


print((judge(num_list, num)))

# Optimized solution
# -*- coding: utf-8 -*-

# 1つ目の入力
num = int(eval(input()))

# スペース区切りの整数の入力
num_list = list(map(int,input().split()))

def judge(num_list, num):
    if num == 1:
        return('Yes')

    decrease = False
    for i in range(0,len(num_list)):
        if i + 1 == num:  # 終わりなので、何もしない
            return('Yes')  # 最後までOKだったので、Yesをリターン

        if num_list[i] == num_list[i+1]:
            pass
        elif num_list[i] < num_list[i+1]:
            decrease = False
        elif num_list[i] - 1 <= num_list[i+1]:
            if decrease:
                return('No')
            else:
                decrease = True
        else:
            return('No')
        '''
        for j in range(i+1,len(num_list)):
          if num_list[i] -1 <= num_list[j]:
            if i+1 == num-1:
                return('Yes')
          else:
            return('No')
        '''


print((judge(num_list, num)))
# Original solution

def s0():return eval(input())
def s1():return input().split()
def s2(n):return [eval(input()) for x in range(n)]
def s3(n):return [input().split() for _ in range(n)]
def s4(n): return [[x for x in s] for s in s2(n)]
def n0():return int(eval(input()))
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(eval(input())) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
def t3(n):return [tuple(int(x) for x in input().split()) for _ in range(n)]
def p0(b,yes="Yes",no="No"): print((yes if b else no))
# from sys import setrecursionlimit
# setrecursionlimit(1000000)
# from collections import Counter,deque,defaultdict
# import itertools
# import math
# import networkx
# from bisect import bisect_left,bisect_right
# from heapq import heapify,heappush,heappop

n=n0()
h=n1()

c=h[n-1]
f=True
for i in range(n-1,-1,-1):
    if c>h[i]:
        c=h[i]
    elif c+1>=h[i]:
        continue
    else:
        f=False
        break
    
p0(f)
    

# Optimized solution

def s0():return eval(input())
def s1():return input().split()
def s2(n):return [eval(input()) for x in range(n)]
def s3(n):return [input().split() for _ in range(n)]
def s4(n): return [[x for x in s] for s in s2(n)]
def n0():return int(eval(input()))
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(eval(input())) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
def t3(n):return [tuple(int(x) for x in input().split()) for _ in range(n)]
def p0(b,yes="Yes",no="No"): print((yes if b else no))
# from sys import setrecursionlimit
# setrecursionlimit(1000000)
# from collections import Counter,deque,defaultdict
# import itertools
# import math
# import networkx
# from bisect import bisect_left,bisect_right
# from heapq import heapify,heappush,heappop

n=n0()
h=n1()

c=h[n-1]
f=True
for i in range(n-2,-1,-1):
    if c>h[i]:
        c=h[i]
    elif c+1>=h[i]:
        continue
    else:
        f=False
        break
    
p0(f)
# Original solution
n = int(eval(input()))
numbers = list(map(int, input().split()))

def checkCondition():
  for i in range(n-1):
    if numbers[i] > numbers[i+1]:
      if numbers[i] - numbers[i+1] > 1:
        return False
  for i in range(-1, -n, -1):
    if numbers[i-1] - numbers[i] == 1:
      numbers[i-1] -= 1
    elif numbers[i-1] - numbers[i] > 1:
      return False
  return True

if n == 1:
  print('Yes')
else:
  if checkCondition():
    print('Yes')
  else:
    print('No')

# Optimized solution
n = int(eval(input()))
numbers = list(map(int, input().split()))

def checkCondition():
  for i in range(n-1):
    if numbers[i] > numbers[i+1] + 1:
      return False
  for i in range(-1, -n, -1):
    if numbers[i-1] - numbers[i] == 1:
      numbers[i-1] -= 1
    elif numbers[i-1] - numbers[i] > 1:
      return False
  return True

if n == 1:
  print('Yes')
else:
  if checkCondition():
    print('Yes')
  else:
    print('No')
# Original solution
n = int(eval(input()))
numbers = list(map(int, input().split()))

def checkCondition():
  for i in range(n-1):
    if numbers[i] > numbers[i+1] + 1:
      return False
  for i in range(-1, -n, -1):
    if numbers[i-1] - numbers[i] == 1:
      numbers[i-1] -= 1
    elif numbers[i-1] - numbers[i] > 1:
      return False
  return True

if n == 1:
  print('Yes')
else:
  if checkCondition():
    print('Yes')
  else:
    print('No')

# Optimized solution
n = int(eval(input()))
numbers = list(map(int, input().split()))

def checkCondition():
  for i in range(n-1):
    if numbers[i] > numbers[i+1]:
      if numbers[i] - numbers[i+1] > 1:
        return False
  for i in range(-1, -n, -1):
    sub = numbers[i-1] - numbers[i]
    if sub == 1:
      numbers[i-1] -= 1
    elif sub > 1:
      return False
  return True

if n == 1:
  print('Yes')
else:
  if checkCondition():
    print('Yes')
  else:
    print('No')
# Original solution
import sys
import bisect
input = sys.stdin.readline

def main():
    n=int(eval(input()))
    h = list(map(int,input().split()))

    ans="Yes"

    for i in range(n-1):
        b=h[0]
        h.pop(0)
        if(b>=min(h)+2):
            ans="No"
        if(ans=="No"):
            break
        
    print(ans)
    
main()

# Optimized solution
import sys
import bisect
input = sys.stdin.readline

def main():
    n=int(eval(input()))
    h = list(map(int,input().split()))

    ans="Yes"
    m=0

    for i in range(n):
        if(m>=h[i]+2):
            ans="No"
        else:
            m=max(m,h[i])
        if(ans=="No"):
            break
        
    print(ans)
    
main()
# Original solution
def c_build_stairs(N, H):
    for i in range(N - 1):
        if H[i] < H[i + 1]:
            H[i + 1] -= 1

    for i in range(N - 1):
        if H[i] > H[i + 1]:
            return 'No'
    return 'Yes'

N = int(eval(input()))
H = [int(i) for i in input().split()]
print((c_build_stairs(N, H)))

# Optimized solution
def c_build_stairs(N, H):
    for i in range(N - 1, 0, -1):
        if H[i - 1] - H[i] >= 2:
            return 'No'
        elif H[i - 1] - H[i] == 1:
            H[i - 1] -= 1
    return 'Yes'

N = int(eval(input()))
H = [int(i) for i in input().split()]
print((c_build_stairs(N, H)))
# Original solution
N = int(eval(input()))
H = list(map(int, input().split()))

for i in range(N-1):
    minh = min(H[i+1:]) # i+1以降のリストをスライス
    if H[i] - minh >= 2:
        print('No')
        break
    else:
        continue
else:
    print('Yes')

"""
# よく考えたら最大だけじゃダメだった
m = H.index(max(H)) # 複数の候補がある場合は最初のインデックスを返す
print(m)
for i in range(m+1, N):
    if H[m] - H[i] >= 2:
        print('No')
        break
    else:
        continue
else:
    print('Yes')
"""

# Optimized solution
N = int(eval(input()))
H = list(map(int, input().split()))

highest = 0
for i in range(N-1):
    highest = max(highest, H[i])
    if highest - 1 <= H[i+1]:
        continue
    else:
        print('No')
        break
else:
    print('Yes')
# Original solution
n=int(eval(input()))
h=list(map(int,input().split()))

for i in range(n-1):
    for j in range(i,n):
        if h[i]-h[j]>=2:
            print('No')
            exit()
print('Yes')

# Optimized solution
n=int(eval(input()))
h=list(map(int,input().split()))


for i in range(1,n):
    if h[i]-1>=h[i-1]:
        h[i]-=1
x=sorted(h)

print(('Yes' if x==h else 'No'))
# Original solution
# -*- coding: utf-8 -*-
n = int(eval(input()))
h = [int(i) for i in input().split()]

flag = True
for i in reversed(list(range(n - 1))):
    if h[i] - h[i + 1] == 1:
        h[i] -= 1
    elif h[i] > h[i + 1]:
        flag = False
if flag:
    print("Yes")
else:
    print("No")


# Optimized solution
# -*- coding: utf-8 -*-
n = int(eval(input()))
h = [int(i) for i in input().split()]

flag = True
for i in reversed(list(range(n - 1))):
    if h[i] - h[i + 1] == 1:
        h[i] -= 1
    elif h[i] > h[i + 1]:
        flag = False
        break
if flag:
    print("Yes")
else:
    print("No")
# Original solution
import sys
sys.setrecursionlimit(10**6)

def solve(n, h, cur, l):
  if cur > n:
    return True
  else:
    if l < h[cur]:
      return solve(n, h, cur+1, h[cur]-1) or solve(n, h, cur+1, h[cur])
    elif l == h[cur]:
      return solve(n, h, cur+1, h[cur])
    else:
      return False

N = int(eval(input()))
H = [-1]+list(map(int, input().split()))+[(10**9)+1]
print(("Yes" if solve(N, H, 1, -1) else "No"))



# Optimized solution
import sys
sys.setrecursionlimit(10**6)

def solve(n, h, cur, l):
  if cur > n:
    return True
  else:
    if l < h[cur]:
      return solve(n, h, cur+1, h[cur]-1)
    elif l == h[cur]:
      return solve(n, h, cur+1, h[cur])
    else:
      return False

N = int(eval(input()))
H = [-1]+list(map(int, input().split()))+[(10**9)+1]
print(("Yes" if solve(N, H, 1, -1) else "No"))
# Original solution
N = int(eval(input()))
H = [int(i) for i in input().split()]

t = 0
if N >= 2:
  for i in range(N-1):
    if H[i] >= min(H[i+1:N]) + 2:
      t += 1
    
if t == 0:
  print("Yes")
else:
  print("No")

# Optimized solution
N = int(eval(input()))
H = [int(i) for i in input().split()]

t = 0
s = 0
if N >= 2:
  for i in range(N-1):
    if H[i] > H[i+1]:
      t += 1
      if t == 2:
        s += 1
    if H[i] < H[i+1]:
      t = 0
    if H[i] >= H[i+1] +2:
      s += 1
if s == 0:
  print("Yes")
else:
  print("No")
# Original solution
n = int(eval(input()))
h=list(map(int,input().split()))

maxh = [i for i, x in enumerate(h) if x==max(h)]
minh = [i for i, x in enumerate(h) if x==min(h)]

if min(maxh)<max(minh) and max(h)-1>min(h):
    print("No")
else:
    print("Yes")

# Optimized solution
n = int(eval(input()))
h=list(map(int,input().split()))
def jud(n,h):
    for i in list(reversed(list(range(0,n-1)))):
        # print(h[i],h[i+1])
        if h[i+1]-h[i]==-1:
            h[i]-=1
            # print(h[i])
            continue
        if h[i]-h[i+1]>1:
            return "No"
            break
    return "Yes"

print((jud(n,h)))
# Original solution
N = int(eval(input()))
H = list(map(int,input().split()))

for i in range(N - 1):
  head = H[i]
  remains = min(H[i + 1:])
  if head - remains >= 2:
    print("No")
    break
else:
  print("Yes")

# Optimized solution
N = int(eval(input()))
H = list(map(int,input().split()))

head = 0

for i in range(N):
  if H[i] >= head:
    head = H[i]
  if head - H[i] >= 2:
    print("No")
    break
else:print("Yes")
# Original solution
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
S,T=read().rstrip().decode().split()
ans=0
for s,t in zip(S,T):
    if s!=t:
        ans+=1
print(ans)

# Optimized solution
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
S,T=read().rstrip().decode().split()
print((sum(s!=t for s,t in zip(S,T))))
# Original solution
import heapq

class Heapq:
    def __init__(self, arr, desc=False):
        if desc:
            arr = [-a for a in arr]
        self.sign = -1 if desc else 1
        self.hq = arr
        heapq.heapify(self.hq)

    def pop(self):
        return heapq.heappop(self.hq) * self.sign

    def push(self, a):
        heapq.heappush(self.hq, a * self.sign)

    def top(self):
        return self.hq[0] * self.sign


n = int(eval(input()))
A = list(map(int,input().split()))

heap_l = Heapq(A[:n])
heap_r = Heapq(A[2*n:],desc=True)

max_l = [0]*(n+1)
min_r = [0]*(n+1)
max_l[0] = sum(A[:n])
min_r[0] = sum(A[2*n:])

for i in range(n):
    p = heap_l.pop()
    heap_l.push(max(A[n+i],p))
    max_l[i+1] = max_l[i]+max(0,A[n+i]-p)

    q = heap_r.pop()
    heap_r.push(min(A[-1-i-n],q))
    min_r[i+1] = min_r[i]+min(0,A[-1-i-n]-q)

ans = -float('inf')

for i in range(n+1):
    ans = max(ans,max_l[i]-min_r[-1-i])

print(ans)

# Optimized solution
from heapq import heappop, heappush, heapify

N = int(eval(input()))
A = list(map(int, input().split()))

L = A[:N]
heapify(L)
sumL = [sum(A[:N])]

for i in range(N):
    x = heappop(L)
    if x < A[N + i]:
        sumL.append(sumL[-1] - x + A[N + i])
        heappush(L, A[N + i])
    else:
        sumL.append(sumL[-1])
        heappush(L, x)

R = list([-x for x in A[2 * N:]])
heapify(R)
sumR = [sum(A[2 * N:])]

for i in range(N):
    x = -heappop(R)
    if x > A[2 * N - 1 - i]:
        sumR.append(sumR[-1] - x + A[2 * N - 1 - i])
        heappush(R, -A[2 * N - 1 - i])
    else:
        sumR.append(sumR[-1])
        heappush(R, -x)

res = -10**18

for i in range(N + 1):
    res = max(res, sumL[i] - sumR[-1 - i])

print(res)