n = gets.to_i
array = gets.split(' ').map(&:to_i)

if n == 1
  puts 0
else
  sum = 0
  (0..n - 2).each do |i|
    if array[i + 1] - array[i] > 0
      sum += array[i + 1] - array[i]
    end
  end
  puts sum
end