
--{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE BangPatterns #-}

module Main where

import Debug.Trace
import Data.List
--import Data.Char
--import qualified Data.Set.Strict as Set
--import qualified Data.Map.Strict as Map

main :: IO ()
main = do
  getLine
  s <- map read . words <$> getLine
  --a `traceShow`
  print $ solve s


solve :: [Int] -> Int
solve s = search 0 [] [head s', head s'] (tail s')
  where
    s' = reverse . sort $ s

search :: Int -> [Int] -> [Int] -> [Int] -> Int
search !k _ _ [] = k
search !k qs [b] ss = search k [] (reverse (b:qs)) ss
search !k qs (b1:b2:bs) (s:ss) = (k, s:ss) `traceShow` search (k + min b1 b2) (s:b1:qs) (b2:bs) ss
