import heapq



def solve():

  n = int(input())

  ar = input().split(' ')

  iar = [int(s) for s in ar]

  

  early = iar[:n]

  mid = iar[n:-n]

  late = iar[-n:]



  heapq.heapify(early)

  tmpsum = sum(early)

  esums = [tmpsum]

  for i in range(n):

    heapq.heappush(early, mid[i])

    emin = heapq.heappop(early)

    tmpsum += (mid[i] - emin)

    esums.append(tmpsum)

  #print esums

 

  lateinv = [-x for x in late]

  heapq.heapify(lateinv)

  tmpsum = sum(lateinv)

  lsums = [tmpsum]

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

    heapq.heappush(lateinv, -mid[-i])

    lmax = heapq.heappop(lateinv)

    tmpsum += (-mid[-i] - lmax)

    lsums.append(tmpsum)

  #print lsums

  

  best = float("-inf")

  for i in range(n+1):

    score = esums[i] + lsums[n-i]

    #print 'i=',i,',n-i=',n-i,',esums[i]=',esums[i],',lsums[n-i]=',lsums[n-i],'score=',score

    if(best < score) : best = score

  return best



if __name__ == '__main__' :

  print(solve())
import heapq

N = int(eval(input()))

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



lsum = sum(src[:N])

lmem = [lsum]

lq = src[:N][:]

heapq.heapify(lq)

for a in src[N:2*N]:

    if a <= lq[0]:

        lmem.append(lmem[-1])

    else:

        lsum += a - heapq.heappop(lq)

        heapq.heappush(lq, a)

        lmem.append(lsum)



rq = [-a for a in src[-N:]]

rsum = sum(rq)

rmem = [rsum]

heapq.heapify(rq)

for a in reversed(src[N:2*N]):

    if -a <= rq[0]:

        rmem.append(rmem[-1])

    else:

        rsum += -a - heapq.heappop(rq)

        heapq.heappush(rq, -a)

        rmem.append(rsum)



ans = -float('inf')

for l,r in zip(lmem, rmem[::-1]):

    ans = max(ans, l+r)

print(ans)
import heapq


def solve():
    n = int(input())
    ar = input().split(" ")
    iar = [int(s) for s in ar]
    early = iar[:n]
    mid = iar[n:-n]
    late = iar[-n:]
    heapq.heapify(early)
    tmpsum = sum(early)
    esums = [tmpsum]
    for i in range(n):
        heapq.heappush(early, mid[i])
        emin = heapq.heappop(early)
        tmpsum += mid[i] - emin
        esums.append(tmpsum)
    # print esums
    lateinv = [-x for x in late]
    heapq.heapify(lateinv)
    tmpsum = sum(lateinv)
    lsums = [tmpsum]
    for i in range(1, n + 1):
        heapq.heappush(lateinv, -mid[-i])
        lmax = heapq.heappop(lateinv)
        tmpsum += -mid[-i] - lmax
        lsums.append(tmpsum)
    # print lsums
    best = float("-inf")
    for i in range(n + 1):
        score = esums[i] + lsums[n - i]
        # print 'i=',i,',n-i=',n-i,',esums[i]=',esums[i],',lsums[n-i]=',lsums[n-i],'score=',score
        if best < score:
            best = score
    return best


if __name__ == "__main__":
    print(solve())

import heapq

N = int(eval(input()))
src = list(map(int, input().split()))
lsum = sum(src[:N])
lmem = [lsum]
lq = src[:N][:]
heapq.heapify(lq)
for a in src[N : 2 * N]:
    if a <= lq[0]:
        lmem.append(lmem[-1])
    else:
        lsum += a - heapq.heappop(lq)
        heapq.heappush(lq, a)
        lmem.append(lsum)
rq = [-a for a in src[-N:]]
rsum = sum(rq)
rmem = [rsum]
heapq.heapify(rq)
for a in reversed(src[N : 2 * N]):
    if -a <= rq[0]:
        rmem.append(rmem[-1])
    else:
        rsum += -a - heapq.heappop(rq)
        heapq.heappush(rq, -a)
        rmem.append(rsum)
ans = -float("inf")
for l, r in zip(lmem, rmem[::-1]):
    ans = max(ans, l + r)
print(ans)

- -def solve(): -    n = int(input()) -    ar = input().split(" ") -    iar = [int(s) for s in ar] -    early = iar[:n] -    mid = iar[n:-n] -    late = iar[-n:] -    heapq.heapify(early) -    tmpsum = sum(early) -    esums = [tmpsum] -    for i in range(n): -        heapq.heappush(early, mid[i]) -        emin = heapq.heappop(early) -        tmpsum += mid[i] - emin -        esums.append(tmpsum) -    # print esums -    lateinv = [-x for x in late] -    heapq.heapify(lateinv) -    tmpsum = sum(lateinv) -    lsums = [tmpsum] -    for i in range(1, n + 1): -        heapq.heappush(lateinv, -mid[-i]) -        lmax = heapq.heappop(lateinv) -        tmpsum += -mid[-i] - lmax -        lsums.append(tmpsum) -    # print lsums -    best = float("-inf") -    for i in range(n + 1): -        score = esums[i] + lsums[n - i] -        # print 'i=',i,',n-i=',n-i,',esums[i]=',esums[i],',lsums[n-i]=',lsums[n-i],'score=',score -        if best < score: -            best = score -    return best - - -if __name__ == "__main__": -    print(solve()) +N = int(eval(input())) +src = list(map(int, input().split())) +lsum = sum(src[:N]) +lmem = [lsum] +lq = src[:N][:] +heapq.heapify(lq) +for a in src[N : 2 * N]: +    if a <= lq[0]: +        lmem.append(lmem[-1]) +    else: +        lsum += a - heapq.heappop(lq) +        heapq.heappush(lq, a) +        lmem.append(lsum) +rq = [-a for a in src[-N:]] +rsum = sum(rq) +rmem = [rsum] +heapq.heapify(rq) +for a in reversed(src[N : 2 * N]): +    if -a <= rq[0]: +        rmem.append(rmem[-1]) +    else: +        rsum += -a - heapq.heappop(rq) +        heapq.heappush(rq, -a) +        rmem.append(rsum) +ans = -float("inf") +for l, r in zip(lmem, rmem[::-1]): +    ans = max(ans, l + r) +print(ans)