
object Main extends App {
  
  val sc = new java.util.Scanner(System.in)
  val N = sc.nextInt()
  val step = scala.collection.mutable.ListBuffer.empty[Long]
  val A = List.fill(N)(sc.nextLong())
  for ((a, i) <- A.zipWithIndex) yield {
    if (i == 0) {
      step += 0
    } else {
      val prev = A(i-1)+step(i-1)
      if (prev <= a) {
        step += 0
      } else {
        step += prev - a
      }
    }
  }
  println(step.sum)  

}
