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)

use std::io::*;
use std::str::FromStr;
#[allow(unused_imports)]
use std::collections::*;

struct Scanner<R: Read> {
    reader: R,
}

#[allow(dead_code)]
impl<R: Read> Scanner<R> {
    fn new(reader: R) -> Scanner<R> {
        Scanner { reader: reader }
    }

    fn safe_read<T: FromStr>(&mut self) -> Option<T> {
        let token = self.reader
            .by_ref()
            .bytes()
            .map(|c| c.unwrap() as char)
            .skip_while(|c| c.is_whitespace())
            .take_while(|c| !c.is_whitespace())
            .collect::<String>();
        if token.is_empty() {
            None
        } else {
            token.parse::<T>().ok()
        }
    }

    fn read<T: FromStr>(&mut self) -> T {
        if let Some(s) = self.safe_read() {
            s
        } else {
            writeln!(std::io::stderr(), "Terminated with EOF").unwrap();
            std::process::exit(0);
        }
    }

    fn vec<T: FromStr>(&mut self, len: usize) -> Vec<T> {
        (0..len).map(|_| self.read()).collect()
    }

    fn mat<T: FromStr>(&mut self, row: usize, col: usize) -> Vec<Vec<T>> {
        (0..row).map(|_| self.vec(col)).collect()
    }
}

trait Joinable {
    fn join(self, sep: &str) -> String;
}

impl<U: ToString, T: Iterator<Item = U>> Joinable for T {
    fn join(self, sep: &str) -> String {
        self.map(|x| x.to_string()).collect::<Vec<_>>().join(sep)
    }
}

#[allow(unused_imports)]
use std::cmp::{min, max};

const MOD: u64 = 1_000_000_007;

fn modpow(a: u64, b: u64) -> u64 {
    let mut a = a;
    let mut b = b;
    let mut res = 1;
    while b != 0 {
        if b & 1 != 0 {
            res = res * a % MOD;
        }
        a = a * a % MOD;
        b /= 2;
    }
    res
}

fn is_prime(n: u32) -> bool {
    if n <= 1 {
        false
    } else if n == 2 || n == 3 || n == 5 {
        true
    } else if n % 2 == 0 || n % 3 == 0 || n % 5 == 0 {
        false
    } else {
        for i in 2..n {
            if i * i > n {
                break;
            }
            if n % i == 0 {
                return false;
            }
        }
        true
    }
}

fn is_prime_table(n: usize) -> Vec<bool> {
    let mut is_prime = vec![true; n + 1];
    is_prime[0] = false;
    is_prime[1] = false;
    for i in 2..n+1 {
        unsafe {
            if *is_prime.get_unchecked(i) {
                let mut j = i * i;
                while j <= n {
                    *is_prime.get_unchecked_mut(j) = false;
                    j += i;
                }
            }
        }
    }
    is_prime
}

fn prime_table(n: usize) -> Vec<u64> {
    let is_prime = is_prime_table(n);
    is_prime.iter().enumerate()
        .filter(|&(_, &isp)| isp).map(|(i, _)| i as u64)
        .collect()
}


fn main() {
    std::thread::Builder::new()
        .stack_size(104_857_600)
        .spawn(solve)
        .unwrap()
        .join()
        .unwrap();
}

fn s(x: i64) -> i64 {
    if x == 0 {
        0
    } else {
        x / x.abs()
    }
}

fn func(a: &Vec<i64>, sign: i64) -> u64 {
    let mut sign = sign;
    let mut ans = 0;
    let mut acc = 0;
    for &x in a.iter() {
        if s(acc + x) == sign {
            acc += x;
        } else {
            ans += (acc + x - sign).abs() as u64;
            acc = sign;
        }
        sign *= -1;
    }
    ans
}

fn solve() {
    let cin = stdin();
    let cin = cin.lock();
    let mut sc = Scanner::new(cin);

    loop {
        let n = sc.read();
        let a: Vec<i64> = sc.vec(n);
        let ans = func(&a, 1);
        let ans = min(ans, func(&a, -1));
        println!("{}", ans);
    }
}