Problem:
Score : 300 points

Problem Statement

You are given an integer sequence of length N. The i-th term in the sequence is a_i.
In one operation, you can select a term and either increment or decrement it by one.

At least how many operations are necessary to satisfy the following conditions?

For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.

For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.

Constraints

2 ≤ n ≤ 10^5

|a_i| ≤ 10^9

Each a_i is an integer.

Input

Input is given from Standard Input in the following format:

n
a_1 a_2 ... a_n

Output

Print the minimum necessary count of operations.

Sample Input 1

4
1 -3 1 0

Sample Output 1

4

For example, the given sequence can be transformed into 1, -2, 2, -2 by four operations. The sums of the first one, two, three and four terms are 1, -1, 1 and -1, respectively, which satisfy the conditions.

Sample Input 2

5
3 -6 4 -5 7

Sample Output 2

0

The given sequence already satisfies the conditions.

Sample Input 3

6
-1 4 3 2 -5 4

Sample Output 3

8

Solution in Rust (1.15.1) (judge verdict: Accepted)

#![allow(unused_parens)]
#![allow(unused_imports)]
#![allow(non_upper_case_globals)]


macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let mut s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($next:expr, [$t:tt]) => {
        {
            let len = read_value!($next, usize);
            (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
        }
    };
    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}


#[cfg(debug_assertions)]
macro_rules! mydbg {
    //($arg:expr) => (dbg!($arg))
    ($arg:expr) => (println!("{:?}",$arg));
}


#[cfg(not(debug_assertions))]
macro_rules! mydbg {
    ($($arg:expr),*) => {};
}


use std::cmp;
use std::collections::HashMap;



use std::ops::{ Add, Sub , Div , Mul };
static global_mod : i64 = 1000000007;

#[derive(Debug,Copy,Clone)]
pub struct ModInt {
    x : i64
}

impl ModInt{
    pub fn  new (p:i64) -> Self {
        let a = (p % global_mod + global_mod)% global_mod;     
        return ModInt{x:a};
    }

    pub fn inv (self) -> Self{
        return self.pow(global_mod-2);

    }

    pub fn pow (self , t:i64) -> Self{
        if(t==0 ) {return ModInt::new(1)};
        let mut a = self.pow(t>>1);
        a = a*a;
        if(t&1 != 0) {a = a* self};
        return a;
    }
}
impl Add for ModInt {
    type Output = ModInt;
    fn add(self,other : ModInt) -> ModInt{
        let ret  = self.x + other.x ;
        return ModInt::new(ret);
    }
}
impl Sub for ModInt {
    type Output = ModInt;
    fn sub(self,other : ModInt) -> ModInt{
        let ret  = self.x - other.x;
        return ModInt::new(ret);
    }
}
impl Mul for ModInt {
    type Output = ModInt;
    fn mul(self,other : ModInt) -> ModInt{
        let ret  = self.x * other.x ;
        return ModInt::new(ret);
    }
}

impl Div for ModInt {
    type Output = ModInt;
    fn div(self,other : ModInt) -> ModInt{
        let ret  = self.x * other.inv().x;
       return ModInt::new(ret);
    }
}

impl std::string::ToString for ModInt{
    fn to_string(&self) -> String{
        return self.x.to_string();
    }
}


pub struct Combination{
    fact : Vec<ModInt>,
    ifact : Vec<ModInt>
}

impl Combination{
    pub fn new(n:i32)->Self{
        let mut fact = vec![ModInt::new(0);(n+1) as usize];
        let mut ifact = vec![ModInt::new(0);(n+1) as usize];
        fact[0] = ModInt::new(1);
        for i in 1..n+1 { fact[i as usize] = fact[(i-1) as usize] * ModInt::new(i as i64) };
        ifact[n as usize] = fact[n as usize].inv();
        for i in (1..n+1).rev(){ ifact[(i-1) as usize] = ifact[i as usize]* ModInt::new(i as i64) }


        let a = Combination{
            fact : fact,
            ifact : ifact
        };
        return a;
    }

    pub fn gen(self , n:i32 , k:i32)-> ModInt{
        mydbg!(self.fact[n as usize]);
        mydbg!(self.ifact[k as usize]);
        mydbg!(self.ifact[(n-k) as usize]);
        if (k < 0 || k > n) {return ModInt::new(0 as i64)};
        return self.fact[n as usize]*self.ifact[k as usize]*self.ifact[(n-k) as usize];

    }
}


use std::num;

fn main() {

    input! { 
       n: usize,
       s: [i32;n],
    }

    let mut ans = 0i64;
    let mut ans2 = 0i64;

    let mut temp = vec![0;n];
    let mut temp2 = vec![0;n];

    for (i,item) in s.iter().enumerate(){
        if(i==0){
            temp[i] = s[i];
            temp2[i] = s[i];
        }
        else{
            temp[i] = temp[i-1]+s[i];
            temp2[i] = temp2[i-1]+s[i];
        }
    }
 
 
    let mut p = 0;

    //0番目をプラスにする場合
    for i in 0..n{
        temp[i] += p;

        if i % 2 == 0 {
            if temp[i] < 1{
                let b = 1-temp[i];
                ans += i64::abs(b as i64);
                p += b;
                temp[i] += b;
                mydbg!(b);
            }
        }
        else {
            if temp[i] > -1{
                let b = -1-temp[i];
                ans += i64::abs(b as i64);
                p += b;
                temp[i] += b;
                mydbg!(b);
            }
        }
        
    }

    p=0;
    //0番目をマイナスにする場合
    for i in 0..n{
        temp2[i] += p;

        if i % 2 == 0 {
            if temp2[i] > -1{
                let b = -1-temp2[i];
                ans2 += i64::abs(b as i64);
                p += b;
                temp2[i] += b;
            }
        }
        else {
            if temp2[i] < 1{
                let b = 1-temp2[i];
                ans2 += i64::abs(b as i64);
                p += b;
                temp2[i] += b;
            }
        }
        
    }

    
    mydbg!(temp);
    mydbg!(temp2);

    println!("{}",cmp::min(ans , ans2));


}