n = gets.chomp.to_i
h = gets.chomp.split(' ').map(&:to_i)
max = -1
result = 0
for i in 0..n-1 do
  if (h[i] >= max)
    max = h[i]
  else
    result += max
    result -= h[i]
    max = h[i]
  end
end
puts result+max