import heapq

n= int(eval(input()))

a=[int(i) for i in input().split()]

heap1=[]

heap2=[]

a_max=[]

b_max=[]

s=0

for i in range(n):

    heapq.heappush(heap1, a[i])

    s+=a[i]

a_max.append(s)

for i in range(n):

    heapq.heappush(heap1,a[n+i])

    mini=heapq.heappop(heap1)

    s=s+a[n+i]-mini

    a_max.append(s)

s=0

for i in range(n):

    heapq.heappush(heap2,-a[2*n+i])

    s=s-a[2*n+i]

b_max.append(s)    

for i in range(n):

    heapq.heappush(heap2,-a[2*n-1-i])

    mini=heapq.heappop(heap2)

    s=s-a[2*n-1-i]-mini

    b_max.append(s)

ans=a_max[0]+b_max[len(b_max)-1]

for i in range(n+1):

    res=a_max[i]+b_max[-(i+1)]

    ans=max(ans, res)

print(ans)
from heapq import heappush, heappop

n=int(eval(input()))

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

q1=[]

q2=[]

s1=[]

s2=[]

for i in range(n):

    heappush(q1, a[i])

s1.append(sum(a[0:n]))

for ai in a[n:2*n]:

    heappush(q1, ai)

    m=heappop(q1)

    s1.append(s1[-1]+ai-m)

a=a[::-1]

for i in range(n):

    heappush(q2, -a[i])

s2.append(-sum(a[0:n]))

for ai in [-i for i in a[n:2*n]]:

    heappush(q2, ai)

    m=heappop(q2)

    s2.append(s2[-1]+ai-m)

ans=s1[0]+s2[-1]

for i in range(n+1):

    ans=max(s1[i]+s2[-i-1], ans)

print(ans)    
import heapq

n = int(eval(input()))
a = [int(i) for i in input().split()]
heap1 = []
heap2 = []
a_max = []
b_max = []
s = 0
for i in range(n):
    heapq.heappush(heap1, a[i])
    s += a[i]
a_max.append(s)
for i in range(n):
    heapq.heappush(heap1, a[n + i])
    mini = heapq.heappop(heap1)
    s = s + a[n + i] - mini
    a_max.append(s)
s = 0
for i in range(n):
    heapq.heappush(heap2, -a[2 * n + i])
    s = s - a[2 * n + i]
b_max.append(s)
for i in range(n):
    heapq.heappush(heap2, -a[2 * n - 1 - i])
    mini = heapq.heappop(heap2)
    s = s - a[2 * n - 1 - i] - mini
    b_max.append(s)
ans = a_max[0] + b_max[len(b_max) - 1]
for i in range(n + 1):
    res = a_max[i] + b_max[-(i + 1)]
    ans = max(ans, res)
print(ans)

from heapq import heappush, heappop

n = int(eval(input()))
a = list(map(int, input().split()))
q1 = []
q2 = []
s1 = []
s2 = []
for i in range(n):
    heappush(q1, a[i])
s1.append(sum(a[0:n]))
for ai in a[n : 2 * n]:
    heappush(q1, ai)
    m = heappop(q1)
    s1.append(s1[-1] + ai - m)
a = a[::-1]
for i in range(n):
    heappush(q2, -a[i])
s2.append(-sum(a[0:n]))
for ai in [-i for i in a[n : 2 * n]]:
    heappush(q2, ai)
    m = heappop(q2)
    s2.append(s2[-1] + ai - m)
ans = s1[0] + s2[-1]
for i in range(n + 1):
    ans = max(s1[i] + s2[-i - 1], ans)
print(ans)

-import heapq +from heapq import heappush, heappop -a = [int(i) for i in input().split()] -heap1 = [] -heap2 = [] -a_max = [] -b_max = [] -s = 0 +a = list(map(int, input().split())) +q1 = [] +q2 = [] +s1 = [] +s2 = [] -    heapq.heappush(heap1, a[i]) -    s += a[i] -a_max.append(s) +    heappush(q1, a[i]) +s1.append(sum(a[0:n])) +for ai in a[n : 2 * n]: +    heappush(q1, ai) +    m = heappop(q1) +    s1.append(s1[-1] + ai - m) +a = a[::-1] -    heapq.heappush(heap1, a[n + i]) -    mini = heapq.heappop(heap1) -    s = s + a[n + i] - mini -    a_max.append(s) -s = 0 -for i in range(n): -    heapq.heappush(heap2, -a[2 * n + i]) -    s = s - a[2 * n + i] -b_max.append(s) -for i in range(n): -    heapq.heappush(heap2, -a[2 * n - 1 - i]) -    mini = heapq.heappop(heap2) -    s = s - a[2 * n - 1 - i] - mini -    b_max.append(s) -ans = a_max[0] + b_max[len(b_max) - 1] +    heappush(q2, -a[i]) +s2.append(-sum(a[0:n])) +for ai in [-i for i in a[n : 2 * n]]: +    heappush(q2, ai) +    m = heappop(q2) +    s2.append(s2[-1] + ai - m) +ans = s1[0] + s2[-1] -    res = a_max[i] + b_max[-(i + 1)] -    ans = max(ans, res) +    ans = max(s1[i] + s2[-i - 1], ans)