n = gets.to_i
f = gets.split.map(&:to_i)
m = f.min
f = f.map{|x| x - m}
count = 0
if m != 0
  count += m
end
until f.empty?
  m = 0
  i = f.index(0)
  j = 0
  arr = []
  while i > j
    arr << f.shift
     j+=1
  end
  f.shift
  m = arr.min unless arr.empty?
  count += m
  arr.map!{|x| x-m}
  if arr.max != 0
    if f[-1] != 0
      f << 0
    end
    f << arr
  end
  f.flatten!
end

p count