require 'pp'

n = gets.to_i
a_n = gets.split.map(&:to_i)

i = 1
sum = 0
while i < n
    if a_n[i-1] < a_n[i]
       diff = a_n[i] - a_n[i-1]
       sum += diff
       a_n[i] += diff
    end
    i += 1
end

puts sum
