import qualified Data.Text as T
import qualified Data.Text.IO as T
import qualified Data.Text.Read as T

main :: IO ()
main = do
  (n : _) <- map unsafeSignedDecimal . T.words <$> T.getLine :: IO [Int]
  bs <- map unsafeSignedDecimal . T.words <$> T.getLine :: IO [Int]
  let
    cs = uncurry (maybe id (:)) . foldr step (Nothing, []) $ bs
    step b (Nothing, []) = (Just b, [b])
    step b (Just b', bs') = (Just b, min b b' : bs')
  print $ sum cs

unsafeSignedDecimal :: T.Text -> Int
unsafeSignedDecimal s = case T.signed T.decimal s of
  Right (n, _) -> n
