fun main(){

    val n = readLine()!!.toInt()

    val list = readLine()!!.split(" ").map(String::toInt)

    var cnt = 0

    for (i in 0..list.size-2){
        if (list[i]<list[i+1]){
            cnt += list[i+1] - list[i]
        }
    }

    println(cnt)

}