n = gets.to_i
h = gets.split.map(&:to_i)
t = [0]*n
l,ans = 0,0
while l != n
  l = 0
  while l < n && h[l] == t[l]
    l += 1
  end
  r = l
  while r < n && h[r] > t[r]
    r += 1
  end
  l.upto(r-1) do |i|
    t[i] += 1
  end
  ans += 1
end
p ans-1