import scala.io.{StdIn => in}
import scala.collection.{mutable => mu}

object Main extends App {
  val n = in.readInt
  val h = in.readLine.split(" ").map(_.toInt)
  val fs = 0 +: h

  def calc(pre:Int, idx:Int):Int = {
    if(!(idx < n) || (pre > 0 && fs(idx+1) == 0)) fs.sum
    else {
      val nx = fs(idx+1)
      if(fs(idx+1) > 0) fs(idx+1) -= 1
      calc(nx, idx+1)
    }
  }
  def rec(acc:Int, cnt:Int):Int = {
    if(acc == 0) cnt
    else {
      rec(calc(0,0),cnt+1)
    }
  }

  val ans = rec(fs.sum,0)
  println(ans)
  // (1 to 5).foreach(i => {calc(0,0);println(fs.mkString(" "))})
}