import Foundation

let rawData = FileHandle.standardInput.availableData
let scanner = Scanner(string: String(data: rawData, encoding: .utf8)!)

let n = scanner.scanInt()!

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

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

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

print(acc)
