fun main(args: Array<String>): Unit {
    val sc = java.util.Scanner(System.`in`)
    val n = sc.nextInt()
    val a = sc.asSequence().take(n).map { it.toInt() }.toMutableList()

    var r = 0
    for (i in 0 until n) {
        while (true) {
            val p = a[i]
            if (p == 0) {
                break
            }
            r++
            (i until n).takeWhile { a[it] >= p }.forEach { a[it]-- }
       }
    }
    println(r)
}