fun main(args: Array<String>) {
    val n = readInputLine().toInt()
    val hs = readInputLine().split(" ").map{it.toInt()}.toList()
    
    println(dfs(hs, 0))
}

fun readInputLine(): String {
    return readLine()!!
}

fun split(list: List<Int>, height: Int): List<List<Int>> {
    val ret = mutableListOf<List<Int>>()
    
    var l = -1
    var splitting = false
    
    for ((i, e) in list.withIndex()) {
        if (!splitting && e > height) {
            l = i
            splitting = true
        } else if (splitting && e == height) {
            ret.add(list.subList(l, i))
            splitting = false
        }
    }
    
    if (splitting) {
        ret.add(list.subList(l, list.size))
    }
    
    return ret
}

fun dfs(list:List<Int>, height:Int): Int {
    var ret = 0
    val splitted = split(list, height)
    
    for (l in splitted) {
        ret += dfs(l, height + 1) + 1
    }
    
    return ret
}
