n = gets.chomp.to_i
hs = gets.chomp.split(" ").map(&:to_i)

def f(i, hs)
  return puts i if hs.max == 0
  prev = 0
  g = 0
  for k in hs do
    g += 1 if prev == 0 && k != 0
    prev = k
  end
  hss = hs.map{ |b| b <= 0 ? 0 : b - 1 }
  f(i + g, hss)
end

f(0, hs)
