import scala.io.Source

object Main {
//  def calcMaxProfit(numbers: Seq[Int]): Int = {
//    val maxProfit: Int = (1 to numbers.size-1)
//      .toStream
//      .map{i =>
//        val a  = numbers(i-1)
//        val bs = numbers.toStream.drop(i)
//        bs.max - a
//      }
//      .max
//    maxProfit
//  }

  def calcMaxProfit(numbers: Seq[Int]): Int = {
    var min       = numbers(0)
    var maxProfit = numbers(1) - min
    for(i <- 1 until numbers.size) {
      maxProfit = math.max(maxProfit, numbers(i) - min)
      min = math.min(min, numbers(i))
    }
    maxProfit
  }


  def main(args: Array[String]): Unit = {
    // Get interator of stdin
    val stdinIterator = Source.stdin.getLines()

    // Skip the first line
    stdinIterator.next()

    // Get input
    val numbers: Seq[Int] = stdinIterator.toSeq.map(_.toInt).toArray

    // Calc max profit
    val maxProfit = calcMaxProfit(numbers)

    // Print max profit
    println(maxProfit)
  }
}
