import qualified Data.ByteString.Char8 as BS
import Data.List
import Data.Maybe

readInt = fst . fromJust . BS.readInt
readIntList = map readInt . BS.words

getInt = readInt <$> BS.getLine
getIntList = readIntList <$> BS.getLine

simulation [] _ _ c = c
simulation (a : as) 0 0 0 = simulation as a 0 0
simulation (a : as) a' 0 0 = simulation as a' a a'
simulation (a : as) a' a'' c
  | a' >= a'' = simulation as a a'' (c + a'')
  | otherwise = simulation as a' a (c + a')

main = do
  n <- getInt
  as <- getIntList
  let as' = reverse (sort as)
  print $ simulation as' 0 0 0
