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

ans = 0
l = 0
r = 0
while l = h.find_index{ |i| i != 0 } do
  m = 101
  l.upto(n - 1) do |i|
    v = h[i]
    break if v == 0 
    r = i
    m = v if v < m   
  end
  h.fill(l..r) do |i|
    h[i] - m
  end
  ans += m
end
puts ans

