n = gets                                                                                                                                                                                                                                                                        
a = gets.split.map(&:to_i)                                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                                                                
def change_count(a, r)                                                                                                                                                                                                                                                          
  sum = 0                                                                                                                                                                                                                                                                       
  change_time = 0 
  r = r <=> 0                                                                                                                                                                                                                                                         
  a.each do |x|                                                                                                                                                                                                                                                                 
    sum += x                                                                                                                                                                                                                                                                    
    if r * sum <= 0 # 符号が違ったとき                                                                                                                                                                                                                                           
      change_time += (r - sum).abs # -1 or +1 までの差分が変更回数                                                                                                                                                                                                      
      sum = r # sum が -1 or 1になる                                                                                                                                                                                                                                      
    end                                                                                                                                                                                                                                                                         
    r *= -1 # 次項の符号をセット                                                                                                                                                                                                                                                
  end                                                                                                                                                                                                                                                                           
  change_time                                                                                                                                                                                                                                                                   
end                                                                                                                                                                                                                                                                             
                                                                                                                                                                                                                                                                                
p [change_count(a, 1), change_count(a, -1)].min

Measured memory_bytes: 10628