n = gets.to_i

hs = [0] + gets.split.map(&:to_i) + [0]
i = 0
loop do
  if hs.all?{ |h| h <= 0 }
    puts i
    break
  end
  s = 0
  t = 0
  
  (0..n+1).each do |i|
    if s == 0 && hs[i] != hs[i+1]
      s = i + 1
    elsif s != 0 && hs[i] == 0
      t = i
    end
    if s != 0 && t != 0
      break
    end
  end

  dif = hs[s...t].min
  i += dif
  (s...t).each do |j|
    hs[j] -= dif
  end
end