let n = readInt()
let a = readIntArray().lazy

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 readString() -> String { readLine()! }
@inlinable @inline(__always) func readInt   () -> Int    { Int(readString())! }
@inlinable @inline(__always) func readDouble() -> Double { Double(readString())! }

// .map(String.init) を挟まないと [String.SubSequence] になって遅くなる（は？）
@inlinable @inline(__always) func readStringArray(_ separator: Character = " ") -> [String] { readString().split(separator: separator).map(String.init) }
@inlinable @inline(__always) func readIntArray   (_ separator: Character = " ") -> [Int]    { readStringArray(separator).map { Int($0)! } }
@inlinable @inline(__always) func readDoubleArray(_ separator: Character = " ") -> [Double] { readStringArray(separator).map { Double($0)! } }
