object Main extends App {
  val sc = new java.util.Scanner(System.in)
  val n = sc.nextInt()
  val as = List.fill(n)(sc.nextInt())
  var max = as.head
  var ans = 0L
  for (a <- as) {
    if (a < max) {
      ans += max - a
    } else {
      max = a
    }
  }

  println(ans)
}
