object Main extends App {
  val in = scala.io.Source.stdin.getLines.toSeq
  val ls = in.tail.head.split(' ').map(_.toLong).toArray
  
  @scala.annotation.tailrec
  private def func(count:Int):Int = {    
    
    val l = ls.indexWhere(_ > 0)
    if(l == -1) return count
    
    val tmp = ls.drop(l).indexWhere(_ == 0)
    var r = if(tmp == -1) ls.size-1 else l + tmp-1
    
    //println(l, r)
    (l to r).foreach{ i =>
      ls(i) -= 1
    }
    func(count+1)
  }
  val ret = func(0)
  println(ret)
}
