object Main {
  def main(args: Array[String]): Unit = {
    val num     = scala.io.StdIn.readInt
    val heights = scala.io.StdIn.readLine.split( " " ).map{ _.toLong }

    var total_step_height = 0L
    var max_height        = 0L

    heights.foreach{ i =>
      if( max_height > i ) {
        total_step_height += max_height - i
      }
      else {
        max_height = i
      }
    }

    println( total_step_height )

  }
}