N = gets.to_i
h = gets.split.map(&:to_i)

count = 0
while true do
  ok = false
  h.each_with_index do |h_d, idx|
    if h_d > 0
      break
    end
    if idx == h.size - 1
      ok = true
    end
  end
  if ok
    break
  end

  if h.min > 0
    h.each_index do |idx|
      h[idx] += -1
    end
  else
    start_idx = 0
    end_idx = N - 1

    h.each_index do |idx|
      if h[idx] != 0
        start_idx = idx
        break
      end
    end
    for idx in start_idx + 1 .. N - 1
      if h[idx] == 0
        end_idx = idx - 1
        break
      end
    end
    for idx in start_idx .. end_idx
      h[idx] += -1
    end
  end

  count += 1
end

puts count