N = gets.to_i
h = gets.split.map(&:to_i)
max = h.max
diff = h.map do |v|
  max - v
end

count = 0

loop do
  start = -1
  finish = -1
  h.each_with_index do |v, index|
    if start == -1
      start = index
      next
    else
      if finish == -1 && v > 0
        finish = index
      end
    end
  end
  if start != -1 && finish != -1
    for i in start..finish
      diff[i] -= 1
    end
  else
    break
  end
end

p count