def count(array)
  cnt = 0
  return cnt if array.empty? || array.all? {|t| t == 0 }

  while array.all? {|t| t > 0 }
    array = array.map {|e| e -= 1 }
    cnt += 1
  end

  split = array.index(0)
  len = array.length

  if split == 0
    cnt += count(array[1..len - 1])
  else
    cnt += count(array[0..(split - 1)]) + count(array[(split + 1)..(len - 1)])
  end

  cnt
end

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

puts count(h)