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
  diff.each_with_index do |v, index|
    if start == -1 && v > 0
      start = index
      next
    else
      if finish == -1 && v == 0
        finish = index - 1
      end
    end
  end
  if start != -1
    finish = N - 1 if finish != -1
    for i in start..finish
      diff[i] -= 1
    end
    count += 1
  else
    break
  end
end
 
p count