import sys

import heapq

input = sys.stdin.buffer.readline

def minus(x):

  return -x

N = int(eval(input()))

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

dpl = [0] * (N + 1)

dpr = [0] * (N + 1)

l = a[: N]

m = a[N: 2 * N]

r = list(map(minus, a[2 * N: ]))

heapq.heapify(l)

heapq.heapify(r)

dpl[0] = sum(l)

dpr[0] = sum(r)

for i in range(N):

  heapq.heappush(l, m[i])

  dpl[i + 1] = dpl[i] - heapq.heappop(l) + m[i]

  heapq.heappush(r, -m[-1 - i])

  dpr[i + 1] = dpr[i] - heapq.heappop(r) - m[-1 - i]

res = -float("inf")

for i in range(N + 1):

  j = N - i

  res = max(res, dpl[i] + dpr[j])

print(res)
import sys

import heapq

hpush = heapq.heappush

hpop = heapq.heappop

input = sys.stdin.readline

N = int(eval(input()))

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

h = []

ltable = [0] * (N + 1)

for i in range(N):

  ltable[0] += a[i]

  hpush(h, a[i])

for i in range(N):

  hpush(h, a[N + i])

  ltable[i + 1] = ltable[i] + a[N + i] - hpop(h)



a.reverse()

h = []

rtable = [0] * (N + 1)

for i in range(N):

  rtable[0] += a[i]

  hpush(h, -a[i])

for i in range(N):

  hpush(h, -a[N + i])

  rtable[i + 1] = rtable[i] + a[N + i] + hpop(h)

#print(ltable, rtable)

res = -10 ** 15

for i in range(N + 1): res = max(res, ltable[i] - rtable[N - i])

print(res)
import sys
import heapq

input = sys.stdin.buffer.readline


def minus(x):
    return -x


N = int(eval(input()))
a = list(map(int, input().split()))
dpl = [0] * (N + 1)
dpr = [0] * (N + 1)
l = a[:N]
m = a[N : 2 * N]
r = list(map(minus, a[2 * N :]))
heapq.heapify(l)
heapq.heapify(r)
dpl[0] = sum(l)
dpr[0] = sum(r)
for i in range(N):
    heapq.heappush(l, m[i])
    dpl[i + 1] = dpl[i] - heapq.heappop(l) + m[i]
    heapq.heappush(r, -m[-1 - i])
    dpr[i + 1] = dpr[i] - heapq.heappop(r) - m[-1 - i]
res = -float("inf")
for i in range(N + 1):
    j = N - i
    res = max(res, dpl[i] + dpr[j])
print(res)

import sys
import heapq

hpush = heapq.heappush
hpop = heapq.heappop
input = sys.stdin.readline
N = int(eval(input()))
a = list(map(int, input().split()))
h = []
ltable = [0] * (N + 1)
for i in range(N):
    ltable[0] += a[i]
    hpush(h, a[i])
for i in range(N):
    hpush(h, a[N + i])
    ltable[i + 1] = ltable[i] + a[N + i] - hpop(h)
a.reverse()
h = []
rtable = [0] * (N + 1)
for i in range(N):
    rtable[0] += a[i]
    hpush(h, -a[i])
for i in range(N):
    hpush(h, -a[N + i])
    rtable[i + 1] = rtable[i] + a[N + i] + hpop(h)
# print(ltable, rtable)
res = -(10**15)
for i in range(N + 1):
    res = max(res, ltable[i] - rtable[N - i])
print(res)

-input = sys.stdin.buffer.readline - - -def minus(x): -    return -x - - +hpush = heapq.heappush +hpop = heapq.heappop +input = sys.stdin.readline -dpl = [0] * (N + 1) -dpr = [0] * (N + 1) -l = a[:N] -m = a[N : 2 * N] -r = list(map(minus, a[2 * N :])) -heapq.heapify(l) -heapq.heapify(r) -dpl[0] = sum(l) -dpr[0] = sum(r) +h = [] +ltable = [0] * (N + 1) -    heapq.heappush(l, m[i]) -    dpl[i + 1] = dpl[i] - heapq.heappop(l) + m[i] -    heapq.heappush(r, -m[-1 - i]) -    dpr[i + 1] = dpr[i] - heapq.heappop(r) - m[-1 - i] -res = -float("inf") +    ltable[0] += a[i] +    hpush(h, a[i]) +for i in range(N): +    hpush(h, a[N + i]) +    ltable[i + 1] = ltable[i] + a[N + i] - hpop(h) +a.reverse() +h = [] +rtable = [0] * (N + 1) +for i in range(N): +    rtable[0] += a[i] +    hpush(h, -a[i]) +for i in range(N): +    hpush(h, -a[N + i]) +    rtable[i + 1] = rtable[i] + a[N + i] + hpop(h) +# print(ltable, rtable) +res = -(10**15) -    j = N - i -    res = max(res, dpl[i] + dpr[j]) +    res = max(res, ltable[i] - rtable[N - i])