import Foundation

let n = Int(readLine()!)!
let scanner = Scanner(string: readLine()!)

var acc = 0
var min = scanner.scanInt()!

for _ in 1..<n {
  let num = scanner.scanInt()!

  if min > num {
    acc += min - num
  } else {
    min = num
  }
}

print(acc)
