import java.util.Scanner

object Main {
  def main(args: Array[String]): Unit = {
    val sc = new Scanner(System.in)
    val n = sc.nextInt()
    val h = Vector.fill(n)(sc.nextInt)

    @scala.annotation.tailrec
    def count(vector: Vector[Int], acc: Int): Int = {
      if (vector.isEmpty) acc
      else if (vector.forall(_ > 0)) count(vector.map(_ - 1), acc + 1)
      else if (vector.forall(_ == 0)) acc
      else {
        val idx = vector.indexOf(0)
        if (idx == 0) count(vector.tail, acc)
        else if (idx == vector.size - 1) count(vector.init, acc)
        else count((vector.take(idx).map(_ - 1) :+ 0) ++ vector.drop(idx + 1), acc + 1)
      }
    }

    println(count(h, 0))
  }
}
