import java.util.Scanner

import scala.annotation.tailrec

object Main extends App {
  val sc = new Scanner(System.in)

  val n = sc.nextInt
  val hl = Seq.fill(n)(sc.nextInt)


  val res = cntLoop(hl)
  println(res)

  @tailrec
  def cntLoop(list: Seq[Int] , cnt: Int = 0): Int = {
    if (list.forall(_ == 0) ) cnt
    else cntLoop(elementMinusOne(list), cnt + separate(list).size)
  }

  @tailrec
  def separate(list: Seq[Int], lists: Seq[Seq[Int]] = Seq.empty): Seq[Seq[Int]] = {
    list match {
      case Nil => lists
      case nonEmpty => {
        val target = nonEmpty.dropWhile(_ == 0)
        val (take, drop) = target.span(_ > 0)
        val next = drop.dropWhile(_ == 0)
        separate(next, lists :+ take)
      }
    }
  }

  def elementMinusOne(list: Seq[Int]): Seq[Int] = {
    list.map{
      case 0 => 0
      case e => e - 1
    }
  }
}