def f(list = [])
    sum = 0
    k = 0
    slice_list = []
    
    num_watering = list.min
    list.map!{|i| i - num_watering}
    sum += num_watering
    
    while list.size > 0 do
        if list[k] == 0 and k == 0
            list.shift
        
        elsif list[k] == 0
            sum += f(list.shift(k))
            list.shift
            k = 0
            
        elsif k == list.size-1
            sum += f(list)
            break
        else
            k+=1
        end
    end
    return sum
end




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

p f(list)