import scala.util.control.TailCalls._

object Main {
  def main(args: Array[String]): Unit = {
    val n = io.StdIn.readInt()
    val h = io.StdIn.readLine().split(" ").map(_.toInt).toVector

    def count(vector: Vector[Int], acc: Int): TailRec[Int] = {
      if (vector.isEmpty) done(acc)
      else if (vector.forall(_ > 0)) tailcall(count(vector.map(_ - 1), acc + 1))
      else if (vector.forall(_ == 0)) done(acc)
      else {
        val idx = vector.indexOf(0)
        for {
          x <- tailcall(count(vector.take(idx), 0))
          y <- tailcall(count(vector.drop(idx + 1), acc))
        } yield x + y
      }
    }

    println(count(h, 0).result)
  }
}
