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 LuaJIT (2.0.4) (judge verdict: Accepted)

local mfl, mce, mmi, mma = math.floor, math.ceil, math.min, math.max
local SegTree = {}
SegTree.updateAll = function(self)
  for i = self.stagenum - 1, 1, -1 do
    for j = 1, self.cnt[i] do
      self.stage[i][j] = self.func(self.stage[i + 1][j * 2 - 1], self.stage[i + 1][j * 2])
    end
  end
end
SegTree.create = function(self, ary, func, emptyvalue)
  self.func, self.emptyvalue = func, emptyvalue
  local stagenum, mul = 1, 1
  self.cnt, self.stage, self.size = {1}, {{}}, {}
  while mul < #ary do
    mul, stagenum = mul * 2, stagenum + 1
    self.cnt[stagenum], self.stage[stagenum] = mul, {}
  end
  for i = 1, stagenum do self.size[i] = self.cnt[stagenum + 1 - i] end
  self.stagenum = stagenum
  for i = 1, #ary do self.stage[stagenum][i] = ary[i] end
  for i = #ary + 1, mul do self.stage[stagenum][i] = emptyvalue end
  self:updateAll()
end
SegTree.addValue = function(self, idx, val)
  self.stage[self.stagenum][idx] = self.stage[self.stagenum][idx] + val
  for i = self.stagenum - 1, 1, -1 do
    local dst = mce(idx / 2)
    local rem = dst * 4 - 1 - idx
    self.stage[i][dst] = self.func(self.stage[i + 1][idx], self.stage[i + 1][rem])
    idx = dst
  end
end
SegTree.new = function(ary, func, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(ary, func, emptyvalue)
  return obj
end

SegTree.takeLeft = function(self)
  local ret, retpos = self.emptyvalue, 0
  local t1, t2, t3 = {1}, {1}, {self.size[1]}
  while 0 < #t1 do
    local stage, l, r = t1[#t1], t2[#t1], t3[#t1]
    table.remove(t1) table.remove(t2) table.remove(t3)
    local sz = self.size[stage]
    if sz <= r + 1 - l then
      local tmp = self.func(ret, self.stage[stage][mce(l / sz)])
      if tmp < 1 then
        ret, retpos = tmp, l + sz - 1
        if sz ~= 1 then table.insert(t1, stage + 1) table.insert(t2, l + sz) table.insert(t3, r) end
      else
        if sz ~= 1 then table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, l + sz - 2) end
      end
    else
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, r)
    end
  end
  retpos = retpos + 1
  self:addValue(retpos, -1)
  return retpos
end

local n = io.read("*n", "*l")
local a = {}
local str = io.read()
for v in str:gmatch("%d+") do
  table.insert(a, tonumber(v))
end
local value_list, value_to_idx = {}, {}
for i = 1, n * 2 do
  if not value_to_idx[a[i]] then
    value_to_idx[a[i]] = true
    table.insert(value_list, a[i])
  end
end
table.sort(value_list)
value_to_idx = {}
for i = 1, #value_list do value_to_idx[value_list[i]] = i end
local st = nil
local leftsum = 0
do
  local stary = {}
  for i = 1, #value_list do stary[i] = 0 end
  for i = 1, n do
    stary[value_to_idx[a[i]]] = stary[value_to_idx[a[i]]] + 1
    leftsum = leftsum + a[i]
  end
  st = SegTree.new(stary, function(a,b)return a+b end, 0)
end
leftsum = {leftsum}
for i = 1, n do
  st:addValue(value_to_idx[a[n + i]], 1)
  local pos = st:takeLeft()
  leftsum[i + 1] = leftsum[i] + a[n + i] - value_list[pos]
end
-- r
value_list, value_to_idx = {}, {}
for i = n + 1, n * 3 do
  if not value_to_idx[-a[i]] then
    value_to_idx[-a[i]] = true
    table.insert(value_list, -a[i])
  end
end
table.sort(value_list)
value_to_idx = {}
for i = 1, #value_list do value_to_idx[value_list[i]] = i end
local st = nil
local rightsum = 0
do
  local stary = {}
  for i = 1, #value_list do stary[i] = 0 end
  for i = 2 * n + 1, n * 3 do
    stary[value_to_idx[-a[i]]] = stary[value_to_idx[-a[i]]] + 1
    rightsum = rightsum - a[i]
  end
  st = SegTree.new(stary, function(a,b)return a+b end, 0)
end
rightsum = {rightsum}
for i = n, 1, -1 do
  st:addValue(value_to_idx[-a[n + i]], 1)
  local pos = st:takeLeft()
  rightsum[n + 2 - i] = rightsum[n + 1 - i] - a[n + i] - value_list[pos]
end
local ret = leftsum[1] + rightsum[n + 1]
for i = 2, n + 1 do
  ret = mma(ret, leftsum[i] + rightsum[n + 2 - i])
end
print(ret)