module Main where import Data.List (sortBy) type Input = [Int] type Output = Int parse :: String -> Input parse = map read . words . (!! 1) . lines solve :: Input -> Output solve = loop 0 0 . sortBy (flip compare) where loop _ acc [] = acc loop i acc xs | length xs < 2 ^ i = acc + i * sum xs | otherwise = let (a, b) = splitAt (2 ^ i) xs in loop (succ i) (acc + i * sum a) b render :: Output -> String render = show main :: IO () main = interact ( render . solve . parse )