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)
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)

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)

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)

-import sys +import bisect -input = sys.stdin.readline -A = tuple(map(int, input().split())) -hq = list(A[:N]) -heapq.heapify(hq) -s = [0] * (N + 1) -s[0] = sum(hq) +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) -    x = heapq.heappop(hq) -    if x >= A[N + i]: -        s[i + 1] = s[i] -        heapq.heappush(hq, x) +    k = a[i + N] +    x = heapq.heappop(b) +    if x < k: +        L[i + 1] = L[i] + k - x +        heapq.heappush(b, k) -        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) +        L[i + 1] = L[i] +        heapq.heappush(b, x) -    x = heapq.heappop(hq) -    x = -x -    if x <= A[N + i]: -        t[i] = t[i + 1] -        heapq.heappush(hq, -x) +    k = a[i + N] +    x = heapq.heappop(c) +    if -x > k: +        heapq.heappush(c, -k) +        R[i] = R[i + 1] + x + k -        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) +        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)