gets
a = gets.split.map &:to_i

escapable = [0] * a.size

block = 0
(0..a.size-1).each do |i|
	x = a[i]
	if x < 0
		block = [block, x.abs].min
	elsif x > 0
		e = [x, block].min
		escapable[i] = e if e > escapable[i]
	else
		block = Float::INFINITY
	end
	block -= 1
end

block = 0
(a.size-1).step(0, -1) do |i|
	x = a[i]
	if x < 0
		block = [block, x.abs].min
	elsif x > 0
		e = [x, block].min
		escapable[i] = e if e > escapable[i]
	else
		block = Float::INFINITY
	end
	block -= 1
end

p escapable.inject(:+)