let n = Int(readLine()!)!
let a = readLine()!.split(separator: " ").map(String.init).lazy.map { Int($0)! }

var acc = 0
var min = 0

for num in a {
  if min > num {
    acc = acc &+ (min - num)
  } else {
    min = num
  }
}

print(acc)
