object Main extends App {
  
  val sc = new java.util.Scanner(System.in)
  sc.useDelimiter("\n")
  val N = sc.nextInt()
  val A = sc.next().split(" ").map(_.toLong)
  var step: List[Int] = List()
  val ans = for ((a, i) <- A.zipWithIndex) yield {
    if (i == 0 || A(i-1)+step(i-1) <= a) {
      step = step :+ 0
    } else {
      step = step :+ A(i-1)+step(i-1) - a
    }
  }
  println(step.sum)
 
}