import io.StdIn.{readInt, readLine}
import math.max
object Main extends App {
  val n = readInt
  val as = readLine.split(" ").map(_.toLong)

  def traverse(list: List[Long], max_tmp: Long, sum: Long): Long = list match {
    case Nil  => sum
    case head::tail => traverse(tail, max(max_tmp, head), sum+max(0, max_tmp-head))
  }

  val result = traverse(as.toList, 0, 0)
  println(result)
}