module Main where

import qualified Data.ByteString.Char8         as BS
import qualified Data.ByteString.Lazy.Char8    as BSL
import qualified Data.ByteString.Builder       as BSB
import           Data.Char                      ( isSpace )
import           Data.List                      ( unfoldr )
import qualified Data.Vector.Unboxed           as VU
import qualified Data.Vector.Unboxed.Mutable   as VUM
import qualified Data.Vector.Algorithms.Tim as VAT
import           Data.IntSet                    ( IntSet )
import qualified Data.IntSet                   as IntSet
import           Data.Ord

main :: IO ()
main = do
  n <- readLn :: IO Int
  as <- VU.unfoldr (BS.readInt . BS.dropWhile isSpace) <$> BS.getLine
  as' <- do
    x <- VU.thaw as
    VAT.sortBy (comparing Down) x
    VU.freeze x
  let s = IntSet.singleton $ VU.head as'
  let ans = VU.foldl' go (s, 0) $ VU.tail as'
  print $ snd ans
 where 
  go :: (IntSet, Int) -> Int -> (IntSet, Int)
  go (s, c) a = 
    let
      v = IntSet.findMax s
      s' = IntSet.insert a $ IntSet.insert a $ IntSet.deleteMax s
    in (s', c + v)
