Problem:
Score : 500 points

Problem Statement

Let N be a positive integer.

There is a numerical sequence of length 3N, a = (a_1, a_2, ..., a_{3N}).
Snuke is constructing a new sequence of length 2N, a', by removing exactly N elements from a without changing the order of the remaining elements.
Here, the score of a' is defined as follows: (the sum of the elements in the first half of a') - (the sum of the elements in the second half of a').

Find the maximum possible score of a'.

Constraints

1 ≤ N ≤ 10^5

a_i is an integer.

1 ≤ a_i ≤ 10^9

Partial Score

In the test set worth 300 points, N ≤ 1000.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_{3N}

Output

Print the maximum possible score of a'.

Sample Input 1

2
3 1 4 1 5 9

Sample Output 1

1

When a_2 and a_6 are removed, a' will be (3, 4, 1, 5), which has a score of (3 + 4) - (1 + 5) = 1.

Sample Input 2

1
1 2 3

Sample Output 2

-1

For example, when a_1 are removed, a' will be (2, 3), which has a score of 2 - 3 = -1.

Sample Input 3

3
8 2 2 7 4 6 5 3 8

Sample Output 3

5

For example, when a_2, a_3 and a_9 are removed, a' will be (8, 7, 4, 6, 5, 3), which has a score of (8 + 7 + 4) - (6 + 5 + 3) = 5.

Solution in Pascal (FPC 2.6.2) (judge verdict: Accepted)

const
        fi='arc074_b.inp';
        fo='arc074_b.out';
var
        res:int64;
        n,nheap:longint;
        P1,P2:Array[0..300001] of int64;
        A:Array[0..300001] of longint;
        Heap:Array[0..100001] of longint;
{------------------------------------}
procedure swap(var xx1,xx2:longint);
var tg:longint;
        begin
                tg:=xx1;
                xx1:=xx2;
                xx2:=tg;
        end;
{------------------------------------}
procedure upheapmin(k:longint);
        begin
                if (k=1) or (Heap[k]>=Heap[k div 2]) then exit
                else
                        begin
                                swap(Heap[k],Heap[k div 2]);
                                upheapmin(k div 2);
                        end;
        end;
{------------------------------------}
procedure downheapmin(k:longint);
var vtk:longint;
        begin
                if (2*k<=nheap) then
                begin
                        if (2*k+1<=nheap) and (Heap[2*k+1]<Heap[2*k]) then vtk:=2*k+1 else vtk:=2*k;
                        if Heap[k]>Heap[vtk] then
                        begin
                                swap(Heap[k],Heap[vtk]);
                                downheapmin(vtk);
                        end;
                end;
        end;
{------------------------------------}
procedure pushmin(pt:longint);
        begin
                inc(nheap);
                Heap[nheap]:=pt;
                upheapmin(nheap);
        end;
{------------------------------------}
procedure upheapmax(k:longint);
        begin
                if (k=1) or (Heap[k]<=Heap[k div 2]) then exit
                else
                        begin
                                swap(Heap[k],Heap[k div 2]);
                                upheapmax(k div 2);
                        end;
        end;
{------------------------------------}
procedure downheapmax(k:longint);
var vtk:longint;
        begin
                if (2*k<=nheap) then
                begin
                        if (2*k+1<=nheap) and (Heap[2*k+1]>Heap[2*k]) then vtk:=2*k+1 else vtk:=2*k;
                        if Heap[k]<Heap[vtk] then
                        begin
                                swap(Heap[k],Heap[vtk]);
                                downheapmax(vtk);
                        end;
                end;
        end;
{------------------------------------}
procedure pushmax(pt:longint);
        begin
                inc(nheap);
                Heap[nheap]:=pt;
                upheapmax(nheap);
        end;
{------------------------------------}
procedure main;
var o:longint;
        begin
                read(n);
                for o:=1 to 3*n do read(A[o]);
                nheap:=0;
                for o:=1 to n do
                begin
                        pushmin(A[o]);
                        inc(P1[n],A[o]);
                end;
                for o:=n+1 to 2*n do
                begin
                        P1[o]:=P1[o-1];
                        if A[o]>Heap[1] then
                        begin
                                P1[o]:=P1[o]-Heap[1]+A[o];
                                Heap[1]:=A[o];
                                downheapmin(1);
                        end;
                end;
                nheap:=0;
                for o:=3*n downto 2*n+1 do
                begin
                        pushmax(A[o]);
                        inc(P2[2*n+1],A[o]);
                end;
                for o:=2*n downto n+1 do
                begin
                        P2[o]:=P2[o+1];
                        if A[o]<Heap[1] then
                        begin
                                P2[o]:=P2[o]-Heap[1]+A[o];
                                Heap[1]:=A[o];
                                downheapmax(1);
                        end;
                end;
                res:=-1000000000000000;
                for o:=n to 2*n do if res<P1[o]-P2[o+1] then res:=P1[o]-P2[o+1];
                write(res);
        end;
{------------------------------------}
begin
//        assign(input,fi); reset(input);
  //      assign(output,fo);rewrite(output);
        main;
end.