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

@count = 0


def calc(s, e)
  p [s, e]
  return if @h[s..e].all?{|a| a == 0}
  min = @h[s..e].min
  (s..e).each{|i| @h[i] -= min}
  @count += min
  l = -1

  (s..e).each do |i|
    if (i == e)
      if (@h[e] != 0) && (l == -1)
        @count += @h[e]
      elsif @h[e] != 0
        calc(l, e)
      elsif l != -1
        calc(l, e - 1)
      end
    else
      if @h[i] == 0
        if l != -1
          calc(l, i - 1)
        end
        l = -1
      else
        l = l == -1 ? i : l
      end
    end
  end
end

calc(0, n - 1)

puts @count