-- https://atcoder.jp/contests/abc173/tasks/abc173_d
-- 大きい順にソートして先頭からその時点で最も高得点を得られる位置に挿入する

import Data.List
import Data.Ord

type Value = Int
type Point = Int

insertPoint :: Value -> [(Value, Point)] -> [(Value, Point)]
insertPoint new [] = [(new, 0)]
insertPoint new [(v, p)] = [(v, p), (new, v)]
insertPoint new (a: ps) = if new > fst a
  then (new, fst a): a: ps
  else a: insertPoint new ps

main :: IO ()
main = do
  _ <- getLine
  friendnesses <- sortOn Down . map read . words <$> getLine :: IO [Int]
  let sorted = foldl (flip insertPoint) [] friendnesses
  -- print sorted
  print . sum $ map snd sorted

