let input = require('fs').readFileSync('/dev/stdin', 'utf8')
// let input = require('fs').readFileSync('./sample_c.txt', 'utf8')
.split(/\n| /)
.map(item => parseInt(item))
.slice(1,-1)

let max = input[0]
let stage = 0
input.slice(1).forEach( item => {
    if(item < max) {
        stage += max - item 
    } else {
        max = item
    }
} )

console.log(stage)