n = gets.to_i
hs = gets.split(" ").map{|n|n.to_i}
ans = 0
cut_start = -1
cut_end = -1
while !hs.all?{|nn|nn == 0}
(0).upto(hs.size - 1) do|n|
  if cut_start == -1
    if(hs[n] > 0)
      cut_start = n;
    end
    if (n == hs.size-1)
    cut_end = n
    (cut_start..cut_end).each do|nn|
      hs[nn] -= 1
    end
    ans += 1
    cut_start = -1
    cut_end = -1
    break;
    end
    next
  end


  # もう水やりしなくていいor端まで来た
  if(hs[n] == 0)
    cut_end = n-1
    (cut_start..cut_end).each do|nn|
      hs[nn] -= 1
    end
    ans += 1
    cut_start = -1
    cut_end = -1
    break;
  end
  if (n == hs.size-1)
  cut_end = n
  (cut_start..cut_end).each do|nn|
    hs[nn] -= 1
  end
  ans += 1
  cut_start = -1
  cut_end = -1
  break;
  end

  
  if(hs[n] > 0)
    next
  end
end
end
puts ans.to_s