import scala.io.StdIn._

object Main extends App {

  val n = readInt()
  val A = readLine().split(" ").map(_.toInt)

  def solve() = {
    val maxAndSum = (0L,0L)
    A.foldLeft(maxAndSum) { case ((max, sum), a) =>
      val add = if(max > a) max - a else 0
      (Math.max(max, a), sum + add)
    }._2
  }

  println(solve())

}

