import java.util.Scanner

object Main extends App {
  val sc = new Scanner(System.in)
  val N = sc.nextInt
  val A = Array.fill(N)(sc.nextLong)

  val z = A.foldLeft((0L, 0L))((z, x) => if (x >= z._2) (z._1, x) else (z._1 + z._2 - x, z._2))._1

  println(z)
}
