
import Data.List
import Debug.Trace

foo :: [Int] -> Int
foo (x:[y]) = x + y
foo nums@(x:xs) =
  let
    mids = init xs
    mini = minimum mids
    idx  = 1 + head (elemIndices mini mids)
  in
    trace (show nums) $ foo $ take (idx-1) nums ++ [(nums!!(idx-1))+mini, (nums!!(idx+1))+mini] ++ drop (idx+2) nums

main = do
  _ <- getLine
  nums <- map read . words <$> getLine :: IO [Int]
  print $ foo nums

