lines = []
while line = gets
    lines << line.chomp.split(' ').map(&:to_i)
end

N = lines[0][0]

current_max = 0
ans = 0
for i in 1..N do
  if lines[1][i-1] < current_max then
    ans += current_max - lines[1][i-1]
  else
    current_max = lines[1][i-1]
  end
end
  
puts ans