let n = readInt()
let a = readArray().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)

@inlinable @inline(__always) func readInt() -> Int { Int(readLine()!)! }
@inlinable @inline(__always) func readArray(_ separator: Character = " ") -> [String] { readLine()!.split(separator: separator).map(String.init) }
