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 Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.NoSuchElementException;

public class Main {
	private static FastScanner sc = new FastScanner();

	public static void main(String[] args) {
		int n = sc.nextInt();
		
		int[] a = new int[3*n];
		for(int i=0; i<3*n; i++) {
			a[i] = sc.nextInt();
		}
		
		int l_length = 3 * n / 2;
		int r_length = 3 * n - l_length;
		
		long score_max1 = Integer.MIN_VALUE;
		while(l_length > n && r_length > n) {
			int[] l = new int[l_length];
			for(int i=0; i<l_length; i++) {
				l[i] = a[i];
			}
			
			int[] r = new int[r_length];
			for(int i=0; i<r_length; i++) {
				r[i] = a[l_length + i];
			}
			
			Arrays.sort(l);
			Arrays.sort(r);
			
			long l_sum = 0;
			for(int i=l_length - n; i<l_length; i++) {
				l_sum += l[i];
			}
			
			long r_sum = 0;
			for(int i=0; i<n; i++) {
				r_sum += r[i];
			}
			
			if(l_sum - r_sum > score_max1) {
				score_max1 = l_sum - r_sum;
//				System.out.println(l_length + " " + r_length + " " + l_sum + " " + r_sum + " " + score_max1);
			} else {
				break;
			}
			
			l_length--;
			r_length++;
		}
		
		long score_max2 = Integer.MIN_VALUE;
		l_length = 3 * n / 2 + 1;
		r_length = 3 * n - l_length;
		while(l_length >= n && r_length >= n) {
			int[] l = new int[l_length];
			for(int i=0; i<l_length; i++) {
				l[i] = a[i];
			}
			
			int[] r = new int[r_length];
			for(int i=0; i<r_length; i++) {
				r[i] = a[l_length + i];
			}
			
			Arrays.sort(l);
			Arrays.sort(r);
			
			long l_sum = 0;
			for(int i=l_length - n; i<l_length; i++) {
				l_sum += l[i];
			}
			
			long r_sum = 0;
			for(int i=0; i<n; i++) {
				r_sum += r[i];
			}
			
			if(l_sum - r_sum > score_max2) {
				score_max2 = l_sum - r_sum;
//				System.out.println(l_length + " " + r_length + " " + l_sum + " " + r_sum + " " + score_max2);
			} else {
				break;
			}
			
			l_length++;
			r_length--;
		}
		
		System.out.println(Math.max(score_max1, score_max2));
	}

	static class FastScanner {
	    private final InputStream in = System.in;
	    private final byte[] buffer = new byte[1024];
	    private int ptr = 0;
	    private int buflen = 0;
	    private boolean hasNextByte() {
	        if (ptr < buflen) {
	            return true;
	        }else{
	            ptr = 0;
	            try {
	                buflen = in.read(buffer);
	            } catch (IOException e) {
	                e.printStackTrace();
	            }
	            if (buflen <= 0) {
	                return false;
	            }
	        }
	        return true;
	    }
	    private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
	    private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
	    private void skipUnprintable() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;}
	    public boolean hasNext() { skipUnprintable(); return hasNextByte();}
	    public String next() {
	        if (!hasNext()) throw new NoSuchElementException();
	        StringBuilder sb = new StringBuilder();
	        int b = readByte();
	        while(isPrintableChar(b)) {
	            sb.appendCodePoint(b);
	            b = readByte();
	        }
	        return sb.toString();
	    }
	    public long nextLong() {
	        return Long.parseLong(next());
	    }
	    public int nextInt(){
	    	return Integer.parseInt(next());
	    }
	}
}