import heapq

n = int(eval(input()))

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



FN = []

SN = []

for i in range(n):

    FN.append(a[i])

for i in range(2*n,3*n):

    SN.append(-a[i])

heapq.heapify(FN)

heapq.heapify(SN)





sumFN = [0]*(n+1)

sumSN = [0]*(n+1)

sumFN[0] = sum(FN)

sumSN[n] = -sum(SN)



for i in range(1,n+1):

    k = 0

    k += a[n-1+i]

    heapq.heappush(FN,a[n-1+i])

    k -= heapq.heappop(FN)

    sumFN[i] = sumFN[i-1]+k



for i in range(n-1,-1,-1):

    k = 0

    k += a[n+i]

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

    k -= -heapq.heappop(SN)

    sumSN[i] = sumSN[i+1]+k



res = sumFN[0]-sumSN[0]

for i in range(n+1):

    res = max(res,sumFN[i]-sumSN[i])

print(res)

import heapq



N = int(eval(input()))

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

n1 = A[:N]

s1 = [sum(n1)]

heapq.heapify(n1)

for i in range(N):

    heapq.heappush(n1,A[N+i])

    p = heapq.heappop(n1)

    s1.append(s1[-1]-p+A[N+i])



s3 = [-(sum(A[-N:]))]

n3 = [-a for a in A[-N:]]

heapq.heapify(n3)

for i in range(N):

    heapq.heappush(n3,-A[-N-i-1])

    p = heapq.heappop(n3)

    s3.append(s3[-1]-p-A[-N-i-1])

M = -10**15

# print(s3)

for i,j in zip(s1,s3[::-1]):

    # print(i,j)

    M = max(M,i+j)



print(M)
import heapq

n = int(eval(input()))
a = list(map(int, input().split()))
FN = []
SN = []
for i in range(n):
    FN.append(a[i])
for i in range(2 * n, 3 * n):
    SN.append(-a[i])
heapq.heapify(FN)
heapq.heapify(SN)
sumFN = [0] * (n + 1)
sumSN = [0] * (n + 1)
sumFN[0] = sum(FN)
sumSN[n] = -sum(SN)
for i in range(1, n + 1):
    k = 0
    k += a[n - 1 + i]
    heapq.heappush(FN, a[n - 1 + i])
    k -= heapq.heappop(FN)
    sumFN[i] = sumFN[i - 1] + k
for i in range(n - 1, -1, -1):
    k = 0
    k += a[n + i]
    heapq.heappush(SN, -a[n + i])
    k -= -heapq.heappop(SN)
    sumSN[i] = sumSN[i + 1] + k
res = sumFN[0] - sumSN[0]
for i in range(n + 1):
    res = max(res, sumFN[i] - sumSN[i])
print(res)

import heapq

N = int(eval(input()))
A = list(map(int, input().split()))
n1 = A[:N]
s1 = [sum(n1)]
heapq.heapify(n1)
for i in range(N):
    heapq.heappush(n1, A[N + i])
    p = heapq.heappop(n1)
    s1.append(s1[-1] - p + A[N + i])
s3 = [-(sum(A[-N:]))]
n3 = [-a for a in A[-N:]]
heapq.heapify(n3)
for i in range(N):
    heapq.heappush(n3, -A[-N - i - 1])
    p = heapq.heappop(n3)
    s3.append(s3[-1] - p - A[-N - i - 1])
M = -(10**15)
# print(s3)
for i, j in zip(s1, s3[::-1]):
    # print(i,j)
    M = max(M, i + j)
print(M)

-n = int(eval(input())) -a = list(map(int, input().split())) -FN = [] -SN = [] -for i in range(n): -    FN.append(a[i]) -for i in range(2 * n, 3 * n): -    SN.append(-a[i]) -heapq.heapify(FN) -heapq.heapify(SN) -sumFN = [0] * (n + 1) -sumSN = [0] * (n + 1) -sumFN[0] = sum(FN) -sumSN[n] = -sum(SN) -for i in range(1, n + 1): -    k = 0 -    k += a[n - 1 + i] -    heapq.heappush(FN, a[n - 1 + i]) -    k -= heapq.heappop(FN) -    sumFN[i] = sumFN[i - 1] + k -for i in range(n - 1, -1, -1): -    k = 0 -    k += a[n + i] -    heapq.heappush(SN, -a[n + i]) -    k -= -heapq.heappop(SN) -    sumSN[i] = sumSN[i + 1] + k -res = sumFN[0] - sumSN[0] -for i in range(n + 1): -    res = max(res, sumFN[i] - sumSN[i]) -print(res) +N = int(eval(input())) +A = list(map(int, input().split())) +n1 = A[:N] +s1 = [sum(n1)] +heapq.heapify(n1) +for i in range(N): +    heapq.heappush(n1, A[N + i]) +    p = heapq.heappop(n1) +    s1.append(s1[-1] - p + A[N + i]) +s3 = [-(sum(A[-N:]))] +n3 = [-a for a in A[-N:]] +heapq.heapify(n3) +for i in range(N): +    heapq.heappush(n3, -A[-N - i - 1]) +    p = heapq.heappop(n3) +    s3.append(s3[-1] - p - A[-N - i - 1]) +M = -(10**15) +# print(s3) +for i, j in zip(s1, s3[::-1]): +    # print(i,j) +    M = max(M, i + j) +print(M)