# -*- 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])])))
# -*- 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)
# -*- 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])])))

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

-from heapq import heapify, heappop, heappush -from copy import deepcopy +from heapq import heapify, heapreplace -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])]))) +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)