object Main extends App {
  val n = scala.io.StdIn.readInt()
  val aInput = scala.io.StdIn.readLine()
  val aArray = aInput.split(" ").map(_.toLong).toVector

  var beforeStep = -1L
  var result = 0L

  aArray.foreach { a =>
    var updatedA = a
    if (beforeStep > 0L) {
      val sabun = beforeStep - a
      if (sabun > 0) {
        result = result + sabun
        updatedA = updatedA + sabun
      }
    }
    beforeStep = updatedA
  }
  println(result)
}