const input = require("fs").readFileSync("/dev/stdin", "utf8")

const lines = input.split('\n')
let N = lines[0] - 0
let A = lines[1].split(' ').map(Number)

let max = A[0]
let sum = 0
for (let i = 1; i < N; i++) {
    if (max < A[i]) {
        max = A[i]
    } else if (max > A[i]) {
        sum += max - A[i]
    }
}
console.log(sum)