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 C (GCC 5.4.1) (judge verdict: Runtime Error)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N 100000
#define MAX 1000000

int main(void) {
  int n;  //数列の長さ（要素の数）
  long sum1 = 0;  //奇数番目の要素の合計値
  long sum2 = 0;  //偶数番目の要素の合計値
  long tmp; //1, -1の繰り返し計算用temp
  long lcount = 0;  //奇数+, 偶数-のパターン
  long rcount = 0;  //奇数-, 偶数+のパターン
  long a[N]; //入力各要素（数字）
  char input[MAX];  //入力文字列
  int i = 0,j = 0;  //for文用（jは使ってない）
  int cp = 0,tcp = 0; //tp, input用の何文字目かを示すカウンタ
  char tp[12];  //入力処理用のtemp
  tp[12] = '\0';  //初期化
  fgets(input, MAX, stdin);
  n = atoi(input);  //nに入力を受け取る

  fgets(input, MAX, stdin);  //各要素を受け取る

  //printf("%s\n",input);
  for(i = 0; i < n;  i++){
    while(input[cp] != ' ' && input[cp] != '\n'){ //スペースまで入力を切る
      tp[tcp] = input[cp];
      tcp++;
      cp++;
    }
    tp[tcp] = '\0';
    tcp = 0;
    cp++;
    a[i] = atoi(tp);
    //printf("i:%d tp:%s a[%d]:%d\n",i,tp,i,a[i]);

  }
  //init
/*  for(i = 0; i < n; i++){ //要素を偶奇番目毎に足し合わせる
    if(i % 2 == 0) sum2 += a[i];
    else sum1 += a[i];
    //printf("%d %d %d %d\n",i,a[i],sum1,sum2 );
  }*/
  tmp = a[0];
/*  if(sum1 == sum2){
    if(a[0] < 0){
      sum1++;
    }else{
      sum2++;
    }
  }*/
  for(i = 1; i < n; i++){ //偶数番目が-1以下になるまで引いて操作カウントをインクリメント
      if(i % 2 == 0){
        tmp += a[i];
        if(tmp > -1){
          lcount += tmp + 1;
          tmp = -1;
          //printf("hoge1\n" );
        }
      } else {  //奇数番目が1以上になるまで足して操作カウントをインクリメント
        tmp += a[i];
        if(tmp < 1){
          lcount += 1 - tmp;
          tmp = 1;
          //printf("hoge2\n" );
        }
      }
  }
  tmp = a[0];
  for(i = 1; i < n; i++){ //奇数番目が-1以下になるまで引いて操作カウントをインクリメント
      if(i % 2 == 1){
        tmp += a[i];
        if(tmp > -1){
          rcount += tmp + 1;
          tmp = -1;
          //printf("hoge3\n" );
        }
      } else {  //偶数番目が1以上になるまで足して操作カウントをインクリメント
        tmp += a[i];
        if(tmp < 1){
          rcount += 1 - tmp;
          tmp = 1;
          //printf("hoge4\n" );
        }
      }
  }
  printf("%ld\n",lcount > rcount?rcount:lcount);  //l>rならr, そうでないならl（rの砲方が小さいならr）
  return 0;
}