You are given a positive integer N and a numerical sequence of length 3N. You need to construct a new sequence of length 2N by removing exactly N elements from the original sequence without changing the order of the remaining elements. The score of the new sequence is defined as the sum of the elements in the first half minus the sum of the elements in the second half. Find the maximum possible score of the new sequence. Constraints: 1 ≤ N ≤ 10^5, ai is an integer, 1 ≤ ai ≤ 10^9. In the input, you are given N followed by the elements of the sequence. Output the maximum possible score of the new sequence.

# Buggy version:
import heapq

n = int(input().strip())
a = list(map(int, input().split()))
pre = a[:n]
heapq.heapify(pre)
suf = list(map(lambda x: -x, a[(2 * n):]))
heapq.heapify(suf)
left = {}
right = {}
pre_sum = sum(pre)
suf_sum = -sum(suf)
left[n - 1] = pre_sum
right[2 * n] = suf_sum
for i in range(n, 2 * n):
    heapq.heappush(pre, a[i])
    pre_sum = pre_sum + a[i] - heapq.heappop(pre)
    left[i] = pre_sum
for i in range(2 * n - 1, n - 1, -1):
    heapq.heappush(suf, -a[i])
    suf_sum = suf_sum + a[i] + heapq.heappop(suf)
    right[i] = suf_sum
ans = -float('inf')
print(left)
print(right)
for i in range(n - 1, 2 * n - 1):
    ans = max(ans, left[i] - right[i + 1])
print(ans)


# Fixed version:
import heapq

n = int(input().strip())
a = list(map(int, input().split()))
pre = a[:n]
heapq.heapify(pre)
suf = list(map(lambda x: -x, a[(2 * n):]))
heapq.heapify(suf)
left = {}
right = {}
pre_sum = sum(pre)
suf_sum = -sum(suf)
left[n - 1] = pre_sum
right[2 * n] = suf_sum
for i in range(n, 2 * n):
    heapq.heappush(pre, a[i])
    pre_sum = pre_sum + a[i] - heapq.heappop(pre)
    left[i] = pre_sum
for i in range(2 * n - 1, n - 1, -1):
    heapq.heappush(suf, -a[i])
    suf_sum = suf_sum + a[i] + heapq.heappop(suf)
    right[i] = suf_sum
ans = -float('inf')
for i in range(n - 1, 2 * n):
    ans = max(ans, left[i] - right[i + 1])
print(ans)

# Buggy version:
N = int(input())
tmp = list(map(int, input().split(' ')))
former_A = deque(tmp)
latter_A = deque(tmp.copy())
former_heap = list()
latter_heap = list()
for _ in range(N):
    former_heap.append(former_A.popleft())
    latter_heap.append(- latter_A.pop())
heapify(former_heap)
heapify(latter_heap)

former_sum = sum(former_heap)
former_sum_list = [former_sum]
for k in range(N):
    new_a = former_A.popleft()
    heappush(former_heap, new_a)
    min_a = heappop(former_heap)
    former_sum += new_a - min_a
    former_sum_list.append(former_sum)

latter_sum = sum(latter_heap)
latter_sum_list = [latter_sum]
for k in range(N):
    new_a = - latter_A.pop()
    heappush(latter_heap, new_a)
    min_a = heappop(latter_heap)
    latter_sum += new_a - min_a
    latter_sum_list.append(latter_sum)
latter_sum_list.reverse()

answer = max([former + latter for former, latter in zip(former_sum_list, latter_sum_list)])
print(answer)

# Fixed version:
from collections import deque
from heapq import heapify, heappop, heappush
N = int(input())
tmp = list(map(int, input().split(' ')))
former_A = deque(tmp)
latter_A = deque(tmp.copy())
former_heap = list()
latter_heap = list()
for _ in range(N):
    former_heap.append(former_A.popleft())
    latter_heap.append(- latter_A.pop())
heapify(former_heap)
heapify(latter_heap)

former_sum = sum(former_heap)
former_sum_list = [former_sum]
for k in range(N):
    new_a = former_A.popleft()
    heappush(former_heap, new_a)
    min_a = heappop(former_heap)
    former_sum += new_a - min_a
    former_sum_list.append(former_sum)

latter_sum = sum(latter_heap)
latter_sum_list = [latter_sum]
for k in range(N):
    new_a = - latter_A.pop()
    heappush(latter_heap, new_a)
    min_a = heappop(latter_heap)
    latter_sum += new_a - min_a
    latter_sum_list.append(latter_sum)
latter_sum_list.reverse()

answer = max([former + latter for former, latter in zip(former_sum_list, latter_sum_list)])
print(answer)

# Buggy version:
import heapq
n=int(input())
a=list(map(int,input().split()))
s1=sum(a[:n])
hq1=a[:n]
heapq.heapify(hq1)
A=[s1]
for i in range(n,2*n):
  s1+=a[i]
  heapq.heappush(hq1,a[i])
  x=heapq.heappop(hq1)
  s1-=x
  A.append(s1)
a=a[::-1]
s2=sum(a[:n])
hq2=a[:n]
heapq.heapify(hq2)
B=[s2]
for i in range(n,2*n):
  s2+=a[i]
  heapq.heappush(hq2,a[i])
  x=heapq.heappop(hq2)
  s2-=x
  B.append(s2)
B=B[::-1]
ans=-10**18
for i in range(len(A)):
  ans=max(ans,A[i]-B[i])
print(ans)

# Fixed version:
import heapq
n=int(input())
a=list(map(int,input().split()))
s1=sum(a[:n])
hq1=a[:n]
heapq.heapify(hq1)
A=[s1]
for i in range(n,2*n):
  s1+=a[i]
  heapq.heappush(hq1,a[i])
  x=heapq.heappop(hq1)
  s1-=x
  A.append(s1)
a=a[::-1]
s2=sum(a[:n])
hq2=[-a[i] for i in range(n)]
heapq.heapify(hq2)
B=[s2]
for i in range(n,2*n):
  s2+=a[i]
  heapq.heappush(hq2,-a[i])
  x=heapq.heappop(hq2)
  s2-=-x
  B.append(s2)
B=B[::-1]
ans=-10**18
for i in range(len(A)):
  ans=max(ans,A[i]-B[i])
print(ans)

# Buggy version:
n = int(input())
A = list(map(int, input().split()))

pre = [0]*(n+1)
pre_list = [e for e in A[:n]]
heapq.heapify(pre_list)
pre_sum = sum(pre_list)
pre[0] = pre_sum
for i in range(n, 2*n):
    small = heapq.heappushpop(pre_list, A[i])
    pre_sum += A[i] - small
    pre[i-n+1] = pre_sum

suf = [0]*(n+1)
suf_list = [-e for e in A[2*n:]]
heapq.heapify(suf_list)
suf_sum = -sum(suf_list)
suf[-1] = suf_sum
for i in range(2*n-1, n-1, -1):
    large = -heapq.heappushpop(suf_list, A[i])
    suf_sum += A[i] - large
    suf[i-n] = suf_sum

# print(pre)
# print(suf)

print(max([pre[i] - suf[i] for i in range(n+1)]))


# Fixed version:
import heapq

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

pre = [0]*(n+1)
pre_list = [e for e in A[:n]]
heapq.heapify(pre_list)
pre_sum = sum(pre_list)
pre[0] = pre_sum
for i in range(n, 2*n):
    small = heapq.heappushpop(pre_list, A[i])
    pre_sum += A[i] - small
    pre[i-n+1] = pre_sum

suf = [0]*(n+1)
suf_list = [-e for e in A[2*n:]]
heapq.heapify(suf_list)
suf_sum = -sum(suf_list)
suf[-1] = suf_sum
for i in range(2*n-1, n-1, -1):
    large = -heapq.heappushpop(suf_list, -A[i])
    suf_sum += A[i] - large
    suf[i-n] = suf_sum

# print(pre)
# print(suf)

print(max([pre[i] - suf[i] for i in range(n+1)]))

# Buggy version:

import heapq 
n = int(input())
An = list(map(int, input().split()))
former = An[:n]
latter = An[2*n:]
latter = list(map(lambda x:-x, latter))
heapq.heapify(former)
heapq.heapify(latter)

sum_former = sum(former)
sum_latter = sum(latter)

former_list = [0]*(n+1) 
latter_list = [0]*(n+1) 
former_list[0] = sum_former 
latter_list[0] = sum_latter 

for i in range(n, 2*n):
  heapq.heappush(former, An[i])
  m = heapq.heappop(former)
  former_list[i-n+1] = former_list[i-n] + An[i] - m 

for i in range(2*n-1, n-1, -1):
  heapq.heappush(latter, -An[i])
  m = heapq.heappop(latter)
  latter_list[2*n-i] = latter_list[2*n-i-1] - An[i] - m

ans = 0
for i in range(n):
  ans = max(ans, former_list[i]+latter_list[n-i])
print(ans)

# Fixed version:

import heapq 
n = int(input())
An = list(map(int, input().split()))
former = An[:n]
latter = An[2*n:]
latter = list(map(lambda x:-x, latter))
heapq.heapify(former)
heapq.heapify(latter)

sum_former = sum(former)
sum_latter = sum(latter)

former_list = [0]*(n+1) 
latter_list = [0]*(n+1) 
former_list[0] = sum_former 
latter_list[0] = sum_latter 

for i in range(n, 2*n):
  heapq.heappush(former, An[i])
  m = heapq.heappop(former)
  former_list[i-n+1] = former_list[i-n] + An[i] - m 

for i in range(2*n-1, n-1, -1):
  heapq.heappush(latter, -An[i])
  m = heapq.heappop(latter)
  latter_list[2*n-i] = latter_list[2*n-i-1] - An[i] - m

latter_list.reverse()
ans = -float('inf')
for i in range(n+1):
  ans = max(ans, former_list[i]+latter_list[i])
print(ans)

# Buggy version:
import heapq
n=int(input())
l=list(map(int,input().split()))
q=[]
for i in range(n):
    heapq.heappush(q,l[i])
a1=[0]*(n+1)
a1[0]=sum(l[:n])
for i in range(n):
    p=heapq.heappop(q)
    a1[i+1]=a1[i]-p+max(p,l[i+n])
    heapq.heappush(q,max(p,l[i+n]))
q=[]
l.reverse()
for i in range(n):
    heapq.heappush(q,-l[i])
a1.reverse()
a1[0]-=sum(l[:n])
z=sum(l[:n])
for i in range(n):
    p=heapq.heappop(q)
    z=z-min(-p,l[i+n])
    a1[i+1]-=z
    heapq.heappush(q,max(p,-l[i+n])
print(max(a1))

# Fixed version:
import heapq
n=int(input())
l=list(map(int,input().split()))
q=[]
for i in range(n):
    heapq.heappush(q,l[i])
a1=[0]*(n+1)
a1[0]=sum(l[:n])
for i in range(n):
    p=heapq.heappop(q)
    a1[i+1]=a1[i]-p+max(p,l[i+n])
    heapq.heappush(q,max(p,l[i+n]))
#print(a1)
q=[]
l.reverse()
for i in range(n):
    heapq.heappush(q,-l[i])
a1.reverse()
a1[0]-=sum(l[:n])
z=sum(l[:n])
for i in range(n):
    p=heapq.heappop(q)
    z=z+p+min(-p,l[i+n])
    a1[i+1]-=z
    heapq.heappush(q,max(p,-l[i+n]))
print(max(a1))

# Buggy version:
maxi=-10**99 
n=int(input())
import heapq 
l=[int(i) for i in input().split()]
k=n
sm=sum(l[0:n])
z1=[0]*(n+1)
z2=[0]*(n+1)
h=[]
z1[0]=sm 
j=1 
heapq.heapify(h)
for i in range(n):
    heapq.heappush(h,l[i])
for i in range(n,2*n):
    p=l[i]
    if p>h[0]: 
        sm+=p 
        sm-=h[0]
        heapq.heappop(h)
        heapq.heappush(h,p)
    else:
        pass 
    z1[j]=sm 
    j+=1 
sm=sum(l[2*n:3*n])
h=[]
heapq.heapify(h)
sm=0 
for i in range(2*n,3*n):
    heapq.heappush(h,-l[i])
    sm+=(l[i])
j=n 
z2[j]=sm  
j-=1 
#print(h[0])
#print(sm)
for i in range(2*n-1,n-1,-1): 
    p=-l[i]
    if p>h[0]:
        #print('jif')
        sm=sm+h[0]-p
        heapq.heappop(h)
        heapq.heappush(h,p)
        
    else:
        pass 
    z2[j]=sm 
    j-=1 
maxi=0 
#print(z1)
#print(z2)
for i in range(n+1):
    maxi=max(maxi,z1[i]-z2[i])
print(maxi)

# Fixed version:
maxi=-10**99 
n=int(input())
import heapq 
l=[int(i) for i in input().split()]
k=n
sm=sum(l[0:n])
z1=[0]*(n+1)
z2=[0]*(n+1)
h=[]
z1[0]=sm 
j=1 
heapq.heapify(h)
for i in range(n):
    heapq.heappush(h,l[i])
for i in range(n,2*n):
    p=l[i]
    if p>h[0]: 
        sm+=p 
        sm-=h[0]
        heapq.heappop(h)
        heapq.heappush(h,p)
    else:
        pass 
    z1[j]=sm 
    j+=1 
sm=sum(l[2*n:3*n])
h=[]
heapq.heapify(h)
sm=0 
for i in range(2*n,3*n):
    heapq.heappush(h,-l[i])
    sm+=(l[i])
j=n 
z2[j]=sm  
j-=1 
#print(h[0])
#print(sm)
for i in range(2*n-1,n-1,-1): 
    p=-l[i]
    if p>h[0]:
        #print('jif')
        sm=sm+h[0]-p
        heapq.heappop(h)
        heapq.heappush(h,p)
        
    else:
        pass 
    z2[j]=sm 
    j-=1 
maxi=-(10**99)
#print(z1)
#print(z2)
for i in range(n+1):
    maxi=max(maxi,z1[i]-z2[i])
print(maxi)

# Buggy version:
import heapq

n=int(input())
arr=list(map(int,input().split()))
q1=[]
tmp=0
for val in arr[:n]:
  heapq.heappush(q1,val)
  tmp+=val
sum1=[tmp]
for val in arr[n:2*n]:
  tmp=heapq.heappop(q1)
  if val>tmp:
    heapq.heappush(q1,val)
    sum1.append(sum1[-1]+(val-tmp))
  else:
    heapq.heappush(q1,tmp)
    sum1.append(sum1[-1])
print(sum1)
q2=[]
tmp=0
for val in arr[2*n:]:
  heapq.heappush(q2,-val)
  tmp+=val
sum2=[tmp]
for val in arr[2*n-1:n-1:-1]:
  tmp=-heapq.heappop(q2)
  if val<tmp:
    heapq.heappush(q2,-val)
    sum2.append(sum2[-1]+(val-tmp))
  else:
    heapq.heappush(q2,-tmp)
    sum2.append(sum2[-1])
sum2=sum2[::-1]
print(sum2)
ans=-10**18
for i in range(n+1):
  tmp=sum1[i]-sum2[i]
  ans=max(ans,tmp)
print(ans)

# Fixed version:
import heapq

n=int(input())
arr=list(map(int,input().split()))
q1=[]
tmp=0
for val in arr[:n]:
  heapq.heappush(q1,val)
  tmp+=val
sum1=[tmp]
for val in arr[n:2*n]:
  tmp=heapq.heappop(q1)
  if val>tmp:
    heapq.heappush(q1,val)
    sum1.append(sum1[-1]+(val-tmp))
  else:
    heapq.heappush(q1,tmp)
    sum1.append(sum1[-1])
q2=[]
tmp=0
for val in arr[2*n:]:
  heapq.heappush(q2,-val)
  tmp+=val
sum2=[tmp]
for val in arr[2*n-1:n-1:-1]:
  tmp=-heapq.heappop(q2)
  if val<tmp:
    heapq.heappush(q2,-val)
    sum2.append(sum2[-1]+(val-tmp))
  else:
    heapq.heappush(q2,-tmp)
    sum2.append(sum2[-1])
sum2=sum2[::-1]
ans=-10**18
for i in range(n+1):
  tmp=sum1[i]-sum2[i]
  ans=max(ans,tmp)
print(ans)

# Buggy version:
import heapq
n=int(input())
A=[int(i) for i in input().split()]
AL=A[:n]
AR=[0]*n
for i in range(2*n,3*n):
  AR[i-2*n]-=A[i]
ALS,ARS=[0]*(n+1),[0]*(n+1)
ALS[0],ARS[0]=sum(AL),-sum(AR)
heapq.heapify(AL)
heapq.heapify(AR)
for i in range(n):
  l,r=A[n+i],-A[2*n-i-1]
  heapq.heappush(AL,l)
  ALS[i+1]=ALS[i]+l-heapq.heappop(AL)
  heapq.heappush(AR,r)
  ARS[i+1]=ARS[i]+r-heapq.heappop(AR)
ans=-float("inf")
for i in range(n+1):
  ans=max(ans,ALS[i]-ARS[n-i])
print(ans)

# Fixed version:
import heapq
n=int(input())
A=[int(i) for i in input().split()]
AL=A[:n]
AR=[0]*n
for i in range(2*n,3*n):
  AR[i-2*n]-=A[i]
ALS,ARS=[0]*(n+1),[0]*(n+1)
ALS[0],ARS[0]=sum(AL),sum(AR)
heapq.heapify(AL)
heapq.heapify(AR)
for i in range(n):
  l,r=A[n+i],-A[2*n-i-1]
  heapq.heappush(AL,l)
  ALS[i+1]=ALS[i]+l-heapq.heappop(AL)
  heapq.heappush(AR,r)
  ARS[i+1]=ARS[i]+r-heapq.heappop(AR)
ans=-float("inf")
for i in range(n+1):
  ans=max(ans,ALS[i]+ARS[n-i])
print(ans)

# Buggy version:
N = int(input())
*A, = map(int,input().split())
q = []
for a in A[:N]:
    heapq.heappush(q,a)
S1 = [sum(q)]
for a in A[N:N*2]:
    if a>q[0]:
        S1.append(S1[-1] + a - q[0])
        heapq.heapreplace(q,a)
    else:
        S1.append(S1[-1])
q = []
for a in A[2*N:]:
    heapq.heappush(q,-a)
S2 = [sum(q)]
for a in A[N:N*2][::-1]:
    if a<-q[0]:
        S2.append(S2[-1] - a - q[0])
        heapq.heapreplace(q,-a)
    else:
        S2.append(S2[-1])
S2 = S2[::-1]
print(max(map(sum,zip(S1,S2))))

# Fixed version:
import heapq
N = int(input())
*A, = map(int,input().split())
q = []
for a in A[:N]:
    heapq.heappush(q,a)
S1 = [sum(q)]
for a in A[N:N*2]:
    if a>q[0]:
        S1.append(S1[-1] + a - q[0])
        heapq.heapreplace(q,a)
    else:
        S1.append(S1[-1])
q = []
for a in A[2*N:]:
    heapq.heappush(q,-a)
S2 = [sum(q)]
for a in A[N:N*2][::-1]:
    if a<-q[0]:
        S2.append(S2[-1] - a - q[0])
        heapq.heapreplace(q,-a)
    else:
        S2.append(S2[-1])
S2 = S2[::-1]
print(max(map(sum,zip(S1,S2))))

# Buggy version:
def f(x):
    y=x[:n];heapify(y);z=sum(y);s=t=z
    for i in x[n:n*2]:z+=i-heappushpop(y,i);s,t=min(s,z),max(t,z);
    return s,t
from heapq import*;n,*a=map(int,open(0).read().split());print(max(map(sum,zip(f([-i for i in a[n:][::-1]]),f(a)[::-1]))))

# Fixed version:
def f(x):
    y=x[:n];heapify(y);z=sum(y);s=[z]
    for i in x[n:n*2]:z+=i-heappushpop(y,i);s+=[z]
    return s
from heapq import*;n,*a=map(int,open(0).read().split());print(max(map(sum,zip(f([-i for i in a[n:][::-1]]),f(a)[::-1]))))

# Buggy version:
import heapq
N = int(input())
A = list(map(int, input().split()))

al = A[:N]
heapq.heapify(al)
sal = sum(al)
suml = [sal]
for i in range(N, 2*N):
    if al[0] < A[i]:
        tmp = heapq.heappop(al)
        heapq.heappush(al, A[i])
        sal += A[i]-tmp
    suml.append(sal)

ar = [-a for a in A[2*N:]]
heapq.heapify(ar)
sar = -sum(ar)
sumr = [sar]

ans = -10**15
for i in range(N, 2*N):
    if -ar[0] > A[3*N-1-i]:
        tmp = -heapq.heappop(ar)
        heapq.heappush(ar, -A[3*N-1-i])
        sar += A[3*N-1-i]-tmp
    sumr.append(sar)
for i in range(len(suml)):
    ans = max(ans, suml[i]-sumr[-i])
print(ans)


# Fixed version:
import heapq
N = int(input())
A = list(map(int, input().split()))

al = A[:N]
heapq.heapify(al)
sal = sum(al)
suml = [sal]
for i in range(N, 2*N):
    if al[0] < A[i]:
        tmp = heapq.heappop(al)
        heapq.heappush(al, A[i])
        sal += A[i]-tmp
    suml.append(sal)

ar = [-a for a in A[2*N:]]
heapq.heapify(ar)
sar = -sum(ar)
sumr = [sar]

ans = -10**15
for i in range(N, 2*N):
    if -ar[0] > A[3*N-1-i]:
        tmp = -heapq.heappop(ar)
        heapq.heappush(ar, -A[3*N-1-i])
        sar += A[3*N-1-i]-tmp
    sumr.append(sar)
for i in range(len(suml)):
    ans = max(ans, suml[i]-sumr[-(i+1)])
print(ans)

# Buggy version:
#062-D
import heapq
import copy
n=int(input())
a=list(map(int,input().split()))
ca=copy.deepcopy(a)
f=ca[:n]
heapq.heapify(f)
fs=sum(f)
fsl=[0 for i in range(n+1)]
fsl[0]=fs
for i in range(n,2*n):
    heapq.heappush(f,a[i])
    fs=fs+a[i]-heapq.heappop(f)
    fsl[i-n+1]=fs
l=list(map(lambda x:-x,ca[2*n:]))
heapq.heapify(l)
ls=sum(l)
lsl=[0 for i in range(n+1)]
lsl[0]=ls
for i in range(n,2*n):
    heapq.heappush(l,-a[3*n-i-1])
    ls=ls-a[3*n-i-1]-heapq.heappop(l)
    lsl[2*n-i]=ls
ans=-float('inf')
for i in range(n+1):
    ans=max(ans,fsl[i]-lsl[i])
print(ans)

# Fixed version:
import heapq
import copy
n=int(input())
a=list(map(int,input().split()))
ca=copy.deepcopy(a)
f=ca[:n]
heapq.heapify(f)
fs=sum(f)
fsl=[0 for i in range(n+1)]
fsl[0]=fs
for i in range(n,2*n):
    heapq.heappush(f,a[i])
    fs=fs+a[i]-heapq.heappop(f)
    fsl[i-n+1]=fs
l=list(map(lambda x:-x,ca[2*n:]))
heapq.heapify(l)
ls=sum(l)
lsl=[0 for i in range(n+1)]
lsl[-1]=ls
for i in range(n,2*n):
    heapq.heappush(l,-a[3*n-i-1])
    ls=ls-a[3*n-i-1]-heapq.heappop(l)
    lsl[2*n-i-1]=ls
ans=-float('inf')
for i in range(n+1):
    ans=max(ans,fsl[i]+lsl[i])
print(ans)

# Buggy version:
#import sys
from heapq import heapify, heappushpop
from heapq import _heapify_max as heapify_max, _siftup_max


def heappushpop_max(heap, item):
    if heap and heap[0] > item:
        item, heap[0] = heap[0], item
        _siftup_max(heap, 0)
    return item


fin = sys.stdin.readline
N, *a_list = map(int, open(0).read().split())

left_pq = a_list[:N]
right_pq = a_list[2 * N:]
heapify(left_pq)
heapify_max(right_pq)

left_sum = [sum(left_pq)]
right_sum = [sum(right_pq)]
for k in range(N, 2 * N):
    left_item = a_list[k]
    right_item = a_list[-k - 1]
    left_sum.append(left_sum[-1] - heappushpop(left_pq, left_item) + left_item)
    right_sum.append(right_sum[-1] - heappushpop_max(right_pq, right_item) + right_item)

max_sum = max(map(sum, zip(left_sum, [-item for item in right_sum[::-1]])))
print(max_sum)


# Fixed version:
import sys
from heapq import heapify, heappushpop
from heapq import _heapify_max as heapify_max, _siftup_max


def heappushpop_max(heap, item):
    if heap and heap[0] > item:
        item, heap[0] = heap[0], item
        _siftup_max(heap, 0)
    return item


fin = sys.stdin.readline
N = int(fin())
a_list = [int(elem) for elem in fin().split()]

left_pq = a_list[:N]
right_pq = a_list[2 * N:]
heapify(left_pq)
heapify_max(right_pq)

left_sum = [sum(left_pq)]
right_sum = [sum(right_pq)]
for k in range(N, 2 * N):
    left_item = a_list[k]
    right_item = a_list[-k - 1]
    left_sum.append(left_sum[-1] - heappushpop(left_pq, left_item) + left_item)
    right_sum.append(right_sum[-1] - heappushpop_max(right_pq, right_item) + right_item)

max_sum = max(map(sum, zip(left_sum, [-item for item in right_sum[::-1]])))
print(max_sum)

# Buggy version:
import heapq
n = int(input())
a = list(map(int, input().split()))

a1 = a[:n]
ax = a[n:2*n]
a2 = list(map(lambda x: -x, a[2*n:]))
print('a2', a2)

sum_a1 = sum(a1)
fdif1 = [sum_a1]
heapq.heapify(a1)
for i in range(n):
    p = heapq.heappushpop(a1,ax[i])
    fdif1.append(fdif1[-1]+ax[i]-p)
    
sum_a2 = sum(a2)
fdif2 = [sum_a2]
heapq.heapify(a2)
print('a2', a2)
for i in range(n):
    p = heapq.heappushpop(a2,-ax[-1-i])
    fdif2.append(fdif2[-1]-ax[-1-i]-p)
fdif2.reverse()

fdif = [fdif1[i]+fdif2[i] for i in range(n)]
print(max(fdif))

# Fixed version:
import heapq
n = int(input())
a = list(map(int, input().split()))

a1 = a[:n]
ax = a[n:2*n]
a2 = list(map(lambda x: -x, a[2*n:]))
#print('a2', a2)

sum_a1 = sum(a1)
fdif1 = [sum_a1]
heapq.heapify(a1)
for i in range(n):
    p = heapq.heappushpop(a1,ax[i])
    fdif1.append(fdif1[-1]+ax[i]-p)
    
sum_a2 = sum(a2)
fdif2 = [sum_a2]
heapq.heapify(a2)
#print('a2', a2)
for i in range(n):
    p = heapq.heappushpop(a2,-ax[-1-i])
    fdif2.append(fdif2[-1]-ax[-1-i]-p)
fdif2.reverse()

fdif = [fdif1[i]+fdif2[i] for i in range(n+1)]
print(max(fdif))

# Buggy version:
import heapq
n = int(input())
a = list(map(int,input().split()))
aka = a[:n]
heapq.heapify(aka)
akasum = [0] * (n+1)
akasum[0] = sum(aka)
for i in range(n,2*n):
  b = heapq.heappop(aka)
  if b >= a[i]:
    heapq.heappush(aka, b)
  else:
    heapq.heappush(aka, a[i])
  akasum[i-n+1] = sum(aka)
ao = a[2*n:]
aosum = [0] * (n+1)
aosum[-1] = sum(ao)
for i in range(n,2*n):
  b = heapq.heappop(ao)
  if b >= a[-i-1]:
    heapq.heappush(ao, b)
  else:
    heapq.heappush(ao, a[-i-1])
  aosum[-i+n-2] = sum(ao)
ans = - float("inf")
for i in range(n+1):
  kota = akasum[i] - aosum[i]
  ans = max(kota,ans)
print(ans)

# Fixed version:
import heapq
n = int(input())
a = list(map(int,input().split()))
aka = a[:n]
heapq.heapify(aka)
akasum = [0] * (n+1)
akasum[0] = sum(aka)
ao = a[2*n:]
aosum = [0] * (n+1)
aosum[-1] = sum(ao)
for i in range(len(ao)):
  ao[i] = -ao[i]
heapq.heapify(ao)
for i in range(n,2*n):
  b = heapq.heappop(aka)
  if b >= a[i]:
    heapq.heappush(aka, b)
    akasum[i-n+1] = akasum[i-n] 
  else:
    heapq.heappush(aka, a[i])
    akasum[i-n+1] = akasum[i-n] + a[i] - b
  b = heapq.heappop(ao)
  if b >=  -a[-i-1]:
    heapq.heappush(ao, b)
    aosum[-i+n-2] = aosum[-i+n-1]
  else:
    heapq.heappush(ao, -a[-i-1])
    aosum[-i+n-2] = aosum[-i+n-1] + a[-i-1] + b
ans = - float("inf")
for i in range(n+1):
  kota = akasum[i] - aosum[i]
  ans = max(kota,ans)
print(ans)

# Buggy version:
import bisect
N = int(input())
A = list(map(int, input().split()))

L = sorted(A[:2*N])
sumL = sum(L[N:])
R = sorted(A[N:])
sumR = sum(R[:N])

AL = [sumL]
p = N
BL = [True] * (2*N)
for a in reversed(A[N:2*N]):  # sumLはだんだん減る
    ind = bisect.bisect_left(L, a)
    if ind >= p:
        sumL -= L[ind]
        p -= 1
        while BL[p] == False:
            p -= 1
        sumL += L[p]
    AL.append(sumL)
    BL[ind] = False
AR = [sumR]
p = N-1
BR = [True] * (2*N)
for a in A[N:2*N]:  # sumRはだんだん増える
    ind = bisect.bisect_left(R, a)
    if ind <= p:
        sumR -= R[ind]
        p += 1
        while BR[p] == False:
            p += 1
        sumR += R[p]
    AR.append(sumR)
    BR[ind] = False
AL.reverse()
C = [AL[i] - AR[i] for i in range(N+1)]
print(AL)
print(AR)
print(max(C))


# Fixed version:
import bisect
N = int(input())
A = list(map(int, input().split()))
L = sorted(A[:2*N])
sumL = sum(L[N:])
R = sorted(A[N:])
sumR = sum(R[:N])
AL = [sumL]
p = N
BL = [False] * (2*N)
for a in reversed(A[N:2*N]):  # sumLはだんだん減る
    ind = bisect.bisect_left(L, a)
    if BL[ind] != False:
        BL[ind] += 1
        ind += BL[ind] - 1
    if ind >= p:
        sumL -= L[ind]
        p -= 1
        while BL[p] != False:
            p -= 1
        sumL += L[p]
    AL.append(sumL)
    BL[ind] = 1
AR = [sumR]
p = N-1
BR = [False] * (2*N)
for a in A[N:2*N]:  # sumRはだんだん増える
    ind = bisect.bisect_left(R, a)
    if BR[ind] != False:
        BR[ind] += 1
        ind += BR[ind] - 1
    if ind <= p:
        sumR -= R[ind]
        p += 1
        while BR[p] != False:
            p += 1
        sumR += R[p]
    AR.append(sumR)
    BR[ind] = 1
AL.reverse()
C = [AL[i] - AR[i] for i in range(N+1)]
print(max(C))

# Buggy version:
import heapq

n = int(input())
a = list(map(int, input().split()))

red, blue = a[:n], [-1*i for i in a[2*n:]]
sr, sb = sum(red), sum(blue)

results = [0] * (n + 1)
results[0] = sr
results[-1] = sb

heapq.heapify(red)
heapq.heapify(blue)

for k in range(n, 2*n):
    v = a[k]
    p = heapq.heappushpop(red, v)
    sr += v - p
    results[i-n+1] += sr

for k in range(2*n-1, n-1, -1):
    v = -1 * a[k]
    p = heapq.heappushpop(blue, v)
    sb += v - p
    results[i-n] += sb

print(max(results))


# Fixed version:
import heapq

n = int(input())
a = list(map(int, input().split()))

red, blue = a[:n], [-1*i for i in a[2*n:]]
sr, sb = sum(red), sum(blue)

results = [0] * (n + 1)
results[0] = sr
results[-1] = sb

heapq.heapify(red)
heapq.heapify(blue)

for k in range(n, 2*n):
    v = a[k]
    p = heapq.heappushpop(red, v)
    sr += v - p
    results[k-n+1] += sr

for k in range(2*n-1, n-1, -1):
    v = -1 * a[k]
    p = heapq.heappushpop(blue, v)
    sb += v - p
    results[k-n] += sb

print(max(results))

# Buggy version:
import heapq
N=int(input())
a=list(map(int,input().split()))
earl=[]
late=[]
for i in range(N):
  heapq.heappush(earl,a[i])
  heapq.heappush(late,-a[2*N+i])
E=0
L=0
for i in range(N):
  E+=a[i]
  L-=a[2*N+i]
e=[E]*(N+1)
l=[L]*(N+1)
for i in range(N):
  e[i+1]=e[i]+a[N+i]-heapq.heappushpop(earl,a[N+i])
  l[N-1-i]=l[N-i]-a[2*N-1-i]-heapq.heappushpop(late,-a[2*N-1-i])

ans=e[N]+l[N]
for i in range(N):
  ans=max(ans,a[i]+l[i])
print(ans)

# Fixed version:
import heapq
N=int(input())
a=list(map(int,input().split()))
earl=[]
late=[]
for i in range(N):
  heapq.heappush(earl,a[i])
  heapq.heappush(late,-a[2*N+i])
E=0
L=0
for i in range(N):
  E+=a[i]
  L-=a[2*N+i]
e=[E]*(N+1)
l=[L]*(N+1)
for i in range(N):
  e[i+1]=e[i]+a[N+i]-heapq.heappushpop(earl,a[N+i])
  l[N-1-i]=l[N-i]-a[2*N-1-i]-heapq.heappushpop(late,-a[2*N-1-i])

ans=e[N]+l[N]
for i in range(N):
  ans=max(ans,e[i]+l[i])
print(ans)

# Buggy version:
N = int(input())
array = [int(n) for n in input().split()]
import copy
import heapq

lst = [None for i in range(N + 1)]
left_lst = copy.deepcopy(lst)
right_lst = copy.deepcopy(lst)
right_array = heapq.heapify(array[:N])
left_array = heapq.heapify([-n for n in array[-N:]])
l_sum = sum(left_array)
r_sum = sum(right_array)
left_lst[0] = l_sum
right_lst[0] = r_sum

for k in range(N, 2 * N):
    new_a, new_b = array[k], array[-k - 1]*-1
    heapq.heappush(left_array,new_a)
    heapq.heappush(right_array,new_b)
    old_a = heapq.heappop(left_array)
    l_sum += (new_a - old_a)
    left_lst[k - N + 1] = l_sum
    right_array.append(new_b)
    old_b = heapq.heappop(right_array)
    r_sum += (new_b - old_b)
    right_lst[k - N + 1] = r_sum
lst = [a + b for a, b in zip(left_lst, list(reversed(right_lst)))]
print(max(lst))

# Fixed version:
N = int(input())
array = [int(n) for n in input().split()]
import copy
import heapq
lst = [None for i in range(N + 1)]
left_lst = copy.deepcopy(lst)
right_lst = copy.deepcopy(lst)
left_array = array[:N]
right_array= [-n for n in array[-N:]]
l_sum = sum(left_array)
r_sum = sum(right_array)
heapq.heapify(left_array)
heapq.heapify(right_array)
left_lst[0] = l_sum
right_lst[0] = r_sum

for k in range(N, 2 * N):
    new_a, new_b = array[k], array[-k - 1]*-1
    heapq.heappush(left_array,new_a)
    heapq.heappush(right_array,new_b)
    old_a = heapq.heappop(left_array)
    l_sum += (new_a - old_a)
    left_lst[k - N + 1] = l_sum
    old_b = heapq.heappop(right_array)
    r_sum += (new_b - old_b)
    right_lst[k - N + 1] = r_sum
lst = [ a + b for a, b in zip(left_lst, list(reversed(right_lst)))]
print(max(lst))

# Buggy version:
def f(a):
	s=0;q=a[:n];s=sum(q);heapify(q);yield s
	for e in a[n:2*n]:s+=e-heappushpop(q,e);yield s
from heapq import*;r=reversed;n,*a=map(int,open(0).read().split());print(max(map(sum,zip(f(a),r(f([-e for e in r(a)]))))))

# Fixed version:
def f(a):
	s=0;q=a[:n];s=sum(q);heapify(q);r=[s]
	for e in a[n:2*n]:s+=e-heappushpop(q,e);r+=[s]
	return r
from heapq import*;r=reversed;n,*a=map(int,open(0).read().split());print(max(map(sum,zip(f(a),r(f([-e for e in r(a)]))))))

# Buggy version:
def f(a):
	s=0;q=[]
	for i in range(n):s+=a[i];heappush(q,a[i])
	r=[s]
	for i in range(n,2*n):s+=a[i];s-=heappushpop(q,a[i]);r+=[s]
	return r
import sys;from heapq import*;n,*a=list(map(int,sys.stdin.read().split()));print(max(x+y for x,y in zip(f(a),f(reversed(-e for e in a)))))

# Fixed version:
def f(a):
	s=0;q=[]
	for i in range(n):s+=a[i];heappush(q,a[i])
	r=[s]
	for i in range(n,2*n):s+=a[i];s-=heappushpop(q,a[i]);r+=[s]
	return r
import sys;from heapq import*;n,*a=list(map(int,sys.stdin.read().split()));print(max(map(sum,zip(f(a),list(reversed(f(list(reversed([-e for e in a])))))))))