n = gets.chomp.to_i
talls = gets.chomp.split.map(&:to_i)

back = 0
step = 0

talls.each do |tall|
    if back > tall
        step += back - tall
        back = tall + (back - tall)
    else 
        back = tall
    end
end

puts step