Search is not available for this dataset
name
stringlengths 2
88
| description
stringlengths 31
8.62k
| public_tests
dict | private_tests
dict | solution_type
stringclasses 2
values | programming_language
stringclasses 5
values | solution
stringlengths 1
983k
|
---|---|---|---|---|---|---|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
l = list(map(int, input().split()))
last_sum = l[0]
ans_l = []
for j in range(2):
ans = 0
if j == 0:
if last_sum <= 0:
ans += 1 - last_sum
last_sum += 1 - last_sum
else:
if last_sum >= 0:
ans += -1 - last_sum
last_sum += -1 - last_sum
for i in range(n - 1):
# print(last_sum)
if last_sum > 0:
if last_sum + l[i + 1] < 0:
last_sum += l[i + 1]
else:
a = -1 - last_sum - l[i + 1]
ans += abs(a)
last_sum += a + l[i + 1]
else:
if last_sum + l[i + 1] > 0:
last_sum += l[i + 1]
else:
a = 1 - last_sum - l[i + 1]
ans += abs(a)
last_sum += a + l[i + 1]
ans_l.append(ans)
print(min(ans_l))
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] a = new int[n];
for(int i = 0 ; i < n ; i++) a[i] = sc.nextInt();
long sum = 0, ans = 0, ans2 = 0;
// + - + - + ...
for(int i = 0 ; i < n ; i++) {
sum += a[i];
if(i % 2 == 0 && sum <= 0) {
ans += 1 - sum;
sum = 1;
} else if(i % 2 == 1 && sum >= 0) {
ans += sum + 1;
sum = -1;
}
}
// - + - + - ...
for(int i = 0 ; i < n ; i++) {
sum += a[i];
if(i % 2 == 0 && sum >= 0) {
ans += sum + 1;
sum = -1;
} else if(i % 2 == 1 && sum <= 0) {
ans += 1 - sum;
sum = 1;
}
}
System.out.println(Math.min(ans, ans2));
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
int min_ans = INT_MAX;
for (int mod = 0; mod < 2; ++mod) {
int ans = 0;
int sum = 0;
for (int i = 0; i < n; ++i) {
int sign = ((i % 2) == mod) * -2 + 1;
sum += a[i];
if (sign * sum <= 0) {
int diff = sign - sum;
sum = sign;
ans += abs(diff);
}
}
min_ans = min(min_ans, ans);
}
cout << min_ans << "\n";
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using llong = long long;
const int MOD = 1000000007;
int main(int argc, char** argv) {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<int> s(n, 0);
s[0] = a[0];
cout << s[0] << endl;
for (int i = 1; i < n; i++) {
s[i] = a[i] + s[i - 1];
cout << s[i] << endl;
}
int sum = 0;
int c1 = 0, c2 = 0;
int sign = 1;
for (int i = 0; i < n; i++) {
sum += a[i];
if (sum * sign <= 0) {
c1 += abs(sum) + 1;
sum = sign;
}
sign *= -1;
}
sign = -1;
sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (sum * sign <= 0) {
c2 += abs(sum) + 1;
sum = sign;
}
sign *= -1;
}
cout << min(c1, c2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
a_lst = [int(x) for x in input().split()]
b_lst = []
for i in a_lst:
b_lst.append(i)
def my_sign(num):
return (num > 0) - (num < 0)
cnt_p = 0
cnt_n = 0
sum_lst = []
sum2_lst = []
for i in range(N):
if i == 0:
if a_lst[i] == 0:
a_lst[i] = 1
cnt_p += 1
sum_lst.append(a_lst[i])
else:
sum_lst.append(a_lst[i] + sum_lst[i - 1])
if my_sign(sum_lst[i]) == my_sign(sum_lst[i - 1]) or my_sign(sum_lst[i]) == 0:
cnt_p += max(-my_sign(sum_lst[i - 1]), sum_lst[i]) - min(-my_sign(sum_lst[i - 1]), sum_lst[i])
a_lst[i] += -my_sign(sum_lst[i - 1]) - sum_lst[i]
sum_lst[i] = -my_sign(sum_lst[i - 1])
for i in range(N):
if i == 0:
if b_lst[i] == 0:
b_lst[i] = -1
cnt_n += 1
sum2_lst.append(b_lst[i])
else:
sum2_lst.append(b_lst[i] + sum2_lst[i - 1])
if my_sign(sum2_lst[i]) == my_sign(sum2_lst[i - 1]) or my_sign(sum2_lst[i]) == 0:
cnt_n += max(-my_sign(sum2_lst[i - 1]), sum2_lst[i]) - min(-my_sign(sum2_lst[i - 1]), sum2_lst[i])
b_lst[i] += -my_sign(sum2_lst[i - 1]) - sum2_lst[i]
sum2_lst[i] = -my_sign(sum2_lst[i - 1])
print(min(cnt_p, cnt_n))
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
int a[100010];
int sum[100010] = {0};
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
int ans = 0;
if (a[0] >= 0) {
for (int i = 0; i < n; i++) {
int j = i;
while (j >= 0) {
sum[i] += a[j];
j--;
}
if (i % 2 == 0) {
if (sum[i] <= 0) {
while (sum[i] <= 0) {
sum[i]++;
a[i]++;
ans++;
}
}
} else {
if (sum[i] >= 0) {
while (sum[i] >= 0) {
sum[i]--;
a[i]--;
ans++;
}
}
}
}
if (sum[n - 1] == 0) ans++;
} else {
for (int i = 0; i < n; i++) {
int j = i;
while (j >= 0) {
sum[i] += a[j];
j--;
}
if (i % 2 == 0) {
if (sum[i] >= 0) {
while (sum[i] >= 0) {
sum[i]--;
a[i]--;
ans++;
}
}
} else {
if (sum[i] <= 0) {
while (sum[i] <= 0) {
sum[i]++;
a[i]++;
ans++;
}
}
}
}
if (sum[n - 1] == 0) ans++;
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long s1, s2, c1, c2, a;
for (int i = 1; i <= n; i++) {
cin >> a;
s1 += a;
s2 += a;
if (i % 2) {
if (s1 <= 0) c1 += 1 - s1, s1 = 1;
if (s2 >= 0) c2 += 1 + s2, s2 = -1;
} else {
if (s1 >= 0) c1 += 1 + s1, s1 = -1;
if (s2 <= 0) c2 += 1 - s2, s2 = 1;
}
}
cout << min(c1, c2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | # -*- coding: utf-8 -*-
"""
Created on Sat Sep 8 15:51:53 2018
@author: maezawa
"""
def f(n, a0, cnt, sa):
a = a0[:]
for i in range(n-1):
sa += a[i]
na = -sa//abs(sa)*(abs(sa)+1)
if abs(a[i+1]) > abs(na) and a[i+1]*na > 0:
continue
else:
cnt += abs(na-a[i+1])
a[i+1] = na
return cnt
n = int(input())
a = list(map(int, input().split()))
sa = 0
cnt = 0
if a[0] == 0:
a[0] = 1
cnt =1
cnt0 = f(n, a, cnt, sa)
a[0] = -1
cnt1 = f(n, a, cnt, sa)
cnt = min([cnt0,cnt1])
else:
cnt = f(n, a, cnt, sa)
print(cnt)
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n=int(input())
b=list(map(int,input().split()))
a=b[:]
condition=''
cnt=0
wa=0
for i in range(n):
wa+=a[i]
if i == 0:
if a[i]>0:
condition='minus'
else:
condition='plus'
elif condition == 'plus':
condition='minus'
if wa<=0:
cnt+=abs(wa)+1
a[i]+=abs(wa)+1
wa+=abs(wa)+1
elif condition == 'minus':
condition='plus'
if wa>=0:
cnt+=abs(wa)+1
a[i]-=abs(wa)+1
wa-=abs(wa)+1
cnt1=cnt
a=b[:]
condition=''
cnt=0
wa=0
for i in range(n):
wa+=a[i]
if i == 0:
a[i]=int(a[i]/abs(a[i])*(-1))
cnt+=abs(a[i])+1
wa=a[i]
if a[i]>0:
condition='minus'
else:
condition='plus'
elif condition == 'plus':
condition='minus'
if wa<=0:
cnt+=abs(wa)+1
a[i]+=abs(wa)+1
wa+=abs(wa)+1
elif condition == 'minus':
condition='plus'
if wa>=0:
cnt+=abs(wa)+1
a[i]-=abs(wa)+1
wa-=abs(wa)+1
cnt2=cnt
print(min(cnt1,cnt2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::{BufWriter, Write};
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
($($r:tt)*) => {
let stdin = std::io::stdin();
let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
let mut next = move || -> String{
bytes
.by_ref()
.map(|r|r.unwrap() as char)
.skip_while(|c|c.is_whitespace())
.take_while(|c|!c.is_whitespace())
.collect()
};
input_inner!{next, $($r)*}
};
}
macro_rules! input_inner {
($next:expr) => {};
($next:expr, ) => {};
($next:expr, $var:ident : $t:tt $($r:tt)*) => {
let $var = read_value!($next, $t);
input_inner!{$next $($r)*}
};
}
macro_rules! read_value {
($next:expr, [graph1; $len:expr]) => {{
let mut g = vec![vec![]; $len];
let ab = read_value!($next, [(usize1, usize1)]);
for (a, b) in ab {
g[a].push(b);
g[b].push(a);
}
g
}};
($next:expr, ( $($t:tt),* )) => {
( $(read_value!($next, $t)),* )
};
($next:expr, [ $t:tt ; $len:expr ]) => {
(0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
};
($next:expr, chars) => {
read_value!($next, String).chars().collect::<Vec<char>>()
};
($next:expr, usize1) => (read_value!($next, usize) - 1);
($next:expr, [ $t:tt ]) => {{
let len = read_value!($next, usize);
read_value!($next, [$t; len])
}};
($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}
#[allow(unused)]
macro_rules! debug {
($($format:tt)*) => (write!(std::io::stderr(), $($format)*).unwrap());
}
#[allow(unused)]
macro_rules! debugln {
($($format:tt)*) => (writeln!(std::io::stderr(), $($format)*).unwrap());
}
/*
mod mod_int {
use std::ops::*;
pub trait Mod: Copy { fn m() -> i64; }
#[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct ModInt<M> { pub x: i64, phantom: ::std::marker::PhantomData<M> }
impl<M: Mod> ModInt<M> {
// x >= 0
pub fn new(x: i64) -> Self { ModInt::new_internal(x % M::m()) }
fn new_internal(x: i64) -> Self {
ModInt { x: x, phantom: ::std::marker::PhantomData }
}
pub fn pow(self, mut e: i64) -> Self {
debug_assert!(e >= 0);
let mut sum = ModInt::new_internal(1);
let mut cur = self;
while e > 0 {
if e % 2 != 0 { sum *= cur; }
cur *= cur;
e /= 2;
}
sum
}
#[allow(dead_code)]
pub fn inv(self) -> Self { self.pow(M::m() - 2) }
}
impl<M: Mod, T: Into<ModInt<M>>> Add<T> for ModInt<M> {
type Output = Self;
fn add(self, other: T) -> Self {
let other = other.into();
let mut sum = self.x + other.x;
if sum >= M::m() { sum -= M::m(); }
ModInt::new_internal(sum)
}
}
impl<M: Mod, T: Into<ModInt<M>>> Sub<T> for ModInt<M> {
type Output = Self;
fn sub(self, other: T) -> Self {
let other = other.into();
let mut sum = self.x - other.x;
if sum < 0 { sum += M::m(); }
ModInt::new_internal(sum)
}
}
impl<M: Mod, T: Into<ModInt<M>>> Mul<T> for ModInt<M> {
type Output = Self;
fn mul(self, other: T) -> Self { ModInt::new(self.x * other.into().x % M::m()) }
}
impl<M: Mod, T: Into<ModInt<M>>> AddAssign<T> for ModInt<M> {
fn add_assign(&mut self, other: T) { *self = *self + other; }
}
impl<M: Mod, T: Into<ModInt<M>>> SubAssign<T> for ModInt<M> {
fn sub_assign(&mut self, other: T) { *self = *self - other; }
}
impl<M: Mod, T: Into<ModInt<M>>> MulAssign<T> for ModInt<M> {
fn mul_assign(&mut self, other: T) { *self = *self * other; }
}
impl<M: Mod> Neg for ModInt<M> {
type Output = Self;
fn neg(self) -> Self { ModInt::new(0) - self }
}
impl<M> ::std::fmt::Display for ModInt<M> {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
self.x.fmt(f)
}
}
impl<M: Mod> ::std::fmt::Debug for ModInt<M> {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
let (mut a, mut b, _) = red(self.x, M::m());
if b < 0 {
a = -a;
b = -b;
}
write!(f, "{}/{}", a, b)
}
}
impl<M: Mod> From<i64> for ModInt<M> {
fn from(x: i64) -> Self { Self::new(x) }
}
// Finds the simplest fraction x/y congruent to r mod p.
// The return value (x, y, z) satisfies x = y * r + z * p.
fn red(r: i64, p: i64) -> (i64, i64, i64) {
if r.abs() <= 10000 {
return (r, 1, 0);
}
let mut nxt_r = p % r;
let mut q = p / r;
if 2 * nxt_r >= r {
nxt_r -= r;
q += 1;
}
if 2 * nxt_r <= -r {
nxt_r += r;
q -= 1;
}
let (x, z, y) = red(nxt_r, r);
(x, y - q * z, z)
}
} // mod mod_int
macro_rules! define_mod {
($struct_name: ident, $modulo: expr) => {
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
struct $struct_name {}
impl mod_int::Mod for $struct_name { fn m() -> i64 { $modulo } }
}
}
const MOD: i64 = 1_000_000_007;
define_mod!(P, MOD);
type ModInt = mod_int::ModInt<P>;
//n^p mod m
fn repeat_square(n: i64, p: i64, m: i64) -> i64 {
if p == 0 {
1
} else if p == 1 {
n % m
} else if p % 2 == 0 {
repeat_square(n, p / 2, m).pow(2) % m
} else {
(n * repeat_square(n, p - 1, m)) % m
}
}
fn ncr_mod(n: i64, r: i64, m: i64) -> i64 {
let mut denominator = n;
let mut numerator = 1;
for i in 1..r {
denominator = (denominator * (n - i)) % m;
numerator = (numerator * (i + 1)) % m;
}
(denominator * repeat_square(numerator, m - 2, m)) % m
}
*/
fn solve() {
let out = std::io::stdout();
let mut out = BufWriter::new(out.lock());
macro_rules! puts {
($($format:tt)*) => (let _ = write!(out,$($format)*););
}
input! {
n: usize,
a: [i32; n],
}
let mut cnt_odd = 0;
let mut cnt_even = 0;
let mut cum_1 = vec![0; n];
let mut cum_2 = vec![0; n];
//cum_1[even] < 0,cum_2[odd] < 0
if a[0] >= 0 {
cnt_even += a[0].abs() + 1;
cum_1[0] = a[0];
cum_2[0] = -1;
} else {
cnt_odd += a[0].abs() + 1;
cum_1[0] = 1;
cum_2[0] = a[0];
}
//+ - + -
for i in 1..n {
cum_1[i] = cum_1[i-1] + a[i];
if i % 2 != 0 {
if cum_1[i] >= 0 {
cnt_odd += cum_1[i].abs() + 1;
cum_1[i] = -1;
}
} else {
if cum_1[i] <= 0 {
cnt_odd += cum_1[i].abs() + 1;
cum_1[i] = 1;
}
}
}
//- + - +
for i in 1..n {
cum_2[i] = cum_2[i-1] + a[i];
if i % 2 == 0 {
if cum_2[i] >= 0 {
cnt_even += cum_2[i].abs() + 1;
cum_2[i] = -1;
}
} else {
if cum_2[i] <= 0 {
cnt_even += cum_2[i].abs() + 1;
cum_2[i] = 1;
}
}
}
puts!("{}\n",min(cnt_odd, cnt_even));
}
fn main() {
// In order to avoid potential stack overflow, spawn a new thread.
let stack_size = 104_857_600; // 100 MB
let thd = std::thread::Builder::new().stack_size(stack_size);
thd.spawn(|| solve()).unwrap().join().unwrap();
} |
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
static const int MAX = 100005;
int n;
int A[MAX];
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> A[i];
}
int ans1 = 0;
int sum = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 1) {
if (sum + A[i] >= 1) {
sum += A[i];
continue;
} else {
ans1 += 1 - (sum + A[i]);
sum = 1;
}
} else {
if (sum + A[i] <= -1) {
sum += A[i];
continue;
} else {
ans1 += (sum + A[i]) - (-1);
sum = -1;
}
}
}
sum = 0;
int ans2 = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
if (sum + A[i] >= 1) {
sum += A[i];
continue;
} else {
ans2 += 1 - (sum + A[i]);
sum = 1;
}
} else {
if (sum + A[i] <= -1) {
sum += A[i];
continue;
} else {
ans2 += (sum + A[i]) - (-1);
sum = -1;
}
}
}
cout << min(ans1, ans2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.*;
public class Main {
public static void main(String[] args) {
new Main().execute();
}
public void execute() {
Scanner sc = new Scanner(System.in);
final int N = sc.nextInt();
long[] A = new long[N];
for (int i = 0; i < N; i++) {
long ai = sc.nextLong();
A[i] = ai;
}
long cnt = 0;
if(A[0] ==0) {
A[0] = 1;
long cntA = countOps(A);
A[0] = -1;
long cntB = countOps(A);
cnt = Math.min(cntA, cntB);
}else {
cnt = countOps(A);
}
System.out.println(cnt);
sc.close();
}
private long countOps(long[] A) {
long[] arr = A.clone();
long sum = arr[0];
long cnt = 0;
for (int i = 1; i < arr.length; i++) {
if (sum > 0) {
if (sum + arr[i] >= 0) {
cnt += sum + arr[i] + 1;
arr[i] = -sum - 1;
sum = -1;
} else {
sum = sum + arr[i];
}
} else {// sum <0
if (sum + arr[i] <= 0) {
cnt += (sum + arr[i]) * -1 + 1;
arr[i] = -sum + 1;
sum = 1;
} else {
sum = sum + arr[i];
}
}
}
return cnt;
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.io.*;
import java.util.*;
public class Main{
static int n;
static long[] a;
static long count;
static long sum;
public static void main(String[] args) throws IOException{
MyReader r = new MyReader();
n = r.i();
a = r.ll();
sum = a[0];
count = 0;
if(a[0] == 0){
count = 1;
sum = 1;
solve();
long temp = count;
count = 1;
sum = -1;
solve();
count = Math.min(count, temp);
} else {
solve();
long temp = count;
sum = -a[1]-1;
count = Math.abs(a[0]+a[1]+1);
solve();
temp = Math.min(temp, count);
sum = -a[1]+1;
count = Math.abs(-a[0]+a[1]+1);
solve();
count = Math.min(temp, count);
}
println(count);
}
static void solve(){
for(int i = 1; i < n; i++){
if(sum < 0){
if(a[i]+sum <= 0){
count += -(a[i]+sum)+1;
sum = 1;
} else
sum = a[i]+sum;
} else{
if(a[i]+sum>=0){
count += a[i]+sum+1;
sum = -1;
} else
sum = a[i]+sum;
}
}
}
static void print(Object o){
System.out.print(o.toString());
}
static void println(Object o){
System.out.println(o.toString());
}
static int Int(String s){
return Integer.parseInt(s);
}
static long Long(String s){
return Long.parseLong(s);
}
static class MyReader extends BufferedReader{
MyReader(){
super(new InputStreamReader(System.in));
}
String s() throws IOException{
return readLine();
}
String[] ss() throws IOException{
return s().split(" ");
}
int i() throws IOException{
return Int(s());
}
int[] ii() throws IOException{
String[] ss = ss();
int size = ss.length;
int[] ii = new int[size];
for(int j = 0; j < size; j++) ii[j] = Integer.parseInt(ss[j]);
return ii;
}
long l() throws IOException{
return Long(s());
}
long[] ll() throws IOException{
String[] ss = ss();
int size = ss.length;
long[] ll = new long[size];
for(int j = 0; j < size; j++) ll[j] = Long.parseLong(ss[j]);
return ll;
}
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
long long int ans1 = 0, ans2 = 0;
int tmp = a[0];
if (tmp <= 0) {
ans1 += (1 - tmp);
tmp = 1;
}
for (int i = 1; i < n; i++) {
tmp += a[i];
if (i % 2 == 1) {
if (tmp >= 0) {
ans1 += abs(-1 - tmp);
tmp = -1;
}
} else {
if (tmp <= 0) {
ans1 += abs(1 - tmp);
tmp = 1;
}
}
}
tmp = a[0];
if (tmp >= 0) {
ans2 += (-1 - tmp);
tmp = -1;
}
for (int i = 1; i < n; i++) {
tmp += a[i];
if (i % 2 == 1) {
if (tmp <= 0) {
ans2 += abs(1 - tmp);
tmp = 1;
}
} else {
if (tmp >= 0) {
ans2 += abs(-1 - tmp);
tmp = -1;
}
}
}
cout << min(ans1, ans2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
unsigned int manipulation(vector<int>& a) {
unsigned int m = 0;
if (a[0] == 0) {
m++;
if (a[1] == 0)
a[0] = 1;
else if (a[1] == 1)
a[0] = -1;
else if (a[1] > 1)
a[0] = -a[1] + 1;
else if (a[1] == -1)
a[0] = 1;
else
a[0] = -a[1] + 1;
}
int sum = a[0];
bool is_sum_above_0 = sum > 0;
for (unsigned int ii = 1; ii < a.size(); ++ii) {
sum += a[ii];
if (is_sum_above_0) {
while (sum >= 0) {
m++;
sum--;
}
} else {
while (sum <= 0) {
m++;
sum++;
}
}
is_sum_above_0 = !is_sum_above_0;
}
return m;
}
int main() {
unsigned int n;
cin >> n;
vector<int> a(n);
for (unsigned int ii = 0; ii < n; ++ii) cin >> a[ii];
cout << manipulation(a) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long ans = 0;
vector<long long> sum(n);
sum[0] = a[0];
for (int i = 1; i < n; i++) {
sum[i] = sum[i - 1] + a[i];
if (signbit(sum[i]) == signbit(sum[i - 1])) {
ans += abs(sum[i]) + 1;
sum[i] = sum[i - 1] / abs(sum[i - 1]) * (-1);
} else if (sum[i] == 0) {
sum[i] = sum[i - 1] / abs(sum[i - 1]) * (-1);
ans += 1;
}
cout << sum[i] << endl;
}
cout << ans;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
li = list(map(int,input().split()))
ans = 0
cnt = 0
s = 0
for i in range(n):
if i == 0:
ans += li[i]
if ans > 0:
s = 1
else:
s = -1
else:
ans += li[i]
if ans <= 0 and s == -1:
while True:
ans += 1
cnt += 1
if ans > 0:
s == 1
break
if ans >= 0 and s == 1:
while True:
ans -= 1
cnt += 1
if ans < 0:
s == -1
break
s *= -1
print(cnt) |
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int,input().split()))
S1 = 0
S2 = 0
#S1が奇数番目が正の場合、S2が偶数番目が負の場合
cnt = 0
for i,num in enumerate(a):
S1 += num
if i % 2 == 0 and S1 <= 0:
cnt1 += 1 - S1
S1 = 1
if i % 2 != 0 and S1 >= 0:
cnt1 += 1 + S1
S1 = -1
S2 += num
if i % 2 == 0 and S2 >= 0:
cnt2 += 1 + S2
S2 = -1
if i % 2 != 0 and S2 <= 0:
cnt2 += 1 - S2
S2 = 1
print(cnt1 if cnt1 <= cnt2 else cnt2)
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int,input().split()))
def find(a):
f = "+" if a[0] < 0 else "-"
s = a[0]
c = 0
#print((s,f,c))
for i in range(1,n):
if f == "+" and s + a[i] > 0:
f = "-"
s = s + a[i]
# print((s,f,c))
elif f == "+":
c = c + abs(s + a[i])+1
f = "-"
s = 1
# print((s,f,c))
elif f == "-" and s + a[i] < 0:
f = "+"
s = s + a[i]
# print((s,f,c))
else:
c = c + abs(s+a[i])+1
f = "+"
s = -1
return c
if a[0] = 0:
a[0] = -1
cmin = find(a)
a[0] = 1
cmax = find(a)
c = min(cmin,cmax)
else:
c = find(a)
print(c)
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
int main() {
int N;
cin >> N;
vector<int> a(N);
for (int i = 0; i < (N); ++i) {
cin >> a[i];
}
long long count = 0;
if (a[0] == 0) {
for (int i = 1; i < N; i++) {
if (a[i] > 0) {
if (i % 2 == 0) {
a[0]++;
} else {
a[0]--;
}
break;
} else if (a[i] < 0) {
if (i % 2 == 0) {
a[0]--;
} else {
a[0]++;
}
break;
}
if (i == N - 1) a[0]++;
}
count++;
}
long long sum = 0;
for (int i = 0; i < (N - 1); ++i) {
sum += a[i];
long long next_sum = sum + a[i + 1];
if ((sum < 0 && next_sum > 0) || (sum > 0 && next_sum < 0)) {
} else {
if (sum < 0) {
count += 1 - next_sum;
a[i + 1] += 1 - next_sum;
} else {
count += next_sum + 1;
a[i + 1] -= (next_sum + 1);
}
}
}
cout << count << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
sum = a[0]
count = 0
for i in range(1, n) :
temp = sum
sum += a[i]
if temp > 0 and sum > 0 :
count += sum + 1
sum = -1
elif temp < 0 and sum < 0 :
count += -sum + 1
sum = 1
if sum == 0 :
count += 1
print(count)
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
using vl = vector<long long>;
using vvl = vector<vector<long long>>;
using vs = vector<string>;
const int mod = 1000000007;
class mint {
public:
long long x;
mint(long long x = 0) : x((x % mod + mod) % mod) {}
mint operator-() const { return mint(-x); }
mint& operator+=(const mint& a) {
if ((x += a.x) >= mod) x -= mod;
return *this;
}
mint& operator-=(const mint& a) {
if ((x += mod - a.x) >= mod) x -= mod;
return *this;
}
mint& operator*=(const mint& a) {
(x *= a.x) %= mod;
return *this;
}
mint operator+(const mint& a) const {
mint res(*this);
return res += a;
}
mint operator-(const mint& a) const {
mint res(*this);
return res -= a;
}
mint operator*(const mint& a) const {
mint res(*this);
return res *= a;
}
mint pow(long long t) const {
if (!t) return 1;
mint a = pow(t >> 1);
a *= a;
if (t & 1) a *= *this;
return a;
}
mint inv() const { return pow(mod - 2); }
mint& operator/=(const mint& a) { return (*this) *= a.inv(); }
mint operator/(const mint& a) const {
mint res(*this);
return res /= a;
}
friend ostream& operator<<(ostream& os, const mint& m) {
os << m.x;
return os;
}
};
long long modpow(long long x, long long n, long long p = 1000000007) {
if (n == 0) return 1 % p;
if (n % 2 == 0)
return modpow(x * x % p, n / 2, p);
else
return x * modpow(x, n - 1, p) % p;
}
void Main() {
long long N;
cin >> N;
vl v(N);
for (long long i = 0; i < N; i++) cin >> v[i];
long long ans = 0;
if (v[0]) {
long long flg = (v[0] > 0);
long long acc = v[0];
for (long long i = 1; i < N; i++) {
acc += v[i];
if (flg && acc >= 0) {
ans += acc + 1;
acc = -1;
} else if (!flg && acc <= 0) {
ans += -acc + 1;
acc = 1;
}
flg ^= 1;
}
} else {
long long flg = 0;
long long ans1 = 1;
long long acc = -1;
for (long long i = 1; i < N; i++) {
acc += v[i];
if (flg && acc >= 0) {
ans1 += acc + 1;
acc = -1;
} else if (!flg && acc <= 0) {
ans1 += -acc + 1;
acc = 1;
}
flg ^= 1;
}
flg = 1;
long long ans2 = 1;
acc = 1;
for (long long i = 1; i < N; i++) {
acc += v[i];
if (flg && acc >= 0) {
ans2 += acc + 1;
acc = -1;
} else if (!flg && acc <= 0) {
ans2 += -acc + 1;
acc = 1;
}
flg ^= 1;
}
ans = min(ans1, ans2);
}
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
long long t = 1;
for (long long i = 0; i < t; i++) Main();
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
A = list(map(int, input().split()))
sum = A[0]
ans = 0
if sum == 0:
if A[1] >= 0:
sum = -1
elif A[1] < 0:
sum = 1
ans += 1
for i in range(1, len(A)):
if sum > 0:
if sum + A[i] >= 0:
ans += abs(sum + A[i]) + 1
sum = -1
else:
sum += A[i]
elif sum < 0:
if sum + A[i] <= 0:
ans += abs(sum + A[i]) + 1
sum = 1
else:
sum += A[i]
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long int ans = 0;
long long int sum = 0;
cin >> sum;
for (auto i = 1; i < n; ++i) {
long long int a;
cin >> a;
if ((sum > 0 && sum + a >= 0) || (sum < 0 && sum + a <= 0)) {
ans += (abs(sum + a) + 1);
sum = (sum > 0 ? -1 : 1);
} else {
sum += a;
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long MOD = 1000000007;
int main() {
int N;
cin >> N;
vector<long long> sum_a(N, 0);
long long ans = 0;
int flag = 1;
for (int i = 0; i < N; i++) {
int a;
cin >> a;
if (i == 0) {
if (a < 0) {
flag = -1;
}
sum_a[i] = flag * a;
} else {
sum_a[i] = sum_a[i - 1] + flag * a;
}
if (i % 2 == 0 && sum_a[i] <= 0) {
ans += -sum_a[i] + 1;
sum_a[i] = 1;
} else if (i % 2 == 1 && sum_a[i] >= 0) {
ans += sum_a[i] + 1;
sum_a[i] = -1;
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> a(N);
for (auto &i : a) cin >> i;
int64_t sum = 0;
int64_t cnt = 0;
int sign = a.at(0) / abs(a.at(0));
for (int i = 0; i < N; i++) {
sum += a.at(i);
if (sign * sum <= 0) {
cnt += abs(sum) + 1;
sum = sign;
}
sign *= -1;
}
cout << cnt << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const int INF = 1e9;
int guki(int a) {
if (a % 2 == 0)
return 0;
else
return 1;
}
using namespace std;
int main() {
int N;
cin >> N;
int sum = 0, ans = 0, a;
cin >> a;
sum += a;
if (a < 0) {
for (int i = 1; i < N; i++) {
cin >> a;
sum += a;
if ((i % 2 == 0) && (0 <= sum)) {
int x = abs(sum + 1);
sum -= x;
ans += abs(x);
} else if ((i % 2 == 1) && (sum <= 0)) {
int x = abs(1 - sum);
sum += x;
ans += abs(x);
}
}
} else {
for (int i = 1; i < N; i++) {
cin >> a;
sum += a;
if ((i % 2 == 0) && (sum <= 0)) {
int x = abs(1 - sum);
sum += x;
ans += abs(x);
} else if ((i % 2 == 1) && (0 <= sum)) {
int x = abs(sum + 1);
sum -= x;
ans += abs(x);
}
}
}
cout << ans << endl;
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vll = vector<long long>;
using vp = vector<pair<int, int>>;
using vvi = vector<vector<int>>;
using vvc = vector<vector<char>>;
using vvll = vector<vector<long long>>;
template <class T>
inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
inline bool chmin(T &a, T b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vll a(n);
for (int i = 0; i < (int)(n); i++) cin >> a[i];
auto f = [&](ll x) {
ll sm = x;
ll res = 0;
for (int i = 1; i < n; ++i) {
if (sm > 0) {
if (!(a[i] < -sm)) {
res += a[i] - (-sm - 1);
a[i] = -sm - 1;
}
} else {
if (!(-sm < a[i])) {
res += (-sm + 1) - a[i];
a[i] = -sm + 1;
}
}
sm += a[i];
}
return res;
};
ll ans;
if (a[0] == 0) {
ll res1 = f(-1) + 1;
ll res2 = f(1) + 1;
ans = min(res1, res2);
} else {
ll res1 = f(a[0]);
ll res2;
if (a[0] > 0)
res2 = f(-1) + a[0] + 1;
else
res2 = f(1) + -a[0] + 1;
ans = min(res1, res2);
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n1=int(input())
l1=list(map(int,input().split()))
total=l1[0]
Num=0
for j in range(1,n1):
pretotal=total
total=total+l1[j]
if pretotal ==0:
total=total+(total)/abs(total)
Num=Num+1
while (pretotal*total>0) or (total ==0):
if total==0:
Num=Num+1
if pretotal<0:
total=1
else:
total=-1
elif pretotal<0:
Num=Num-total+1
total=+1
print(total)
elif pretotal>0:
Num=Num+total+1
total=-1
print(Num) |
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, sum;
vector<int> a(100000);
int ans1 = 0, ans2 = 0;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
if (a[0] < 1) {
ans1 += abs(1 - a[0]);
sum = 1;
} else
sum = a[0];
for (int i = 1; i < n; i++) {
if (i % 2 != 0 && sum + a[i] >= 0) {
ans1 += abs(sum * (-1) - 1 - a[i]);
sum = -1;
} else if (i % 2 == 0 && sum + a[i] <= 0) {
ans1 += abs(sum * (-1) + 1 - a[i]);
sum = 1;
} else
sum += a[i];
}
if (a[0] > 1) {
ans2 += abs(-1 - a[0]);
sum = -1;
} else
sum = a[0];
for (int i = 1; i < n; i++) {
if (i % 2 == 0 && sum + a[i] >= 0) {
ans2 += abs(sum * (-1) - 1 - a[i]);
sum = -1;
} else if (i % 2 != 0 && sum + a[i] <= 0) {
ans2 += abs(sum * (-1) + 1 - a[i]);
sum = 1;
} else
sum += a[i];
}
cout << min(ans1, ans2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int ms = 1e5 + 9;
int val;
int vet[ms];
int main() {
int f = 0;
long long soma = 0, ans = 0;
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> vet[i];
soma = vet[0];
if (soma < 0)
f = 1;
else if (soma == 0 and vet[1] < 0) {
ans++;
soma++;
} else if (soma == 0 and vet[1] >= 0) {
ans++;
soma++;
f = 1;
}
for (int i = 1; i < n; i++) {
val = vet[i];
soma += val;
if (f) {
if (soma == 0) {
ans += 1;
soma++;
} else if (soma < 0) {
ans += ((-soma) + 1);
soma = 1;
}
} else {
if (soma == 0) {
ans++;
soma--;
} else if (soma > 0) {
ans += (soma + 1);
soma = -1;
}
}
f = 1 - f;
}
cout << ans << "\n";
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int d[n];
for (int i = 0; i < n; i++) {
cin >> d[i];
}
int count = 0;
int sum = d[0];
int f = 0;
if (d[0] > 0) {
f = -1;
}
if (d[0] < 0) {
f = 1;
}
for (int i = 1; i < n; i++) {
sum += d[i];
if (sum == 0) {
if (f == 1) {
count++;
f = -1;
sum = 1;
continue;
}
if (f == -1) {
count++;
f = 1;
sum = -1;
continue;
}
}
if (sum > 0) {
if (f == 1) {
f = -1;
continue;
}
if (f == -1) {
count += sum + 1;
sum = -1;
f = 1;
continue;
}
}
if (sum < 0) {
if (f == -1) {
f = 1;
continue;
}
if (f == 1) {
count += 1 - sum;
sum = 1;
f = -1;
continue;
}
}
}
int ccount = 0;
int ssum;
int ff = 0;
if (d[0] > 0) {
ff = 1;
ccount = 1 + d[0];
ssum = -1;
}
if (d[0] < 0) {
ff = -1;
ccount = 1 - d[0];
ssum = 1;
}
for (int i = 1; i < n; i++) {
sum += d[i];
if (ssum == 0) {
if (ff == 1) {
ccount++;
ff = -1;
ssum = 1;
continue;
}
if (ff == -1) {
ccount++;
ff = 1;
ssum = -1;
continue;
}
}
if (ssum > 0) {
if (ff == 1) {
ff = -1;
continue;
}
if (ff == -1) {
ccount += sum + 1;
ssum = -1;
ff = 1;
continue;
}
}
if (ssum < 0) {
if (ff == -1) {
ff = 1;
continue;
}
if (ff == 1) {
ccount += 1 - sum;
ssum = 1;
ff = -1;
continue;
}
}
}
cout << min(count, ccount) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
X = [a[0]]
ans = 0
#+-+-のとき
if a[0] >= 0:
for i in range(n-1):
#print(X[i], a[i+1])
num = X[i] + a[i+1]
if i % 2 != 0: #iが奇数の時はnumの値は正
if num < 0:
X.append(abs(num))
ans += abs(num) + 1
else:
X.append(num)
else: #iが偶数のときnumの値は負
if num > 0:
X.append(num*-1)
ans += abs(num) + 1
else:
X.append(num)
#-+-+のとき
else:
for i in range(n-1):
#print(X[i], a[i+1])
num = X[i] + a[i+1]
if i % 2 != 0: #iが奇数の時はnumの値は負
if num < 0:
X.append(num)
else:
ans += abs(num) + 1
X.append(-1)
else: #iが偶数のときnumの値は正
if num > 0:
X.append(num)
else:
X.append(num*-1)
ans += abs(num) + 1
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n=int(input())
a=list(map(int,input().split()))
ans=0
sum0=a[0]
for i in a[1:]:
if sum0>0:
if sum0+i>=0:
ans+=sum0+i+1
sum0=-1
else:sum0=sum0+i
else:
if sum0+i<=0:
ans+=abs(sum0+i)+1
sum0=1
else:sum0=sum0+i
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long dy[4] = {1, 0, -1, 0};
long long dx[4] = {0, 1, 0, -1};
bool check(long long a, long long b) {
if ((a <= 0 && b > 0) || (a >= 0 && b < 0)) return true;
return false;
}
int32_t main() {
long long n;
cin >> n;
vector<long long> v(n);
long long sum = 0, cnt = 0;
for (long long i = 0; i < n; i++) {
cin >> v[i];
long long t = sum;
sum += v[i];
if (sum == 0) {
if (i > 0) {
if (t > 0) {
cnt += (1 - v[i]);
sum--;
} else {
cnt += (1 + v[i]);
sum++;
}
} else {
for (long long j = 1; j < n; j++) {
if (v[j] > 0) {
sum += pow((-1), j);
cnt++;
break;
} else if (v[j] < 0) {
sum += pow((-1), j + 1);
cnt++;
break;
}
}
}
continue;
}
if (i > 0) {
if (!check(sum, t)) {
if (sum > 0) {
cnt += (sum + 1);
sum -= (sum + 1);
} else if (sum < 0) {
cnt += (1 - sum);
sum += (1 - sum);
}
}
}
}
cout << cnt << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
nums = list(map(int, input().split()))
sum_n = 0
ans = 10**5+1
for start in [-1, 1]:
before = start
cnt = 0
for num in nums:
sum_n += num
if before*sum_n >= 0:
if before < 0:
cnt += abs(1-sum_n)
before = 1
else:
cnt += abs(-1-sum_n)
before = -1
else:
before = sum_n
print(cnt)
ans = min(ans, cnt)
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
int body(std::vector<int>& a) {
int ans = 0;
std::vector<int> s(a.size());
s.at(0) = a.at(0);
for (unsigned int i = 1; i < a.size(); i++) {
s.at(i) = s.at(i - 1) + a.at(i);
}
for (unsigned int i = 1; i < s.size(); i++) {
if (s.at(i - 1) > 0 && s.at(i) >= 0) {
int n = s.at(i) + 1;
ans += n;
for (unsigned int j = i; j < s.size(); j++) {
s.at(j) -= n;
}
}
if (s.at(i - 1) < 0 && s.at(i) <= 0) {
int n = -1 * s.at(i) + 1;
ans += n;
for (unsigned int j = i; j < s.size(); j++) {
s.at(j) += n;
}
}
}
return ans;
}
int main(int argc, char** argv) {
int n;
std::cin >> n;
std::vector<int> a(n);
for (int i = 0; i < n; i++) {
std::cin >> a.at(i);
}
int ans;
if (a.at(0) != 0) {
ans = body(a);
} else {
a.at(0) = -1;
int ans_a = body(a);
a.at(0) = 1;
int ans_b = body(a);
ans = std::min(ans_a, ans_b);
}
std::cout << ans << std::endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
clock_t CLOCK;
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using mll = map<ll, ll>;
using qll = queue<ll>;
using P = pair<ll, ll>;
constexpr ll INF = 0x3f3f3f3f3f3f3f3f;
constexpr ld PI = 3.141592653589793238462643383279;
ll get_digit(ll x) { return to_string(x).size(); }
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
vector<P> factorize(ll n) {
vector<P> result;
for (ll i = 2; i * i <= n; ++i) {
if (n % i == 0) {
result.push_back({i, 0});
while (n % i == 0) {
n /= i;
result.back().second++;
}
}
}
if (n != 1) {
result.push_back({n, 1});
}
return result;
}
vll divisor(ll n) {
vll ret;
for (ll i = 1; i * i <= n; i++) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n) ret.push_back(n / i);
}
}
sort(ret.begin(), ret.end());
return (ret);
}
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll N;
cin >> N;
ll ans1 = 0;
ll ans2 = 0;
ll current_num1;
ll current_num2;
for (ll i = 0; i < (ll)(N); ++i) {
ll a;
cin >> a;
if (i == 0) {
current_num1 = a;
current_num2 = a;
continue;
}
current_num1 += a;
current_num2 += a;
if (i % 2 == 0) {
if (current_num1 >= 0) {
ans1 += abs(current_num1) + 1;
current_num1 = -1;
}
if (current_num2 <= 0) {
ans2 += abs(current_num2) + 1;
current_num2 = 1;
}
} else {
if (current_num1 <= 0) {
ans1 += abs(current_num1) + 1;
current_num1 = 1;
}
if (current_num2 >= 0) {
ans2 += abs(current_num2) + 1;
current_num2 = -1;
}
}
}
ll ans = min(ans1, ans2);
cout << ans << "\n";
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
not_0 = n
for i in range(n):
if a[i]:
not_0 = i
break
if not_0 == n:
print(1+2*(n-1))
exit()
ans = 0
if not_0 == 0:
b = [a[0]]
else:
if abs(a[not_0]) == 1:
a[not_0] *= 2
ans = 2*not_0
else:
ans = 1 + 2*(not_0-1)
if a[not_0] > 0:
b = [a[not_0] - 1]
else:
b = [a[not_0] + 1]
tmp = b[0]
for i in range(not_0+1, n):
tmp += a[i]
b.append(tmp)
for i in range(not_0+1, n):
if b[i-1]*b[i] < 0:
continue
else:
if b[i-1] > 0:
d = b[i] + 1
ans += d
for j in range(i, n):
b[j] -= d
else:
d = -b[i] + 1
ans += d
for j in range(i, n):
b[j] += d
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
ans = 0
temp=a[0]
for i in range(n - 1):
if temp > 0:
temp+=a[i+1]
if temp < 0:
pass
else:
ans += (temp + 1)
temp -= (temp+1)
else:
temp+=a[i+1]
if temp > 0:
pass
else:
ans -= (temp - 1)
temp -= (temp - 1)
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T>
inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
template <class T>
inline T sqr(T x) {
return x * x;
}
const double EPS = 1e-10;
const double PI = acos(-1.0);
pair<long long, long long> maxP(vector<long long> a, long long size) {
pair<long long, long long> p;
long long Max = a[0];
long long place = 0;
for (int i = (0); i < (size); ++i) {
if (a[i] > Max) {
Max = a[i];
place = i;
}
}
p.first = Max;
p.second = place;
return p;
}
pair<long long, long long> minP(vector<long long> a, long long size) {
pair<long long, long long> p;
long long min = a[0];
long long place = 0;
for (int i = (0); i < (size); ++i) {
if (a[i] < min) {
min = a[i];
place = i;
}
}
p.first = min;
p.second = place;
return p;
}
long long sumL(vector<long long> a, long long size) {
long long sum = 0;
for (int i = (0); i < (size); ++i) {
sum += a[i];
}
return sum;
}
long long counT(vector<long long> a, long long t) {
sort(a.begin(), a.end());
return upper_bound(a.begin(), a.end(), t) -
lower_bound(a.begin(), a.end(), t);
}
long long DIV[1000 + 1][1000 + 1];
void divide(long long n, long long m) {
DIV[0][0] = 1;
for (int i = (1); i < (n + 1); ++i) {
DIV[i][0] = 0;
}
for (int i = (0); i < (n + 1); ++i) {
DIV[i][1] = 1;
}
for (int i = (1); i < (m + 1); ++i) {
for (int t = (0); t < (n + 1); ++t) {
if (DIV[t][i] > 0) continue;
if (t >= i) {
DIV[t][i] = DIV[t - i][i] + DIV[t][i - 1];
} else {
DIV[t][i] = DIV[t][i - 1];
}
}
}
}
bool IsPrime(int num) {
if (num < 2)
return false;
else if (num == 2)
return true;
else if (num % 2 == 0)
return false;
double sqrtNum = sqrt(num);
for (int i = 3; i <= sqrtNum; i += 2) {
if (num % i == 0) {
return false;
}
}
return true;
}
class UnionFind {
public:
vector<long long> par;
vector<long long> rank;
UnionFind(long long N) : par(N), rank(N) {
for (int i = (0); i < (N); ++i) par[i] = i;
for (int i = (0); i < (N); ++i) rank[i] = 0;
}
~UnionFind() {}
long long root(long long x) {
if (par[x] == x)
return x;
else {
par[x] = root(par[x]);
return par[x];
}
}
void unite(long long x, long long y) {
long long rx = root(x);
long long ry = root(y);
if (rx == ry) return;
if (rank[rx] < rank[ry]) {
par[rx] = ry;
} else {
par[ry] = rx;
if (rank[rx] == rank[ry]) {
rank[rx]++;
}
}
}
bool same(long long x, long long y) {
long long rx = root(x);
long long ry = root(y);
return rx == ry;
}
};
class BFS_shortestDistance {
public:
BFS_shortestDistance(vector<vector<char> > p_, long long h_, long long w_) {
p = p_;
h = h_;
w = w_;
initial_number = h * w * 2;
for (int i = (0); i < (h); ++i) {
vector<long long> k(w);
for (int t = (0); t < (w); ++t) k[t] = initial_number;
field.push_back(k);
}
}
vector<vector<char> > p;
long long h;
long long w;
long long initial_number;
vector<vector<long long> > field;
pair<long long, long long> plus(pair<long long, long long> &a,
pair<long long, long long> &b) {
pair<long long, long long> p;
p.first = a.first + b.first;
p.second = a.second + b.second;
return p;
}
bool equal(pair<long long, long long> &a, pair<long long, long long> &b) {
return (a.first == b.first && a.second == b.second);
}
bool is_in_field(int h, int w, const pair<long long, long long> &point) {
const int c = point.second;
const int r = point.first;
return (0 <= c && c < w) && (0 <= r && r < h);
}
void init() {
for (int i = (0); i < (field.size()); ++i) {
for (int t = (0); t < (field[i].size()); ++t) {
field[i][t] = initial_number;
}
}
}
void shortest(long long sy, long long sx) {
init();
pair<long long, long long> c[4];
c[0].first = 0;
c[0].second = 1;
c[1].first = 0;
c[1].second = -1;
c[2].first = 1;
c[2].second = 0;
c[3].first = -1;
c[3].second = 0;
queue<pair<long long, long long> > Q;
pair<long long, long long> s;
s.first = sy;
s.second = sx;
field[sy][sx] = 0;
Q.push(s);
while (Q.empty() == false) {
pair<long long, long long> now = Q.front();
Q.pop();
for (int u = 0; u < 4; u++) {
pair<long long, long long> x = c[u];
pair<long long, long long> next = plus(now, x);
if (is_in_field(h, w, next)) {
if (p[next.first][next.second] == '.') {
if (field[next.first][next.second] == initial_number) {
field[next.first][next.second] = field[now.first][now.second] + 1;
Q.push(next);
} else {
}
}
}
}
}
}
};
bool Ischanged(long long a, long long b) {
if (a * b < 0) {
return true;
} else {
return false;
}
}
int main() {
long long n;
cin >> n;
vector<long long> a(n);
for (int i = (0); i < (n); ++i) cin >> a[i];
long long sum = 0;
long long count = 0;
for (int i = (0); i < (n); ++i) {
if (i == 0) {
sum += a[i];
if (sum == 0 && n != 1) {
sum = 1;
count++;
} else if (sum == 0 && n == 1) {
count++;
}
} else {
long long was = sum;
sum += a[i];
if (Ischanged(was, sum)) {
continue;
} else {
if (sum < 0) {
count += abs(sum) + 1;
sum = 1;
} else if (sum > 0) {
count += abs(sum) + 1;
sum = -1;
} else {
if (was < 0) {
sum = 1;
} else {
sum = -1;
}
count++;
}
}
}
}
long long sum2 = 0;
long long count2 = 0;
for (int i = (0); i < (n); ++i) {
if (i == 0) {
sum2 += a[i];
if (sum2 == 0 && n != 1) {
sum2 = -1;
count2++;
} else if (sum2 == 0 && n == 1) {
count2++;
}
} else {
long long was = sum2;
sum2 += a[i];
if (Ischanged(was, sum2)) {
continue;
} else {
if (sum2 < 0) {
count2 += abs(sum2) + 1;
sum2 = 1;
} else if (sum2 > 0) {
count2 += abs(sum2) + 1;
sum2 = -1;
} else {
if (was < 0) {
sum2 = 1;
} else {
sum2 = -1;
}
count2++;
}
}
}
}
cout << min(count, count2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long INF = 1000000000;
const long long MOD = (long long)1e9 + 7;
template <class T>
inline T in() {
T x;
cin >> x;
return x;
}
signed main() {
long long n = in<long long>();
vector<long long> v(n, 0);
for (long long i = 0; i < n; i++) {
if (i == 0)
cin >> v[i];
else {
long long x = in<long long>();
v[i] = v[i - 1] + x;
}
}
long long sign = v[0] / abs(v[0]);
long long cnt = 0;
for (long long i = 1; i < n; i++) {
if (v[i] * sign >= 0) {
long long d = abs(v[i]) + 1;
cnt += d;
for (long long j = i + 1; j < n; j++) v[j] += d * sign * -1;
}
sign *= -1;
}
cout << cnt << "\n";
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll=long long;
#define int ll
#define FOR(i,a,b) for(int i=int(a);i<int(b);i++)
#define REP(i,b) FOR(i,0,b)
int read(){
int i;
scanf("%lld",&i);
return i;
}
int sign(int s){
return (s>0?1:-1);
}
signed main(){
// your code goes here
int N = read();
int a[N];
int sum[N]={0};
int count=0;
REP(i,N){
a[i] = read();
//cout << a[i];
}
if(a[0] == 0){
a[0] = -sign(a[0]);
count++;
}
sum[0] = a[0];
FOR(i,1,N){
sum[i] = sum[i-1]+a[i];
if(sum[i] == 0){
sum[i] -= sum[i-1];
count++;
}
else if(sign(sum[i])==sign(sum[i-1])){
int bef=a[i];
sum[i] = sum[i]-a[i];
a[i] = -sign(sum[i-1])*(abs(sum[i-1])+1);
sum[i] = sum[i-1]+a[i];
count += abs(bef - a[i]);
}
}
REP(i,N) cout << a[i];
cout << count;
} |
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9 + 7;
long long n, g, ans, a[100001];
int main() {
cin >> n;
bool flag = 1;
int p;
for (int i = 1; i <= (n); i++) {
cin >> a[i];
if (flag && a[i] != 0) {
p = i;
flag = 0;
}
}
if (p != 1) {
ans += (p - 1) * 2 - 1;
if (a[p] > 0) {
ans += a[p] + 1;
} else {
ans += 1 - a[p];
}
}
g = a[p];
for (int i = p + 1; i <= n; i++) {
if (g > 0) {
g += a[i];
if (g > -1) {
ans += g + 1;
g = -1;
}
} else {
g += a[i];
if (g < 1) {
ans += 1 - g;
g = 1;
}
}
}
cout << ans;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
cin.sync_with_stdio(false);
int n;
cin >> n;
ll a[10000];
ll sum = 0;
int flag = true;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
ll count = 0;
sum = a[0];
for (int i = 1; i < n; i++) {
if (sum < 0 && sum + a[i] <= 0) {
count += abs(sum) - a[i] + 1;
sum = 1;
} else if (sum > 0 && sum + a[i] >= 0) {
count += abs(sum + a[i] + 1);
sum = -1;
} else {
sum += a[i];
}
}
cout << count << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("4");
WillReturn.Add("1 -3 1 0");
//4
}
else if (InputPattern == "Input2") {
WillReturn.Add("5");
WillReturn.Add("3 -6 4 -5 7");
//0
}
else if (InputPattern == "Input3") {
WillReturn.Add("6");
WillReturn.Add("-1 4 3 2 -5 4");
//8
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static void Main()
{
List<string> InputList = GetInputList();
int[] AArr = InputList[1].Split(' ').Select(X => int.Parse(X)).ToArray();
long Cost1 = Solve(AArr, true);
long Cost2 = Solve(AArr, false);
Console.WriteLine(Math.Min(Cost1, Cost2));
}
//最初の符号を引数として、コストを求める
static long Solve(int[] pArr, bool pIsFirstPlus)
{
long Cost = 0;
long RunSum = 0;
for (int I = 0; I <= pArr.GetUpperBound(0); I++) {
RunSum += pArr[I];
if (I % 2 == 0 && pIsFirstPlus) {
if (RunSum > 0) continue;
Cost += Math.Abs(RunSum) + 1;
RunSum = 1;
}
else {
if (RunSum < 0) continue;
Cost += Math.Abs(RunSum) + 1;
RunSum = -1;
}
}
return Cost;
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> an(n);
for (int i = 0; i < n; ++i) {
cin >> an[i];
}
int accum = 0;
int sign = 0;
bool non_zero = true;
int cnt = 0;
for (int i = 0; i < n; ++i) {
auto new_accum = accum + an[i];
if (i == 0) {
if (new_accum == 0) {
int next_sign = an[1] > 0 ? 1 : -1;
new_accum -= next_sign;
++cnt;
an[0] = -next_sign;
}
accum = new_accum;
sign = accum > 0 ? 1 : -1;
} else {
if (new_accum * accum >= 0) {
int x = -sign - new_accum;
new_accum += x;
cnt += abs(x);
an[i] += x;
}
int new_sign = new_accum > 0 ? 1 : -1;
accum = new_accum;
sign = new_sign;
}
}
cout << cnt << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
inline void solve() {
long long n;
cin >> n;
vector<long long> a(n);
for (long long &i : a) cin >> i;
long long p = 0, ne = 0, s = 0;
for (long long i = 0; i < n; i++) {
s += a[i];
if (i & 1) {
if (s >= 0) {
p += s + 1;
s = -1;
}
} else {
if (s <= 0) {
p += abs(s) + 1;
s = 1;
}
}
}
s = 0;
for (long long i = 0; i < n; i++) {
s += a[i];
if (i % 2 == 0) {
if (s >= 0) {
ne += s + 1;
s = -1;
}
} else {
if (s <= 0) {
ne += abs(s) + 1;
s = 1;
}
}
}
cout << min(p, ne) << endl;
}
signed main() {
long long n = 1;
cin >> n;
while (n--) solve();
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
cnt=0
for i in range(1,n):
# 条件満たすまでループ
while True:
#print(a)
now_tmp = sum(a[:i])
next_tmp = sum(a[:i+1])
#print(i, now_tmp, next_tmp)
# 符号が逆転していればOK かつ 現在までの総和が0でない
# 異なる符号を掛けるとマイナスになる
if now_tmp * next_tmp <0 and now_tmp !=0:
break
else:
# 現在の合計がマイナスの場合
if now_tmp < 0:
a[i] += next_tmp+1
cnt +=abs(next_tmp+1)
# 現在の合計がプラスの場合
elif now_tmp > 0 :
a[i] += -next_tmp-1
cnt +=abs(next_tmp+1)
# 現在の合計が0の場合
elif now_tmp == 0 :
# 1個前がプラスの場合、
if sum(a[:i-1]) > 0:
a[i] -=1
cnt +=1
# 1個前がマイナスの場合
else:
a[i] +=1
cnt +=1
print(cnt) |
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n=int(input())
a=list(map(int,input().split()))
s=[0]*n
s[0]=a[0]
cnt=0
for i in range(1,n):
s[i]=a[i]+s[i-1]
num=0
if s[i]<0 and s[i-1]<0:
num+=1-s[i]
cnt+=num
a[i]+=num
s[i]=s[i-1]+a[i]
elif 0<s[i] and 0<s[i-1]:
num+=(-1-s[i])
cnt+=abs(num)
a[i]+=num
s[i]=s[i-1]+a[i]
elif s[i]==0:
if s[i-1]<0:
s[i]+=1
cnt+=1
elif 0<s[i-1]:
s[i]-=1
cnt+=1
print(cnt) |
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException{
Sequence solver = new Sequence();
solver.readInput();
solver.solve();
solver.writeOutput();
}
static class Sequence {
private int n;
private int a[];
private int output;
private Scanner scanner;
public Sequence() {
this.scanner = new Scanner(System.in);
}
public void readInput() {
n = Integer.parseInt(scanner.next());
a = new int[n];
for(int i=0; i<n; i++) {
a[i] = Integer.parseInt(scanner.next());
}
}
private int count(boolean sign) {
int count=0;
long sum=0;
for(int i=0; i<n; i++) {
sum += a[i];
if((i%2==0) == sign) {
// a[i]までの合計を正にするとき
if(sum<=0) {
count += Math.abs(sum)+1;
sum = 1;
}
} else if((i%2==0) != sign){
// a[i]までの合計を負にするとき
if(0<=sum) {
count += Math.abs(sum)+1;
sum = -1;
}
}
}
return count;
}
public void solve() {
output = Math.min(count(true), count(false));
}
public void writeOutput() {
System.out.println(output);
}
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
inline void chmin(T1 &a, T2 b) {
if (a > b) a = b;
}
template <typename T1, typename T2>
inline void chmax(T1 &a, T2 b) {
if (a < b) a = b;
}
template <typename T>
T gcd(T a, T b) {
if (b == 0) return a;
return gcd(b, a % b);
}
int ans = (int)(1e9 + 7);
void solve(int n, vector<int> a, int key) {
int sum = 0;
int cnt = 0;
for (int i = 0; i < (n); ++i) {
if ((key == 0 && i % 2 == 0) || (key == 1 && i % 2 != 0)) {
sum += a[i];
if (sum == 0) {
cnt++;
sum = 1;
} else if (sum < 0) {
cnt += -sum + 1;
sum = 1;
}
} else {
sum += a[i];
if (sum == 0) {
cnt++;
sum = -1;
} else if (sum > 0) {
cnt += sum + 1;
sum = -1;
}
}
}
chmin(ans, cnt);
}
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int &ai : a) cin >> ai;
solve(n, a, 0);
solve(n, a, 1);
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.Arrays;
import java.util.Scanner;
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
int n = scanner.nextInt();
int[] a = new int[n];
IntStream.range(0, n).forEach(i -> a[i] = scanner.nextInt());
// sum1=[1,-1,...], sum2=[-1,1,...]
int[] sum1 = new int[n], sum2 = new int[n];
Arrays.fill(sum1, 1);
Arrays.fill(sum2, -1);
IntStream.range(0, n / 2).forEach(i -> {
sum1[2 * i + 1] = -1;
sum2[2 * i + 1] = 1;
});
System.out.println(Math.min(getResult(a, sum1), getResult(a, sum2)));
}
}
/**
* @param a 数値配列
* @param sum 変更したい合計値の配列
* @return 変更すべきステップ数
*/
private static int getResult(final int[] a, final int[] sum) {
int n = a.length, now = 0, result = 0;
for (int i = 0; i < n; i++) {
now += a[i];
if (sum[i] * now <= 0) {
result += Math.abs(sum[i] - now);
now = sum[i];
}
}
return result;
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | #!/usr/bin/env python3
import copy
n = int(input())
am = list(map(int, input().split()))
pcnt = 0
mcnt = 0
f = 0 #前の項の符号(1:+、2:-)
wa = 0
#最初の数字を正とするとき
a = copy.copy(am)
f = 2
for i in range(len(a)):
wa += a[i]
if f == 1:#-にする必要あり
if wa > -1:
pcnt += wa + 1
a[i] -= wa + 1
f = 2
elif f == 2: #+にする必要あり
if wa < 1:
pcnt += -wa + 1
a[i] += -wa + 1
f = 1
wa = sum(a[0: i + 1])
#最初の数字を負とするとき
a = copy.copy(am)
f = 1
for i in range(len(a)):
wa += a[i]
if f == 1: # -にする必要あり
if wa > -1:
mcnt += wa + 1
a[i] -= wa + 1
f = 2
elif f == 2: # +にする必要あり
if wa < 1:
mcnt += -wa + 1
a[i] += -wa + 1
f = 1
wa = sum(a[0: i + 1])
print(min(pcnt, mcnt))
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int maxn = 200010;
long long n, m, md, ans;
long long a[maxn], pre[maxn];
;
long long read() {
long long s = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
s = s * 10 + ch - '0';
ch = getchar();
}
return s * f;
}
int main() {
md = 0, ans = 0;
memset(pre, 0, sizeof(pre));
n = read();
for (long long i = 1; i <= n; i++) {
a[i] = read();
pre[i] = a[i];
pre[i] += pre[i - 1];
}
for (long long i = 1; i < n; i++) {
long long tmp = md;
if (((pre[i] + tmp) * (pre[i + 1] + tmp) >= 0)) {
if ((pre[i] + tmp) < 0) {
md += (1ll - (pre[i + 1] + tmp));
ans += (1ll - (pre[i + 1] + tmp));
} else {
md -= ((pre[i + 1] + tmp) + 1ll);
ans += (1ll + (pre[i + 1] + tmp));
}
}
}
printf("%lld\n", ans);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | def count_op(n, a, i_sum):
op_num = 0
for i in range(1, n):
tmp_sum = i_sum + a[i]
if i_sum > 0:
if tmp_sum == 0:
op_num += 1
a[i] -= 1
tmp_sum -= 1
elif tmp_sum > 0:
op_num += tmp_sum + 1
a[i] -= tmp_sum + 1
tmp_sum = -1
else:
if tmp_sum == 0:
op_num += 1
a[i] += 1
tmp_sum += 1
elif tmp_sum < 0:
op_num += -tmp_sum + 1
a[i] += -tmp_sum + 1
tmp_sum = 1
i_sum = tmp_sum
return op_num
n = int(input())
a = [int(i) for i in input().split()]
if a[0] != 0:
i_sum = a[0]
print(count_op(n, a, a[0]))
else:
b = a[:]
a[0] = 1
op_num_a = count_op(n, a, a[0]) + 1
b[0] = -1
op_num_b = count_op(n, b, b[0]) + 1
print(min(op_num_a, op_num_b)) |
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<ll> v(n, 0ll);
for (int i = (int)(0); i < (int)(n); i++) cin >> v[i];
vector<ll> memo(n + 1, 0ll);
memo[1] = v[0];
ll cnt = 0;
for (int i = 2; i <= n; i++) {
memo[i] = memo[i - 1] + v[i - 1];
cerr << "memo"
":[ ";
for (auto macro_vi : memo) {
cerr << macro_vi << " ";
}
cerr << "]" << endl;
if (not(memo[i - 1] * memo[i] < 0)) {
if (memo[i] < 0) {
int plus = memo[i] - 1 + 1;
memo[i] += memo[i] * -1 + 1;
cnt += plus;
} else if (memo[i] > 0) {
int plus = memo[i] * -1 - 1;
memo[i] += memo[i] * -1 - 1;
cnt -= plus;
} else {
if (memo[i - 1] < 0) {
memo[i]++;
cnt++;
} else {
memo[i]--;
cnt++;
}
}
}
cerr << "("
"i"
","
"cnt"
"):("
<< i << "," << cnt << ")" << endl;
cerr << "memo"
":[ ";
for (auto macro_vi : memo) {
cerr << macro_vi << " ";
}
cerr << "]" << endl;
}
cout << cnt << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | package main
import "fmt"
func main() {
var n, w, t, cnt, s int
var isPlus bool
fmt.Scan(&n)
fmt.Scan(&t)
if t > 0 {
isPlus = true
}
for i := 0; i < n-1; i++ {
fmt.Scan(&w)
t += w
s = 0
if isPlus {
if t >= 0 {
s = t + 1
t = -1
}
isPlus = false
} else {
if t <= 0 {
s = t*-1 + 1
t = 1
}
isPlus = true
}
cnt += s
}
fmt.Println(cnt)
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long INF = 100100100100100100;
const long long MOD = 1000000007;
long long my_abs(long long a);
long long a_n(long long a, long long n);
long long my_gcd(long long a, long long b);
long long inv(long long a);
long long madd(long long a, long long b, long long c);
long long msub(long long a, long long b);
long long mtime(long long a, long long b, long long c);
bool nega(long long a) {
if (a < 0)
return true;
else
return false;
}
bool posi(long long a) {
if (a > 0)
return true;
else
return false;
}
int main() {
long long n;
cin >> n;
vector<long long> a(n);
for (long long(i) = 0; (i) < (long long)(n); (i)++) cin >> a[i];
long long ans = 0, sum = 0;
for (long long(i) = 0; (i) < (long long)(n); (i)++) {
sum += a[i];
if (i == 0)
continue;
else {
if (sum == 0) {
if (sum - a[i] > 0) {
ans++;
sum--;
} else {
ans++;
sum++;
}
} else if (nega(sum - a[i]) && nega(sum)) {
ans += (my_abs(sum) + 1);
sum += (my_abs(sum) + 1);
} else if (posi(sum - a[i]) && posi(sum)) {
ans += (sum + 1);
sum -= (sum + 1);
}
}
}
cout << ans << endl;
return 0;
}
long long my_abs(long long a) {
if (a >= 0)
return a;
else
return -1 * a;
}
long long a_n(long long a, long long n) {
if (n == 0) return 1;
long long ret = a, count = 1;
while (count * 2 < n) {
ret *= ret;
count *= 2;
}
if (count == n)
return ret;
else
return (ret * a_n(a, n - count));
}
long long my_gcd(long long a, long long b) {
if (b == 0) return a;
return my_gcd(b, a % b);
}
long long inv(long long a) { return a_n(a, MOD - 2); }
long long madd(long long a, long long b, long long c) {
long long ret = (a + b) % MOD;
return (ret + c) % MOD;
}
long long msub(long long a, long long b) {
if (a < b)
return (a - b + MOD) % MOD;
else
return (a - b) % MOD;
}
long long mtime(long long a, long long b, long long c) {
long long ret = (a * b) % MOD;
return (ret * c) % MOD;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
A = list(map(int, input().split()))
acc = [0] * n
acc[0] = A[0]
for i in range(1, n):
acc[i] = acc[i - 1] + A[i]
ans = 0
cur = acc[0]
x = 0
for i in range(1, n):
acc[i] += x
if cur > 0:
if acc[i] >= 0:
ans += acc[i] + 1
x -= acc[i] + 1
acc[i] = -1
else:
if acc[i] < 0:
ans += abs(acc[i]) + 1
x += abs(acc[i]) + 1
acc[i] = 1
cur = acc[i]
if acc[n - 1] == 0:
ans += 1
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
int[] list = new int[sc.nextInt()];
for (int i=0; i < list.length ; i++){
list[i] = sc.nextInt();
}
int beforeSum = 0;
int cnt = 0;
for (int i=0; i < list.length ; i++){
int sum = beforeSum + list[i];
if(i != 0){
if( beforeSum * sum > 0) {
int orgNum = list[i];
list[i] = - (beforeSum + beforeSum/Math.abs(beforeSum));
cnt += Math.abs(orgNum - list[i]);
sum = beforeSum + list[i];
}
else if (beforeSum * sum == 0){
list[i] -= beforeSum/Math.abs(beforeSum);
cnt++;
sum = beforeSum + list[i];
}
}
beforeSum = sum;
}
System.out.println(cnt);
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
auto calc(std::vector<int64_t>& vec, int64_t sum) -> uint64_t {
uint64_t result = 0;
bool is_sum_negative = sum < 0;
for (int i = 1; i < vec.size(); ++i) {
sum += vec[i];
auto tmp = std::abs(sum) + 1;
if (is_sum_negative) {
if (sum <= 0) {
sum += tmp;
result += tmp;
assert(sum == 1);
}
} else {
if (sum >= 0) {
sum -= tmp;
result += tmp;
assert(sum == -1);
}
}
is_sum_negative = !is_sum_negative;
}
return result;
}
int main(int argc, char const* argv[]) {
uint64_t n;
std::cin >> n;
auto vec = std::vector<int64_t>(n);
for (auto& v : vec) {
std::cin >> v;
}
int64_t sum = vec[0];
auto result_0 = std::numeric_limits<uint64_t>::max();
auto result_1 = result_0;
if (sum == 0) {
sum = -1;
result_0 = 1;
result_0 += calc(vec, sum);
sum = 1;
result_1 = 1;
result_1 += calc(vec, sum);
} else {
result_0 = calc(vec, sum);
}
auto result = std::min(result_0, result_1);
std::cout << result << std::endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] a = new int[n];
int[] s = new int[n];
int sum = 0;
a[0] = scanner.nextInt();
sum += a[0];
boolean check;
if(a[0] < 0){
check = false;
}else{
check = true;
}
int count = 0;
for(int i=1;i<n;i++){
a[i] = scanner.nextInt();
int x = sum + a[i];
int y = 0;
if(check && x >= 0){
y = -1 - x;
}else if(!check && x < 0){
y = 1 - x;
}
a[i] += y;
count += Math.abs(y);
sum += a[i];
//System.out.println(y + " " + a[i] + " " + sum);
check = !check;
}
if(sum == 0){
count ++;
}
System.out.println(count);
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int Even(vector<int> a) {
int res = 0;
int temp = 0;
for (long long i = 0; i < (long long)(a.size()); i++) {
temp += a[i];
if (i % 2 == 0) {
while (temp <= 0) {
temp += 1;
res += 1;
}
} else {
while (temp >= 0) {
temp += -1;
res += 1;
}
}
}
return res;
}
int Odd(vector<int> a) {
int res = 0;
int temp = 0;
for (long long i = 0; i < (long long)(a.size()); i++) {
temp += a[i];
if (i % 2 == 0) {
while (temp >= 0) {
temp += -1;
res += 1;
}
} else {
while (temp <= 0) {
temp += 1;
res += 1;
}
}
}
return res;
}
int main() {
int n, ans;
cin >> n;
vector<int> a(n);
for (long long i = 0; i < (long long)(n); i++) {
cin >> a[i];
}
int cnt = 0;
int b[n];
b[0] = a[0];
if (b[0] == 0 and a[1] > 0) {
b[0] = -1;
cnt += 1;
} else if (b[0] == 0 and a[1] < 0) {
b[0] = 1;
cnt += 1;
}
cout << min(Even(a), Odd(a)) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | function Main(s) {
var s = s.split("\n");
var n = parseInt(s[0], 10);
var a = s[1].split(" ").map(e => parseInt(e, 10));
var acc1 = 0, cnt1 = 0, arr1 = [];
var acc2 = 0, cnt2 = 0, arr2 = [];
for (var i = 0; i < n; i++) {
acc1 += a[i];
if (i != 0) {
if (arr1[i - 1] > 0) {
if (acc1 >= 0) {
cnt1 += (acc1 + 1);
acc1 -= (acc1 + 1);
}
} else {
if (acc1 <= 0) {
cnt1 += (Math.abs(acc1) + 1);
acc1 += (Math.abs(acc1) + 1);
}
}
}
arr1.push(acc1);
}
console.log(cnt1);
}
Main(require("fs").readFileSync("/dev/stdin", "utf8")); |
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, chk;
long long ans = 0, ans2 = 0;
scanf("%d", &n);
vector<int> a(n);
for (auto& e : a) scanf("%d", &e);
chk = a[0];
for (int i = 1; i < n; i++) {
if (i % 2) {
chk += a[i];
if (chk >= 0) {
ans += chk + 1;
chk = -1;
}
} else {
chk += a[i];
if (chk <= 0) {
ans += -1 * chk + 1;
chk = 1;
}
}
}
chk = a[0];
for (int i = 1; i < n; i++) {
if (i % 2 == 0) {
chk += a[i];
if (chk >= 0) {
ans += chk + 1;
chk = -1;
}
} else {
chk += a[i];
if (chk <= 0) {
ans += -1 * chk + 1;
chk = 1;
}
}
}
if (a[0] == 0)
printf("%lld\n", min(ans, ans2));
else if (a[0] > 0)
printf("%lld\n", ans);
else
printf("%lld\n", ans2);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, a, ans = 0, d = 0;
cin >> n >> a;
int sum[n];
sum[0] = a;
for (int i = 1; i < n; i++) {
cin >> a;
sum[i] = sum[i - 1] + a;
}
if (sum[0] == 0) d++;
for (int i = 1; i < n; i++) {
if ((sum[i - 1] + d) * (sum[i] + d) >= 0) {
ans += abs(sum[i] + d) + 1;
if (sum[i - 1] + d < 0) {
d += -(sum[i] + d) + 1;
} else if (sum[i - 1] + d > 0) {
d += -(sum[i] + d) - 1;
}
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
result = []
for i in range(1):
num = 0
r = 0
for j in range(len(a)):
num += a[j]
if (j + i) % 2 == 0:
if num <= 0:
r -= num - 1
num = 1
else:
if num >= 0:
r += num + 1
num = -1
result.append(r)
print(min(result))
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num_count = sc.nextInt();
int[] numbers = new int[num_count];
for(int i = 0;i < numbers.length;i++){
numbers[i] = sc.nextInt();
}
int res = 0;
int id = 0;
do{
boolean flag = true;
int sum = 0;
int pre_sum = 0;
int i;
for(i = 0;i < numbers.length;i++){
sum += numbers[i];
if(pre_sum > 0 && sum > 0){
flag = false;
break;
}
if(pre_sum < 0 && sum < 0){
flag = false;
break;
}
if(sum == 0){
flag = false;
break;
}
pre_sum = sum;
}
if(flag)break;
if(sum > 0){
numbers[i] -= sum + 1;
res += sum + 1;
}else{
numbers[i] -= sum - 1;
res += -sum + 1;
}
}while(true);
System.out.println(res);
sc.close();
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long mod = 1e09;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
long long a[n];
for (int i = 0; i < n; i++) cin >> a[i];
long long s[n + 1];
s[0] = 0;
int cp = 0, cm = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
if (s[i] + a[i] <= 0) {
cp += 1 - (s[i] + a[i]);
s[i + 1] = 1;
} else
s[i + 1] = s[i] + a[i];
} else {
if (s[i] + a[i] >= 0) {
cp += 1 + (s[i] + a[i]);
s[i + 1] = -1;
} else
s[i + 1] = s[i] + a[i];
}
}
for (int i = 0; i < n; i++) {
if (i % 2) {
if (s[i] + a[i] <= 0) {
cm += 1 - (s[i] + a[i]);
s[i + 1] = 1;
} else
s[i + 1] = s[i] + a[i];
} else {
if (s[i] + a[i] >= 0) {
cm += 1 + (s[i] + a[i]);
s[i + 1] = -1;
} else
s[i + 1] = s[i] + a[i];
}
}
cout << min(cp, cm) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void) {
long long n, i, j, sw, sw2, count = 0, add = 0, adda = 0;
cin >> n;
vector<long long> a(n);
for (i = 0; i < n; i++) {
cin >> a[i];
adda += a[i];
}
if (a[0] == 0) {
a[0]++;
count++;
add++;
}
if (a[0] > 0)
sw = 1;
else
sw = -1;
add += a[0];
if ((adda > 0 && n % 2 == 1) || (adda < 0 && n % 2 == 0)) {
} else {
if (a[0] < 0) {
while (a[0] != 1) {
add++;
a[0]++;
count++;
}
} else {
while (a[0] != -1) {
add--;
a[0]--;
count++;
}
}
}
if (a[0] > 0)
sw = 1;
else
sw = -1;
for (i = 1; i < n; i++) {
add += a[i];
if (sw == 1) {
if (add < 0) {
} else {
while (add != -1) {
a[i]--;
add--;
count++;
}
}
} else {
if (add > 0) {
} else {
while (add != 1) {
a[i]++;
add++;
count++;
}
}
}
if (a[i] > 0)
sw = 1;
else
sw = -1;
}
cout << count << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int get_sum(vector<int> &a, int position) {
int sum = 0;
for (int i = 0; i <= position; i++) {
sum += a[i];
}
return sum;
}
int get_operation_count(vector<int> &a, int n, bool is_start_negative) {
int diff_sum = 0;
int operation_count = 0;
for (int i = 0; i < n; i++) {
int sum = get_sum(a, i) + diff_sum;
int is_positive = i % 2 == 0;
if (is_start_negative) is_positive = !is_positive;
if (is_positive && sum <= 0) {
int diff_abs = abs(1 - sum);
operation_count += diff_abs;
diff_sum += diff_abs;
} else if (!is_positive && 0 <= sum) {
int diff_abs = abs(-1 - sum);
operation_count += diff_abs;
diff_sum -= diff_abs;
}
}
if (get_sum(a, n) == 0) operation_count++;
return operation_count;
}
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
int positive_count = get_operation_count(a, n, true);
int negative_count = get_operation_count(a, n, false);
cout << min(negative_count, positive_count) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
long long sum;
cin >> n >> sum;
long long ans = 0;
if (sum == 0) {
sum = 1;
ans = 1;
}
for (int i = 0; i < n - 1; ++i) {
int a;
cin >> a;
if ((a + sum) * sum >= 0) {
if (sum > 0) {
ans += a + sum + 1;
sum = -1;
} else {
ans += -(a + sum) + 1;
sum = 1;
}
} else {
sum += a;
}
}
cout << ans << "\n";
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
a = list(map(int, input().split()))
res = []
for start in [1, -1]:
ans = 0
_a = [a[0]]
prev_sign = start
for i in range(0, N):
c = a[i]
if c >= 0 and prev_sign > 0:
ans += abs(c - (-1))
c = -1
elif c <= 0 and prev_sign < 0:
ans += abs(c - 1)
c = 1
if c > 0:
prev_sign = 1
else:
prev_sign = -1
_a.append(c)
__a = [_a[0]]
acm_sum = _a[0]
for i in range(1, N):
c = _a[i]
if abs(acm_sum) >= abs(c):
if c < 0:
c = -1*(abs(acm_sum)+1)
else:
c = abs(acm_sum)+1
acm_sum += c
ans += abs(c - _a[i])
__a.append(c)
res.append(ans)
print(min(res)) |
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | package sample.code;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
long[] a = null;
try(Scanner sc = new Scanner(System.in)){
// N入力 aスペース区切り入力
a = new long[sc.nextInt()];
for(int i = 0; i < a.length; i ++) {
a[i] = sc.nextLong();
}
}
long start = a[0];
long start2 = a[1];
long ret = 0L;
if(start > 0 && start2 > 0) {
if(start < start2) {
ret += Math.abs(start - start2) +1;
start = -1;
}
} else if (start <= 0 && start2 <= 0) {
if(start > start2) {
ret += Math.abs(start - start2) +1;
start = +1;
}
}
boolean isNaturalNum = true;
if(start < 0) {
isNaturalNum = false;
}
for(int i = 1; i < a.length; i++) {
long temp2 = start + a[i];
if(isNaturalNum) {
if(temp2 >= 0) {
ret += Math.abs(start + a[i]) + 1 ;
start = -1;
} else {
//OK
start = temp2;
}
isNaturalNum = false;
} else {
if(temp2 <= 0) {
ret += Math.abs(start + a[i]) + 1;
start = 1;
} else {
start = temp2;
}
isNaturalNum = true;
}
}
System.out.println(ret);
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long MOD7 = 1000000007;
const long long MOD9 = 1000000009;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
long long N;
cin >> N;
vector<long long> vec(N);
for (long long i = 0; i < N; i++) cin >> vec[i];
long long res, partial, distance_0;
vector<long long> res_vec;
bool flag_before;
for (long long n = 0; n < 2; ++n) {
res = 0;
if (vec[0] == 0) {
if (n == 0) {
partial = +1;
} else {
partial = -1;
}
} else {
partial = vec[0];
}
flag_before = partial > 0;
for (long long i = 1; i < N; ++i) {
partial += vec[i];
distance_0 = abs(partial) + 1;
if (flag_before) {
if (partial >= 0) {
res += distance_0;
partial -= distance_0;
}
} else {
if (partial <= 0) {
res += distance_0;
partial += distance_0;
}
}
flag_before = !flag_before;
}
res_vec.push_back(res);
}
cout << *min_element(((res_vec)).begin(), ((res_vec)).end()) << "\n";
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(int argc, const char* argv[]) {
int n;
cin >> n;
int a[100010];
for (int i = 0; i < n; ++i) cin >> a[i];
long long int res = 0;
bool plus = false;
long long int sum = a[0];
if (a[0] > 0)
plus = true;
else if (a[0] < 0)
plus = false;
int j = 1;
while (sum == 0) {
if (a[j] > 0) {
++res;
sum = (j % 2 == 0) ? 1 : -1;
plus = (j % 2 == 0) ? true : false;
} else if (a[j] < 0) {
++res;
sum = (j % 2 == 0) ? -1 : 1;
plus = (j % 2 == 0) ? false : true;
}
++j;
if (j == n) {
cout << 1 + 2 * (n - 1) << endl;
return 0;
}
}
for (int i = 0; i < n - 1; ++i) {
if (sum + a[i + 1] > 0) {
if (plus == true) {
res += sum + a[i + 1] + 1;
sum = -1;
plus = false;
} else {
sum += a[i + 1];
plus = true;
}
} else if (sum + a[i + 1] < 0) {
if (plus == false) {
res += -(sum + a[i + 1] - 1);
sum = 1;
plus = true;
} else {
sum += a[i + 1];
plus = false;
}
} else if (sum + a[i + 1] == 0) {
if (plus == true) {
++res;
sum = -1;
} else {
++res;
sum = 1;
}
}
}
cout << res << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
long long int func(std::vector<long long int>& a, int n) {
long long int count = 0;
signed long long sum = a[0];
for (int i = 1; i < n; i++) {
if (sum >= 0) {
sum += a[i];
if (sum >= 0) {
count += sum + 1;
sum = -1;
}
} else {
sum += a[i];
if (sum <= 0) {
count += -sum + 1;
sum = 1;
}
}
}
return count;
}
int main(void) {
int n;
std::cin >> n;
std::vector<long long int> a(n);
for (int i = 0; i < n; i++) {
std::cin >> a[i];
}
long long int count = 0;
if (a[0] == 0) {
count++;
a[0] = -1;
long long int count1 = func(a, n);
a[0] = +1;
long long int count2 = func(a, n);
count += std::min(count1, count2);
} else {
count += func(a, n);
}
std::cout << count << std::endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python2 | n=int(raw_input())
a=map(int,raw_input().split(' '))
c=0
for i in range(n):
s=sum(a[0:i+1])
if s==0:
if i==n-1:
a[i]+=1
c+=1
elif a[i+1]>=0:
a[i]-=1
c+=1
else:
a[i]+=1
c+=1
if i==(n-1): break
s=sum(a[0:i+1])
n_s=s+a[i+1]
if s*n_s>0:
if s>=0:
while n_s>=0:
a[i+1]-=1
c+=1
n_s=s+a[i+1]
else:
while n_s<=0:
a[i+1]+=1
c+=1
n_s=s+a[i+1]
print a
print c |
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | def first_positive(n,a):
count = 0
sum = a[0]
#正ならTrue
flag = True
if a[0] <= 0:
sum = 1
count += abs(1 - a[0])
for i in range(1,n):
if sum + a[i] < 0 and flag == True:
sum += a[i]
flag = False
continue
elif sum + a[i] < 0 and flag == False:
flag = True
count += abs(1 - sum - a[i])
sum = 1
elif sum + a[i] >= 0 and flag == True:
flag = False
count += abs(-1 - sum - a[i])
sum = -1
elif sum + a[i] >= 0 and flag == False:
sum += a[i]
flag = True
continue
return count
def first_negative(n,a):
count = 0
sum = a[0]
#正ならTrue
flag = True
if a[0] >= 0:
sum = -1
count += abs(-1 - a[0])
for i in range(1,n):
if sum + a[i] < 0 and flag == True:
sum += a[i]
flag = False
continue
elif sum + a[i] < 0 and flag == False:
flag = True
count += abs(1 - sum - a[i])
sum = 1
elif sum + a[i] >= 0 and flag == True:
flag = False
count += abs(-1 - sum - a[i])
sum = -1
elif sum + a[i] >= 0 and flag == False:
sum += a[i]
flag = True
continue
return count
if __name__ == '__main__':
n = int(input())
a = [int(i) for i in input().split()]
p = first_positive(n,a)
n = first_negative(n,a)
if p < n:
print(p)
else:
print(n)
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
A = list(map(int, input().split()))
ans_1 = 0
sum_1 = A[0]
if A[0] <= 0:
ans_1 += abs(A[0])+1
sum_1 = ans_1
for i in range(1, len(A)):
a = A[i]
prev_sum = sum_1
sum_1 += a
if sum_1 * prev_sum >= 0:
ans_1 += abs(prev_sum+a)+1
if prev_sum > 0:
sum_1 = -1
else:
sum_1 = 1
ans_2 = 0
sum_2 = A[0]
if A[0] >= 0:
ans_2 += abs(A[0])+1
sum_2 = -ans_2
for i in range(1, len(A)):
a = A[i]
prev_sum = sum_2
sum_2 += a
if sum_2 * prev_sum >= 0:
ans_2 += abs(prev_sum+a)+1
if prev_sum > 0:
sum_2 = -1
else:
sum_2 = 1
print(min(ans_1, ans_2))
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
A = [int(i) for i in input().split()]
seq_sum = A[0]
sign = 1 if seq_sum >= 0 else -1
count = 0
for i in range(1, n):
if A[i] * sign * (-1) > seq_sum * sign:
seq_sum += A[i]
sign = sign * (-1)
else:
count += abs(sign * (-1) - (seq_sum + A[i]))
A[i] += sign * (-1) - (seq_sum + A[i])
seq_sum += A[i]
sign = sign * (-1)
print(count) |
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
int main() {
long long n;
cin >> n;
long long c[2], s[2];
long long a;
for (int i = 0; i != n; ++i) {
cin >> a;
for (int j : {0, 1}) {
s[j] += a;
auto p = 1 - (i + j) % 2 * 2;
if (s[j] * p <= 0) {
c[j] += abs(p - s[j]);
s[j] = p;
}
}
}
cout << min(c[0], c[1]) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long int n;
cin >> n;
vector<long long int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long int ans = 0;
long long int cnt = a[0];
for (int i = 1; i < n; i++) {
if (i % 2 == 0) {
if (0 < cnt + a[i]) {
cnt += a[i];
} else {
ans += 1 - (cnt + a[i]);
cnt = 1;
}
} else {
if (cnt + a[i] < 0) {
cnt += a[i];
} else {
ans += 1 + (cnt + a[i]);
cnt = -1;
}
}
}
long long int ans2 = 0;
cnt = a[0];
for (int i = 1; i < n; i++) {
if (i % 2 == 1) {
if (0 < cnt + a[i]) {
cnt += a[i];
} else {
ans2 += 1 - (cnt + a[i]);
cnt = 1;
}
} else {
if (cnt + a[i] < 0) {
cnt += a[i];
} else {
ans2 += 1 + (cnt + a[i]);
cnt = -1;
}
}
}
if (ans > ans2) {
ans = ans2;
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> vec;
int a;
for (int i = 0; i < n; i++) {
cin >> a;
vec.emplace_back(a);
}
int ans = 0;
int wa[100001];
wa[0] = vec[0];
for (int i = 1; i < n; i++) {
wa[i] = wa[i - 1] + vec[i];
if (wa[i - 1] > 0) {
if (wa[i] >= 0) {
ans += wa[i] + 1;
wa[i] = -1;
}
} else if (wa[i - 1] < 0) {
if (wa[i] <= 0) {
ans += -wa[i] + 1;
wa[i] = 1;
}
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N = 0;
cin >> N;
vector<long long int> A(N), a(N), b(N);
for (int i = 0; i < N; i++) cin >> A[i];
for (int i = 0; i < N; i++) a[i] = A[i];
for (int i = 0; i < N; i++) b[i] = A[i];
long long int M = 0;
long long int cnt1 = 0, cnt2 = 0;
cnt1 += abs(1 - a[0]);
a[0] = 1;
for (int i = 1; i < N; i++) {
M += a[i - 1];
if (i % 2 == 0) {
cnt1 += abs(1 - (M + a[i]));
a[i] = 1 - (M + a[i]);
} else {
cnt1 += abs(-1 - (M + a[i]));
a[i] = -1 - (M + a[i]);
}
}
cnt2 += abs(-1 - b[0]);
b[0] = -1;
for (int i = 1; i < N; i++) {
M += b[i - 1];
if (i % 2 == 1) {
cnt2 += abs(1 - (M + b[i]));
a[i] = 1 - (M + b[i]);
} else {
cnt2 += abs(-1 - (M + b[i]));
a[i] = -1 - (M + b[i]);
}
}
long long int ans = min(cnt1, cnt2);
cout << ans;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<long long int> A(N);
for (int i = 0; i < N; i++) cin >> A[i];
long long unsigned int cnt, cnt1, cnt2 = 0;
long long int M = 0;
M = A[0];
for (int i = 1; i < N; i++) {
if (i % 2 == 0) {
cnt1 += abs(1 - M);
} else {
cnt1 += abs(-1 - M);
}
M += A[i];
}
M = A[0];
for (int i = 1; i < N; i++) {
if (i % 2 == 1) {
cnt2 += abs(1 - M);
} else {
cnt2 += abs(-1 - M);
}
}
cnt = min(cnt1, cnt2);
cout << cnt;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
a = [int(i) for i in input().split()]
def pura(A):
ans = 0
check = 0
if A[0] <= 0:
ans += abs(1-A[0])
A[0] = 1
check += A[0]
for i in range(1, N):
if i % 2 != 0:
if check + A[i] >= 0:
ans += abs(A[i] + 1 + check)
A[i] = -1 + check
else:
if check + A[i] <= 0:
ans += abs(A[i] - (1 + check))
A[i] = 1 - check
check += A[i]
return ans
print(pura(a)) |
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n, *A = map(int, open(0).read().split())
def sgn(n):
return 0 if n==0 else 1 if n>0 else -1
C = [0, 0]
S = [1, -1]
for a in A:
for i, s in enumerate(S):
sgn_sum = sgn(s)
if sgn(s+a) == -sgn_sum:
S[i] += a
else:
C[i] += abs(s+a+sgn_sum)
S[i] = -sgn_sum
print(min(C)) |
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void) {
long long n, i, j, sw, sw2, count = 0, add = 0;
cin >> n;
vector<long long> a(n);
for (i = 0; i < n; i++) cin >> a[i];
if (a[0] > 0)
sw = 1;
else
sw = -1;
add += a[0];
for (i = 1; i < n; i++) {
add += a[i];
if (sw == 1) {
if (add < 0) {
} else {
if (a[i] >= 0) {
while (add != -1) {
a[i]--;
add--;
count++;
}
} else {
while (add != -1) {
a[i]--;
add--;
count++;
}
}
}
} else {
if (add > 0) {
} else {
if (a[i] <= 0) {
while (add != 1) {
a[i]++;
add++;
count++;
}
} else {
while (add != 1) {
a[i]++;
add++;
count++;
}
}
}
}
if (a[i] > 0)
sw = 1;
else
sw = -1;
}
cout << count << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = input()
a = map(int, input().split())
def f(x):
cnt = 0
cur = 0
for ai in a:
cur += ai
cur *= x
if cur < 0:
cnt += 1 - ai
x *= -1
print(cnt)
print(min(f(1), f(-1)) |
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int,input().split()))
ttl = a[0]
cst = 0
if a[0]>=0:
flg = 1
elif a[0]<0:
flg = -1
for i in range(1,n):
ttl += a[i]
if ttl*flg < 0:
flg *= -1
else:
if flg > 0:
memo = abs(ttl)+1
ttl -= memo
cst += memo
elif flg < 0:
memo = abs(ttl)+1
ttl += memo
cst += memo
flg *= -1
ttl = a[0]
cst2 = 0
print(min(cst,cst2))
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n;
int a[100001];
int main() {
int cul;
int ans = 0;
cin >> n;
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
int j = 0;
int count = 0;
while (a[j] == 0) {
j++;
count++;
}
cul = a[count];
for (int i = count + 1; i < n; i++) {
if (cul > 0) {
while ((a[i] + cul) >= 0) {
ans++;
a[i]--;
}
cul += a[i];
} else {
while ((a[i] + cul) <= 0) {
ans++;
a[i]++;
}
cul += a[i];
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | import Control.Monad
import Data.List
main = readLn >>= main'
where
main' n = getLine >>= print . solve' n . fmap read . words
solve' n (x : xs) = min (solve n $ x : xs) (solve n $ negate x : xs)
solve :: Int -> [Int] -> Int
solve c (x : xs)
| x /= 0 = fst $ foldl' ff (0, x) xs
| null zs = zeroCount c
| otherwise = fst $ foldl' ff (zeroCount $ length ys, negate z `div` abs z) zs
where
ff (acc, s) n
| s * next < 0 = (acc, next)
| s * next > 0 = (acc + abs next + 1 , negate s `div` abs s)
| otherwise = (acc + 1, negate s `div` abs s)
where
next = s + n
(ys, zs) = span (== 0) xs
z = head zs
zeroCount = pred . (2 *)
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, ansa = 0, ansb = 0, suma = 0, sumb = 0;
cin >> n;
for (int i = 0; i < (n); i++) {
int c;
cin >> c;
suma += c;
sumb += c;
if (i % 2 == 0) {
if (suma <= 0) {
ansa += (1 - suma);
suma = 1;
}
if (sumb >= 0) {
ansb += (sumb + 1);
sumb = -1;
}
} else {
if (suma >= 0) {
ansa += (suma + 1);
suma = -1;
}
if (sumb <= 0) {
ansb += (1 - sumb);
sumb = 1;
}
}
}
cout << min(ansa, ansb) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java |
import java.io.*;
import static java.lang.Math.*;
import static java.lang.Math.min;
import java.util.*;
import java.util.stream.*;
/**
* @author baito
*/
class P implements Comparable<P> {
int x, y;
P(int a, int b) {
x = a;
y = b;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof P)) return false;
P p = (P) o;
return x == p.x && y == p.y;
}
@Override
public int hashCode() {
return Objects.hash(x, y);
}
@Override
public int compareTo(P p) {
return x == p.x ? y - p.y : x - p.x; //xで昇順にソート
//return (x == p.x ? y - p.y : x - p.x) * -1; //xで降順にソート
//return y == p.y ? x - p.x : y - p.y;//yで昇順にソート
//return (y == p.y ? x - p.x : y - p.y)*-1;//yで降順にソート
}
}
@SuppressWarnings("unchecked")
public class Main {
static StringBuilder sb = new StringBuilder();
static int INF = 1234567890;
static int MINF = -1234567890;
static long LINF = 123456789123456789L;
static long MLINF = -123456789123456789L;
static long MOD = 1000000007;
static double EPS = 1e-10;
static int[] y4 = {0, 1, 0, -1};
static int[] x4 = {1, 0, -1, 0};
static int[] y8 = {0, 1, 0, -1, -1, 1, 1, -1};
static int[] x8 = {1, 0, -1, 0, 1, -1, 1, -1};
static ArrayList<Long> Fa;
static boolean[] isPrime;
static int[] primes;
static char[][] ban;
static long maxRes = MLINF;
static long minRes = LINF;
static boolean DEBUG = true;
static int N;
static long[] A;
public static void solve() throws Exception {
//longを忘れるなオーバーフローするぞ
N = ni();
A = nla(N);
long sum = A[0];
long cou = 0;
boolean plus = A[0] >= 0 ? false : true;
for (int i = 1; i < N; i++) {
if (plus) {
long now = sum + A[i];
if (now < 0) {
cou += (-now) + 1;
A[i] += (-now) + 1;
} else if (now == 0) {
cou++;
A[i]++;
}
sum += A[i];
plus = false;
} else {
long now = sum + A[i];
if (now > 0) {
cou += (now) + 1;
A[i] -= (now) + 1;
} else if (now == 0) {
cou++;
A[i]--;
}
sum += A[i];
plus = true;
}
}
System.out.println(cou);
}
public static boolean calc(long va) {
//貪欲にギリギリセーフを選んでいく。
int v = (int) va;
return true;
}
//条件を満たす最大値、あるいは最小値を求める
static int mgr(long ok, long ng, long w) {
//int ok = 0; //解が存在する
//int ng = N; //解が存在しない
while (Math.abs(ok - ng) > 1) {
long mid;
if (ok < 0 && ng > 0 || ok > 0 && ng < 0) mid = (ok + ng) / 2;
else mid = ok + (ng - ok) / 2;
if (calc(mid)) {
ok = mid;
} else {
ng = mid;
}
}
if (calc(ok)) return (int) ok;
else return -1;
}
boolean equal(double a, double b) {
return a == 0 ? abs(b) < EPS : abs((a - b) / a) < EPS;
}
public static void matPrint(long[][] a) {
for (int hi = 0; hi < a.length; hi++) {
for (int wi = 0; wi < a[0].length; wi++) {
System.out.print(a[hi][wi] + " ");
}
System.out.println("");
}
}
//rにlを掛ける l * r
public static long[][] matMul(long[][] l, long[][] r) throws IOException {
int lh = l.length;
int lw = l[0].length;
int rh = r.length;
int rw = r[0].length;
//lwとrhが,同じである必要がある
if (lw != rh) throw new IOException();
long[][] res = new long[lh][rw];
for (int i = 0; i < lh; i++) {
for (int j = 0; j < rw; j++) {
for (int k = 0; k < lw; k++) {
res[i][j] = modSum(res[i][j], modMul(l[i][k], r[k][j]));
}
}
}
return res;
}
public static long[][] matPow(long[][] a, int n) throws IOException {
int h = a.length;
int w = a[0].length;
if (h != w) throw new IOException();
long[][] res = new long[h][h];
for (int i = 0; i < h; i++) {
res[i][i] = 1;
}
long[][] pow = a.clone();
while (n > 0) {
if (bitGet(n, 0)) res = matMul(pow, res);
pow = matMul(pow, pow);
n >>= 1;
}
return res;
}
public static void chMax(long v) {
maxRes = Math.max(maxRes, v);
}
public static void chMin(long v) {
minRes = Math.min(minRes, v);
}
//2点間の行き先を配列に持たせる
static int[][] packE(int n, int[] from, int[] to) {
int[][] g = new int[n][];
int[] p = new int[n];
for (int f : from)
p[f]++;
for (int t : to)
p[t]++;
for (int i = 0; i < n; i++)
g[i] = new int[p[i]];
for (int i = 0; i < from.length; i++) {
g[from[i]][--p[from[i]]] = to[i];
g[to[i]][--p[to[i]]] = from[i];
}
return g;
}
public static boolean bitGet(BitSet bit, int keta) {
return bit.nextSetBit(keta) == keta;
}
public static boolean bitGet(long bit, int keta) {
return ((bit >> keta) & 1) == 1;
}
public static int restoreHashA(long key) {
return (int) (key >> 32);
}
public static int restoreHashB(long key) {
return (int) (key & -1);
}
//正の数のみ
public static long getHashKey(int a, int b) {
return (long) a << 32 | b;
}
public static long sqrt(long v) {
long res = (long) Math.sqrt(v);
while (res * res > v) res--;
return res;
}
public static int u0(int a) {
if (a < 0) return 0;
return a;
}
public static long u0(long a) {
if (a < 0) return 0;
return a;
}
public static int[] toIntArray(int a) {
int[] res = new int[keta(a)];
for (int i = res.length - 1; i >= 0; i--) {
res[i] = a % 10;
a /= 10;
}
return res;
}
public static Integer[] toIntegerArray(int[] ar) {
Integer[] res = new Integer[ar.length];
for (int i = 0; i < ar.length; i++) {
res[i] = ar[i];
}
return res;
}
public static long bitGetCombSizeK(int k) {
return (1 << k) - 1;
}
//k個の次の組み合わせをビットで返す 大きさに上限はない 110110 -> 111001
public static long bitNextComb(long comb) {
long x = comb & -comb; //最下位の1
long y = comb + x; //連続した下の1を繰り上がらせる
return ((comb & ~y) / x >> 1) | y;
}
public static int keta(long num) {
int res = 0;
while (num > 0) {
num /= 10;
res++;
}
return res;
}
public static boolean isOutofIndex(int x, int y, int w, int h) {
if (x < 0 || y < 0) return true;
if (w <= x || h <= y) return true;
return false;
}
public static boolean isOutofIndex(int x, int y, char[][] ban) {
if (x < 0 || y < 0) return true;
if (ban[0].length <= x || ban.length <= y) return true;
return false;
}
public static int arrayCount(int[] a, int v) {
int res = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] == v) res++;
}
return res;
}
public static int arrayCount(long[] a, int v) {
int res = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] == v) res++;
}
return res;
}
public static int arrayCount(int[][] a, int v) {
int res = 0;
for (int hi = 0; hi < a.length; hi++) {
for (int wi = 0; wi < a[0].length; wi++) {
if (a[hi][wi] == v) res++;
}
}
return res;
}
public static int arrayCount(long[][] a, int v) {
int res = 0;
for (int hi = 0; hi < a.length; hi++) {
for (int wi = 0; wi < a[0].length; wi++) {
if (a[hi][wi] == v) res++;
}
}
return res;
}
public static int arrayCount(char[][] a, char v) {
int res = 0;
for (int hi = 0; hi < a.length; hi++) {
for (int wi = 0; wi < a[0].length; wi++) {
if (a[hi][wi] == v) res++;
}
}
return res;
}
public static void setPrimes() {
int n = 100001;
isPrime = new boolean[n];
List<Integer> prs = new ArrayList<>();
Arrays.fill(isPrime, true);
isPrime[0] = isPrime[1] = false;
for (int i = 2; i * i <= n; i++) {
if (!isPrime[i]) continue;
prs.add(i);
for (int j = i * 2; j < n; j += i) {
isPrime[j] = false;
}
}
primes = new int[prs.size()];
for (int i = 0; i < prs.size(); i++)
primes[i] = prs.get(i);
}
public static void revSort(int[] a) {
Arrays.sort(a);
reverse(a);
}
public static void revSort(long[] a) {
Arrays.sort(a);
reverse(a);
}
public static int[][] copy(int[][] ar) {
int[][] nr = new int[ar.length][ar[0].length];
for (int i = 0; i < ar.length; i++)
for (int j = 0; j < ar[0].length; j++)
nr[i][j] = ar[i][j];
return nr;
}
/**
* <h1>指定した値以上の先頭のインデクスを返す</h1>
* <p>配列要素が0のときは、0が返る。</p>
*
* @return<b>int</b> : 探索した値以上で、先頭になるインデクス
* 値が無ければ、挿入できる最小のインデックス
*/
public static <T extends Number> int lowerBound(final List<T> lis, final T value) {
int low = 0;
int high = lis.size();
int mid;
while (low < high) {
mid = ((high - low) >>> 1) + low; //(low + high) / 2 (オーバーフロー対策)
if (lis.get(mid).doubleValue() < value.doubleValue()) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
/**
* <h1>指定した値より大きい先頭のインデクスを返す</h1>
* <p>配列要素が0のときは、0が返る。</p>
*
* @return<b>int</b> : 探索した値より上で、先頭になるインデクス
* 値が無ければ、挿入できる最小のインデックス
*/
public static <T extends Number> int upperBound(final List<T> lis, final T value) {
int low = 0;
int high = lis.size();
int mid;
while (low < high) {
mid = ((high - low) >>> 1) + low; //(low + high) / 2 (オーバーフロー対策)
if (lis.get(mid).doubleValue() < value.doubleValue()) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
/**
* <h1>指定した値以上の先頭のインデクスを返す</h1>
* <p>配列要素が0のときは、0が返る。</p>
*
* @return<b>int</b> : 探索した値以上で、先頭になるインデクス
* 値が無ければ、挿入できる最小のインデックス
*/
public static int lowerBound(final int[] arr, final int value) {
int low = 0;
int high = arr.length;
int mid;
while (low < high) {
mid = ((high - low) >>> 1) + low; //(low + high) / 2 (オーバーフロー対策)
if (arr[mid] < value) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
/**
* <h1>指定した値より大きい先頭のインデクスを返す</h1>
* <p>配列要素が0のときは、0が返る。</p>
*
* @return<b>int</b> : 探索した値より上で、先頭になるインデクス
* 値が無ければ、挿入できる最小のインデックス
*/
public static int upperBound(final int[] arr, final int value) {
int low = 0;
int high = arr.length;
int mid;
while (low < high) {
mid = ((high - low) >>> 1) + low; //(low + high) / 2 (オーバーフロー対策)
if (arr[mid] <= value) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
/**
* <h1>指定した値以上の先頭のインデクスを返す</h1>
* <p>配列要素が0のときは、0が返る。</p>
*
* @return<b>int</b> : 探索した値以上で、先頭になるインデクス
* 値がなければ挿入できる最小のインデックス
*/
public static long lowerBound(final long[] arr, final long value) {
int low = 0;
int high = arr.length;
int mid;
while (low < high) {
mid = ((high - low) >>> 1) + low; //(low + high) / 2 (オーバーフロー対策)
if (arr[mid] < value) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
/**
* <h1>指定した値より大きい先頭のインデクスを返す</h1>
* <p>配列要素が0のときは、0が返る。</p>
*
* @return<b>int</b> : 探索した値より上で、先頭になるインデクス
* 値がなければ挿入できる最小のインデックス
*/
public static long upperBound(final long[] arr, final long value) {
int low = 0;
int high = arr.length;
int mid;
while (low < high) {
mid = ((high - low) >>> 1) + low; //(low + high) / 2 (オーバーフロー対策)
if (arr[mid] <= value) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
//次の順列に書き換える、最大値ならfalseを返す
public static boolean nextPermutation(int A[]) {
int len = A.length;
int pos = len - 2;
for (; pos >= 0; pos--) {
if (A[pos] < A[pos + 1]) break;
}
if (pos == -1) return false;
//posより大きい最小の数を二分探索
int ok = pos + 1;
int ng = len;
while (Math.abs(ng - ok) > 1) {
int mid = (ok + ng) / 2;
if (A[mid] > A[pos]) ok = mid;
else ng = mid;
}
swap(A, pos, ok);
reverse(A, pos + 1, len - 1);
return true;
}
//次の順列に書き換える、最小値ならfalseを返す
public static boolean prevPermutation(int A[]) {
int len = A.length;
int pos = len - 2;
for (; pos >= 0; pos--) {
if (A[pos] > A[pos + 1]) break;
}
if (pos == -1) return false;
//posより小さい最大の数を二分探索
int ok = pos + 1;
int ng = len;
while (Math.abs(ng - ok) > 1) {
int mid = (ok + ng) / 2;
if (A[mid] < A[pos]) ok = mid;
else ng = mid;
}
swap(A, pos, ok);
reverse(A, pos + 1, len - 1);
return true;
}
static long ncr2(int a, int b) {
if (b == 0) return 1;
else if (a < b) return 0;
long res = 1;
for (int i = 0; i < b; i++) {
res *= a - i;
res /= i + 1;
}
return res;
}
static long ncrdp(int n, int r) {
if (n < r) return 0;
long[][] dp = new long[n + 1][r + 1];
for (int ni = 0; ni < n + 1; ni++) {
dp[ni][0] = dp[ni][ni] = 1;
for (int ri = 1; ri < ni; ri++) {
dp[ni][ri] = dp[ni - 1][ri - 1] + dp[ni - 1][ri];
}
}
return dp[n][r];
}
public static int mod(int a, int m) {
return a >= 0 ? a % m : (int) (a + ceil(-a * 1.0 / m) * m) % m;
}
static long modNcr(int n, int r) {
if (n < 0 || r < 0 || n < r) return 0;
if (Fa == null || Fa.size() <= n) factorial(n);
long result = Fa.get(n);
result = modMul(result, modInv(Fa.get(n - r)));
result = modMul(result, modInv(Fa.get(r)));
return result;
}
public static long modSum(long... lar) {
long res = 0;
for (long l : lar)
res = (res + l % MOD) % MOD;
if (res < 0) res += MOD;
res %= MOD;
return res;
}
public static long modDiff(long a, long b) {
long res = a % MOD - b % MOD;
if (res < 0) res += MOD;
res %= MOD;
return res;
}
public static long modMul(long... lar) {
long res = 1;
for (long l : lar)
res = (res * l % MOD) % MOD;
if (res < 0) res += MOD;
res %= MOD;
return res;
}
public static long modDiv(long a, long b) {
long x = a % MOD;
long y = b % MOD;
long res = (x * modInv(y)) % MOD;
return res;
}
static long modInv(long n) {
return modPow(n, MOD - 2);
}
static void factorial(int n) {
if (Fa == null) {
Fa = new ArrayList<>();
Fa.add(1L);
Fa.add(1L);
}
for (int i = Fa.size(); i <= n; i++) {
Fa.add((Fa.get(i - 1) * i) % MOD);
}
}
static long modPow(long x, long n) {
long res = 1L;
while (n > 0) {
if ((n & 1) == 1) {
res = res * x % MOD;
}
x = x * x % MOD;
n >>= 1;
}
return res;
}
//↑nCrをmod計算するために必要
static long lcm(long n, long r) {
return n / gcd(n, r) * r;
}
static int gcd(int n, int r) {
return r == 0 ? n : gcd(r, n % r);
}
static long gcd(long n, long r) {
return r == 0 ? n : gcd(r, n % r);
}
static <T> void swap(T[] x, int i, int j) {
T t = x[i];
x[i] = x[j];
x[j] = t;
}
static void swap(int[] x, int i, int j) {
int t = x[i];
x[i] = x[j];
x[j] = t;
}
public static void reverse(int[] x) {
int l = 0;
int r = x.length - 1;
while (l < r) {
int temp = x[l];
x[l] = x[r];
x[r] = temp;
l++;
r--;
}
}
public static void reverse(long[] x) {
int l = 0;
int r = x.length - 1;
while (l < r) {
long temp = x[l];
x[l] = x[r];
x[r] = temp;
l++;
r--;
}
}
public static void reverse(char[] x) {
int l = 0;
int r = x.length - 1;
while (l < r) {
char temp = x[l];
x[l] = x[r];
x[r] = temp;
l++;
r--;
}
}
public static void reverse(int[] x, int s, int e) {
int l = s;
int r = e;
while (l < r) {
int temp = x[l];
x[l] = x[r];
x[r] = temp;
l++;
r--;
}
}
static int length(int a) {
int cou = 0;
while (a != 0) {
a /= 10;
cou++;
}
return cou;
}
static int length(long a) {
int cou = 0;
while (a != 0) {
a /= 10;
cou++;
}
return cou;
}
static int cou(boolean[] a) {
int res = 0;
for (boolean b : a) {
if (b) res++;
}
return res;
}
static int cou(String s, char c) {
int res = 0;
for (char ci : s.toCharArray()) {
if (ci == c) res++;
}
return res;
}
static int countC2(char[][] a, char c) {
int co = 0;
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[0].length; j++)
if (a[i][j] == c) co++;
return co;
}
static int countI(int[] a, int key) {
int co = 0;
for (int i = 0; i < a.length; i++)
if (a[i] == key) co++;
return co;
}
static int countI(int[][] a, int key) {
int co = 0;
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[0].length; j++)
if (a[i][j] == key) co++;
return co;
}
static void fill(int[][] a, int v) {
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[0].length; j++)
a[i][j] = v;
}
static void fill(char[][] a, char c) {
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[0].length; j++)
a[i][j] = c;
}
static void fill(long[][] a, long v) {
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[0].length; j++)
a[i][j] = v;
}
static void fill(int[][][] a, int v) {
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[0].length; j++)
for (int k = 0; k < a[0][0].length; k++)
a[i][j][k] = v;
}
static int max(int... a) {
int res = Integer.MIN_VALUE;
for (int i : a) {
res = Math.max(res, i);
}
return res;
}
static long max(long... a) {
long res = Integer.MIN_VALUE;
for (long i : a) {
res = Math.max(res, i);
}
return res;
}
static long min(long... a) {
long res = Long.MAX_VALUE;
for (long i : a) {
res = Math.min(res, i);
}
return res;
}
static int max(int[][] ar) {
int res = Integer.MIN_VALUE;
for (int i[] : ar)
res = Math.max(res, max(i));
return res;
}
static long max(long[][] ar) {
long res = Integer.MIN_VALUE;
for (long i[] : ar)
res = Math.max(res, max(i));
return res;
}
static int min(int... a) {
int res = Integer.MAX_VALUE;
for (int i : a) {
res = Math.min(res, i);
}
return res;
}
static int min(int[][] ar) {
int res = Integer.MAX_VALUE;
for (int i[] : ar)
res = Math.min(res, min(i));
return res;
}
static int sum(int[] a) {
int cou = 0;
for (int i : a)
cou += i;
return cou;
}
static long sum(long[] a) {
long cou = 0;
for (long i : a)
cou += i;
return cou;
}
//FastScanner
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer tokenizer = null;
public static String next() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
/*public String nextChar(){
return (char)next()[0];
}*/
public static String nextLine() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
return reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken("\n");
}
public static long nl() {
return Long.parseLong(next());
}
public static String n() {
return next();
}
public static int ni() {
return Integer.parseInt(next());
}
public static double nd() {
return Double.parseDouble(next());
}
public static int[] nia(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = ni();
}
return a;
}
//1-index
public static int[] niao(int n) {
int[] a = new int[n + 1];
for (int i = 1; i < n + 1; i++) {
a[i] = ni();
}
return a;
}
public static int[] niad(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = ni() - 1;
}
return a;
}
public static P[] npa(int n) {
P[] p = new P[n];
for (int i = 0; i < n; i++) {
p[i] = new P(ni(), ni());
}
return p;
}
public static P[] npad(int n) {
P[] p = new P[n];
for (int i = 0; i < n; i++) {
p[i] = new P(ni() - 1, ni() - 1);
}
return p;
}
public static int[][] nit(int h, int w) {
int[][] a = new int[h][w];
for (int hi = 0; hi < h; hi++) {
for (int wi = 0; wi < w; wi++) {
a[hi][wi] = ni();
}
}
return a;
}
public static int[][] nitd(int h, int w) {
int[][] a = new int[h][w];
for (int hi = 0; hi < h; hi++) {
for (int wi = 0; wi < w; wi++) {
a[hi][wi] = ni() - 1;
}
}
return a;
}
static int[][] S_ARRAY;
static long[][] S_LARRAY;
static int S_INDEX;
static int S_LINDEX;
//複数の配列を受け取る
public static int[] niah(int n, int w) {
if (S_ARRAY == null) {
S_ARRAY = new int[w][n];
for (int i = 0; i < n; i++) {
for (int ty = 0; ty < w; ty++) {
S_ARRAY[ty][i] = ni();
}
}
}
return S_ARRAY[S_INDEX++];
}
public static long[] nlah(int n, int w) {
if (S_LARRAY == null) {
S_LARRAY = new long[w][n];
for (int i = 0; i < n; i++) {
for (int ty = 0; ty < w; ty++) {
S_LARRAY[ty][i] = ni();
}
}
}
return S_LARRAY[S_LINDEX++];
}
public static char[] nca() {
char[] a = next().toCharArray();
return a;
}
public static char[][] nct(int h, int w) {
char[][] a = new char[h][w];
for (int i = 0; i < h; i++) {
a[i] = next().toCharArray();
}
return a;
}
//スペースが入っている場合
public static char[][] ncts(int h, int w) {
char[][] a = new char[h][w];
for (int i = 0; i < h; i++) {
a[i] = nextLine().replace(" ", "").toCharArray();
}
return a;
}
public static char[][] nctp(int h, int w, char c) {
char[][] a = new char[h + 2][w + 2];
//char c = '*';
int i;
for (i = 0; i < w + 2; i++)
a[0][i] = c;
for (i = 1; i < h + 1; i++) {
a[i] = (c + next() + c).toCharArray();
}
for (i = 0; i < w + 2; i++)
a[h + 1][i] = c;
return a;
}
//スペースが入ってる時用
public static char[][] nctsp(int h, int w, char c) {
char[][] a = new char[h + 2][w + 2];
//char c = '*';
int i;
for (i = 0; i < w + 2; i++)
a[0][i] = c;
for (i = 1; i < h + 1; i++) {
a[i] = (c + nextLine().replace(" ", "") + c).toCharArray();
}
for (i = 0; i < w + 2; i++)
a[h + 1][i] = c;
return a;
}
public static long[] nla(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = nl();
}
return a;
}
public static long[][] nlt(int h, int w) {
long[][] a = new long[h][w];
for (int hi = 0; hi < h; hi++) {
for (int wi = 0; wi < w; wi++) {
a[hi][wi] = nl();
}
}
return a;
}
public static void main(String[] args) throws Exception {
long startTime = System.currentTimeMillis();
solve();
System.out.flush();
long endTime = System.currentTimeMillis();
if (DEBUG) System.err.println(endTime - startTime);
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long calcSum(int i, long long na[]) {
long long sum = 0;
for (int j = 0; j <= i; j++) {
sum += na[j];
}
return sum;
}
int check(int n, int s, long long a[]) {
long long cnt = 0;
long long na[n];
for (int i = 0; i < n; i++) {
na[i] = a[i];
long long sum = calcSum(i, na);
if (s == 0) {
if (i % 2 == 0 && sum <= 0) {
long long dx = 1 - sum;
na[i] += dx;
cnt += abs(dx);
} else if (i % 2 != 0 && sum >= 0) {
long long dx = -1 - sum;
na[i] += dx;
cnt += abs(dx);
}
}
if (s == 1) {
if (i % 2 != 0 && sum <= 0) {
long long dx = 1 - sum;
na[i] += dx;
cnt += abs(dx);
} else if (i % 2 == 0 && sum >= 0) {
long long dx = -1 - sum;
na[i] += dx;
cnt += abs(dx);
}
}
}
return cnt;
}
int main() {
int n;
cin >> n;
long long a[100000];
for (int i = 0; i < n; i++) cin >> a[i];
cout << min(check(n, 0, a), check(n, 1, a)) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int64_t> a_vec(n);
for (int i = 0; i < n; ++i) cin >> a_vec.at(i);
int64_t min_step;
for (int j = 0; j < 2; ++j) {
int sign = (2 * j - 1);
int sum = 0;
int step = 0;
for (int i = 0; i < n; ++i, sign *= -1) {
sum += a_vec.at(i);
if (sign * sum > 0) continue;
step += -sign * sum + 1;
sum = sign;
}
if (j == 0 || min_step > step) min_step = step;
}
cout << min_step << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | package main
import (
"fmt"
"math"
)
func main() {
var n float64
fmt.Scan(&n)
var counter1, counter2 float64 = 0, 0
var total1, total2 float64 = 0, 0
var a float64
for i := 0; i < int(n); i++ {
fmt.Scan(&a)
total1 += a
total2 += a
if i%2 == 0 {
if total1 <= 0 {
counter1 += math.Abs(total1) + 1
total1 = 1
}
} else {
if total1 >= 0 {
counter1 += math.Abs(total1) + 1
}
}
if i%2 == 0 {
if total2 >= 0 {
counter2 += math.Abs(total2) + 1
total2 = -1
}
} else {
if total2 <= 0 {
counter2 += math.Abs(total2) + 1
total2 = 1
}
}
}
if counter1 < counter2 {
fmt.Println(counter1)
} else {
fmt.Println(counter2)
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | 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.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <bits/stdc++.h>
int main(void) {
long long n, ans = 0;
scanf("%lld", &n);
long long a[n], sum[n];
for (long long i = 0; i < n; i++) {
scanf("%lld", &a[i]);
}
sum[0] = a[0];
if (sum[0] == 0 && a[1] > 0) {
sum[0]--;
ans++;
} else if (sum[0] == 0 && a[1] <= 0) {
sum[0]++;
ans--;
}
for (long long i = 1; i < n; i++) {
sum[i] = a[i] + sum[i - 1];
if (sum[i - 1] < 0) {
if (sum[i] <= 0) {
ans += (llabs(sum[i]) + 1);
sum[i] += (llabs(sum[i]) + 1);
}
} else {
if (sum[i] >= 0) {
ans += (llabs(sum[i]) + 1);
sum[i] -= (llabs(sum[i]) + 1);
}
}
}
printf("%lld\n", ans);
return 0;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.