fun main(args : Array<String>) {
    val N = readLine()!!.toInt()
    val h = readLine()!!.split(" ").map { it.toInt() }.toMutableList()
    // Let's think we want to decrement all h with the process.

    // while all h is not zero

    var answer = 0

    // Find a window which doesn't contains 0 in a row
    while (true) {
        var index = 0
        // find start index
        while (index < N && h[index] == 0) {
            index++
        }
        val startIndex = index // inclusive
        if (startIndex == N) {
            break
        }

        while (index < N && h[index] != 0) {
            index++
        }
        val endIndex = index // exclusive

        answer++
        (startIndex..(endIndex-1)).forEach {
            h[it]--
        }
    }

    println(answer)
}
