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 mma = math.max
local AvlTree = {}
AvlTree.makenode = function(self, val, parent)
  local node = {}
  node.v, node.lc, node.rc = val, 0, 0
  node.p = parent
  -- node.l, node.r = nil, nil
  return node
end
AvlTree.create = function(self, lessthan)
  self.lessthan = lessthan
end

AvlTree.recalcCount = function(self, node)
  if node then
    if node.l then node.lc = 1 + mma(node.l.lc, node.l.rc)
    else node.lc = 0
    end
    if node.r then node.rc = 1 + mma(node.r.lc, node.r.rc)
    else node.rc = 0
    end
  end
end
AvlTree.recalcCountAll = function(self, node)
  while node do
    self:recalcCount(node)
    node = node.p
  end
end

AvlTree.rotR = function(self, child, parent)
  local granp = parent.p
  child.r, parent.l = parent, child.r
  child.p, parent.p = granp, child
  if parent.l then parent.l.p = parent end
  if granp then
    if granp.l == parent then
      granp.l = child
    else
      granp.r = child
    end
  else -- granp == nil
    self.root = child
  end
  self:recalcCountAll(parent)
end

AvlTree.rotL = function(self, child, parent)
  local granp = parent.p
  child.l, parent.r = parent, child.l
  child.p, parent.p = granp, child
  if parent.r then parent.r.p = parent end
  if granp then
    if granp.r == parent then
      granp.r = child
    else
      granp.l = child
    end
  else -- granp == nil
    self.root = child
  end
  self:recalcCountAll(parent)
end

AvlTree.add = function(self, val)
  if not self.root then self.root = self:makenode(val) return end
  local pos = self.root
  local added = false
  while not added do
    if self.lessthan(val, pos.v) then
      if pos.l then
        pos = pos.l
      else
        pos.l = self:makenode(val, pos)
        pos = pos.l
        added = true
      end
    else
      if pos.r then
        pos = pos.r
      else
        pos.r = self:makenode(val, pos)
        pos = pos.r
        added = true
      end
    end
  end
  self:recalcCountAll(pos)
  local resolve_child = pos
  while resolve_child do
    local child, parent = resolve_child, resolve_child.p
    resolve_child = nil
    if not parent then
      break
    end
    if parent.l == child then
      if parent.lc - 1 == parent.rc then
        resolve_child = parent
      elseif parent.lc - 2 == parent.rc then
        if child.lc - 1 == child.rc then
          self:rotR(child, parent)
        else
          local cr = child.r
          self:rotL(cr, child)
          self:rotR(cr, parent)
        end
      end
    else -- parent.r == child
      if parent.rc - 1 == parent.lc then
        resolve_child = parent
      elseif parent.rc - 2 == parent.lc then
        if child.rc - 1 == child.lc then
          self:rotL(child, parent)
        else
          local cl = child.l
          self:rotR(cl, child)
          self:rotL(cl, parent)
        end
      end
    end
  end
end

AvlTree.rmsub = function(self, node)
  while node do
    self:recalcCount(node)
    if node.lc == node.rc then
      node = node.p
    elseif node.lc + 1 == node.rc then
      self:recalcCountAll(node.p)
      node = nil
    else
      if node.r.lc == node.r.rc then
        self:rotL(node.r, node)
        self:recalcCountAll(node)
        node = nil
      elseif node.r.lc + 1 == node.r.rc then
        local nr = node.r
        self:rotL(nr, node)
        node = nr
      else
        local nrl = node.r.l
        self:rotR(nrl, node.r)
        self:rotL(nrl, node)
        node = nrl
      end
    end
  end
end

AvlTree.pop = function(self)
  local edge = self.root
  while edge.l do
    edge = edge.l
  end
  local v = edge.v
  if edge.p then
    if edge.r then edge.r.p = edge.p end
    edge.p.l = edge.r
    self:rmsub(edge.p)
  else
    if edge.r then edge.r.p = nil end
    self.root = edge.r
  end
  return v
end

AvlTree.new = function(lessthan)
    local obj = {}
    setmetatable(obj, {__index = AvlTree})
    obj:create(lessthan)
    return obj
end

local n = io.read("*n", "*l")
local a = {}
local s = io.read()
for str in s:gmatch("%d+") do
  table.insert(a, tonumber(str))
end
local leftsum = {0}
local avleft = AvlTree.new(function(x, y) return x < y end)
for i = 1, n do
  leftsum[1] = leftsum[1] + a[i]
  avleft:add(a[i])
end
for i = 1, n do
  avleft:add(a[i + n])
  leftsum[i + 1] = leftsum[i] + a[i + n] - avleft:pop()
end
local avright = AvlTree.new(function(x, y) return x > y end)
local rightsum = {0}
for i = 1, n do
  rightsum[1] = rightsum[1] + a[i + 2 * n]
  avright:add(a[i + 2 * n])
end
for i = 1, n do
  avright:add(a[2 * n + 1 - i])
  rightsum[i + 1] = rightsum[i] + a[2 * n + 1 - i] - avright:pop()
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)