fun main(args:Array<String>) {
    val hanaCount = readLine()!!.toInt()
    val hana = mutableListOf(readLine()!!.split(" ").map { it.toInt() }.toMutableList())
    var current = conv(hana)

    var ans = 0
    while (current.isNotEmpty()) {
        ans += current.size
        current = current.map { a-> a.map { it - 1 }.toMutableList() }.toMutableList()
        current = conv(current)
    }
    println(ans)
}

fun conv(list:MutableList<MutableList<Int>>):MutableList<MutableList<Int>> {
    val ret = mutableListOf<MutableList<Int>>()

    list.forEach{a->
        var temp = mutableListOf<Int>()
        for(i in a) {
            if(i <= 0) {
                if(temp.isNotEmpty()) {
                    ret.add(temp)
                    temp = mutableListOf()
                }
            } else {
                temp.add(i)
            }
        }
        if(temp.isNotEmpty()) {
            ret.add(temp)
        }
    }
    return ret
}
