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 (GCC 5.4.1) (judge verdict: Accepted)

#include <stdio.h>
#include <stdlib.h>

#define datatype int

long long int max(long long int a, long long int b){
	if(a > b){
		return a;
	}
	else{
		return b;
	}
}

long long int min(long long int a, long long int b){
	if(a < b){
		return a;
	}
	else{
		return b;
	}
}

typedef enum{
	false,
	true
}boolean;

typedef struct node_sub{
	datatype val;
	struct node_sub *parent;
	struct node_sub *prev;
	struct node_sub *next;
	struct node_sub *left;
	struct node_sub *right;
}node;

typedef struct {
	long long int num;
	node *root;
	node *last;
}heap;

long long int compare(datatype a, datatype b){
	if(a < b){
		return -1;
	}
	if(a == b){
		return 0;
	}
	if(a > b){
		return 1;
	}
}

heap *make_heap(){
	heap *h = (heap *)malloc(sizeof(heap));
	h->num = 0;
	h->root = NULL;
	h->last = NULL;
	return h;
}

boolean is_empty(heap *h){
	if(h->num == 0){
		return true;
	}
	else{
		return false;
	}
}

void up_heapify(node *n, heap *h){
	if(n->parent != NULL){
		if(compare(n->val, n->parent->val) == -1){
			datatype tmp;
			tmp = n->val;
			n->val = n->parent->val;
			n->parent->val = tmp;
			up_heapify(n->parent, h);
		}
	}
}

void insert(datatype val, heap *h){
	node *new = (node *)malloc(sizeof(node));
	new->val = val;
	new->next = NULL;
	new->left = NULL;
	new->right = NULL;
	if(is_empty(h) == true){
		new->parent = NULL;
		new->prev = NULL;
		h->root = new;
		h->last = new;
	}
	else if(h->num == 1){
		new->parent = h->root;
		new->prev = h->root;
		h->root->left = new;
		h->root->next = new;
		h->last = new;
		up_heapify(new, h);
	}
	else{
		new->prev = h->last;
		h->last->next = new;
		if(h->last->parent->right == NULL){
			new->parent = h->last->parent;
			h->last->parent->right = new;
		}
		else{
			new->parent = h->last->parent->next;
			h->last->parent->next->left = new;
		}
		h->last = new;
		up_heapify(new, h);
	}
	h->num++;
}

void down_heappify(node *n, heap *h){
	if(n->left != NULL){
		datatype tmp;
		if(n->right == NULL){
			if(compare(n->val, n->left->val) == 1){
				tmp = n->val;
				n->val = n->left->val;
				n->left->val = tmp;
				down_heappify(n->left, h);
			}
		}
		else{
			if(compare(n->left->val, n->right->val) != 1 && compare(n->val, n->left->val) == 1){
				tmp = n->val;
				n->val = n->left->val;
				n->left->val = tmp;
				down_heappify(n->left, h);
			}
			else if(compare(n->left->val, n->right->val) == 1 && compare(n->val, n->right->val) == 1){
				tmp = n->val;
				n->val = n->right->val;
				n->right->val = tmp;
				down_heappify(n->right, h);
			}
		}
	}
}

datatype take_min(heap *h){
	datatype ans = h->root->val;
	node *ln = h->last;
	if(h->num == 1){
		h->root = NULL;
		h->last = NULL;
	}
	else{
		ln->prev->next = NULL;
		if(ln->parent->right == NULL){
			ln->parent->left = NULL;
		}
		else{
			ln->parent->right = NULL;
		}
		h->last = ln->prev;
		h->root->val = ln->val;
		down_heappify(h->root, h);
	}
	free(ln);
	h->num--;
	return ans;
}

long long int main(){
	long long int N, i, a, b;
	scanf("%lld", &N);
	heap *hl = make_heap();
	heap *hr = make_heap();
	long long int *ac = (long long int *)malloc(sizeof(long long int) * N);
	long long int *sumleft = (long long int *)malloc(sizeof(long long int) * (N + 1));
	long long int *sumright = (long long int *)malloc(sizeof(long long int) * (N + 1));
	long long int sum = 0;
	for(i = 0; i < N; i++){
		scanf("%lld", &a);
		insert(a, hl);
		sum += a;
	}
	sumleft[0] = sum;
	for(i = 0; i < N; i++){
		scanf("%lld", &ac[i]);
	}
	sum = 0;
	for(i = 0; i < N; i++){
		scanf("%lld", &a);
		insert(-a, hr);
		sum += a;
	}
	sumright[N] = sum;
	for(i = 0; i < N; i++){
		a = take_min(hl);
		b = max(a, ac[i]);
		sumleft[i + 1] = sumleft[i] + b - a;
		insert(b, hl);
	}
	for(i = 0; i < N; i++){
		a = -take_min(hr);
		b = min(a, ac[N - i - 1]);
		sumright[N - i - 1] = sumright[N - i] + b - a;
		insert(-b, hr);
	}
	long long int ans = sumleft[0] - sumright[0];
	for(i = 0; i <= N; i++){
		ans = max(ans, sumleft[i] - sumright[i]);
	}
	printf("%lld\n", ans);
	return 0;
}