CodeEff/ECCO huggingface dataset:
# Original solution
n = int(eval(input()))
a = list(map(int, input().split()))

ans = -10**15
for i in range(n, 2*n+1):
    a_front = a[0 : i]
    a_back = a[i : 3*n]
    a_front.sort(reverse=True)
    a_back.sort()
    tmp = sum(a_front[0:n]) - sum(a_back[0:n])
    if tmp > ans:
        ans = tmp

print(ans)    

# Optimized solution
from heapq import heappop, heapify, heappush

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

a_front = a[0 : n]
a_back = list([-1*x for x in a[2*n : 3*n]])
heapify(a_front)
heapify(a_back)

sum_f = [0]*(n+1)
sum_b = [0]*(n+1)
sum_f[0] = sum(a_front)
sum_b[n] = sum(a_back)*-1

for i in range(n, 2*n):
    # 前半の処理
    heappush(a_front, a[i])
    sum_f[i-n+1] = sum_f[i-n] + a[i] - heappop(a_front)
    # 後半の処理
    heappush(a_back, -1 * a[3*n-i-1])
    sum_b[2*n-i-1] = sum_b[2*n-i] + a[3*n-i-1] + heappop(a_back)

ans = sum_f[0] - sum_b[0]
for i in range(1, n+1):
    tmp = sum_f[i] - sum_b[i]
    if tmp > ans:
        ans = tmp
        
print(ans)
# Original solution
from heapq import heapify, heappop, heappush

N, *A = list(map(int, open(0).read().split()))

Q = A[:N]
heapify(Q)

R = [sum(Q)]
for k in range(N, 2 * N):
    heappush(Q, A[k])
    m = heappop(Q)
    R.append(R[-1] + A[k] - m)

Q = [-a for a in A[2 * N:]]
heapify(Q)

B = [sum(Q)]
for k in reversed(list(range(N, 2 * N))):
    heappush(Q, -A[k])
    m = heappop(Q)
    B.append(B[-1] - A[k] - m)

print((max(r + b for r, b in zip(R, reversed(B)))))


# Optimized solution
from heapq import heapify, heappop, heappush

N, *A = list(map(int, open(0).read().split()))

Q = A[:N]
heapify(Q)

R = [sum(Q)]
for a in A[N:2 * N]:
    heappush(Q, a)
    R.append(R[-1] + a - heappop(Q))

Q = [-a for a in A[2 * N:]]
heapify(Q)

B = [sum(Q)]
for a in reversed(A[N:2 * N]):
    heappush(Q, -a)
    B.append(B[-1] - a - heappop(Q))

print((max(r + b for r, b in zip(R, reversed(B)))))
# Original solution
from heapq import heapify, heappop, heappush

N, *A = list(map(int, open(0).read().split()))

Q = A[:N]
heapify(Q)

R = [sum(Q)]
for a in A[N:2 * N]:
    heappush(Q, a)
    R.append(R[-1] + a - heappop(Q))

Q = [-a for a in A[2 * N:]]
heapify(Q)

B = [sum(Q)]
for a in reversed(A[N:2 * N]):
    heappush(Q, -a)
    B.append(B[-1] - a - heappop(Q))

print((max(r + b for r, b in zip(R, reversed(B)))))


# Optimized solution
from heapq import heapify, heappushpop

N, *A = list(map(int, open(0).read().split()))

Q = A[:N]
heapify(Q)

R = [sum(Q)]
for a in A[N:2 * N]:
    R.append(R[-1] + a - heappushpop(Q, a))

Q = [-a for a in A[2 * N:]]
heapify(Q)

B = [sum(Q)]
for a in reversed(A[N:2 * N]):
    B.append(B[-1] - a - heappushpop(Q, -a))

print((max(r + b for r, b in zip(R, reversed(B)))))
# Original solution
import heapq
N = int(eval(input()))
a = list(map(int,input().split()))
fa = a[:N:]
ba = list([x*(-1) for x in a[2*N::]])
heapq.heapify(fa)
heapq.heapify(ba)
suml = [sum(fa)]
k = -2

for i in range(N, 2*N):
    heapq.heappush(fa, a[i])
    heapq.heappop(fa)
    suml.append(sum(fa))

suml[-1] += sum(ba)

for j in range(2*N-1, N-1, -1):
        heapq.heappush(ba, -a[j])
        heapq.heappop(ba)
        suml[k] += sum(ba)
        k -= 1
    


print((max(suml)))


    


# Optimized solution
import heapq
N = int(eval(input()))
a = list(map(int,input().split()))
fa = a[:N:]
ba = list([x*(-1) for x in a[2*N::]])
heapq.heapify(fa)
heapq.heapify(ba)
sumf = sum(fa)
sumb = sum(ba)
suml = [sumf]

k = -2

for i in range(N, 2*N):
    heapq.heappush(fa, a[i])
    sumf += a[i]
    sumf -= heapq.heappop(fa)
    suml.append(sumf)

suml[-1] += sumb

for j in range(2*N-1, N-1, -1):
        heapq.heappush(ba, -a[j])
        sumb -= a[j]
        sumb -= heapq.heappop(ba)
        suml[k] += sumb
        k -= 1
    


print((max(suml)))
# Original solution
def i1():
 return int(eval(input()))
def i2():
 return [int(i) for i in input().split()]
n=i1()
a=i2()
import heapq
q=a[:n]
rq=[-a[i] for i in range(2*n,3*n)[::-1]]
d=[sum(q)]
rd=[sum(rq)]

heapq.heapify(q)
heapq.heapify(rq)

for i in range(n,2*n):
 heapq.heappush(q, a[i])
 heapq.heappop(q)
 d.append(sum(q))
for i in range(n,2*n)[::-1]:
 heapq.heappush(rq, -a[i])
 heapq.heappop(rq)
 rd.append(sum(rq))
c=-float("inf")
for i in range(len(d)):
  c=max(c,d[i]+rd[len(rd)-1-i])
print(c)

# Optimized solution
def i1():
 return int(eval(input()))
def i2():
 return [int(i) for i in input().split()]
n=i1()
a=i2()
import heapq
q=a[:n]
rq=[-a[i] for i in range(2*n,3*n)[::-1]]
dd=sum(q)
rdd=sum(rq)
d=[dd]
rd=[rdd]

heapq.heapify(q)
heapq.heapify(rq)

for i in range(n,2*n):
 heapq.heappush(q, a[i])
 x=heapq.heappop(q)
 dd=dd+a[i]-x
 d.append(dd)
for i in range(n,2*n)[::-1]:
 heapq.heappush(rq, -a[i])
 x=heapq.heappop(rq)
 rdd=rdd-a[i]-x
 rd.append(rdd)
c=-float("inf")
for i in range(len(d)):
  c=max(c,d[i]+rd[len(rd)-1-i])
print(c)
# Original solution
import heapq
import sys
from collections import deque
input=sys.stdin.readline

n=int(eval(input()))
a=[int(i) for i in input().split()]
#f=[-i for i in a[:n]]
f=a[:n]
l=[-i for i in a[2*n:]]
heapq.heapify(f)
heapq.heapify(l)
fs=[0]*(n+1)
ls=[0]*(n+1)
fs[0]=sum(f)
ls[n]=-sum(l)

for i in range(n):
    c=a[n+i]
    m=heapq.heappushpop(f,c)
    fs[i+1]=fs[i]+c-m
#    print(f,c,m)

for i in range(n):
    c=a[2*n-1-i]
    m=heapq.heappushpop(l,-c)
    ls[n-i-1]=ls[n-i]+c+m
#    print(l,-c,m)


ans=fs[0]-ls[0]
for k in range(n+1):
    ans=max(ans,fs[k]-ls[k])
print(ans)#,a,n,fs,ls)


# Optimized solution
import heapq
n=int(eval(input()))
a=[int(i) for i in input().split()]
f=a[:n]
l=[-i for i in a[2*n:]]
heapq.heapify(f)
heapq.heapify(l)
fs=[0]*(n+1)
ls=[0]*(n+1)
fs[0]=sum(f)
ls[n]=-sum(l)

for i in range(n):
    c=a[n+i]
    m=heapq.heappushpop(f,c)
    fs[i+1]=fs[i]+c-m

for i in range(n):
    c=a[2*n-1-i]
    m=heapq.heappushpop(l,-c)
    ls[n-i-1]=ls[n-i]+c+m

ans=fs[0]-ls[0]
for k in range(n+1):
    ans=max(ans,fs[k]-ls[k])
print(ans)
# Original solution
import heapq

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

left = A[:N]
lefty = sum(left)
right = [-A[i] for i in range(3*N-1, 2*N-1, -1)]
righty = sum(right)
left_list = [lefty]
right_list = [righty]

heapq.heapify(left)
heapq.heapify(right)


for i in range(N, 2*N):
    heapq.heappush(left, A[i])
    x = heapq.heappop(left)
    lefty += A[i] - x
    left_list.append(lefty)

for i in range(2*N-1, N-1, -1):
    heapq.heappush(right, -A[i])
    y = heapq.heappop(right)
    righty += -A[i] - y
    right_list.append(righty)

ans = - float('inf')
for i in range(len(left_list)):
    ans = max(ans, left_list[i]+right_list[len(right_list)-1-i])

print(ans)


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


# L = heapq.heapify(A[:N])
# R = heapq.heapify(A[2*N:]) この記法ないらしい
# BC125C の累積GCDの直後にやっているので、似たやり方で解いてみる
L = A[:N]
R = [-a for a in A[2*N:]]
L_sum, R_sum = sum(L), sum(R)
heapq.heapify(L)
heapq.heapify(R)

L_acc = [L_sum]
R_acc = [R_sum]
for i in range(N, 2*N):
    if L[0] >= A[i]:
        if i > N:
            L_acc.append(L_acc[-1])
        elif i == N:
            pass
    else:
        heapq.heappush(L, A[i])
        out = heapq.heappop(L)
        L_acc.append(L_acc[-1] - out + A[i])
    #print(L_acc)

for i in range(2*N-1, N-1, -1):
    if R[0] >= -A[i]:
        if i <= 2*N-1:
            R_acc.append(R_acc[-1])
        elif i == 2*N-1:
            pass
    else:
        heapq.heappush(R, -A[i])
        out = heapq.heappop(R)
        R_acc.append(R_acc[-1] - out - A[i])
    #print(R_acc)

ans = -float('inf')
for i in range(len(L_acc)):
     ans = max(ans, L_acc[i]+R_acc[-(i+1)])

print(ans)
# Original solution
import heapq
N = int(eval(input()))
a = list(map(int, input().split()))
m = len(a) // 2

cumsumA = []
prefix = [x for x in a[:N]]
heapq.heapify(prefix)
cumsumA.append(sum(prefix))
for k in range(N, 2*N):
  heapq.heappush(prefix, a[k])
  heapq.heappop(prefix)
  cumsumA.append(sum(prefix))

cumsumB = []
suffix = [-x for x in a[2*N:]]
heapq.heapify(suffix)
cumsumB.append(-sum(suffix))
for k in range(2*N-1, N-1, -1):
  heapq.heappush(suffix, -a[k])
  heapq.heappop(suffix)
  cumsumB.append(-sum(suffix))

res = -float('inf')
for i in range(0, len(cumsumA)):
  res = max(res, cumsumA[i] - cumsumB[len(cumsumA)-i-1])

print(res)
  


# Optimized solution
import heapq
N = int(eval(input()))
a = list(map(int, input().split()))
m = len(a) // 2

cumsumA = []
prefix = [x for x in a[:N]]
heapq.heapify(prefix)
cumsumA.append(sum(prefix))
cursum = sum(prefix)
for k in range(N, 2*N):
  heapq.heappush(prefix, a[k])
  ele = heapq.heappop(prefix)
  cursum = cursum + a[k] - ele
  cumsumA.append(cursum)
  # cumsumA.append(sum(prefix))

cumsumB = []
suffix = [-x for x in a[2*N:]]
heapq.heapify(suffix)
cumsumB.append(-sum(suffix))
cursum = -sum(suffix)
for k in range(2*N-1, N-1, -1):
  heapq.heappush(suffix, -a[k])
  ele = heapq.heappop(suffix)
  cursum = cursum + a[k] + ele
  cumsumB.append(cursum)

res = -float('inf')
for i in range(0, len(cumsumA)):
  res = max(res, cumsumA[i] - cumsumB[len(cumsumA)-i-1])

print(res)
# Original solution
import heapq

def solve(n,a):
    res = -float('inf')
    for k in range(n,2*n+1):
        a1 = a[:k]
        a2 = [-a[i] for i in range(k,3*n)]

        heapq.heapify(a1)
        heapq.heapify(a2)

        while len(a1) > n:
            heapq.heappop(a1)
        while len(a2) > n:
            heapq.heappop(a2)
        
        tmp = sum(a1) + sum(a2)

        res = max(res,tmp)

    return res

if __name__ == '__main__':
    n = int(eval(input()))
    a = list(map(int,input().split()))
    print((solve(n,a)))

# Optimized solution
import heapq

def solve(n,a):
    a1 = a[:n]
    a2 = [-a[i] for i in range(2*n,3*n)]

    l = [[0,0] for i in range(n+1)]

    heapq.heapify(a1)
    heapq.heapify(a2)

    l[0][0] = sum(a1)
    l[n][1] = sum(a2)
    
    for k in range(n,2*n):

        heapq.heappush(a1,a[k])
        tmp = heapq.heappop(a1)
        l[k-n+1][0] = l[k-n][0] + a[k] - tmp

        heapq.heappush(a2,-a[3*n-k-1])
        tmp = heapq.heappop(a2)
        l[2*n-k-1][1] = l[2*n-k][1] - a[3*n-k-1] - tmp

    res = -float('inf')

    for ans in l:
        res = max(res,ans[0]+ans[1])

    return res

if __name__ == '__main__':
    n = int(eval(input()))
    a = list(map(int,input().split()))
    print((solve(n,a)))
# Original solution
# -*- coding: utf-8 -*-
def inpl(): return list(map(int, input().split()))
from heapq import heapify, heappop, heappush
from copy import deepcopy

N = int(eval(input()))
A = inpl()

before = deepcopy(A[:N])
heapify(before)
middle = A[N:-N]
after = [-a for a in A[-N:]]
heapify(after)

sb = sum(A[:N])
sa = -sum(A[-N:])

SB = [sb]
SA = [sa]

for m in middle:
    b = heappop(before)
    if b < m:
        heappush(before, m)
        sb += m - b
    else:
        heappush(before, b)
    SB.append(sb)    

for m in middle[::-1]:
    m = -m
    a = heappop(after)
    if a < m:
        heappush(after, m)
        sa += m - a
    else:
        heappush(after, a)
    SA.append(sa) 

    
print((max([b+a for b, a in zip(SB, SA[::-1])])))

# Optimized solution
# -*- coding: utf-8 -*-
def inpl(): return list(map(int, input().split()))
from heapq import heapify, heapreplace 

N = int(eval(input()))
A = inpl()
L = A[:N]
M = A[N:2*N]
R = [-a for a in A[2*N:]]
ans = sum(L) + sum(R)
heapify(L)
heapify(R)


i = 0
j = N+1

S = [0]*(N+1)

s = 0
for i, m in enumerate(M):
    if m > L[0]:
        s += m - heapreplace(L, m)
    S[i+1] += s

s = 0
for i, m in enumerate(M[::-1]):
    if m < -R[0]:
        s += -m - heapreplace(R, -m)
    S[-i-2] += s

ans += max(S)
print(ans)
# Original solution
import sys
import heapq
input=sys.stdin.readline
N=int(eval(input()))
A=tuple(map(int,input().split()))
hq=list(A[:N])
heapq.heapify(hq)
s=[0]*(N+1)
s[0]=sum(hq)
for i in range(N):
  x=heapq.heappop(hq)
  if x >= A[N+i]:
    s[i+1]=s[i]
    heapq.heappush(hq,x)
  else:
    s[i+1]=s[i]+A[N+i]-x
    heapq.heappush(hq,A[N+i])
    
hq=list([-x for x in A[2*N:]])
heapq.heapify(hq)
t=[0]*(N+1)
t[-1]=-sum(hq)
for i in range(N-1,-1,-1):
  x=heapq.heappop(hq)
  x=-x
  if x <= A[N+i]:
    t[i]=t[i+1]
    heapq.heappush(hq,-x)
  else:
    t[i]=t[i+1]-x+A[N+i]
    heapq.heappush(hq,-A[N+i])
ans=-10**15
for x,y in zip(s,t):
  ans=max(x-y,ans)
print(ans)

# Optimized solution
import heapq
import bisect
N = int(eval(input()))
a = list(map(int, input().split()))

b = a[:N]
c = a[-N:]
L = [0]*(N+1)
R = [0]*(N+1)

L[0] = sum(b)


heapq.heapify(b)

R[-1] = sum(c)
c = list([-x for x in c])
heapq.heapify(c)

for i in range(N):
    k = a[i+N]
    x = heapq.heappop(b)
    if x < k:
        L[i+1] = L[i] + k - x
        heapq.heappush(b, k)
    else:
        L[i+1] = L[i]
        heapq.heappush(b, x)

for i in range(N-1, -1, -1):
    k = a[i+N]
    x = heapq.heappop(c)
    if -x > k:
        heapq.heappush(c, -k)
        R[i] = R[i+1] + x + k
    else:
        R[i] = R[i+1]
        heapq.heappush(c, x)
ans = -10**14
for i in range(N+1):
    ans = max(ans, L[i]-R[i])
#print(L, R)
print(ans)
# Original solution
# -*- coding: utf-8 -*-

import heapq


class MaxHeap:
    def __init__(self, li):
        self.heap = [-e for e in li]
        heapq.heapify(self.heap)

    def push(self, val):
        heapq.heappush(self.heap, -val)

    def pop(self):
        return -heapq.heappop(self.heap)


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

fh_sum = 0
fh = []
sh_sum = 0
sh = MaxHeap([])
for i in range(n):
    heapq.heappush(fh, a[i])
    fh_sum += a[i]
    sh.push(a[-(i+1)])
    sh_sum += a[-(i+1)]

fh_sums = [fh_sum]
sh_sums = [sh_sum]
for k in range(n, 2 * n):
    fh_sum += a[k]
    heapq.heappush(fh, a[k])
    fh_sum -= heapq.heappop(fh)
    fh_sums.append(fh_sum)
    sh_sum += a[-(k+1)]
    sh.push(a[-(k+1)])
    sh_sum -= sh.pop()
    sh_sums.append(sh_sum)

sh_sums = sh_sums[::-1]

ans = -float("inf")
for fh_sum, sh_sum in zip(fh_sums, sh_sums):
    score = fh_sum - sh_sum
    ans = max(ans, score)

print(ans)


# Optimized solution
from heapq import heapify, heappop, heappush


class MaxHeap:
    def __init__(self, li):
        self.hp = []
        for e in li:
            heappush(self.hp, -e)

    def push(self, x):
        heappush(self.hp, -x)

    def pop(self):
        ret = heappop(self.hp)
        ret *= -1
        return ret

    def seak(self):
        return -self.hp[0]

    def sum(self):
        return -sum(self.hp)


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

f = a[:n]
s = a[n:2*n]
t = a[2*n:]

heapify(f)
t = MaxHeap(t)

f_sm = sum(f)
f_sm_li = [f_sm]
for e in s:
    heappush(f, e)
    f_sm += e
    pp = heappop(f)
    f_sm -= pp
    f_sm_li.append(f_sm)

t_sm = t.sum()
t_sm_li = [t_sm]
for e in s[::-1]:
    t.push(e)
    t_sm += e
    pp = t.pop()
    t_sm -= pp
    t_sm_li.append(t_sm)

t_sm_li = t_sm_li[::-1]
ans = -float("inf")
for ef, et in zip(f_sm_li, t_sm_li):
    ans = max(ans, ef - et)

print(ans)
# Original solution
import heapq
n = int(eval(input()))
a = [int(i) for i in input().split()]
k = 0
a_front = []
a_center = []
a_back = []
front_sum = 0
back_sum = 0

for i in a:
    k += 1
    if k <= n:
        a_front += [i]
        front_sum += i
    elif k <= 2 * n:
        a_center += [i]
    else:
        a_back += [-i]
        back_sum += i

heapq.heapify(a_front)
heapq.heapify(a_back)

front_sums = [front_sum]
back_sums = [back_sum]

for i in range(n):
        front_sum += a_center[i] - heapq.heappushpop(a_front, a_center[i])
        front_sums += [front_sum]
        back_sum += a_center[-i-1] + heapq.heappushpop(a_back, -a_center[-i-1])
        back_sums += [back_sum]

print((max([front_sums[i] - back_sums[-i-1] for i in range(n+1)])))
#print(front_sums, back_sums)


# Optimized solution
import heapq

def calc(front_sum, back_sum):
    heapq.heapify(a_front)
    heapq.heapify(a_back)
    front_sums = [front_sum]
    back_sums = [back_sum]
    for i in range(n):
        front_sum += a_center[i] - heapq.heappushpop(a_front, a_center[i])
        front_sums += [front_sum]
        back_sum += a_center[-i - 1] + heapq.heappushpop(a_back, -a_center[-i - 1])
        back_sums += [back_sum]
    return max([front_sums[i] - back_sums[-i-1] for i in range(n+1)])



if __name__ == "__main__":
    n = int(eval(input()))
    a = [int(i) for i in input().split()]
    k = 0
    a_front = []
    a_center = []
    a_back = []
    front_sum = 0
    back_sum = 0

    for i in a:
        k += 1
        if k <= n:
            a_front += [i]
            front_sum += i
        elif k <= 2 * n:
            a_center += [i]
        else:
            a_back += [-i]
            back_sum += i

    print((calc(front_sum, back_sum)))
# Original solution
import heapq

def calc(front_sum, back_sum):
    heapq.heapify(a_front)
    heapq.heapify(a_back)
    front_sums = [front_sum]
    back_sums = [back_sum]
    for i in range(n):
        front_sum += a_center[i] - heapq.heappushpop(a_front, a_center[i])
        front_sums += [front_sum]
        back_sum += a_center[-i - 1] + heapq.heappushpop(a_back, -a_center[-i - 1])
        back_sums += [back_sum]
    return max([front_sums[i] - back_sums[-i-1] for i in range(n+1)])



if __name__ == "__main__":
    n = int(eval(input()))
    a = [int(i) for i in input().split()]
    k = 0
    a_front = []
    a_center = []
    a_back = []
    front_sum = 0
    back_sum = 0

    for i in a:
        k += 1
        if k <= n:
            a_front += [i]
            front_sum += i
        elif k <= 2 * n:
            a_center += [i]
        else:
            a_back += [-i]
            back_sum += i

    print((calc(front_sum, back_sum)))



# Optimized solution
import heapq

def calc(front_sum, back_sum):
    heapq.heapify(a_front)
    heapq.heapify(a_back)
    front_sums = [front_sum]
    back_sums = [back_sum]
    for i in a_center:
        front_sum += i - heapq.heappushpop(a_front, i)
        front_sums += [front_sum]
    a_center.reverse()
    for i in a_center:
        back_sum += i + heapq.heappushpop(a_back, -i)
        back_sums += [back_sum]
    return max([front_sums[i] - back_sums[-i-1] for i in range(n+1)])



if __name__ == "__main__":
    n = int(eval(input()))
    a = [int(i) for i in input().split()]
    k = 0
    a_front = []
    a_center = []
    a_back = []
    front_sum = 0
    back_sum = 0

    for i in a:
        k += 1
        if k <= n:
            a_front += [i]
            front_sum += i
        elif k <= 2 * n:
            a_center += [i]
        else:
            a_back += [-i]
            back_sum += i

    print((calc(front_sum, back_sum)))
# Original solution
import heapq

def calc(front_sum, back_sum):
    heapq.heapify(a_front)
    heapq.heapify(a_back)
    front_sums = [front_sum]
    back_sums = [back_sum]
    for i in a_center:
        front_sum += i - heapq.heappushpop(a_front, i)
        front_sums += [front_sum]
    a_center.reverse()
    for i in a_center:
        back_sum += i + heapq.heappushpop(a_back, -i)
        back_sums += [back_sum]
    return max([front_sums[i] - back_sums[-i-1] for i in range(n+1)])



if __name__ == "__main__":
    n = int(eval(input()))
    a = [int(i) for i in input().split()]
    k = 0
    a_front = []
    a_center = []
    a_back = []
    front_sum = 0
    back_sum = 0

    for i in a:
        k += 1
        if k <= n:
            a_front += [i]
            front_sum += i
        elif k <= 2 * n:
            a_center += [i]
        else:
            a_back += [-i]
            back_sum += i

    print((calc(front_sum, back_sum)))



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

def calc(front_sum, back_sum):
    heapq.heapify(a_front)
    heapq.heapify(a_back)
    front_sums = [front_sum]
    back_sums = [back_sum]
    for i in a_center:
        front_sum += i - heapq.heappushpop(a_front, i)
        front_sums += [front_sum]
    a_center.reverse()
    for i in a_center:
        back_sum += i + heapq.heappushpop(a_back, -i)
        back_sums += [back_sum]
    return max([front_sums[i] - back_sums[-i-1] for i in range(n+1)])



if __name__ == "__main__":
    n = int(eval(input()))
    a = [int(i) for i in input().split()]
    k = 0
    a_front = []
    a_center = []
    a_back = []
    front_sum = 0
    back_sum = 0

    for i in a:
        k += 1
        if k <= n:
            a_front += [i]
            front_sum += i
        elif k <= 2 * n:
            a_center += [i]
        else:
            a_back += [-i]
            back_sum += i

    print((calc(front_sum, back_sum)))
# Original solution
import heapq

n = int(eval(input()))
array = [int(i) for i in input().split()]
ans=-1e14
l_array = []
r_array = []
l_sum = [0]*(n+1)
r_sum = [0]*(n+1)

# left-n and right-n numbers
for i in range(n):
    heapq.heappush(l_array, array[i])
    heapq.heappush(r_array, (-array[3*n-1-i], array[3*n-1-i]))

l_sum[0] = sum(l_array)
r_sum[n] = sum([i[1] for i in r_array])

for i in range(n, 2*n):
    heapq.heappush(l_array, array[i])
    heapq.heappush(r_array, (-array[3*n-1-i], array[3*n-1-i]))
    heapq.heappop(l_array)
    heapq.heappop(r_array)

    l_sum[i-n+1] = sum(l_array)
    r_sum[2*n-1-i] = sum([j[1] for j in r_array])

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

print(ans)

# Optimized solution
import heapq

n = int(eval(input()))
array = [int(i) for i in input().split()]
ans=-1e14
l_array = []
r_array = []
l_sum = [0]*(n+1)
r_sum = [0]*(n+1)

# left-n and right-n numbers
for i in range(n):
    heapq.heappush(l_array, array[i])
    heapq.heappush(r_array, (-array[3*n-1-i], array[3*n-1-i]))

l_sum[0] = sum(l_array)
r_sum[n] = sum([i[1] for i in r_array])

for i in range(n, 2*n):
    heapq.heappush(l_array, array[i])
    heapq.heappush(r_array, (-array[3*n-1-i], array[3*n-1-i]))
    l_min = heapq.heappop(l_array)
    r_max = heapq.heappop(r_array)[1]

    # l_sum[i-n+1] = sum(l_array)
    # r_sum[2*n-1-i] = sum([j[1] for j in r_array])
    l_sum[i-n+1] = l_sum[i-n] - l_min + array[i]
    r_sum[2*n-1-i] = r_sum[2*n-i] - r_max + array[3*n-1-i]

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

print(ans)