import scala.io.Source

object Main extends App {
  case class State[S, A](run: S => (S, A)) {
    def map[B](f: A => B): State[S, B] = flatMap(a => State.pure(f(a)))
    def flatMap[B](f: A => State[S, B]): State[S, B] = State { s =>
      val (s2, a) = run(s)
      f(a).run(s2)
    }
  }
  object State {
    def pure[S, A](a: A): State[S, A] = State[S, A](s => (s, a))
  }

  def fact(a: Int): State[(Int, Int), Int] = State { s =>
    val next = (Math.max(s._1, a - s._2), Math.min(s._2, a))
    (next, a)
  }

  implicit class StreamOps[S, A](stream: Stream[State[S, A]]) {
    def sequence: State[S, Stream[A]] =
      stream.foldRight(State.pure[S, Stream[A]](Stream.empty[A])) { (a: State[S, A], acc: State[S, Stream[A]]) =>
        a.flatMap(sa => acc.map(sa #:: _))
      }
  }

  val lines = Source.stdin.getLines()
  val n     = lines.next().toInt

  val list: State[(Int, Int), Stream[Int]] =
    lines.toStream
      .take(n)
      .map(_.toInt)
      .map(fact)
      .sequence

  println(list.run((Integer.MIN_VALUE, Integer.MAX_VALUE))._1._1)
}

