Language: C++

using System;
using System.Collections.Generic;
using System.Linq;
using static Input;
public class Hello{
    public static void Main(){
        int N = NextInt();
        int[] a = LineInt();
        long[] memo = new long[3*N];
        long Num = 0;
        PriorityQueue<long,long> PQ1 = new PriorityQueue<long,long>();
        for(int i = 0;i < N;i++){
            Num += a[i];
            PQ1.Add(a[i],a[i]);
        }
        memo[N-1] = Num;
        for(int i = N;i < 2*N;i++){
            //追加して最小を吐き出す
            Num += a[i];
            PQ1.Add(a[i],a[i]);
            Num -= PQ1.Take();
            //iまでの取れる最大値をメモする
            memo[i] = Num;
        }
        //逆方向も考える
        Num = 0;
        PriorityQueue<long,long> PQ2 = new PriorityQueue<long,long>(false);
        long ans = 0;
        for(int i = 2*N;i < 3*N;i++){
            Num += a[i];
            PQ2.Add(a[i],a[i]);
        }
        ans = memo[2*N-1]-Num;
        for(int i = 2*N - 1;i >= N;i--){
            //大きいのから追い出していく
            Num += a[i];
            PQ2.Add(a[i],a[i]);
            Num -= PQ2.Take();
            //現在の取れる後半の最小の値 = Num
            //このときの前半の最大値 = memo[i-1]
            ans = Math.Max(ans,memo[i-1] - Num);
        }
        Console.WriteLine(ans);
    }
}
public class PriorityQueue<Tkey, Tvalue> where Tkey : IComparable
{
private List<Tkey> keys = new List<Tkey>();
private List<Tvalue> values = new List<Tvalue>();
private int size = 0;
private static bool Descending;
private static int Compare(Tkey a, Tkey b) { return (Descending ? a.CompareTo(b) : b.CompareTo(a)); }
 
public PriorityQueue(bool b = true) { Clear(); Descending = b; }
public int Count() { return size; }
public void Clear() { keys = new List<Tkey>(); values = new List<Tvalue>(); size = 0; }
public void Add(Tkey e, Tvalue v)
{
keys.Add(e);
values.Add(v);
int x = size;
size += 1;
while (x != 0)
{
int par = (x % 2 == 0 ? x - 2 : x - 1) / 2;
if (Compare(keys[par], e) <= 0) break;
Tkey a = keys[x]; keys[x] = keys[par]; keys[par] = a;
Tvalue b = values[x]; values[x] = values[par]; values[par] = b;
x = par;
}
}
public void Poll()
{
Tkey k = keys[size - 1];
Tvalue v = values[size - 1];
int x = 0;
size--;
if (size == 0) { Clear(); return; }
keys.RemoveAt(size);
values.RemoveAt(size);
while (true)
{
int l = x * 2 + 1;
int r = x * 2 + 2;
if (size - 1 < l) break;
int y = 0;
if (size - 1 == l) { y = l; }
else if (Compare(keys[l], keys[r]) == 1) { y = r; }
else { y = l; }
if (Compare(keys[y], k) > 0) break;
keys[x] = keys[y];
values[x] = values[y];
x = y;
}
keys[x] = k;
values[x] = v;
}
public Tvalue Take() { Tvalue ret = values[0]; Poll(); return ret; }
public Tvalue Peek() { return values[0]; }
public void Debug()
{
Console.Write("key : ");
foreach (var k in keys) Console.Write(k + " ");
Console.WriteLine();
Console.Write("value : ");
foreach (var k in values) Console.Write(k + " ");
Console.WriteLine();
}
}
public class Input
{
private static Queue<string> queue = new Queue<string>();
private static void Confirm() { if (queue.Count == 0) foreach (var s in Console.ReadLine().Split(' ')) queue.Enqueue(s); }
public static int NextInt() { Confirm(); return int.Parse(queue.Dequeue()); }
public static long NextLong() { Confirm(); return long.Parse(queue.Dequeue()); }
public static string NextString() { Confirm(); return queue.Dequeue(); }
public static double NextDouble() { Confirm(); return double.Parse(queue.Dequeue()); }
public static int[] LineInt() { return Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); }
public static long[] LineLong() { return Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); }
public static string[] LineString() { return Console.ReadLine().Split(' ').ToArray(); }
public static double[] LineDouble() { return Console.ReadLine().Split(' ').Select(double.Parse).ToArray(); }
}

Compiler output:
a.cc:1:7: error: expected nested-name-specifier before 'System'
    1 | using System;
      |       ^~~~~~
a.cc:2:7: error: expected nested-name-specifier before 'System'
    2 | using System.Collections.Generic;
      |       ^~~~~~
a.cc:3:7: error: expected nested-name-specifier before 'System'
    3 | using System.Linq;
      |       ^~~~~~
a.cc:4:7: error: expected nested-name-specifier before 'static'
    4 | using static Input;
      |       ^~~~~~
a.cc:5:1: error: expected unqualified-id before 'public'
    5 | public class Hello{
      | ^~~~~~
a.cc:46:1: error: expected unqualified-id before 'public'
   46 | public class PriorityQueue<Tkey, Tvalue> where Tkey : IComparable
      | ^~~~~~
a.cc:110:1: error: expected unqualified-id before 'public'
  110 | public class Input
      | ^~~~~~