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 qualified Data.Foldable as F

main :: IO ()
main = do
    n <- readLn
    as <- (map read . words) <$> getLine
    print $ solve n as

-- |
--
-- >>> solve 2 [3,1,4,1,5,9]
-- 1
--
-- >>> solve 1 [1,2,3]
-- -1
solve :: Int -> [Int] -> Int
solve n as = maximum $ zipWith (-) y1s y2s
  where
    (a1s,bs,a2s) = split3 as n
    y1s = map (F.foldl1 (+)) $ scanl (-<-) (fromListMin a1s) (reverse bs)
    y2s = map (F.foldl1 (+)) $ reverse $ scanl (->-) (fromListMax a2s) bs

split3 :: [a] -> Int -> ([a], [a], [a])
split3 zs n = (xs,ys,zs'')
  where
    (xs, zs') = split2 ([], zs) n
    (ys, zs'') = split2 ([], zs') n

split2 :: ([a], [a]) -> Int -> ([a], [a])
split2 (_,[]) _ = undefined
split2 (xs,ys) 0 = (xs,ys)
split2 (xs,y0:ys) n = split2 (y0:xs,ys) (n - 1)

-- Heap
data SkewHeap a = Empty | Node a (SkewHeap a) (SkewHeap a)
            deriving (Show, Read, Eq)

singleton :: a -> SkewHeap a
singleton x = Node x Empty Empty

(+<+) :: Ord a => SkewHeap a -> SkewHeap a -> SkewHeap a
Empty               +<+ t2                  = t2
t1                  +<+ Empty               = t1
t1@(Node x1 l1 r1)  +<+ t2@(Node x2 l2 r2)
   | x1 <= x2   = Node x1 (t2 +<+ r1) l1
   | otherwise  = Node x2 (t1 +<+ r2) l2

(+>+) :: Ord a => SkewHeap a -> SkewHeap a -> SkewHeap a
Empty               +>+ t2                  = t2
t1                  +>+ Empty               = t1
t1@(Node x1 l1 r1)  +>+ t2@(Node x2 l2 r2)
   | x1 >= x2   = Node x1 (t2 +>+ r1) l1
   | otherwise  = Node x2 (t1 +>+ r2) l2

(+<) :: Ord a => SkewHeap a -> a -> SkewHeap a
heap +< x = singleton x +<+ heap

(+>) :: Ord a => SkewHeap a -> a -> SkewHeap a
heap +> x = singleton x +>+ heap

popMin :: Ord a => SkewHeap a -> (Maybe a, SkewHeap a)
popMin Empty        = (Nothing, Empty)
popMin (Node x l r) = (Just x, l +<+ r)

popMax :: Ord a => SkewHeap a -> (Maybe a, SkewHeap a)
popMax Empty        = (Nothing, Empty)
popMax (Node x l r) = (Just x, l +>+ r)

fromListMin :: Ord a => [a] -> SkewHeap a
fromListMin = foldl (+<) Empty

fromListMax :: Ord a => [a] -> SkewHeap a
fromListMax = foldl (+>) Empty

(-<-) :: Ord a => SkewHeap a -> a -> SkewHeap a
(-<-) heap x = snd . popMin $ heap +< x

(->-) :: Ord a => SkewHeap a -> a -> SkewHeap a
(->-) heap x = snd . popMax $ heap +> x

instance Foldable SkewHeap where
    foldMap _ Empty = mempty
    foldMap f (Node x l r) = foldMap f l `mappend` f x `mappend` foldMap f r