
import scala.io.StdIn

object Main extends App {
  StdIn.readLine()
  val heights=StdIn.readLine().split(' ').map(_.toLong)

  var humidai:Long=0
  var highest:Long=0
  for(h <- heights){
    if(h < highest){
      humidai = humidai + highest-h
    }
    if(h>highest){
      highest=h
    }

  }
  println(humidai)
}






