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

def split(ary)
  s = []
  a = []
  ary.each do |e|
    if e == 0
      s.push(a) unless a.empty?
      a = []
    else
      a.push(e)
    end
  end
  s.push(a)
  s
end

def dfs(ary, sum)
  return 0 if ary.empty?
  return sum if ary.all? { |e| e == 0 }
  if ary.any? { |e| e == 0 }
    split(ary).map { |a| dfs(a, 0) }.inject(:+) + sum
  else
    water = ary.min
    ary = ary.map{ |e| e - water }
    sum += water
    dfs(ary, sum)
  end
end

puts dfs(h, 0)
