N = gets.to_i
hs = gets.chomp.split(' ').map(&:to_i)
array = []
(N-2).times do |i|
  if hs[i+1]<= hs[i] && hs[i+1] <hs[i+2] 
    array.push(i+1)
  end
end
array.push(N-1)
stt = 0
result = 0
max = 0
hole = 0
array.each do |i|
  max = 0
  hs[stt..i].each do |item|
    if item>max
    max = item
    end
  end
  if i == N-1
    result += max
  else
	result=result+max-hs[i]
  end
  stt = i+1
end
puts result