def recursion(ary)
  counter = 0
  while ary.min != 0 do
    ary.map! { |x|  x-= 1 }
    counter += 1
  end
  if ary.first != 0 && ary.last != 0
    ary1 = ary[0..(ary.index(0) - 1)]
    counter += recursion(ary1)
    ary2 = ary[(ary.index(0) + 1)..-1]
    counter += recursion(ary2)
  elsif ary.length == 1
    return counter
  elsif ary.first == 0
    ary.shift
    counter += recursion(ary)
  elsif ary.last == 0
    ary.pop
    counter += recursion(ary)
  end
  counter
end
def main()
  n = gets.chomp.to_i
  h = gets.chomp.strip.split(' ').map(&:to_i)
  puts "#{recursion(h)}"
end
main