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)) { case ((sum, prevHeight), height) => if (height >= prevHeight) (sum, height) else (sum + prevHeight - height, prevHeight)) }._1

  println(z)
}