import Foundation

let input1 = readLine()!
let input2 = readLine()!
let n = Int(input1)!
let a = input2.components(separatedBy: " ").map { Int($0)! }
var current = a[0]
var res = 0
for i in (1..<n) {
  if (a[i] < current) {
    res += (current - a[i])
  } else {
    current = a[i]
  }
}
print(res)