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 Ruby (2.3.3) (judge verdict: Time Limit Exceeded)

class PriorityQueue
	class Node
		attr_reader :value, :priority
		def initialize(value, priority)
			@value = value
			@priority = priority
		end
	end

	def initialize
		@heap = [nil]
	end

	def size
		@heap.size - 1
	end

	def empty?
		size == 0
	end

	def peek
		@heap[1].value
	end

	def enqueue(value, priority)
		@heap.push(Node.new(value, priority))
		up_heap
		self
	end

	def dequeue
		return nil if empty?
		return @heap.pop.value if size == 1
		ret = @heap[1].value
		@heap[1] = @heap.pop
		down_heap
		ret
	end

	private

	def up_heap
		pos = size
		while pos > 1
			parent = pos / 2
			break if @heap[pos].priority <= @heap[parent].priority
			tmp = @heap[pos]
			@heap[pos] = @heap[parent]
			@heap[parent] = tmp
			pos = parent
		end
	end

	def down_heap
		pos = 1
		loop{
			left = pos * 2 
			right = pos * 2 + 1
			break if left >= @heap.size
			if right >= @heap.size || @heap[left].priority >= @heap[right].priority
				break if @heap[pos].priority >= @heap[left].priority
				tmp = @heap[pos]
				@heap[pos] = @heap[left]
				@heap[left] = tmp
				pos = left
			else
				break if @heap[pos].priority >= @heap[right].priority
				tmp = @heap[pos]
				@heap[pos] = @heap[right]
				@heap[right] = tmp
				pos = right
			end
		}
	end
end

N = gets.to_i
A = gets.split.map{|i|i.to_i}
a1 = PriorityQueue.new
a2 = PriorityQueue.new
s1 = [0]
s2 = [0]
N.times{|i|
	a1.enqueue(A[i], -A[i])
	a2.enqueue(A[N * 2 + i], A[N * 2 + i])
	s1[0] += A[i]
	s2[0] += A[N * 2 + i]
}

N.times{|i|
	a = A[N + i]
	s = s1[-1]
	if a1.peek < a
		a1.enqueue(a, -a)
		d = a1.dequeue
		s += a
		s -= d
	end
	s1 << s

	a = A[N * 2 - 1 - i]
	s = s2[-1]
	if a2.peek > a
		a2.enqueue(a, a)
		d = a2.dequeue
		s += a
		s -= d
	end
	s2 << s
}

max = -N * 10 ** 9
(N + 1).times{|i|
	d = s1[i] - s2[N - i]
	if max < d
		max = d
	end
}
puts max