# ABC140E

class SegTree:

    def __init__(self, init_val, ide_ele, seg_func):

        self.segfunc = seg_func

        N = len(init_val)

        self.num = 2**(N-1).bit_length()

        self.ide_ele = ide_ele

        self.seg=[self.ide_ele]*2*self.num

        for i in range(N):

            self.seg[i+self.num-1]=init_val[i]    

        for i in range(self.num-2,-1,-1) :

            self.seg[i]=self.segfunc(self.seg[2*i+1],self.seg[2*i+2]) 

        

    def update(self, k, x):

        k += self.num-1

        self.seg[k] = x

        while k+1:

            k = (k-1)//2

            self.seg[k] = self.segfunc(self.seg[k*2+1],self.seg[k*2+2])

        

    def query(self, p, q):

        if q<=p:

            return self.ide_ele

        p += self.num-1

        q += self.num-2

        res=self.ide_ele

        while q-p>1:

            if p&1 == 0:

                res = self.segfunc(res,self.seg[p])

            if q&1 == 1:

                res = self.segfunc(res,self.seg[q])

                q -= 1

            p = p//2

            q = (q-1)//2

        if p == q:

            res = self.segfunc(res,self.seg[p])

        else:

            res = self.segfunc(self.segfunc(res,self.seg[p]),self.seg[q])

        return res

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

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

B = sorted([(a, i) for i, a in enumerate(A)], key=lambda x:x[0])

z = dict()

for i, (b, j) in enumerate(B):

    z[(b, j)] = i



ss = SegTree([3*N]*3*N, 3*N, min)

bs = []

y = 0

for i, x in enumerate(A):

    if i == 2*N:

        break

    ss.update(z[(x, i)], z[(x, i)])

    y += x

    if i < N-1:

        continue

    if i == N-1:

        bs.append(y)

    else:

        mx = ss.query(0, 3*N)

        y = y - B[mx][0]

        ss.update(mx, 3*N)

        bs.append(y)



ss = SegTree([-1]*3*N, -1, max)

nbs = []

#tmp = set()

y = 0

for i, x in enumerate(A[::-1]):

    if i == 2*N:

        break

    ss.update(z[(x, 3*N-i-1)], z[(x, 3*N-i-1)])

#    tmp.add((x, i))

    y += x

    if i < N-1:

        continue

    if i == N-1:

        nbs.append(y)

    else:

        mx = ss.query(0, 3*N)

#        print(max(tmp), B[mx])

#        tmp.remove(max(tmp))

        y = y - B[mx][0]

        ss.update(mx, -1)

        nbs.append(y)



#print(B)

#print(bs)

#print(nbs)

r = -10**18

for i in range(N+1):

    r = max(r, bs[i] - nbs[N-i])

print(r)

from heapq import *

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

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

s = sum(A[:N])

rs = [s] + [0] * N

l = []

for i in range(N):

    heappush(l, A[i])

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

    s += A[i]

    heappush(l, A[i])

    x = heappop(l)

    s -= x

    rs[i-N+1] = s

l2 = []

s2 = 0

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

    heappush(l2, -A[i])

    s2+=A[i]



rs2 = [0] * N + [s2]

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

    s2 += A[i]

    heappush(l2, -A[i])

    x = -heappop(l2)

#    print(A[i])

#    print(x)

    s2 -= x

    rs2[i-N] = s2

#print(rs2)

r = -10**18

for i in range(N+1):

    c = rs[i] - rs2[i]

    r = max(c, r)

print(r)

# ABC140E
class SegTree:
    def __init__(self, init_val, ide_ele, seg_func):
        self.segfunc = seg_func
        N = len(init_val)
        self.num = 2 ** (N - 1).bit_length()
        self.ide_ele = ide_ele
        self.seg = [self.ide_ele] * 2 * self.num
        for i in range(N):
            self.seg[i + self.num - 1] = init_val[i]
        for i in range(self.num - 2, -1, -1):
            self.seg[i] = self.segfunc(self.seg[2 * i + 1], self.seg[2 * i + 2])

    def update(self, k, x):
        k += self.num - 1
        self.seg[k] = x
        while k + 1:
            k = (k - 1) // 2
            self.seg[k] = self.segfunc(self.seg[k * 2 + 1], self.seg[k * 2 + 2])

    def query(self, p, q):
        if q <= p:
            return self.ide_ele
        p += self.num - 1
        q += self.num - 2
        res = self.ide_ele
        while q - p > 1:
            if p & 1 == 0:
                res = self.segfunc(res, self.seg[p])
            if q & 1 == 1:
                res = self.segfunc(res, self.seg[q])
                q -= 1
            p = p // 2
            q = (q - 1) // 2
        if p == q:
            res = self.segfunc(res, self.seg[p])
        else:
            res = self.segfunc(self.segfunc(res, self.seg[p]), self.seg[q])
        return res


(N,) = list(map(int, input().split()))
A = list(map(int, input().split()))
B = sorted([(a, i) for i, a in enumerate(A)], key=lambda x: x[0])
z = dict()
for i, (b, j) in enumerate(B):
    z[(b, j)] = i
ss = SegTree([3 * N] * 3 * N, 3 * N, min)
bs = []
y = 0
for i, x in enumerate(A):
    if i == 2 * N:
        break
    ss.update(z[(x, i)], z[(x, i)])
    y += x
    if i < N - 1:
        continue
    if i == N - 1:
        bs.append(y)
    else:
        mx = ss.query(0, 3 * N)
        y = y - B[mx][0]
        ss.update(mx, 3 * N)
        bs.append(y)
ss = SegTree([-1] * 3 * N, -1, max)
nbs = []
# tmp = set()
y = 0
for i, x in enumerate(A[::-1]):
    if i == 2 * N:
        break
    ss.update(z[(x, 3 * N - i - 1)], z[(x, 3 * N - i - 1)])
    #    tmp.add((x, i))
    y += x
    if i < N - 1:
        continue
    if i == N - 1:
        nbs.append(y)
    else:
        mx = ss.query(0, 3 * N)
        #        print(max(tmp), B[mx])
        #        tmp.remove(max(tmp))
        y = y - B[mx][0]
        ss.update(mx, -1)
        nbs.append(y)
# print(B)
# print(bs)
# print(nbs)
r = -(10**18)
for i in range(N + 1):
    r = max(r, bs[i] - nbs[N - i])
print(r)

from heapq import *

(N,) = list(map(int, input().split()))
A = list(map(int, input().split()))
s = sum(A[:N])
rs = [s] + [0] * N
l = []
for i in range(N):
    heappush(l, A[i])
for i in range(N, 2 * N):
    s += A[i]
    heappush(l, A[i])
    x = heappop(l)
    s -= x
    rs[i - N + 1] = s
l2 = []
s2 = 0
for i in range(2 * N, 3 * N):
    heappush(l2, -A[i])
    s2 += A[i]
rs2 = [0] * N + [s2]
for i in range(2 * N - 1, N - 1, -1):
    s2 += A[i]
    heappush(l2, -A[i])
    x = -heappop(l2)
    #    print(A[i])
    #    print(x)
    s2 -= x
    rs2[i - N] = s2
# print(rs2)
r = -(10**18)
for i in range(N + 1):
    c = rs[i] - rs2[i]
    r = max(c, r)
print(r)

-# ABC140E -class SegTree: -    def __init__(self, init_val, ide_ele, seg_func): -        self.segfunc = seg_func -        N = len(init_val) -        self.num = 2 ** (N - 1).bit_length() -        self.ide_ele = ide_ele -        self.seg = [self.ide_ele] * 2 * self.num -        for i in range(N): -            self.seg[i + self.num - 1] = init_val[i] -        for i in range(self.num - 2, -1, -1): -            self.seg[i] = self.segfunc(self.seg[2 * i + 1], self.seg[2 * i + 2]) - -    def update(self, k, x): -        k += self.num - 1 -        self.seg[k] = x -        while k + 1: -            k = (k - 1) // 2 -            self.seg[k] = self.segfunc(self.seg[k * 2 + 1], self.seg[k * 2 + 2]) - -    def query(self, p, q): -        if q <= p: -            return self.ide_ele -        p += self.num - 1 -        q += self.num - 2 -        res = self.ide_ele -        while q - p > 1: -            if p & 1 == 0: -                res = self.segfunc(res, self.seg[p]) -            if q & 1 == 1: -                res = self.segfunc(res, self.seg[q]) -                q -= 1 -            p = p // 2 -            q = (q - 1) // 2 -        if p == q: -            res = self.segfunc(res, self.seg[p]) -        else: -            res = self.segfunc(self.segfunc(res, self.seg[p]), self.seg[q]) -        return res - +from heapq import * -B = sorted([(a, i) for i, a in enumerate(A)], key=lambda x: x[0]) -z = dict() -for i, (b, j) in enumerate(B): -    z[(b, j)] = i -ss = SegTree([3 * N] * 3 * N, 3 * N, min) -bs = [] -y = 0 -for i, x in enumerate(A): -    if i == 2 * N: -        break -    ss.update(z[(x, i)], z[(x, i)]) -    y += x -    if i < N - 1: -        continue -    if i == N - 1: -        bs.append(y) -    else: -        mx = ss.query(0, 3 * N) -        y = y - B[mx][0] -        ss.update(mx, 3 * N) -        bs.append(y) -ss = SegTree([-1] * 3 * N, -1, max) -nbs = [] -# tmp = set() -y = 0 -for i, x in enumerate(A[::-1]): -    if i == 2 * N: -        break -    ss.update(z[(x, 3 * N - i - 1)], z[(x, 3 * N - i - 1)]) -    #    tmp.add((x, i)) -    y += x -    if i < N - 1: -        continue -    if i == N - 1: -        nbs.append(y) -    else: -        mx = ss.query(0, 3 * N) -        #        print(max(tmp), B[mx]) -        #        tmp.remove(max(tmp)) -        y = y - B[mx][0] -        ss.update(mx, -1) -        nbs.append(y) -# print(B) -# print(bs) -# print(nbs) +s = sum(A[:N]) +rs = [s] + [0] * N +l = [] +for i in range(N): +    heappush(l, A[i]) +for i in range(N, 2 * N): +    s += A[i] +    heappush(l, A[i]) +    x = heappop(l) +    s -= x +    rs[i - N + 1] = s +l2 = [] +s2 = 0 +for i in range(2 * N, 3 * N): +    heappush(l2, -A[i]) +    s2 += A[i] +rs2 = [0] * N + [s2] +for i in range(2 * N - 1, N - 1, -1): +    s2 += A[i] +    heappush(l2, -A[i]) +    x = -heappop(l2) +    #    print(A[i]) +    #    print(x) +    s2 -= x +    rs2[i - N] = s2 +# print(rs2) -    r = max(r, bs[i] - nbs[N - i]) +    c = rs[i] - rs2[i] +    r = max(c, r)