let n = Int(readLine()!)!

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

var sum = 0
var maxHeight = 0

for height in a {
  maxHeight = max(maxHeight, height)
  sum += maxHeight - height
}
print(sum)