import Data.Bits
import Data.List
import Control.Monad
import Data.Array.Unboxed
import Data.Array.MArray
import Data.Array.ST
import Control.Monad.ST
import Data.Maybe (fromJust)
import qualified Data.ByteString.Lazy.Char8 as B
import Debug.Trace

cnt :: Int -> Int
cnt n = sum [ if i > 0 then 2^(i-1) else 1 | i <- [0..ceiling (logBase 2 (fromIntegral n ::Double))], testBit n i]

writedown :: [(Int,Int)] -> IO [(Int,Int)]
writedown [] = []
writedown ls = do
  ls' <- forM ls $ \(x,y) -> do
    putStrLn $ (show x) ++ " " (show y)
    return (x-y)
  writedown ls'

solve :: Int -> [(Int,Int)] -> Int -> IO ()
solve n as i = do
  if testBit n i
    then do
      let (xs,ys) = splitAt 2^(i-1) as
          (rems,zs) = splitAt ((length ys) - 2^(i-1)) ys
      writedown (zip xs zs)
      solve n rems (i+1)
    else solve n as (i+1)

  let ras = reverse as
  return ()



main :: IO ()
main = do
  n <- readLn
  as' <- map read.words <$> getLine :: IO [Int]
  let as = sortOn (0-) as'
  print $ (sum (take (cnt n) as)) - (sum $ drop (cnt n) as) -- 最大値
  solve n as 0
