CodeEff/ECCO huggingface dataset:
# Original solution
from collections import deque
import heapq
import copy

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

ans = sum(an[:N]) - sum(an[2*N:])
left = [0] * (N+1)
right = [0] * (N+1)
left[0] = sum(an[:N])
right[-1] = -sum(an[2*N:])

l = an[:N]
heapq.heapify(l)
m = an[N:2*N]
r = [-i for i in an[2*N:]]
heapq.heapify(r)

for k in range(N):
    heapq.heappush(l, m[k])
    left[1+k] = left[k] + m[k] - heapq.heappop(l)

    heapq.heappush(r, -m[-1-k])
    right[-2-k] = right[-1-k] - heapq.heappop(r) - m[-1-k]

ans = float('-inf')
for i in range(N+1):
    ans = max(ans, left[i]+right[i])


print(ans)

# Optimized solution
import heapq

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

left = [0] * (N+1)
right = [0] * (N+1)

left[0] = sum(an[:N])
right[-1] = -sum(an[2*N:])

l = an[:N]
heapq.heapify(l)
m = an[N:2*N]
r = [-i for i in an[2*N:]]
heapq.heapify(r)

for k in range(N):
    heapq.heappush(l, m[k])
    left[1+k] = left[k] + m[k] - heapq.heappop(l)

    heapq.heappush(r, -m[-1-k])
    right[-2-k] = right[-1-k] - heapq.heappop(r) - m[-1-k]

ans = -float('inf')
for i in range(N+1):
    ans = max(ans, left[i]+right[i])

print(ans)
# Original solution
from heapq import heappush, heappushpop, heappop
import sys
input = sys.stdin.readline

N = int(eval(input()))
a = list(map(int, input().split()))
first = [a[0]]
first_sum = [0] * (3*N)
first_sum[0] = a[0]
for i in range(1, 3*N):
    if i < N:
        first_sum[i] = first_sum[i-1] + a[i]
        heappush(first, a[i])
    else:
        first_sum[i] = first_sum[i-1] + a[i] - heappushpop(first, a[i])

second = [-a[-1]]
second_sum = [0] * (3*N)
second_sum[-1] = a[-1]
for i in range(3*N-1)[::-1]:
    if i >= 2*N:
        second_sum[i] = second_sum[i+1] + a[i]
        heappush(second, -a[i])
    else:
        heappush(second, -a[i])
        x = -heappop(second)
        second_sum[i] = second_sum[i+1] + a[i] - x
ans = max(first_sum[i] - second_sum[i+1] for i in range(N-1, 2*N))
print(ans)


# Optimized solution
from heapq import heappush, heappushpop
import sys
input = sys.stdin.readline

N = int(eval(input()))
a = list(map(int, input().split()))
first = [a[0]]
first_sum = [a[0]] + [0] * (3*N-1)
for i in range(1, 3*N):
    if i < N:
        first_sum[i] = first_sum[i-1] + a[i]
        heappush(first, a[i])
    else:
        first_sum[i] = first_sum[i-1] + a[i] - heappushpop(first, a[i])

second = [-a[-1]]
second_sum = [0] * (3*N-1) + [a[-1]]
for i in range(3*N-1)[::-1]:
    if i >= 2*N:
        second_sum[i] = second_sum[i+1] + a[i]
        heappush(second, -a[i])
    else:
        second_sum[i] = second_sum[i+1] + a[i] - (-heappushpop(second, -a[i]))
ans = max(first_sum[i] - second_sum[i+1] for i in range(N-1, 2*N))
print(ans)
# Original solution
n = int(eval(input()))
as_ =  list(map(int, input().split()))

l = [0]*n
m = [0]*n
r = [0]*n
for i in range(n):
  l[i] = as_[i]
  m[i] = as_[n+i]
  r[i] = (-1)*as_[2*n+i]

import heapq
heapq.heapify(l)
heapq.heapify(r)

l_maxs = [0]*(n+1)
r_mins = [0]*(n+1)

sl = sum(l)
sr = sum(r)

for i in range(n+1):
  if i != 0:
    j = i-1
    heapq.heappush(l, m[j])
    a = heapq.heappop(l)
    sl += m[j] -a
    
    k = -i
    heapq.heappush(r, m[k]*(-1))
    b = heapq.heappop(r)
    sr += m[k]*(-1) - b
    
  l_maxs[i] = sl
  r_mins[i] = sr

#print(l_maxs)
#print(r_mins)

print((max([l_maxs[i] + r_mins[n-i] for i in range(n+1)])))

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

L = A[0:n]
R = A[2*n:3*n]
import heapq
sl = sum(L)
heapq.heapify(L)
sls = [0]*(n+1)
sls[0] = sl
for i in range(n, 2*n):
    heapq.heappush(L, A[i])
    x = heapq.heappop(L)
    sl += A[i]-x
    sls[i-n+1] = sl
#print(sls)

sr = sum(R)
R = [-c for c in R]
heapq.heapify(R)
srs = [0]*(n+1)
srs[-1] = sr
for i in reversed(list(range(n, 2*n))):
    heapq.heappush(R, -A[i])
    x = -heapq.heappop(R)
    sr += A[i]-x
    srs[i-n] = sr
#print(srs)
ans = - 10**18
for i in range(n+1):
    ans = max(ans, sls[i]-srs[i])
print(ans)
# Original solution
import heapq

N = int(eval(input()))
A = list(map(int,input().split()))
C = A[N:(2*N)]

L = []
R = []
for i in range(N):
  heapq.heappush(L,A[i]) #左側にAの左側からN個を追加
  heapq.heappush(R,-A[-1-i]) #右側数列にAの右側からN個を追加。多いほうから取り出すので-1倍

#左側の総和を求める。
ansL = [sum(L)]
for i in range(N):
  heapq.heappush(L,C[i])
  heapq.heappop(L)
  ansL.append(sum(L))

ansR = [sum(R)]
for i in range(N):
  heapq.heappush(R,-C[-1-i])
  heapq.heappop(R)
  ansR.append(sum(R))

ans = -float('inf')
for i in range(N+1):
  temp = ansL[i]+ansR[-1-i]
  ans = max(ans,temp)
#print(ansL,ansR)
print(ans)

# Optimized solution
import heapq

N = int(eval(input()))
A = list(map(int,input().split()))
C = A[N:(2*N)]

L = []
R = []
for i in range(N):
  heapq.heappush(L,A[i]) #左側にAの左側からN個を追加
  heapq.heappush(R,-A[-1-i]) #右側数列にAの右側からN個を追加。多いほうから取り出すので-1倍

#左側の総和を求める。
ansL = [sum(L)]
for i in range(N):
  heapq.heappush(L,C[i])
  rmv = heapq.heappop(L)
  temp = ansL[i] + C[i] - rmv #一つ前の答え＋今新しく加えた数－最小値
  ansL.append(temp)
  #ansL.append(sum(L)) #sum(L)がO(N)かかっている？

ansR = [sum(R)]
for i in range(N):
  heapq.heappush(R,-C[-1-i])
  rmv = heapq.heappop(R)
  temp = ansR[i] - C[-1-i] - rmv
  ansR.append(temp)
  #ansR.append(sum(R)) #sum(R)がO(N)かかっている？

ans = -float('inf')
for i in range(N+1):
  temp = ansL[i]+ansR[-1-i]
  ans = max(ans,temp)
#print(ansL,ansR)
print(ans)
# Original solution
import heapq

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


L = a[:N]
R = a[(N*2):]
L_sum_list = [sum(L)]
R_sum_list = [sum(R)]

R = [-elem for elem in R]
for k in range(N):
    #L側
    L.append(a[N+k])
    heapq.heapify(L)
    rem = a[N+k] - heapq.heappop(L)
    L_sum_list.append(L_sum_list[-1]+rem)

    #R側
    R.append(-(a[N*2-k-1]))
    heapq.heapify(R)
    rem = -(a[N*2-k-1]) - heapq.heappop(R)
    R_sum_list.append(R_sum_list[-1]-rem)

res = []
for k in range(N+1):
    res.append(L_sum_list[k] - R_sum_list[-k-1])
print((max(res)))
    



# Optimized solution
import heapq

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


L = a[:N]
R = a[(N*2):]
L_sum_list = [sum(L)]
R_sum_list = [sum(R)]

R = [-elem for elem in R]

heapq.heapify(L)
heapq.heapify(R)

for k in range(N):
    #L側
    rem = a[N+k] - heapq.heappushpop(L, a[N+k])
    L_sum_list.append(L_sum_list[-1]+rem)

    #R側
    rem = -(a[N*2-k-1]) - heapq.heappushpop(R, -(a[N*2-k-1]))
    R_sum_list.append(R_sum_list[-1]-rem)

res = []
for k in range(N+1):
    res.append(L_sum_list[k] - R_sum_list[-k-1])

print((max(res)))
# Original solution
import heapq
from copy import deepcopy
from sys import stdin

N = int(stdin.readline())
a = list(map(int, stdin.readline().split()))

# 前半と後半
first, second = [], []
heapq.heapify(first)
heapq.heapify(second)

first_sum, second_sum = 0, 0

for i in range(N):
    heapq.heappush(first, a[i])
    first_sum += a[i]
    heapq.heappush(second, -a[3 * N - 1 - i])
    second_sum += a[3 * N - 1 - i]


first_tmp = deepcopy(first)
second_tmp = deepcopy(second)
first_sum_tmp = first_sum
second_sum_tmp = second_sum
ans = -10 ** 15

# first側からk個を取り除き、second側からN - k個を取り除く
for k in range(N + 1):
    first = deepcopy(first_tmp)
    second = deepcopy(second_tmp)
    first_sum = first_sum_tmp
    second_sum = second_sum_tmp
    cnt = 0

    while cnt < k:
        heapq.heappush(first, a[N + cnt])
        first_sum += a[N + cnt]
        first_sum -= heapq.heappop(first)
        cnt += 1

    while cnt < N:
        heapq.heappush(second, -a[N + cnt])
        second_sum += a[N + cnt]
        second_sum -= -heapq.heappop(second)
        cnt += 1

    ans = max(ans, first_sum - second_sum)
    # print(first_sum, second_sum)

print(ans)


# Optimized solution
import heapq
from sys import stdin

N = int(stdin.readline())
a = list(map(int, stdin.readline().split()))

# 前半と後半
first, second = [], []
heapq.heapify(first)
heapq.heapify(second)

first_sum, second_sum = 0, 0

for i in range(N):
    heapq.heappush(first, a[i])
    first_sum += a[i]
    heapq.heappush(second, -a[3 * N - 1 - i])
    second_sum += a[3 * N - 1 - i]

# first, secondそれぞれ0 - Nまで探索
L_sum = [first_sum]
R_sum = [second_sum]

for i in range(N, 2 * N):
    heapq.heappush(first, a[i])
    first_sum += a[i]
    first_sum -= heapq.heappop(first)
    L_sum.append(first_sum)

    heapq.heappush(second, -a[3 * N - 1 - i])
    second_sum += a[3 * N - 1 - i]
    second_sum -= - heapq.heappop(second)
    R_sum.append(second_sum)

ans = -10 ** 15

R_sum = R_sum[::-1]

for i in range(N + 1):
    ans = max(ans, L_sum[i] - R_sum[i])

# print(L_sum, R_sum)
print(ans)
# Original solution
import heapq

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

def solve(R_ite, L_ite, n):
	heapq.heapify(R_ite)
	heapq.heapify(L_ite)

	for _ in range(n):
		heapq.heappop(R_ite)
	for _ in range(N-n):
		heapq.heappop(L_ite)

	return sum(R_ite) + sum(L_ite)

ans = float("inf") * (-1)
for i in range(N+1):
	R = A[:N+i]
	LL = A[N+i:]
	L = [LL[j] * (-1) for j in range(len(LL))]

	ans = max(ans,solve(R,L,i))

print(ans)

# Optimized solution
import heapq

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

pre = A[:N]
epi = A[2*N:]
epi = [epi[j]*(-1) for j in range(N)]
t_pre = sum(pre)
t_epi = sum(epi)

heapq.heapify(pre)
heapq.heapify(epi)

pre_sum = []
epi_sum = []

pre_sum.append(t_pre)

for i in range(N):
	t_push = A[N+i]
	heapq.heappush(pre, t_push)
	t_pop = heapq.heappop(pre)
	t_pre = t_pre + t_push - t_pop
	pre_sum.append(t_pre)


epi_sum.append(t_epi)

for i in range(N):
	t_push = A[-(N+i+1)] * (-1)
	heapq.heappush(epi, t_push)
	t_pop = heapq.heappop(epi)
	t_epi = t_epi + t_push - t_pop
	epi_sum.append(t_epi)

epi_sum = [epi_sum[-i] for i in range(1,N+2)]
ans = []

for i in range(N+1):
	ans.append(pre_sum[i]+epi_sum[i])

print((max(ans)))
# Original solution
from heapq import *

N = int(eval(input()))

A = list(map(int, input().split()))
A_indexed = [(a, i) for i, a in enumerate(A)]

def scores():
  # initial N:2N
  selected_left = A[:N] # min heap
  heapify(selected_left)
  l_score = sum(selected_left)

  temp = sorted(A_indexed[N:])
  selected_right_set = set(temp[:N])
  unselected_right = temp[N:] # min heap
  del temp
  r_score = sum([x[0] for x in selected_right_set])

  yield l_score,r_score

  for i in range(N, 2*N):
    a = A[i]
    # left 使うものを減らす, right 使うものを増やす
    if selected_left[0] < a:
      l_score -= heapreplace(selected_left, a)
      l_score += a
      
    if (a,i) in selected_right_set:
      r_score -= a
      selected_right_set.remove((a,i))

      n_element = heappop(unselected_right)
      while n_element[1] <= i:
        n_element = heappop(unselected_right)
      selected_right_set.add(n_element)
      r_score += n_element[0]

    yield l_score,r_score

# print(list(scores()))
print((max([x[0]-x[1] for x in scores()])))

# Optimized solution
from heapq import *
 
N = int(eval(input()))
A = list(map(int, input().split()))
B = list([-x for x in reversed(A)])

def left_scores(L):
  s = sum(L[:N])
  scores = [s]
  q = L[:N]
  heapify(q)

  for e in L[N:2*N]:
    s -= heappushpop(q, e)
    s += e
    scores.append(s)
  return scores

print((max([x[0]+x[1] for x in zip(left_scores(A), reversed(left_scores(B)))])))
# Original solution
from heapq import *
 
N = int(eval(input()))
A = list(map(int, input().split()))
B = list([-x for x in reversed(A)])

def left_scores(L):
  s = sum(L[:N])
  scores = [s]
  q = L[:N]
  heapify(q)

  for e in L[N:2*N]:
    s -= heappushpop(q, e)
    s += e
    scores.append(s)
  return scores

print((max([x[0]+x[1] for x in zip(left_scores(A), reversed(left_scores(B)))])))

# Optimized solution
from heapq import *
r=reversed
N=int(eval(input()))
A=list(map(int,input().split()))
def w(L):
	q=L[:N]
	heapify(q)
	s=sum(q)
	yield s
	for e in L[N:2*N]:
		s+=e-heappushpop(q,e)
		yield s
print((max(list(map(sum,list(zip(w(A),r(list(w([-a for a in r(A)]))))))))))
# Original solution
import sys
from heapq import heapify, heappush, heappop
from itertools import accumulate

inf = 1<<100

def solve():
    N = int(eval(input()))
    a = [int(i) for i in input().split()]
    ps = [0] + list(accumulate(a))

    psmin = [0] * (N + 1)
    minhp = a[:N]
    heapify(minhp)

    for i in range(1, N + 1):
        heappush(minhp, a[N - 1 + i])
        x = heappop(minhp)
        psmin[i] = psmin[i - 1] + x

    psmax = [0] * (N + 1)
    maxhp = [-ai for ai in a[2*N:]]
    heapify(maxhp)

    for i in range(1, N + 1):
        heappush(maxhp, -a[2*N - i])
        x = -heappop(maxhp)
        psmax[i] = psmax[i - 1] + x

    # print(psmin)
    # print(psmax)

    ans = -inf

    for i in range(0, N + 1):
        # print(ps[N + i], psmin[i], ps[3*N] - ps[N + i], psmax[N - i])
        v = (ps[N + i] - psmin[i]) - (ps[3*N] - ps[N + i] - psmax[N - i])
        ans = max(ans, v)

    print(ans)
    pass

if __name__ == '__main__':
    solve()

# Optimized solution
import sys
from heapq import heapify, heappush, heappop

inf = 1<<60

def solve():
    n = int(eval(input()))
    a = [int(i) for i in input().split()]

    ps = [sum(a[:n])]
    minhp = a[:n]
    heapify(minhp)

    for i in range(n):
        heappush(minhp, a[n + i])
        x = heappop(minhp)
        ps.append(ps[-1] + a[n + i] - x)

    ss = [sum(a[2*n:])]
    maxhp = [-ai for ai in a[2*n:]]
    heapify(maxhp)

    for i in range(n):
        heappush(maxhp, -a[2*n - 1 - i])
        x = -heappop(maxhp)
        ss.append(ss[-1] + a[2*n - 1 - i] - x)

    ans = -inf

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

    print(ans)

if __name__ == '__main__':  
    solve()
# Original solution
from heapq import*
from collections import*
n,*a=list(map(int,open(0).read().split()))
ans=-float("inf")
b=a[:n]
c=deque(a[n:])
heapify(b)
s=sum(b)
s2=sum(nsmallest(n,c))
red=[s-s2]
for i in range(n,n*2):
    s+=a[i]-heappushpop(b,a[i])
    c.popleft()
    s2=sum(nsmallest(n,c))
    red+=[s-s2]
print((max(red)))

#あｈｊｆｋぁｄｓｈｆｌｋじゃｇはｌｋｊｄｊｇｌｋ；あｊｄｇｌｋ；あｄｓｊｇｌ；あｊｓｄｇｌｋ；あｊｓｄｇｌ；かｓ

# Optimized solution
from heapq import*
n,*a=list(map(int,open(0).read().split()))
b=a[:n]
c=[-i for i in a[n:][::-1]]
d=c[:n]
e=c[n:]
heapify(b)
heapify(d)
s=sum(b)
t=sum(d)
red=[s]
blue=[t]
for i in range(n,n*2):
    s+=a[i]-heappushpop(b,a[i])
    t+=c[i]-heappushpop(d,c[i])
    red+=[s]
    blue+=[t]
print((max(list(map(sum,list(zip(red,blue[::-1])))))))

#あｈｊｆｋぁｄｓｈｆｌｋじゃｇはｌｋｊｄｊｇｌｋ；あｊｄｇｌｋ；あｄｓｊｇｌ；あｊｓｄｇｌｋ；あｊｓｄｇｌ；かｓ
# Original solution
from heapq import*;n,*a=list(map(int,open(0).read().split()));b,h,c,p=a[:n],heappushpop,[-i for i in a[n:][::-1]],heapify;s,d=sum(b),c[:n];t=sum(d);p(b);p(d);r,l=[s],[t]
for i in range(n,n*2):s+=a[i]-h(b,a[i]);t+=c[i]-h(d,c[i]);r+=[s];l+=[t]
print((max(list(map(sum,list(zip(r,l[::-1])))))))

# Optimized solution
def f(a):
	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*;n,*a=list(map(int,open(0).read().split()));print((max(list(map(sum,list(zip(f(a),list(f([-e for e in a[::-1]]))[::-1])))))))
# Original solution
def f(a):
	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*;n,*a=list(map(int,open(0).read().split()));print((max(list(map(sum,list(zip(f(a),list(f([-e for e in a[::-1]]))[::-1])))))))

# Optimized solution
def f(l,a):
	q=a[:n];s=sum(q);heapify(q);l+=[s]
	for e in a[n:2*n]:s+=e-heappushpop(q,e);l+=[s]
from heapq import*;n,*a=list(map(int,open(0).read().split()));l=[];f(l,a);f(l,[-i for i in a[::-1]]);m=len(l)//2;print((max(l[i]+l[-i-1]for i in range(m))))
# Original solution
from heapq import heapify, heappop, heappush, heappushpop

N = int(eval(input()))
*A, = list(map(int, input().split()))
INF = 10**16
first = A[:N]
heapify(first)
second = [-a for a in A[2*N:]]
heapify(second)
f = [0]*(N+1)
s = [0]*(N+1)
f[0] = sum(first)
s[N] = - sum(second)
for k in range(1, N+1):
    heappushpop(first, A[N-1+k])
    f[k] = sum(first)
for k in range(N-1, -1, -1):
    heappushpop(second, -A[N+k])
    s[k] = - sum(second)
ans = -INF
for k in range(N+1):
    ans = max(ans, f[k] - s[k])
print(ans)

# Optimized solution
from heapq import heapify, heappushpop

N = int(eval(input()))
*A, = list(map(int, input().split()))
INF = 10**16
first = A[:N]
heapify(first)
second = [-a for a in A[2*N:]]
heapify(second)
f = [0]*(N+1)
s = [0]*(N+1)
f[0] = sum(first)
s[N] = - sum(second)
for k in range(1, N+1):
    a = heappushpop(first, A[N-1+k])
    f[k] = f[k-1] + A[N-1+k] - a
for k in range(N-1, -1, -1):
    a = heappushpop(second, -A[N+k])
    s[k] = s[k+1] + A[N+k] + a
ans = -INF
for k in range(N+1):
    ans = max(ans, f[k] - s[k])
print(ans)
# Original solution
from collections import deque
from heapq import heapify, heappop as pop, heappush as push

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

l = a[:N]
m = deque(a[N:N*2])
r = [x * -1 for x in a[N*2:]] # 中身は負

heapify(l)
heapify(r)

ll = [sum(l)]
for em in m:
    l_min = pop(l)
    if l_min < em:
        ll.append(ll[-1] - l_min + em)
        push(l, em)
    else:
        ll.append(ll[-1])
        push(l, l_min)

rr = [sum(r)] # 中身は負
for em in reversed(m):
    r_max = pop(r) * -1
    if r_max > em:
        rr.append(rr[-1] + r_max - em)
        push(r, em * -1)
    else:
        rr.append(rr[-1])
        push(r, r_max * -1)

print((max(a + b for a, b in zip(ll, reversed(rr)))))

# Optimized solution
from heapq import heapify, heappop as pop, heappush as push

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

l = a[:N]
m = a[N:N*2]
r = [x * -1 for x in a[N*2:]] # 中身は負

heapify(l)
heapify(r)

ll = [sum(l)]
for em in m:
    l_min = pop(l)
    if l_min < em:
        ll.append(ll[-1] - l_min + em)
        push(l, em)
    else:
        ll.append(ll[-1])
        push(l, l_min)

rr = [sum(r)] # 中身は負
for em in reversed(m):
    r_max = pop(r) * -1
    if r_max > em:
        rr.append(rr[-1] + r_max - em)
        push(r, em * -1)
    else:
        rr.append(rr[-1])
        push(r, r_max * -1)

print((max(a + b for a, b in zip(ll, reversed(rr)))))
# Original solution
#C問題
import heapq
N = int(eval(input()))
A = list(map(int,input().split()))

a = []
for i in range(N):
    heapq.heappush(a,A[i])
B = []
for i in range(3*N):
    B.append(-A[3*N-1-i])
b = []
for i in range(N):
    heapq.heappush(b,B[i])

Red = [sum(a) for i in range(N+1)]
Blue = [sum(b) for i in range(N+1)]
inf = float("inf")
MAX = -inf
AR = []
BB = []
for i in range(N):
    AR.append(A[N+i])
    BB.append(B[N+i])
for i,ar in enumerate(AR):
    l = heapq.heappushpop(a,ar)
    Red[i+1] = Red[i]+(ar-l)
for i,bb in enumerate(BB):
    m = heapq.heappushpop(b,bb)
    Blue[i+1] = Blue[i]+(bb-m)
    
for i in range(N+1):
    #print(Red[i],Blue[N-i])
    MAX = max(MAX,Red[i]+Blue[N-i])

#print(A,B)
print(MAX)


# Optimized solution
#C問題
import heapq
N = int(eval(input()))
A = list(map(int,input().split()))

a = []
for i in range(N):
    heapq.heappush(a,A[i])
B = []
for i in range(3*N):
    B.append(-A[3*N-1-i])
b = []
for i in range(N):
    heapq.heappush(b,B[i])

Red = [sum(a)]
Blue = [sum(b)]
inf = float("inf")
MAX = -inf
AR = []
BB = []
for i in range(N):
    AR.append(A[N+i])
    BB.append(B[N+i])
for i,(ar,r) in enumerate(zip(AR,Red)):
    l = heapq.heappushpop(a,ar)
    Red.append(r+(ar-l))
for i,(bb,bl) in enumerate(zip(BB,Blue)):
    m = heapq.heappushpop(b,bb)
    Blue.append(bl+(bb-m))
    
for i in range(N+1):
    #print(Red[i],Blue[N-i])
    MAX = max(MAX,Red[i]+Blue[N-i])

#print(A,B)
print(MAX)
# Original solution
N = int(eval(input()))
A = list(map(int,input().split()))
S = []
P = A[:N]
Q = A[2*N:]
for i in range(N):
    Q[i] *= -1
R = A[N:2*N]
p = [0]*(N+1)
q = [0]*(N+1)
import heapq
heapq.heapify(P)
heapq.heapify(Q)
sp = sum(P)
sq = sum(Q)
p[0] = sp
q[0] = sq

for i in range(1,N+1):
    heapq.heappush(P,R[i-1])
    heapq.heappop(P)
    heapq.heappush(Q,-R[-i])
    heapq.heappop(Q)
    sp = sum(P)
    sq = sum(Q)
    p[i] = sp
    q[i] = sq
ans = -(10**100)
for k in range(N+1):
    ans = max(ans,p[k] + q[N-k])
    
print(ans)


# Optimized solution
N = int(eval(input()))
A = list(map(int,input().split()))
S = []
P = A[:N]
Q = A[2*N:]
for i in range(N):
    Q[i] *= -1
R = A[N:2*N]
p = [0]*(N+1)
q = [0]*(N+1)
import heapq
heapq.heapify(P)
heapq.heapify(Q)
sp = sum(P)
sq = sum(Q)
p[0] = sp
q[0] = sq

for i in range(1,N+1):
    heapq.heappush(P,R[i-1])
    x = heapq.heappop(P)
    heapq.heappush(Q,-R[-i])
    y = heapq.heappop(Q)
    sp = sp + R[i-1] - x
    sq = sq - R[-i] - y
    p[i] = sp
    q[i] = sq
ans = -(10**100)
for k in range(N+1):
    ans = max(ans,p[k] + q[N-k])
    
print(ans)
# Original solution
import sys
import heapq
import copy


def _heappush_max(heap, item):
    heap.append(item)
    heapq._siftdown_max(heap, 0, len(heap)-1)


def _heappop_max(heap):
    """Maxheap version of a heappop."""
    lastelt = heap.pop()    # raises appropriate IndexError if heap is empty
    if heap:
        returnitem = heap[0]
        heap[0] = lastelt
        heapq._siftup_max(heap, 0)
        return returnitem
    return lastelt


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

sum_h1 = [0] * (N+1)
sum_h2 = [0] * (N+1)

'''
N=2
0 1 2 3 4 5 6
3 1 4 1 5 9
'''

h1 = A[0:N]
h2 = A[N*2:N*3]

heapq.heapify(h1)
heapq._heapify_max(h2)

#print("h1", h1)
#print("h2", h2)
sum_h1[0] = sum(h1)
sum_h2[N] = sum(h2)

for k in range(N):
    #print("A[N+k]", A[N+k])
    #print("k+1", k+1)
    heapq.heappushpop(h1, A[N+k])
    sum_h1[k+1] = sum(h1)

    #print("A[2*N-k-1]", A[2*N-k-1])
    #print("N-k-1", N-k-1)
    _heappush_max(h2, A[2*N-k-1])
    _heappop_max(h2)
    sum_h2[N-k-1] = sum(h2)


mx = None

#print("sum_h1", sum_h1)
#print("sum_h2", sum_h2)

for i in range(N+1):
    a = sum_h1[i] - sum_h2[i]

    if(mx == None or a > mx):
        mx = a

print(mx)

'''
for i in range(N+1):
    h1 = A[0:N+i]
    h2 = A[N+i:N*3]

    heapq.heapify(h1)
    heapq._heapify_max(h2)

    for k in range(N):
        if i <= k:
            _heappop_max(h2)
        else:
            heapq.heappop(h1)

    s = sum(h1) - sum(h2)

    if (mx == None or s > mx):
        mx = s
'''


# Optimized solution
import sys
import heapq
import copy


def _heappush_max(heap, item):
    heap.append(item)
    heapq._siftdown_max(heap, 0, len(heap)-1)


def _heappop_max(heap):
    """Maxheap version of a heappop."""
    lastelt = heap.pop()    # raises appropriate IndexError if heap is empty
    if heap:
        returnitem = heap[0]
        heap[0] = lastelt
        heapq._siftup_max(heap, 0)
        return returnitem
    return lastelt


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

sum_h1 = [0] * (N+1)
sum_h2 = [0] * (N+1)

'''
N=2
0 1 2 3 4 5 6
3 1 4 1 5 9
'''

h1 = A[0:N]
h2 = A[N*2:N*3]

heapq.heapify(h1)
heapq._heapify_max(h2)

#print("h1", h1)
#print("h2", h2)
sum_h1[0] = sum(h1)
sum_h2[N] = sum(h2)

for k in range(N):
    #print("A[N+k]", A[N+k])
    #print("k+1", k+1)

    sum_h1[k+1] = sum_h1[k]
    sum_h1[k+1] += A[N+k]
    a = heapq.heappushpop(h1, A[N+k])
    sum_h1[k+1] -= a

    #sum_h1[k+1] = sum(h1)

    #print("A[2*N-k-1]", A[2*N-k-1])
    #print("N-k-1", N-k-1)

    sum_h2[N-k-1] = sum_h2[N-k]
    sum_h2[N-k-1] += A[2*N-k-1]
    _heappush_max(h2, A[2*N-k-1])
    sum_h2[N-k-1] -= _heappop_max(h2)

    #sum_h2[N-k-1] = sum(h2)


mx = None

#print("sum_h1", sum_h1)
#print("sum_h2", sum_h2)

for i in range(N+1):
    a = sum_h1[i] - sum_h2[i]

    if(mx == None or a > mx):
        mx = a

print(mx)

'''
for i in range(N+1):
    h1 = A[0:N+i]
    h2 = A[N+i:N*3]

    heapq.heapify(h1)
    heapq._heapify_max(h2)

    for k in range(N):
        if i <= k:
            _heappop_max(h2)
        else:
            heapq.heappop(h1)

    s = sum(h1) - sum(h2)

    if (mx == None or s > mx):
        mx = s
'''