N = gets.to_i
H = gets.split.map(&:to_i)

def calc(ar)
  if ar.empty?
    0
  elsif ar.size == 1
    ar[0]
  else  
    m = ar.min
    ar.inject([[]]) {|br, x|
      if x == m
        br << []
      else  
        br.last << x - m 
      end
      br
    }.map{|cr| calc(cr)}.inject(:+) + m
  end
end 

puts calc(H)