import scala.io.StdIn.readLong
object Main {
    def main(args: Array[String]): Unit = {
        val len = readLong
        val list = (1l to len).toList.map(x => readLong)
        println(maximumProfit(list)._2)
    }
    
    def maximumProfit(list: List[Long]): (Long, Long) = {
        def go(list: List[Long], min: Long, maxDiff: Long): (Long, Long) = list match {
            case x :: xs if x < min && (x - min) > maxDiff => go(xs, x, x - min)
            case x :: xs if x < min => go(xs, x, maxDiff)
            case x :: xs if (x - min) > maxDiff => go(xs, min, x - min)
            case x :: xs => go(xs, min, maxDiff)
            case _ => (min, maxDiff)
        }
        list match {
            case x :: xs => xs match { 
                case y :: ys => go(xs, x, y - x)
                case _ => (0l, 0l)
            }
            case _ => (0l, 0l)
        }
    }
}
