n = int(eval(input()))

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



b = list(enumerate(a))



l = sorted(b[:2*n], key=lambda x: x[1], reverse=True)

r = sorted(b[n:], key=lambda x: x[1])



big = [0] * (3*n)

small = [0] * (3*n)



for i, (pos, z) in enumerate(l):

    big[pos] = i

for i, (pos, z) in enumerate(r):

    small[pos] = i



first = sum(a[:n])

second = 0

for pos, z in r[:n]:

    second += z

i = 2 * n

j = n-1

ans = first - second

for split in range(n, 2*n):

    if big[split] < i:

        first += a[split]

        i -= 1

        while l[i][0] > split:

            i -= 1

        first -= l[i][1]

    if small[split] <= j:

        second -= a[split]

        j += 1

        while r[j][0] < split:

            j += 1

        second += r[j][1]

    ans = max(ans, first - second)

print(ans)
import heapq

n = int(eval(input()))

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



h = a[:n]

heapq.heapify(h)

first = [0] * (n+1)

s = sum(a[:n])

first[0] = s

for i in range(n):

    s += a[n+i]

    heapq.heappush(h, a[n+i])

    s -= heapq.heappop(h)

    first[i+1] = s



h = list([-x for x in a[2*n:]])

heapq.heapify(h)

second = [0] * (n+1)

s = - sum(a[2*n:])

second[0] = s

for i in range(n):

    s -= a[2*n - i - 1]

    heapq.heappush(h, -a[2*n - i - 1])

    s -= heapq.heappop(h)

    second[i+1] = s



ans = - 10 ** 18

for i in range(n+1):

    ans = max(ans, first[i] + second[n-i])

print(ans)
n = int(eval(input()))
a = list(map(int, input().split()))
b = list(enumerate(a))
l = sorted(b[: 2 * n], key=lambda x: x[1], reverse=True)
r = sorted(b[n:], key=lambda x: x[1])
big = [0] * (3 * n)
small = [0] * (3 * n)
for i, (pos, z) in enumerate(l):
    big[pos] = i
for i, (pos, z) in enumerate(r):
    small[pos] = i
first = sum(a[:n])
second = 0
for pos, z in r[:n]:
    second += z
i = 2 * n
j = n - 1
ans = first - second
for split in range(n, 2 * n):
    if big[split] < i:
        first += a[split]
        i -= 1
        while l[i][0] > split:
            i -= 1
        first -= l[i][1]
    if small[split] <= j:
        second -= a[split]
        j += 1
        while r[j][0] < split:
            j += 1
        second += r[j][1]
    ans = max(ans, first - second)
print(ans)

import heapq

n = int(eval(input()))
a = list(map(int, input().split()))
h = a[:n]
heapq.heapify(h)
first = [0] * (n + 1)
s = sum(a[:n])
first[0] = s
for i in range(n):
    s += a[n + i]
    heapq.heappush(h, a[n + i])
    s -= heapq.heappop(h)
    first[i + 1] = s
h = list([-x for x in a[2 * n :]])
heapq.heapify(h)
second = [0] * (n + 1)
s = -sum(a[2 * n :])
second[0] = s
for i in range(n):
    s -= a[2 * n - i - 1]
    heapq.heappush(h, -a[2 * n - i - 1])
    s -= heapq.heappop(h)
    second[i + 1] = s
ans = -(10**18)
for i in range(n + 1):
    ans = max(ans, first[i] + second[n - i])
print(ans)

+import heapq + -b = list(enumerate(a)) -l = sorted(b[: 2 * n], key=lambda x: x[1], reverse=True) -r = sorted(b[n:], key=lambda x: x[1]) -big = [0] * (3 * n) -small = [0] * (3 * n) -for i, (pos, z) in enumerate(l): -    big[pos] = i -for i, (pos, z) in enumerate(r): -    small[pos] = i -first = sum(a[:n]) -second = 0 -for pos, z in r[:n]: -    second += z -i = 2 * n -j = n - 1 -ans = first - second -for split in range(n, 2 * n): -    if big[split] < i: -        first += a[split] -        i -= 1 -        while l[i][0] > split: -            i -= 1 -        first -= l[i][1] -    if small[split] <= j: -        second -= a[split] -        j += 1 -        while r[j][0] < split: -            j += 1 -        second += r[j][1] -    ans = max(ans, first - second) +h = a[:n] +heapq.heapify(h) +first = [0] * (n + 1) +s = sum(a[:n]) +first[0] = s +for i in range(n): +    s += a[n + i] +    heapq.heappush(h, a[n + i]) +    s -= heapq.heappop(h) +    first[i + 1] = s +h = list([-x for x in a[2 * n :]]) +heapq.heapify(h) +second = [0] * (n + 1) +s = -sum(a[2 * n :]) +second[0] = s +for i in range(n): +    s -= a[2 * n - i - 1] +    heapq.heappush(h, -a[2 * n - i - 1]) +    s -= heapq.heappop(h) +    second[i + 1] = s +ans = -(10**18) +for i in range(n + 1): +    ans = max(ans, first[i] + second[n - i])