_ = gets
heights = [0] + gets.split.map(&:to_i) + [0]
events = []
heights.each_cons(2) do |h1, h2|
  events << [h1, h2 <=> h1]
  events << [h2, h1 <=> h2]
end
events.sort_by!(&:first)
best = k = 0
(heights.uniq - [0]).sort.each do |level|
  k += events.shift[1] while events[0][0] < level
  best = k / 2 if k / 2 > best
end
p best
