import java.util.*

fun main(args : Array<String>) {
    val n = readLine()!!.toInt()
    val hh = readLine()!!.split(" ").map { it.toInt() }
    val ff = IntArray(n){0}

    val stack = Stack<Pair<Int, Int>>()
    stack.push(Pair(0, n-1))

    var cnt = 0L

    while (stack.isNotEmpty()) {
        cnt++
        val seg = stack.pop()
        var sIdx = seg.first
        var eIdx = seg.first

        for (i in seg.first..seg.second) {
            if (ff[i] < hh[i]) {
                ff[i]++
                eIdx = i
            } else {
                if (i == sIdx) {
                    sIdx++
                } else {
                    stack.push(Pair(sIdx, eIdx))
                    sIdx = i+1
                }
            }
        }

        if (sIdx <= seg.second) {
            stack.push(Pair(sIdx, eIdx))
        }
    }

    println(cnt-1)
}