import Foundation
func readInts() -> [Int] { readLine()!.split(separator: " ").map { Int($0)! } }

let n = Int(readLine()!)!
var ai = readInts()
var ans = 0
for i in 1..<n {
  if ai[i-1] > ai[i] {
    let d = ai[i-1] - ai[i]
    ans += d
    ai[i] += d
  }
}
print(ans)