import Foundation

let n = Int(readLine()!)!
let scanner = Scanner(string: readLine()!)
let a = (0..<n).map { _ in scanner.scanInt()! }

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

// for _ in 1..<n {
//   let num = scanner.scanInt()!
for num in a {
  if min > num {
    acc += min - num
  } else {
    min = num
  }
}

print(acc)
