import heapq

N = int(eval(input()))

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



#前半部分の最適化辞書作成

SUM = sum(A[:N])

firstDic = {N-1:SUM}

que = A[:N]

heapq.heapify(que)

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

    SUM += A[i]

    heapq.heappush(que,A[i])

    SUM -= heapq.heappop(que)

    firstDic[i] = SUM





#後半部分の最適化辞書作成

SUM = sum(A[2*N:])*-1

latterDic = {2*N-1:SUM*-1}

que = [A[i]*-1 for i in range(2*N,3*N)]

heapq.heapify(que)

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

    SUM += A[i]*-1

    heapq.heappush(que,A[i]*-1)

    SUM -= heapq.heappop(que)

    latterDic[i-1] = SUM*-1

ans = -1*float("INF")

for k,v in list(firstDic.items()):

    ans = max(ans,v-latterDic[k])



print(ans)
N = int(eval(input()))

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

B = []

for i in range(3*N-1,-1,-1):

    B.append(A[i]*-1)

"""

前半をできるだけおおきく、後半をできるだけ小さくする。

前半はpriority queにぶっこんでいって、最小値を除去していく

"""

import heapq

frontSum = sum(A[:N])

frontQue = A[:N]

heapq.heapify(frontQue)

front = [frontSum]

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

    frontSum += A[i]

    heapq.heappush(frontQue,A[i])

    frontSum -= heapq.heappop(frontQue)

    front.append(frontSum)



backSum = sum(B[:N])

backQue = B[:N]

heapq.heapify(backQue)

back = [backSum*-1]

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

    backSum += B[i]

    heapq.heappush(backQue,B[i])

    backSum -= heapq.heappop(backQue)

    back.append(backSum*-1)

back = back[::-1]



candi = []

for i in range(len(front)):

    candi.append(front[i]-back[i])

print((max(candi)))

import heapq

N = int(eval(input()))
A = list(map(int, input().split()))
# 前半部分の最適化辞書作成
SUM = sum(A[:N])
firstDic = {N - 1: SUM}
que = A[:N]
heapq.heapify(que)
for i in range(N, 2 * N):
    SUM += A[i]
    heapq.heappush(que, A[i])
    SUM -= heapq.heappop(que)
    firstDic[i] = SUM
# 後半部分の最適化辞書作成
SUM = sum(A[2 * N :]) * -1
latterDic = {2 * N - 1: SUM * -1}
que = [A[i] * -1 for i in range(2 * N, 3 * N)]
heapq.heapify(que)
for i in range(2 * N - 1, N - 1, -1):
    SUM += A[i] * -1
    heapq.heappush(que, A[i] * -1)
    SUM -= heapq.heappop(que)
    latterDic[i - 1] = SUM * -1
ans = -1 * float("INF")
for k, v in list(firstDic.items()):
    ans = max(ans, v - latterDic[k])
print(ans)

N = int(eval(input()))
A = list(map(int, input().split()))
B = []
for i in range(3 * N - 1, -1, -1):
    B.append(A[i] * -1)
"""
前半をできるだけおおきく、後半をできるだけ小さくする。
前半はpriority queにぶっこんでいって、最小値を除去していく
"""
import heapq

frontSum = sum(A[:N])
frontQue = A[:N]
heapq.heapify(frontQue)
front = [frontSum]
for i in range(N, 2 * N):
    frontSum += A[i]
    heapq.heappush(frontQue, A[i])
    frontSum -= heapq.heappop(frontQue)
    front.append(frontSum)
backSum = sum(B[:N])
backQue = B[:N]
heapq.heapify(backQue)
back = [backSum * -1]
for i in range(N, 2 * N):
    backSum += B[i]
    heapq.heappush(backQue, B[i])
    backSum -= heapq.heappop(backQue)
    back.append(backSum * -1)
back = back[::-1]
candi = []
for i in range(len(front)):
    candi.append(front[i] - back[i])
print((max(candi)))

+N = int(eval(input())) +A = list(map(int, input().split())) +B = [] +for i in range(3 * N - 1, -1, -1): +    B.append(A[i] * -1) +""" +前半をできるだけおおきく、後半をできるだけ小さくする。 +前半はpriority queにぶっこんでいって、最小値を除去していく +""" -N = int(eval(input())) -A = list(map(int, input().split())) -# 前半部分の最適化辞書作成 -SUM = sum(A[:N]) -firstDic = {N - 1: SUM} -que = A[:N] -heapq.heapify(que) +frontSum = sum(A[:N]) +frontQue = A[:N] +heapq.heapify(frontQue) +front = [frontSum] -    SUM += A[i] -    heapq.heappush(que, A[i]) -    SUM -= heapq.heappop(que) -    firstDic[i] = SUM -# 後半部分の最適化辞書作成 -SUM = sum(A[2 * N :]) * -1 -latterDic = {2 * N - 1: SUM * -1} -que = [A[i] * -1 for i in range(2 * N, 3 * N)] -heapq.heapify(que) -for i in range(2 * N - 1, N - 1, -1): -    SUM += A[i] * -1 -    heapq.heappush(que, A[i] * -1) -    SUM -= heapq.heappop(que) -    latterDic[i - 1] = SUM * -1 -ans = -1 * float("INF") -for k, v in list(firstDic.items()): -    ans = max(ans, v - latterDic[k]) -print(ans) +    frontSum += A[i] +    heapq.heappush(frontQue, A[i]) +    frontSum -= heapq.heappop(frontQue) +    front.append(frontSum) +backSum = sum(B[:N]) +backQue = B[:N] +heapq.heapify(backQue) +back = [backSum * -1] +for i in range(N, 2 * N): +    backSum += B[i] +    heapq.heappush(backQue, B[i]) +    backSum -= heapq.heappop(backQue) +    back.append(backSum * -1) +back = back[::-1] +candi = [] +for i in range(len(front)): +    candi.append(front[i] - back[i]) +print((max(candi)))