fun main(args: Array<String>) {
    abc116c()
}

fun abc116c() {
    // 愚直にやってみる 全体が1以上だったらその分引いて、1以上のところで左右に分割、それを再帰的に処理
    val n = readLine()!!.toInt()
    val heights = readLine()!!.split(" ").map { it.toInt() }
    
    println(calc(heights))
}

fun calc(heights: List<Int>): Int {
    if (heights.isEmpty()) return 0

    val max = heights.max()!!
    val min = heights.min()!!

    if (max == 0) return 0
    if (max == min) return max

    val remains = heights.map { it - min }

    val left = remains.subList(0, remains.indexOf(0))
    val right = remains.subList(remains.indexOf(0) + 1, remains.size)

    return calc(left) + calc(right) + min

}
