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 C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ARC074
{
    public class D
    {
        private void Run()
        {
            var N = int.Parse(Console.ReadLine().Trim());
            var A = Console.ReadLine().Trim().Split().Select(long.Parse).ToArray();

            var headUsingElementQueue = new PriorityQueue<long>();
            long headMax = 0;
            var headMaxArray = new long[A.Length];
            for (int i = 0; i < A.Length; i++)
            {
                var a = A[i];
                if (headUsingElementQueue.Count < N)
                {
                    headUsingElementQueue.Push(a);
                    headMax += a;
                }
                else if (headUsingElementQueue.Peek() < a)
                {
                    var oldMax = headUsingElementQueue.Pop();
                    headMax -= oldMax;
                    headMax += a;
                    headUsingElementQueue.Push(a);
                }
                headMaxArray[i] = headMax;
            }

            var tailNotUsingMinusedElementQueue = new PriorityQueue<long>();
            long tailMin = 0;
            var tailMinArray = new long[A.Length];
            for (int i = A.Length - 1; i >= 0; i--)
            {
                var a = A[i];
                if (tailNotUsingMinusedElementQueue.Count < N)
                {
                    tailNotUsingMinusedElementQueue.Push(-a);
                    tailMin += a;
                }
                else if (tailNotUsingMinusedElementQueue.Peek() * -1 > a)
                {
                    var oldMin = tailNotUsingMinusedElementQueue.Pop() * -1;
                    tailMin -= oldMin;
                    tailMin += a;
                    tailNotUsingMinusedElementQueue.Push(-a);
                }
                tailMinArray[i] = tailMin;
            }

            var result = long.MinValue;
            for (int i = N - 1; i + N + 1 <= A.Length; i++)
            {
                //Console.WriteLine($"{i}, {headMaxArray[i]}, {tailMinArray[i]}");
                result = Math.Max(result, headMaxArray[i] - tailMinArray[i + 1]);
            }

            Console.WriteLine(result);
        }

        public static void Main()
        {
            var old = Console.Out;
            using (var writer = new System.IO.StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false })
            {
                Console.SetOut(writer);
                new D().Run();
                Console.Out.Flush();
                Console.SetOut(old);
            }
        }

        // 最も小さな要素を返します
        public class PriorityQueue<T>
        {
            public PriorityQueue() : this(Comparer<T>.Default) { }
            public PriorityQueue(IComparer<T> compare)
            {
                this.m_buffer = new List<T>();
                this.m_comparer = compare;
            }

            public void Push(T elem)
            {
                int n = this.m_buffer.Count;
                this.m_buffer.Add(elem);
                while (n != 0)
                {
                    int i = (n - 1) / 2;
                    if (this.m_comparer.Compare(this.m_buffer[i], this.m_buffer[n]) > 0)
                    {
                        T temp = this.m_buffer[i];
                        this.m_buffer[i] = this.m_buffer[n];
                        this.m_buffer[n] = temp;
                    }
                    n = i;
                }
            }
            public T Pop()
            {
                int n = this.m_buffer.Count - 1;
                var res = this.m_buffer[0];
                this.m_buffer[0] = this.m_buffer[n];
                this.m_buffer.RemoveAt(n);
                int i = 0, j;
                while (true)
                {
                    j = 2 * i + 1;
                    if (j >= n) { break; }
                    if (j < n - 1 && this.m_comparer.Compare(this.m_buffer[j], this.m_buffer[j + 1]) > 0) { ++j; }
                    if (this.m_comparer.Compare(this.m_buffer[i], this.m_buffer[j]) > 0)
                    {
                        T temp = this.m_buffer[i];
                        this.m_buffer[i] = this.m_buffer[j];
                        this.m_buffer[j] = temp;
                    }
                    i = j;
                }
                return res;
            }
            public T Peek() { return this.m_buffer[0]; }
            public int Count { get { return this.m_buffer.Count; } }
            public void Clear() { this.m_buffer.Clear(); }

            private readonly List<T> m_buffer;
            private readonly IComparer<T> m_comparer;
        }
    }
}