let _ = Int(readLine()!)!
let A = readLine()!.split(separator: " ").compactMap { Int($0) }
var result = 0
var minimum = A[0]
for i in 1..<A.count {
    let diff = A[i] - minimum
    if diff < 0 {
        result += -diff
    } else {
        minimum = A[i]
    }
}
print(result)
