import java.io.BufferedReader
import java.io.InputStreamReader
import java.io.PrintWriter
import kotlin.math.max

object Solve {

  private val br = BufferedReader(InputStreamReader(System.`in`))
  private val out = PrintWriter(System.out)

  fun solve() {

    var n = br.readLine()!!.toInt()
    var a = br.readLine()!!.split(' ').map { it.toLong() }

    var maxA = 0L
    var ans = 0L
    for (len in a) {
      if(len < maxA) {
        ans += maxA - len
      }
      maxA = max(maxA, len)
    }

    out.println(ans)
    out.flush()
  }
}

fun main(args: Array<String>) {
  Solve.solve()
}