import heapq



class Heapq:

    def __init__(self, arr, desc=False):

        if desc:

            arr = [-a for a in arr]

        self.sign = -1 if desc else 1

        self.hq = arr

        heapq.heapify(self.hq)



    def pop(self):

        return heapq.heappop(self.hq) * self.sign



    def push(self, a):

        heapq.heappush(self.hq, a * self.sign)



    def top(self):

        return self.hq[0] * self.sign





n = int(eval(input()))

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



heap_l = Heapq(A[:n])

heap_r = Heapq(A[2*n:],desc=True)



max_l = [0]*(n+1)

min_r = [0]*(n+1)

max_l[0] = sum(A[:n])

min_r[0] = sum(A[2*n:])



for i in range(n):

    p = heap_l.pop()

    heap_l.push(max(A[n+i],p))

    max_l[i+1] = max_l[i]+max(0,A[n+i]-p)



    q = heap_r.pop()

    heap_r.push(min(A[-1-i-n],q))

    min_r[i+1] = min_r[i]+min(0,A[-1-i-n]-q)



ans = -float('inf')



for i in range(n+1):

    ans = max(ans,max_l[i]-min_r[-1-i])



print(ans)
from heapq import heappop, heappush, heapify



N = int(eval(input()))

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



L = A[:N]

heapify(L)

sumL = [sum(A[:N])]



for i in range(N):

    x = heappop(L)

    if x < A[N + i]:

        sumL.append(sumL[-1] - x + A[N + i])

        heappush(L, A[N + i])

    else:

        sumL.append(sumL[-1])

        heappush(L, x)



R = list([-x for x in A[2 * N:]])

heapify(R)

sumR = [sum(A[2 * N:])]



for i in range(N):

    x = -heappop(R)

    if x > A[2 * N - 1 - i]:

        sumR.append(sumR[-1] - x + A[2 * N - 1 - i])

        heappush(R, -A[2 * N - 1 - i])

    else:

        sumR.append(sumR[-1])

        heappush(R, -x)



res = -10**18



for i in range(N + 1):

    res = max(res, sumL[i] - sumR[-1 - i])



print(res)
import heapq


class Heapq:
    def __init__(self, arr, desc=False):
        if desc:
            arr = [-a for a in arr]
        self.sign = -1 if desc else 1
        self.hq = arr
        heapq.heapify(self.hq)

    def pop(self):
        return heapq.heappop(self.hq) * self.sign

    def push(self, a):
        heapq.heappush(self.hq, a * self.sign)

    def top(self):
        return self.hq[0] * self.sign


n = int(eval(input()))
A = list(map(int, input().split()))
heap_l = Heapq(A[:n])
heap_r = Heapq(A[2 * n :], desc=True)
max_l = [0] * (n + 1)
min_r = [0] * (n + 1)
max_l[0] = sum(A[:n])
min_r[0] = sum(A[2 * n :])
for i in range(n):
    p = heap_l.pop()
    heap_l.push(max(A[n + i], p))
    max_l[i + 1] = max_l[i] + max(0, A[n + i] - p)
    q = heap_r.pop()
    heap_r.push(min(A[-1 - i - n], q))
    min_r[i + 1] = min_r[i] + min(0, A[-1 - i - n] - q)
ans = -float("inf")
for i in range(n + 1):
    ans = max(ans, max_l[i] - min_r[-1 - i])
print(ans)

from heapq import heappop, heappush, heapify

N = int(eval(input()))
A = list(map(int, input().split()))
L = A[:N]
heapify(L)
sumL = [sum(A[:N])]
for i in range(N):
    x = heappop(L)
    if x < A[N + i]:
        sumL.append(sumL[-1] - x + A[N + i])
        heappush(L, A[N + i])
    else:
        sumL.append(sumL[-1])
        heappush(L, x)
R = list([-x for x in A[2 * N :]])
heapify(R)
sumR = [sum(A[2 * N :])]
for i in range(N):
    x = -heappop(R)
    if x > A[2 * N - 1 - i]:
        sumR.append(sumR[-1] - x + A[2 * N - 1 - i])
        heappush(R, -A[2 * N - 1 - i])
    else:
        sumR.append(sumR[-1])
        heappush(R, -x)
res = -(10**18)
for i in range(N + 1):
    res = max(res, sumL[i] - sumR[-1 - i])
print(res)

-import heapq +from heapq import heappop, heappush, heapify - -class Heapq: -    def __init__(self, arr, desc=False): -        if desc: -            arr = [-a for a in arr] -        self.sign = -1 if desc else 1 -        self.hq = arr -        heapq.heapify(self.hq) - -    def pop(self): -        return heapq.heappop(self.hq) * self.sign - -    def push(self, a): -        heapq.heappush(self.hq, a * self.sign) - -    def top(self): -        return self.hq[0] * self.sign - - -n = int(eval(input())) +N = int(eval(input())) -heap_l = Heapq(A[:n]) -heap_r = Heapq(A[2 * n :], desc=True) -max_l = [0] * (n + 1) -min_r = [0] * (n + 1) -max_l[0] = sum(A[:n]) -min_r[0] = sum(A[2 * n :]) -for i in range(n): -    p = heap_l.pop() -    heap_l.push(max(A[n + i], p)) -    max_l[i + 1] = max_l[i] + max(0, A[n + i] - p) -    q = heap_r.pop() -    heap_r.push(min(A[-1 - i - n], q)) -    min_r[i + 1] = min_r[i] + min(0, A[-1 - i - n] - q) -ans = -float("inf") -for i in range(n + 1): -    ans = max(ans, max_l[i] - min_r[-1 - i]) -print(ans) +L = A[:N] +heapify(L) +sumL = [sum(A[:N])] +for i in range(N): +    x = heappop(L) +    if x < A[N + i]: +        sumL.append(sumL[-1] - x + A[N + i]) +        heappush(L, A[N + i]) +    else: +        sumL.append(sumL[-1]) +        heappush(L, x) +R = list([-x for x in A[2 * N :]]) +heapify(R) +sumR = [sum(A[2 * N :])] +for i in range(N): +    x = -heappop(R) +    if x > A[2 * N - 1 - i]: +        sumR.append(sumR[-1] - x + A[2 * N - 1 - i]) +        heappush(R, -A[2 * N - 1 - i]) +    else: +        sumR.append(sumR[-1]) +        heappush(R, -x) +res = -(10**18) +for i in range(N + 1): +    res = max(res, sumL[i] - sumR[-1 - i]) +print(res)