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 Haskell (GHC 7.10.3) (judge verdict: Time Limit Exceeded)

import Control.Monad
import qualified Data.Map as M
import Data.Maybe


-- pairing heap
-----------------------------------------------------
data Heap a = Empty | Heap a [(Heap a)] deriving Show

findMin :: Heap a -> a
findMin (Heap h _) = h

merge :: Ord a => Heap a -> Heap a -> Heap a
merge Empty h = h
merge h Empty = h
merge h1@(Heap x hs1) h2@(Heap y hs2)
  | x < y = Heap x (h2:hs1)
  | otherwise = Heap y (h1:hs2)

mergePairs :: Ord a => [Heap a] -> Heap a
mergePairs [] = Empty
mergePairs [h] = h
mergePairs (h1:h2:hs) = merge (merge h1 h2) (mergePairs hs)

insert :: Ord a => a -> Heap a -> Heap a
insert x h = merge (Heap x []) h

deleteMin :: Ord a => Heap a -> Heap a
deleteMin (Heap x hs) = mergePairs hs

fromList :: Ord a => [a] -> Heap a
fromList [] = Empty
fromList (x:xs) = merge (Heap x []) (fromList xs)
-----------------------------------------------------


{-
-- bag
-----------------------------------------------------
type Bag = M.Map Int Int

findMin :: Bag -> Int
findMin bag = fst $ M.findMin bag

findMax :: Bag -> Int
findMax bag = fst $ M.findMax bag

delete :: Int -> Bag -> Bag
delete element bag = newbag
  where quantity = fromMaybe (-1) $ M.lookup element bag
        newbag = if quantity == -1 then bag
                    else if quantity > 1 then M.insertWith (flip (-)) element 1 bag
                            else M.delete element bag

insert :: Int -> Bag -> Bag
insert element bag = M.insertWith (+) element 1 bag

fromList :: [Int] -> Bag
fromList xs = M.fromList $ zip xs (replicate (length xs) 1)
-----------------------------------------------------


sum_large_n :: Bag -> Int -> Int
sum_large_n _ 0 = 0
sum_large_n bag n = (largest + sum_large_n newbag (n - 1))
  where largest = findMax bag
        newbag = delete largest bag

sum_small_n :: Bag -> Int -> Int
sum_small_n _ 0 = 0
sum_small_n bag n = smallest + sum_small_n newbag (n - 1)
  where smallest = findMin bag
        newbag = delete smallest bag


iterate_l :: Int -> [Int] -> Bag -> [Int]
iterate_l _ [] _ = []
iterate_l n (x:xs) bag = ans:iterate_l n xs newbag
  where ans = sum_large_n newbag n
        newbag = insert x bag

iterate_r :: Int -> [Int] -> Bag -> [Int]
iterate_r _ [] _ = []
iterate_r n (x:xs) bag = ans:iterate_r n xs newbag
  where ans = sum_small_n newbag n
        newbag = insert x bag
-}

sum_large_n :: Heap Int -> Int -> Int
sum_large_n _ 0 = 0
sum_large_n h n = ((-largest) + sum_large_n newh (n - 1))
  where largest = findMin h
        newh = deleteMin h

sum_small_n :: Heap Int -> Int -> Int
sum_small_n _ 0 = 0
sum_small_n h n = (smallest + sum_small_n newh (n - 1))
  where smallest = findMin h
        newh = deleteMin h


iterate_l :: Int -> [Int] -> Heap Int -> [Int]
iterate_l _ [] _ = []
iterate_l n (x:xs) h = ans:iterate_l n xs newh
  where ans = sum_large_n newh n
        newh = insert (-x) h

iterate_r :: Int -> [Int] -> Heap Int -> [Int]
iterate_r _ [] _ = []
iterate_r n (x:xs) h = ans:iterate_r n xs newh
  where ans = sum_small_n newh n
        newh = insert x h

main = do
  [n] <- map read . words <$> getLine :: IO [Int]
  in_seq <- map read . words <$> getLine :: IO [Int]
  let left_seq = take n in_seq
  let right_seq = drop (2 * n) in_seq
  let mid_seq = take n $ drop n in_seq
  let mid_r_seq = reverse mid_seq

  let left_init = sum left_seq
  let m_l_seq = left_init:iterate_l n mid_seq (fromList $ map (*(-1)) left_seq)

  let right_init = sum right_seq
  let m_r_seq = right_init:iterate_r n mid_r_seq (fromList right_seq)

  let ans = maximum (zipWith (\a b -> a - b) m_l_seq (reverse m_r_seq))

  print ans