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.Globalization;
using System.IO;

#region code challenges entry point
#if !SOLVER_ATWORKSPACE
class Program{static void Main(string[] args){using (var output = new CC.BP.FormattedStreamWriter(Console.OpenStandardOutput()))using (var input = new CC.BP.BufferedStreamReader(Console.OpenStandardInput()))new CC.ConcreteSolver(input, output).Do();}}
#endif
#endregion

#region code helpers boilerplate
namespace CC.BP
{    
    class BufferedStreamReader : StreamReader { const int QUEUE_INITSIZE = 1000; public BufferedStreamReader(String fileName): base(fileName) { } public BufferedStreamReader(Stream stream): base(stream) { } private Queue<string> buffer = new Queue<string>(QUEUE_INITSIZE); public override string ReadLine() { if (buffer.Count > 0) throw new InvalidOperationException("must read from buffer first"); return base.ReadLine(); } public string ReadToken() { while (buffer.Count == 0) { base.ReadLine().Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries).ToList().ForEach((t) => { buffer.Enqueue(t); }); } return buffer.Dequeue(); } public void ResetBuffer() { buffer = new Queue<string>(QUEUE_INITSIZE); } }
    class FormattedStreamWriter : StreamWriter { public FormattedStreamWriter(Stream stream) : base(stream) { } public FormattedStreamWriter(string filePath) : base(filePath) { } public override IFormatProvider FormatProvider { get { return CultureInfo.InvariantCulture; } } }
    static class IOExtensions
    {
        public static string String(this BufferedStreamReader reader) { return reader.ReadToken(); }
        public static string[] StringArray(this BufferedStreamReader reader, int n) { var array = new string[n]; for (int i = 0; i < n; ++i) { array[i] = reader.ReadToken(); } return array; }
        public static int Int(this BufferedStreamReader reader) { return int.Parse(reader.ReadToken(), CultureInfo.InvariantCulture); }
        public static int[] IntArray(this BufferedStreamReader reader, int n) { var array = new int[n]; for (int i = 0; i < n; ++i) { array[i] = int.Parse(reader.ReadToken(), CultureInfo.InvariantCulture); } return array; }
        public static uint UnsignedInt(this BufferedStreamReader reader) { return uint.Parse(reader.ReadToken(), CultureInfo.InvariantCulture); }
        public static long Long(this BufferedStreamReader reader) { return long.Parse(reader.ReadToken(), CultureInfo.InvariantCulture); }
        public static long[] LongArray(this BufferedStreamReader reader, int n) { var array = new long[n]; for (int i = 0; i < n; ++i) { array[i] = long.Parse(reader.ReadToken(), CultureInfo.InvariantCulture); } return array; }
        public static ulong UnsignedLong(this BufferedStreamReader reader) { return ulong.Parse(reader.ReadToken(), CultureInfo.InvariantCulture); }
        public static double Double(this BufferedStreamReader reader) { return double.Parse(reader.ReadToken(), CultureInfo.InvariantCulture); }
        public static double[] DoubleArray(this BufferedStreamReader reader, int n) { var array = new double[n]; for (int i = 0; i < n; ++i) { array[i] = double.Parse(reader.ReadToken(), CultureInfo.InvariantCulture); } return array; }
        public static decimal Decimal(this BufferedStreamReader reader) { return decimal.Parse(reader.ReadToken(), CultureInfo.InvariantCulture); }
        public static decimal[] DecimalArray(this BufferedStreamReader reader, int n) { var array = new decimal[n]; for (int i = 0; i < n; ++i) { array[i] = decimal.Parse(reader.ReadToken(), CultureInfo.InvariantCulture); } return array; }
        public static void ArrayLine<T>(this FormattedStreamWriter writer, T[] array, int n, string format = "{0}", string separator = " ") { for (int i = 0; i < n; ++i) { if (i > 0) writer.Write(separator); writer.Write(format, array[i]); } writer.WriteLine(); }
        public static void ArrayLine<T>(this FormattedStreamWriter writer, T[] array, string format = "{0}", string separator = " ") { writer.ArrayLine(array, array.Length, format, separator); }
    }
    abstract class BaseSolver
    {
        public static T[][] Create2DArray<T>(int dim1, int dim2) { var res = new T[dim1][]; for (int i = 0; i < dim1; ++i) res[i] = new T[dim2]; return res; }
        public static T[][][] Create3DArray<T>(int dim1, int dim2, int dim3) { var res = new T[dim1][][]; for (int i = 0; i < dim1; ++i) res[i] = Create2DArray<T>(dim2, dim3); return res; }
        public static T[][][][] Create4DArray<T>(int dim1, int dim2, int dim3, int dim4) { var res = new T[dim1][][][]; for (int i = 0; i < dim1; ++i) res[i] = Create3DArray<T>(dim2, dim3, dim4); return res; }
        public abstract void Do();
    }    
}
#endregion

namespace CC
{
    using CC.BP;

    class ConcreteSolver : BaseSolver
    {
        private readonly BufferedStreamReader read;
        private readonly FormattedStreamWriter write;

        public ConcreteSolver(BufferedStreamReader read, FormattedStreamWriter write)
        {
            this.read = read;
            this.write = write;
        }

        public class BinaryHeap
        {
            private List<int> list;

            public int heapSize
            {
                get
                {
                    return list.Count();
                }
            }

            Comparison<int> comparer;

            public BinaryHeap(Comparison<int> comparer, int n)
            {
                this.comparer = comparer;
                list = new List<int>(n);
            }

            public void Add(int value)
            {
                list.Add(value);
                int i = heapSize - 1;
                int parent = (i - 1) / 2;

                while (i > 0 && comparer(list[parent], list[i]) == -1)
                {
                    int temp = list[i];
                    list[i] = list[parent];
                    list[parent] = temp;

                    i = parent;
                    parent = (i - 1) / 2;
                }
            }

            public int GetMax()
            {
                int result = list[0];
                list[0] = list[heapSize - 1];
                list.RemoveAt(heapSize - 1);
                Heapify(0);
                return result;
            }

            public int PeekMax()
            {
                return list[0];                
            }

            public void Heapify(int i)
            {
                int leftChild;
                int rightChild;
                int largestChild;

                for (;;)
                {
                    leftChild = 2 * i + 1;
                    rightChild = 2 * i + 2;
                    largestChild = i;

                    if (leftChild < heapSize && comparer(list[leftChild], list[largestChild]) == 1)
                    {
                        largestChild = leftChild;
                    }

                    if (rightChild < heapSize && comparer(list[rightChild], list[largestChild]) == 1)
                    {
                        largestChild = rightChild;
                    }

                    if (largestChild == i)
                    {
                        break;
                    }

                    int temp = list[i];
                    list[i] = list[largestChild];
                    list[largestChild] = temp;
                    i = largestChild;
                }
            }
        }       

        public override void Do()
        {
            var n = read.Int();
            var n2 = n << 1;
            var n3 = n2 + n;
            var a = read.IntArray(n3);            
            var mx = new long [n3];
            var hmin = new BinaryHeap((x, y) => y.CompareTo(x), n);
            for (int i = 0; i < n; ++i)
            {
                mx[n - 1] += a[i];
                hmin.Add(a[i]);
            }
            var hmax = new BinaryHeap((x, y) => x.CompareTo(y), n);
            var mn = new long [n3];
            for (int i = n2; i < n3; ++i)
            {
                mn[n2] += a[i];
                hmax.Add(a[i]);
            }
            
            for (int i = n; i < n2; ++i)
            {
                if (hmin.PeekMax() < a[i])
                {
                    mx[i] = mx[i - 1] - hmin.GetMax() + a[i];
                    hmin.Add(a[i]);
                }
                else
                    mx[i] = mx[i - 1];
            }            
            for (int i = n2 - 1; i >= n; --i)
            {
                if (hmax.PeekMax() > a[i])
                {
                    mn[i] = mn[i + 1] - hmax.GetMax() + a[i];
                    hmax.Add(a[i]);
                }
                else
                    mn[i] = mn[i + 1];
            }
            long res = long.MinValue;
            for (int i = n; i <= n2; ++i)
                res = Math.Max(mx[i - 1] - mn[i], res);
            write.WriteLine(res);
        }
    }
}