from heapq import heappop, heappush



inf = float('inf')



N = int(eval(input()))

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



# sum_max[k] : [0,k)のうちN個までの総和の最大値

# sum_min[k] : (3N-1-k,3N-1]のうちN個までの総和の最小値

sum_max = [0]*(3*N+1)

sum_min = [0]*(3*N+1)



def init_max():

    res = 0

    hq = []

    for i, a in enumerate(A):

        if len(hq) < N:

            heappush(hq,a)

            res += a

        else:

            if hq[0] < a:

                res -= heappop(hq)

                heappush(hq,a)

                res += a

        sum_max[i+1] = res



def init_min():

    res = 0

    hq = []

    for i, a in enumerate(A[::-1]):

        if len(hq) < N:

            heappush(hq,-a)

            res += a

        else:

            if -hq[0] > a:

                res -= -heappop(hq) 

                heappush(hq,-a)

                res += a

        sum_min[i+1] = res



init_max()

init_min()



ans = -inf



for k in range(N,2*N+1):

    res = sum_max[k] - sum_min[3*N-k]

    ans = max(ans, res)



print(ans)
from heapq import heappop, heappush

inf = float('inf')



N = int(eval(input()))

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



l = []

res = 0

for i in range(N):

    heappush(l, A[i])

    res += A[i]

ls = []

ls.append(res)



for i in range(N, 2*N):

    heappush(l, A[i])

    res += A[i]

    res -= heappop(l)

    ls.append(res)



A.reverse()



s = []

res = 0

for i in range(N):

    heappush(s, -A[i])

    res += A[i]

ss = []

ss.append(res)



for i in range(N, 2*N):

    heappush(s, -A[i])

    res += A[i]

    res -= -heappop(s)

    ss.append(res)



ss.reverse()



ans = -inf



for i in range(N+1):

    score = ls[i] - ss[i]

    ans = max(ans, score)



print(ans)
from heapq import heappop, heappush

inf = float("inf")
N = int(eval(input()))
A = list(map(int, input().split()))
# sum_max[k] : [0,k)のうちN個までの総和の最大値
# sum_min[k] : (3N-1-k,3N-1]のうちN個までの総和の最小値
sum_max = [0] * (3 * N + 1)
sum_min = [0] * (3 * N + 1)


def init_max():
    res = 0
    hq = []
    for i, a in enumerate(A):
        if len(hq) < N:
            heappush(hq, a)
            res += a
        else:
            if hq[0] < a:
                res -= heappop(hq)
                heappush(hq, a)
                res += a
        sum_max[i + 1] = res


def init_min():
    res = 0
    hq = []
    for i, a in enumerate(A[::-1]):
        if len(hq) < N:
            heappush(hq, -a)
            res += a
        else:
            if -hq[0] > a:
                res -= -heappop(hq)
                heappush(hq, -a)
                res += a
        sum_min[i + 1] = res


init_max()
init_min()
ans = -inf
for k in range(N, 2 * N + 1):
    res = sum_max[k] - sum_min[3 * N - k]
    ans = max(ans, res)
print(ans)

from heapq import heappop, heappush

inf = float("inf")
N = int(eval(input()))
A = list(map(int, input().split()))
l = []
res = 0
for i in range(N):
    heappush(l, A[i])
    res += A[i]
ls = []
ls.append(res)
for i in range(N, 2 * N):
    heappush(l, A[i])
    res += A[i]
    res -= heappop(l)
    ls.append(res)
A.reverse()
s = []
res = 0
for i in range(N):
    heappush(s, -A[i])
    res += A[i]
ss = []
ss.append(res)
for i in range(N, 2 * N):
    heappush(s, -A[i])
    res += A[i]
    res -= -heappop(s)
    ss.append(res)
ss.reverse()
ans = -inf
for i in range(N + 1):
    score = ls[i] - ss[i]
    ans = max(ans, score)
print(ans)

-# sum_max[k] : [0,k)のうちN個までの総和の最大値 -# sum_min[k] : (3N-1-k,3N-1]のうちN個までの総和の最小値 -sum_max = [0] * (3 * N + 1) -sum_min = [0] * (3 * N + 1) - - -def init_max(): -    res = 0 -    hq = [] -    for i, a in enumerate(A): -        if len(hq) < N: -            heappush(hq, a) -            res += a -        else: -            if hq[0] < a: -                res -= heappop(hq) -                heappush(hq, a) -                res += a -        sum_max[i + 1] = res - - -def init_min(): -    res = 0 -    hq = [] -    for i, a in enumerate(A[::-1]): -        if len(hq) < N: -            heappush(hq, -a) -            res += a -        else: -            if -hq[0] > a: -                res -= -heappop(hq) -                heappush(hq, -a) -                res += a -        sum_min[i + 1] = res - - -init_max() -init_min() +l = [] +res = 0 +for i in range(N): +    heappush(l, A[i]) +    res += A[i] +ls = [] +ls.append(res) +for i in range(N, 2 * N): +    heappush(l, A[i]) +    res += A[i] +    res -= heappop(l) +    ls.append(res) +A.reverse() +s = [] +res = 0 +for i in range(N): +    heappush(s, -A[i]) +    res += A[i] +ss = [] +ss.append(res) +for i in range(N, 2 * N): +    heappush(s, -A[i]) +    res += A[i] +    res -= -heappop(s) +    ss.append(res) +ss.reverse() -for k in range(N, 2 * N + 1): -    res = sum_max[k] - sum_min[3 * N - k] -    ans = max(ans, res) +for i in range(N + 1): +    score = ls[i] - ss[i] +    ans = max(ans, score)