import java.util.Scanner;

fun main (args : Array<String>) {
  val input = Scanner(System.`in`)
  
  while (input.hasNext()) {
    val N = input.nextInt()
    val A = IntArray(N)
    
    for (i in A.indices) {
      A[i] = input.nextInt()
    }
    
    var stoolHeightSum = 0L
    for (i in 1..A.lastIndex) {
      if (A[i] < A[i - 1]) {
        stoolHeightSum += A[i - 1] - A[i]
        A[i] = A[i - 1]
      }
    }
    
    println(stoolHeightSum)
  } 
}