fun main(args: Array<String>) {
    val nLine = readLine()!!.toInt()
    val values = arrayOfNulls<Int>(nLine)
    for (i in 0..nLine-1) {
        val value = readLine()!!.toInt()
        values[i] = value
    }

    val maxValues = arrayOfNulls<Int>(nLine-1)
    val minValues = arrayOfNulls<Int>(nLine-1)
    val diffValues = arrayOfNulls<Int>(nLine-1)

    var currentMax = values[nLine-1]!!
    for (i in 0..nLine-2) {
        val startIndex = nLine-1-i
        val currentValue = values[startIndex]!!
        currentMax = Math.max(currentMax, currentValue)
        maxValues[startIndex-1] = currentMax
    }

    var currentMin = values[0]!!
    for (i in 0..nLine-2) {
        val currentValue = values[i]!!
        currentMin = Math.min(currentMin, currentValue)
        minValues[i] = currentMin
    }

    for (i in 0..nLine-2) {
        diffValues[i] = maxValues[i]!! - minValues[i]!!
    }

    var maxDiff = Int.MIN_VALUE
    for (i in 0..nLine-2) {
        maxDiff = Math.max(maxDiff, diffValues[i]!!)
    }

    println(maxDiff)
}
