from heapq import heapify, heappop, heappush

from collections import deque



n = int(eval(input()))

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



L = a[:n]

sum_l = [sum(L)]

heapify(L)



ML = a[n:2 * n]

ML = deque(ML)



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

sum_r = [sum(R)]  # あとでReverseする

heapify(R)



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

MR = deque(MR)



for i in range(n):

    add_to_l = ML.popleft()

    heappush(L, add_to_l)

    subtract_to_l = heappop(L)



    t = sum_l[-1] + add_to_l - subtract_to_l

    sum_l.append(t)



# print(sum_l)



for i in range(n):

    add_to_r = MR.pop()

    heappush(R, add_to_r)

    subtract_to_r = heappop(R)



    t = sum_r[-1] + add_to_r - subtract_to_r

    sum_r.append(t)



# print(sum_r)



sum_r = sum_r[::-1]



ans = sum_l[0] + sum_r[0]

for i in range(1, n + 1):

    t = sum_l[i] + sum_r[i]

    ans = max(ans, t)



print(ans)
def main():

    from heapq import heappush, heappushpop



    n = int(eval(input()))

    *a, = list(map(int, input().split()))  # [0,n),[n,2n),[2n,3n)



    to_rm = [0] * (n * 3)



    h = []

    for x in a[:n]:

        heappush(h, x)



    mid = tuple(enumerate(a[n:n * 2], n))



    t = 0

    for i, x in mid:

        mi = heappushpop(h, x)

        t += mi

        to_rm[i] = -t



    h = []

    for x in a[n * 2:n * 3]:

        heappush(h, -x)



    t = 0

    for i, x in reversed(mid):

        ma = -heappushpop(h, -x)

        t += ma

        to_rm[i - 1] += t



    tot = sum(a)

    ret = -10 ** 30

    s = sum(a[:n - 1])

    for tail in range(n - 1, n * 2):

        s += a[tail]

        ret = max(ret, s - (tot - s) + to_rm[tail])  # [0,tail]=前半

    print(ret)





if __name__ == '__main__':

    main()

from heapq import heapify, heappop, heappush
from collections import deque

n = int(eval(input()))
a = list(map(int, input().split()))
L = a[:n]
sum_l = [sum(L)]
heapify(L)
ML = a[n : 2 * n]
ML = deque(ML)
R = list([(-1) * x for x in a[2 * n :]])
sum_r = [sum(R)]  # あとでReverseする
heapify(R)
MR = list([(-1) * x for x in a[n : 2 * n]])
MR = deque(MR)
for i in range(n):
    add_to_l = ML.popleft()
    heappush(L, add_to_l)
    subtract_to_l = heappop(L)
    t = sum_l[-1] + add_to_l - subtract_to_l
    sum_l.append(t)
# print(sum_l)
for i in range(n):
    add_to_r = MR.pop()
    heappush(R, add_to_r)
    subtract_to_r = heappop(R)
    t = sum_r[-1] + add_to_r - subtract_to_r
    sum_r.append(t)
# print(sum_r)
sum_r = sum_r[::-1]
ans = sum_l[0] + sum_r[0]
for i in range(1, n + 1):
    t = sum_l[i] + sum_r[i]
    ans = max(ans, t)
print(ans)

def main():
    from heapq import heappush, heappushpop

    n = int(eval(input()))
    (*a,) = list(map(int, input().split()))  # [0,n),[n,2n),[2n,3n)
    to_rm = [0] * (n * 3)
    h = []
    for x in a[:n]:
        heappush(h, x)
    mid = tuple(enumerate(a[n : n * 2], n))
    t = 0
    for i, x in mid:
        mi = heappushpop(h, x)
        t += mi
        to_rm[i] = -t
    h = []
    for x in a[n * 2 : n * 3]:
        heappush(h, -x)
    t = 0
    for i, x in reversed(mid):
        ma = -heappushpop(h, -x)
        t += ma
        to_rm[i - 1] += t
    tot = sum(a)
    ret = -(10**30)
    s = sum(a[: n - 1])
    for tail in range(n - 1, n * 2):
        s += a[tail]
        ret = max(ret, s - (tot - s) + to_rm[tail])  # [0,tail]=前半
    print(ret)


if __name__ == "__main__":
    main()

-from heapq import heapify, heappop, heappush -from collections import deque +def main(): +    from heapq import heappush, heappushpop -n = int(eval(input())) -a = list(map(int, input().split())) -L = a[:n] -sum_l = [sum(L)] -heapify(L) -ML = a[n : 2 * n] -ML = deque(ML) -R = list([(-1) * x for x in a[2 * n :]]) -sum_r = [sum(R)]  # あとでReverseする -heapify(R) -MR = list([(-1) * x for x in a[n : 2 * n]]) -MR = deque(MR) -for i in range(n): -    add_to_l = ML.popleft() -    heappush(L, add_to_l) -    subtract_to_l = heappop(L) -    t = sum_l[-1] + add_to_l - subtract_to_l -    sum_l.append(t) -# print(sum_l) -for i in range(n): -    add_to_r = MR.pop() -    heappush(R, add_to_r) -    subtract_to_r = heappop(R) -    t = sum_r[-1] + add_to_r - subtract_to_r -    sum_r.append(t) -# print(sum_r) -sum_r = sum_r[::-1] -ans = sum_l[0] + sum_r[0] -for i in range(1, n + 1): -    t = sum_l[i] + sum_r[i] -    ans = max(ans, t) -print(ans) +    n = int(eval(input())) +    (*a,) = list(map(int, input().split()))  # [0,n),[n,2n),[2n,3n) +    to_rm = [0] * (n * 3) +    h = [] +    for x in a[:n]: +        heappush(h, x) +    mid = tuple(enumerate(a[n : n * 2], n)) +    t = 0 +    for i, x in mid: +        mi = heappushpop(h, x) +        t += mi +        to_rm[i] = -t +    h = [] +    for x in a[n * 2 : n * 3]: +        heappush(h, -x) +    t = 0 +    for i, x in reversed(mid): +        ma = -heappushpop(h, -x) +        t += ma +        to_rm[i - 1] += t +    tot = sum(a) +    ret = -(10**30) +    s = sum(a[: n - 1]) +    for tail in range(n - 1, n * 2): +        s += a[tail] +        ret = max(ret, s - (tot - s) + to_rm[tail])  # [0,tail]=前半 +    print(ret) + + +if __name__ == "__main__": +    main()