object Main extends App {
  val n = readInt
  val bs = readLine.split(" ").map(_.toInt) :+ 100007
  println( bs.scanLeft((100007,100007))((acc,x) => (x, math.min(acc._1,x)))
            .foldLeft(-100007)((acc,x) => acc+x._2) )
}