n = gets.chop.to_i
h = gets.chop.split.map(&:to_i)

f = []
n.times { f << 0}

ans = 0
tmp = []

while 1 do
  break if h == f

  for i in 0..n-1 do
    tmp << i unless h[i] == f[i]

    if (h[i] == f[i] || i == n-1) && !tmp.empty? then
      ans +=1
      tmp.each {|k| f[k] +=1 }
      tmp = []
    end
  end
end

puts ans
