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 | java | import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
long[] A = new long[N];
for (int i = 0; i < N; i++) {
A[i] = sc.nextInt();
}
System.out.println( solve(N, A) );
}
private static int solve(int N, long[] A) {
int ans = 0;
long a0 = A[0];
long sum;
if( a0 > 0 ) {
sum = A[0];
} else if( a0 < 0 ) {
sum = A[0];
} else {
long[] A_plus = Arrays.copyOf(A, N);
A_plus[0] = 1;
int sum_plus = solve(N, A_plus) + 1;
long[] A_minus = Arrays.copyOf(A, N);
A_minus[0] = -1;
int sum_minus = solve(N, A_minus) + 1;
return Math.min(sum_plus, sum_minus);
}
for (int i = 1; i < N; i++) {
long a = A[i];
if( sum > 0 ) {
// 次はminusになるのを期待
if( a + sum >= 0 ) {
// sumが-1になるような値にまで変更する
// a + sum が 5 の場合、6 だけ操作すると -1 にできる
long diff = a + sum + 1;
ans += diff;
sum = -1;
} else {
sum += a;
}
} else {
if( a + sum <= 0 ) {
long diff = (a + sum) * -1 + 1;
ans += diff;
sum = 1;
} else {
sum += a;
}
}
}
return 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;
void ctsm(const long long &tmp, long long int &sm, long long int &ct) {
if ((0LL <= sm + tmp) && (0LL < sm)) {
ct += 1LL + sm + tmp;
sm = -1LL;
} else if ((sm + tmp <= 0LL) && (sm < 0LL)) {
ct += 1LL - sm - tmp;
sm = 1LL;
} else
sm = sm + tmp;
}
int main() {
int n;
if (scanf("%d", &n) < 1) return 0;
long long int tmp;
long long int sm = 0LL;
long long int ct = 0LL;
if (scanf("%lld", &tmp) < 1) return 0;
sm = tmp == 0LL ? 1LL : sm + tmp;
ct = tmp == 0LL ? 1LL : ct;
long long int opsm = 0LL;
long long int opct = 0LL;
tmp = (-1LL) * tmp;
opsm = 0LL <= tmp ? 1LL : -1LL;
opct += abs(tmp) + 1LL;
for (int i = 1; i < n; i++) {
if (scanf("%lld", &tmp) < 1) return 0;
ctsm(tmp, sm, ct);
ctsm(tmp, opsm, opct);
}
printf("%lld\n", ct < opct ? ct : opct);
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()))
count = 0
sum_ = 0
for i in range(n):
if sum_ * (sum_+a[i]) <0 or i == 0:
sum_ += a[i]
elif sum_ >= 0:
count += sum_+a[i]+1
a[i] = -sum_-1
sum_ += a[i]
elif sum_ < 0:
count += abs(sum_+a[i])+1
a[i] = -sum_+1
sum_ += a[i]
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 | UNKNOWN | #include <bits/stdc++.h>
int main() {
long long int n, i, a[100000], sum, count = 0, flag = 0;
scanf("%lld", &n);
for (i = 0; i < n; i++) {
scanf("%lld", &a[i]);
if (a[0] == 0 && a[i] != 0 && flag == 0) flag = i;
}
if (flag != 0) {
if ((a[flag] > 0 && flag % 2 == 0) || (a[flag] < 0 && flag % 2 == 1))
a[0] = 1;
else
a[0] = -1;
count++;
}
for (i = 0; i < n; i++) {
if (i == 0)
sum = a[0];
else {
if (sum > 0 && sum + a[i] >= 0) {
count += 1 + sum + a[i];
a[i] = -1 * sum - 1;
sum = -1;
} else if (sum < 0 && sum + a[i] <= 0) {
count += 1 - sum - a[i];
a[i] = -1 * sum + 1;
sum = 1;
} else
sum += a[i];
}
}
printf("%lld\n", count);
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 | import System.IO
import Data.List
main = do
n <- getLine
as <- getLine >>= fmap read . words
putStrLn . show . head . [xs | xs <- iterate mani as, jouken n xs]
mani [] = []
mani [a] = [a-1, a+1]
mani (a:as) = [(a-1) : x | x <- mani as] ++ [(a+1) : x | x <- mani as]
subsum xs j = sum . take j $ xs
jouken n xs = length [i | i <- [1 .. n-1], (subsum xs i) * (subsum xs (i+1)) >= 0] == 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 int VX[] = {0, 1, 0, -1};
const int VY[] = {1, 0, -1, 0};
const long long MOD = pow(10, 9) + 7;
int n;
vector<int> a;
int solve(bool sign) {
int cost = 0;
vector<int> x = a;
for (int i = (1); i < (n); i++) {
sign = !sign;
x[i] += x[i - 1];
if (sign) {
if (x[i] <= 0) {
while (x[i] != 1) {
x[i]++;
cost++;
}
}
} else {
if (x[i] >= 0) {
while (x[i] != -1) {
x[i]--;
cost++;
}
}
}
}
return cost;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> n;
for (int i = (0); i < (n); i++) {
int in;
cin >> in;
a.push_back(in);
}
int s1 = solve(true), s2 = solve(false);
cout << min(s1, s2) << 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 sum = 0;
long long cnt = 0;
for (long long i = 1; i < n; i++) {
if (v[i] * sign >= 0) {
long long d = (sign > 0 ? (v[i] + 1) * -1 : (v[i] - 1) * -1);
sum += d;
cnt += abs(d);
}
if (i < n - 1) v[i + 1] += sum;
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>
int main(void) {
double num[10 * 10 * 10 * 10 * 10];
int i, n, ssign;
double sum = 0;
double count = 0;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%lf", &num[i]);
}
if (num[0] == 0) {
num[0]++;
count++;
}
for (i = 1; i < n; i++) {
sum += num[i - 1];
while (1) {
if (fabs(sum) > fabs(num[i])) {
if (sum < 0) {
num[i]++;
count++;
} else if (sum > 0) {
num[i]--;
count++;
}
} else if (fabs(sum) == fabs(num[i])) {
if (sum < 0) {
num[i]++;
count++;
} else {
num[i]--;
count++;
}
} else if (sum > 0 && num[i] > 0 && fabs(sum) < fabs(num[i])) {
num[i]--;
count++;
} else if (sum < 0 && num[i] < 0 && fabs(sum) < fabs(num[i])) {
num[i]++;
count++;
} else
break;
}
}
for (i = 0; i < n; i++) {
sum += num[i];
if (sum == 0.0) {
if ((sum - num[i]) > 0)
num[i]--;
else
num[i]++;
count++;
}
}
printf("%.0f\n", count);
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;
vector<ll> a(n);
ll sum = 0;
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 | cpp | #include <bits/stdc++.h>
using namespace std;
struct edge {
int to, cost;
};
const int INF = 100000000;
int main() {
long long int n, a[100010], sum = 0, ans = 0;
cin >> n;
for (int i = 0; i < (n); i++) {
cin >> a[i];
}
if (a[0] >= 0) {
sum += a[0];
for (int i = 1; i < n; i++) {
int last = sum;
sum += a[i];
if (i % 2 != 0) {
if (sum > 0) {
ans += abs(sum) + 1;
sum = -1;
}
} else {
if (sum < 0) {
ans += abs(sum) + 1;
sum = 1;
}
}
if (i != 0 && (last - 1 == 0 || last + 1 == 0)) {
if (last - 1 == 0)
sum = 2;
else
sum = -2;
}
}
if (sum == 0) {
ans++;
}
} else {
sum += a[0];
for (int i = 1; i < n; i++) {
int last = sum;
sum += a[i];
if (i % 2 != 0) {
if (sum < 0) {
ans += abs(sum) + 1;
sum = 1;
}
} else {
if (sum > 0) {
ans += abs(sum) + 1;
sum = -1;
}
}
if (i != 0 && (last - 1 == 0 || last + 1 == 0)) {
if (last - 1 == 0)
sum = 2;
else
sum = -2;
}
}
if (sum == 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;
int plussum = 0;
int minussum = 0;
int plusans = 0;
int minusans = 0;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
plussum += a;
minussum += a;
if (i % 2 == 0) {
if (plussum <= 0) {
plusans += abs(plussum - a - 1) - a;
plussum = 1;
}
if (minussum >= 0) {
minusans += a + abs(minussum - a + 1);
minussum = -1;
}
} else {
if (plussum >= 0) {
plusans += a + abs(plussum - a + 1);
plussum = -1;
}
if (minussum <= 0) {
minusans += abs(minussum - a - 1) - a;
minussum = 1;
}
}
}
cout << min(minusans, plusans) << 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 | #include <bits/stdc++.h>
int main() {
long long int n, i, a[100000], sum, count = 0, flag = 0;
scanf("%lld", &n);
for (i = 0; i < n; i++) {
scanf("%lld", &a[i]);
if (a[0] == 0 && a[i] != 0 && flag == 0) flag = i;
}
if (flag != 0) {
if ((a[flag] > 0 && flag % 2 == 0) || (a[flag] < 0 && flag % 2 == 1))
a[0] = 1;
else
a[0] = -1;
count++;
} else if (flag == 0 && a[0] == 0 && a[n - 1] == 0) {
a[0]++;
count++;
}
for (i = 0; i < n; i++) {
if (i == 0)
sum = a[0];
else {
if (sum > 0 && sum + a[i] >= 0) {
count = count + 1 + sum + a[i];
a[i] = -1 * sum - 1;
sum = -1;
} else if (sum < 0 && sum + a[i] <= 0) {
count = count + 1 - sum - a[i];
a[i] = -1 * sum + 1;
sum = 1;
} else if (sum + a[i] == 0) {
if (sum > 0)
a[i]--;
else if (sum < 0)
a[i]++;
count++;
sum = sum + a[i];
} else
sum = sum + a[i];
}
}
printf("%lld\n", count);
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<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long wa = a[0];
long long ans = 0;
for (int i = 1; i < n; i++) {
if (wa > 0) {
wa += a[i];
if (wa > 0) {
ans += wa + 1;
wa -= (wa + 1);
} else if (wa == 0) {
ans++;
wa--;
}
} else if (wa < 0) {
wa += a[i];
if (wa < 0) {
ans += -wa + 1;
wa += -wa + 1;
} else if (wa == 0) {
ans++;
wa++;
}
}
}
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;
vector<int> vec(n);
for (int &x : vec) {
cin >> x;
}
vector<int> v = vec;
int a = 0;
if (v[0] <= 0) {
a += abs(v[0]) + 1;
v[0] += abs(v[0]) + 1;
}
for (int i = 1; i < n; i++) {
v[i] += v[i - 1];
if (v[i] >= 0) {
a += abs(v[i]) + 1;
v[i] -= abs(v[i]) + 1;
}
i++;
if (i == n) break;
v[i] += v[i - 1];
if (v[i] <= 0) {
a += abs(v[i]) + 1;
v[i] += abs(v[i]) + 1;
}
}
vector<int> v2 = vec;
int b = 0;
if (v2[0] >= 0) {
b += abs(v2[0]) + 1;
v2[0] -= abs(v2[0]) + 1;
}
for (int i = 1; i < n; i++) {
v2[i] += v2[i - 1];
if (v2[i] <= 0) {
b += abs(v2[i]) + 1;
v2[i] += abs(v2[i]) + 1;
}
i++;
if (i == n) break;
v2[i] += v2[i - 1];
if (v2[i] >= 0) {
b += abs(v2[i]) + 1;
v2[i] -= abs(v2[i]) + 1;
}
}
cout << min(a, b) << 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 f(int sign, const vector<long long>& a) {
long long cnt = 0;
long long sum = 0;
for (int i = 0; i < a.size(); i++) {
sum += a[i];
if (sign * sum <= 0) {
long long d = sign - sum;
sum += d;
cnt += abs(d);
}
cout << sign << " " << sum << " " << cnt << endl;
sign *= -1;
}
return cnt;
}
int main(void) {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
long long sum[2];
sum[0] = f(1, a);
cout << endl;
sum[1] = f(-1, a);
cout << min(sum[0], sum[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 n;
cin >> n;
vector<long long> v(n);
for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n));
i += 1 - 2 * ((0) > (n)))
cin >> v[i];
long long res = 0;
long long som = v[0];
if (som > 0) {
for (__typeof(n) i = (1) - ((1) > (n)); i != (n) - ((1) > (n));
i += 1 - 2 * ((1) > (n))) {
som = som + v[i];
if (i % 2 == 1) {
if (som < 0)
continue;
else {
res += som + 1;
som = -1;
}
} else {
if (som > 0)
continue;
else {
res += 1 - som;
som = 1;
}
}
}
} else {
for (__typeof(n) i = (1) - ((1) > (n)); i != (n) - ((1) > (n));
i += 1 - 2 * ((1) > (n))) {
som = som + v[i];
if (i % 2 == 0) {
if (som < 0)
continue;
else {
res += som + 1;
som = -1;
}
} else {
if (som > 0)
continue;
else {
res += 1 - som;
som = 1;
}
}
}
}
cout << res;
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 resolve(SL):
# L[0]!=0を起点とする
cnt = 0
for i in range(len(SL)-1):
s0 = SL[i]
s1 = SL[i+1]
if(s0>0 and s1>=0):
SL[(i+1):] = [s-(s1+1) for s in SL[(i+1):]]
cnt += (s1+1)
elif(s0<0 and s1<=0):
SL[(i+1):] = [s+(-s1+1) for s in SL[(i+1):]]
cnt += (-s1+1)
# print(SL)
return cnt
def ans(L):
SL = [sum(L[:(i+1)]) for i in range(len(L))]
c0,c1=0,0
if (L[0]>0):
c0 = resolve(SL)
c1 = (L[0]+1) + resolve(list(map(lambda x:x-(L[0]+1), SL)))
elif (L[0]<0):
c0 = resolve(L)
c1 = (-L[0]+1) + resolve(list(map(lambda x:x+(-L[0]+1), SL)))
else:
c0 = 1 + resolve(list(map(lambda x:x+1, SL)))
c1 = 1 + resolve(list(map(lambda x:x-1, SL)))
return(min(c0,c1))
N = int(input())
L = [int(input(x)) for x in range(N)]
print(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 | python3 | # -*- coding: utf-8 -*-
N = int(input())
a = [int(n) for n in input().split()]
count_a = 0 #+start
count_b = 0 #-start
nowsum = a[0]
if nowsum > 0:
for n in range(1, N):
if nowsum * (nowsum + a[n]) >= 0:
count_a += abs(nowsum + a[n]) + 1
if nowsum < 0:
nowsum = 1
else:
nowsum = -1
else:
nowsum += a[n]
count_b += abs(a[0]) + 1
a[0] = a[0] / abs(a[0]) * -1
nowsum = a[0]
for n in range(1, N):
if nowsum * (nowsum + a[n]) >= 0:
count_b += abs(nowsum + a[n]) + 1
if nowsum < 0:
nowsum = 1
else:
nowsum = -1
else:
nowsum += a[n]
print(min(count_a, count_b))
else:
a[0] = 1
count_a += 1
nowsum = 1
for n in range(1, N):
if nowsum * (nowsum + a[n]) >= 0:
count_a += abs(nowsum + a[n]) + 1
if nowsum < 0:
nowsum = 1
else:
nowsum = -1
else:
nowsum += a[n]
a[0] = -1
count_b += 1
nowsum = -1
for n in range(1, N):
if nowsum * (nowsum + a[n]) >= 0:
count_b += abs(nowsum + a[n]) + 1
if nowsum < 0:
nowsum = 1
else:
nowsum = -1
else:
nowsum += a[n]
print(min(count_a, count_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 namespace std;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << setprecision(9);
int N;
long long a[100000];
cin >> N;
for (int i = 0; i < N; i++) cin >> a[i];
long long ans = 0;
long long sum = a[0];
if (sum != 0) {
for (int i = 1; i < N; i++) {
if (sum > 0) {
sum += a[i];
if (sum >= 0) {
ans += sum + 1;
sum = -1;
}
} else {
sum += a[i];
if (sum <= 0) {
ans += -sum + 1;
sum = 1;
}
}
}
} else {
sum = 1;
ans = 1;
for (int i = 1; i < N; i++) {
if (sum > 0) {
sum += a[i];
if (sum >= 0) {
ans += sum + 1;
sum = -1;
}
} else {
sum += a[i];
if (sum <= 0) {
ans += -sum + 1;
sum = 1;
}
}
}
long long memo = ans;
sum = -1;
ans = 1;
for (int i = 1; i < N; i++) {
if (sum > 0) {
sum += a[i];
if (sum >= 0) {
ans += sum + 1;
sum = -1;
}
} else {
sum += a[i];
if (sum <= 0) {
ans += -sum + 1;
sum = 1;
}
}
}
ans = min(ans, memo);
}
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;
long long int A, all;
unsigned long long score = 0;
int buff;
cin >> N;
cin >> A;
all = A;
for (int i = 0; i < N - 1; i++) {
cin >> A;
if (all * (all + A) >= 0) {
long long int buff;
if (all > 0) {
buff = 0 - 1 - all;
} else {
buff = 1 - all;
}
score += abs(A - buff);
A = buff;
}
all += A;
}
cout << score << 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 = map(int, input().split())
b = 0
cnt_b = 0
c = 0
cnt_c = 0
for i,a in enumerate(A):
b += a
c += a
if (b > 0) != ((i % 2) > 0):
cnt_b += abs(b) + 1
b = 1 if ((i % 2) > 0) else -1
if (c > 0) == ((i % 2) > 0):
cnt_c += abs(c) + 1
c = -1 if ((i % 2) > 0) else 1
print(min(cnt_b, cnt_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() {
int n;
cin >> n;
long long int a[n], sum = 0, cnt = 0;
for (int i = 0; i < n; i++) cin >> a[i];
sum = a[0];
for (int i = 1; i < n; i++) {
if ((sum >= 0 && sum + a[i] >= 0) || (sum <= 0 && sum + a[i] <= 0)) {
if (sum < 0) {
long long int k = sum + a[i];
cnt += abs(k - 1);
sum = 1;
} else {
long long int k = sum + a[i];
cnt += abs(k + 1);
sum = -1;
}
} else
sum += a[i];
}
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())
A = [int(x) for x in input().split()]
if A[0] < 0:
sumP = 1
sumM = A[0]
countP = -A[0] + 1
countM = 0
elif A[0] == 0:
sumP = 1
sumM = -1
countP = 1
countM = 1
else:
sumP = A[0]
sumM = -1
countP = 0
countM = -A[0] + 1
for i in range(1, N):
if sumP > 0:
if A[i] + sumP >= 0:
countP += A[i] + sumP + 1
sumP = -1
else:
sumP += A[i]
else:
if A[i] + sumP <= 0:
countP += -(A[i] + sumP) + 1
sumP = 1
else:
sumP += A[i]
if sumM > 0:
if A[i] + sumM >= 0:
countM += A[i] + sumM + 1
sumM = -1
else:
sumM += A[i]
else:
if A[i] + sumM <= 0:
countM += -(A[i] + sumM) + 1
sumM = 1
else:
sumM += A[i]
print(min(countP, countM)) |
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 change_num(long long p[], int N) {
int res = 0;
long long sum = p[0];
for (int i = 1; i < N; i++) {
if (sum * (sum + p[i]) < 0) {
sum += p[i];
continue;
}
if (sum > 0 && sum + p[i] >= 0) {
sum += p[i];
while (sum >= 0) {
res++;
sum--;
}
continue;
}
if (sum < 0 && sum + p[i] <= 0) {
sum += p[i];
while (sum <= 0) {
res++;
sum++;
}
continue;
}
}
return res;
}
int main() {
int N;
cin >> N;
long long a[N];
for (int i = 0; i < N; i++) cin >> a[i];
int ans = 0;
long long sum = a[0];
if (a[0] == 0) {
int plus_ans;
a[0] = 1;
plus_ans = change_num(a, N) + 1;
int minus_ans = 1;
a[0] = -1;
minus_ans = change_num(a, N) + 1;
if (plus_ans < minus_ans) {
ans = plus_ans;
} else {
ans = minus_ans;
}
} else {
ans = change_num(a, N);
}
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() {
long long n;
cin >> n;
long long a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long sum = a[0];
long long res1 = 0;
long long res2 = 0;
for (int i = 0; i < n; i++) {
if (i == 0) {
if (a[i] > 0)
continue;
else {
sum = 1;
res1 = 1 - a[0];
}
} else if (i % 2 == 0) {
if (sum + a[i] > 0) {
sum += a[i];
continue;
} else {
res1 = res1 + 1 - sum;
sum = 1;
}
} else {
if (sum + a[i] < 0) {
sum += a[i];
continue;
} else {
res1 = res1 + sum + 1;
sum = -1;
}
}
}
for (int i = 0; i < n; i++) {
if (i == 0) {
if (a[i] < 0)
continue;
else {
sum = -1;
res2 = a[0] + 1;
}
} else if (i % 2 == 0) {
if (sum + a[i] < 0) {
sum += a[i];
continue;
} else {
res2 = res2 + 1 + sum;
sum = -1;
}
} else {
if (sum + a[i] > 0) {
sum += a[i];
continue;
} else {
res2 = res2 + 1 - sum;
sum = 1;
}
}
}
cout << min(res1, res2) << 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>
#pragma GCC optimize("-O3")
using namespace std;
void _main();
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
_main();
}
const int inf = INT_MAX / 2;
const long long infl = 1LL << 60;
template <class T>
bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
enum PosiNega { POSITIVE = 0, NEGATIVE = 1 };
int solve(int N, int *a, PosiNega odd_posinega) {
int ans = 0;
int sum = 0;
PosiNega posi_nega = odd_posinega;
for (int i = 0; i < N; i++) {
sum += a[i];
if (POSITIVE == posi_nega) {
if (0 >= sum) {
ans += 1 - sum;
sum = 1;
}
posi_nega = NEGATIVE;
} else {
if (0 <= sum) {
ans += abs(-1 - sum);
sum = -1;
}
posi_nega = POSITIVE;
}
}
return ans;
}
void _main() {
int N;
cin >> N;
int a[N];
for (int i = 0; i < N; i++) cin >> a[i];
int candidate1 = solve(N, a, POSITIVE);
int candidate2 = solve(N, a, NEGATIVE);
int ans = (candidate1 < candidate2) ? candidate1 : candidate2;
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;
long long s[100005], h[100005];
long long n, ans = 0, sum = 0;
long long slove(long long t) {
for (int i = t; i <= n; i++) {
sum = s[i] + s[i - 1];
if (sum >= 0 && s[i - 1] > 0) {
ans += abs(sum + 1);
s[i] = -1;
} else if (sum <= 0 && s[i - 1] < 0) {
ans += abs(sum - 1);
s[i] = 1;
} else
s[i] = s[i] + s[i - 1];
}
return ans;
}
int main() {
long long all = 0;
scanf("%lld", &n);
for (int i = 1; i <= n; i++) scanf("%lld", &s[i]);
if (s[1] == 0) {
for (int i = 1; i <= n; i++) {
if (s[i] == 0)
all++;
else
break;
}
if (all == 1)
ans++;
else
ans = 1 + 2 * (all - 1);
printf("%lld\n", ans);
if (s[all + 1] < 0) {
ans += abs(s[all + 1] + 2);
s[all + 1] = -1;
printf("%lld", slove(all + 2));
} else if (s[all + 1] > 0) {
ans += abs(s[all + 1] - 2);
s[all + 1] = 1;
printf("%lld", slove(all + 2));
}
} else
printf("%lld", slove(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 | cpp | #include <bits/stdc++.h>
using namespace std;
const double PI = 3.1415926535897932384626433832795;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
bool isDiffer(long long a, long long b) {
if (((a > 0) && (b < 0)) || ((a < 0) && (b > 0)))
return true;
else
return false;
}
int main() {
ios::sync_with_stdio(false);
long long n;
cin >> n;
vector<long long> v;
for (int i = 0; i < n; i++) {
long long t;
cin >> t;
v.push_back(t);
}
long long os = v[0];
long long ans = 0;
for (int i = 1; i < n; i++) {
if (!isDiffer(os, v[i] + os)) {
long long ob = (os > 0) ? -1 : 1;
ans += abs(ob - os - v[i]);
v[i] = ob - os;
}
os += v[i];
}
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 | java | import java.util.Scanner;
import java.util.Arrays;
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();
}
int[] sum = new int[n];
sum[0] = a[0];
int count = 0;
if(sum[0] == 0){
count++;
}
count = Math.min(solve1(sum,a,count),solve2(sum,a,count));
System.out.println(count);
}
public static int solve1(int[] sum,int[] a,int count){
for(int i = 0;i < sum.length-1;i++){
sum[i+1] = sum[i] + a[i+1];
if((i+1) % 2 == 1){
if(sum[i+1] >= 0){
count += 1 + sum[i+1];
sum[i+1] = -1;
}
}
if((i+1) % 2 == 0){
if(sum[i+1] <= 0){
count += 1 - sum[i+1];
sum[i+1] = 1;
}
}
}
return count;
}
public static int solve2(int[] sum,int[] a,int count){
for(int i = 0;i < sum.length-1;i++){
sum[i+1] = sum[i] + a[i+1];
if((i+1) % 2 == 1){
if(sum[i+1] <= 0){
count += 1 - sum[i+1];
sum[i+1] = 1;
}
}
if((i+1) % 2 == 0){
if(sum[i+1] >= 0){
count += 1 + sum[i+1];
sum[i+1] = -1;
}
}
}
return 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 main() {
int t;
vector<int> answer(2, 0);
int sumi = 0;
bool flag = true;
cin >> t;
vector<int> A(t);
for (int i = 0; i < t; i++) {
cin >> A[i];
}
for (int j = 0; j < 2; j++) {
for (int i = 0; i < t; i++) {
sumi += A[i];
if (sumi == 0) {
answer[j] += 1;
if (flag) {
sumi = -1;
} else {
sumi = 1;
}
} else if (sumi > 0 == flag) {
answer[j] += abs(sumi) + 1;
if (sumi > 0) {
sumi = -1;
} else {
sumi = 1;
}
}
flag = !flag;
}
flag = false;
sumi = 0;
}
cout << min(answer[0], answer[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 | python3 | # -*- coding: utf-8 -*-
#############
# Libraries #
#############
import sys
input = sys.stdin.readline
import math
#from math import gcd
import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache
#############
# Constants #
#############
MOD = 10**9+7
INF = float('inf')
#############
# Functions #
#############
######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]
######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()
#####Shorten#####
def DD(arg): return defaultdict(arg)
#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)
######Combination######
kaijo_memo = []
def kaijo(n):
if(len(kaijo_memo) > n):
return kaijo_memo[n]
if(len(kaijo_memo) == 0):
kaijo_memo.append(1)
while(len(kaijo_memo) <= n):
kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
return kaijo_memo[n]
gyaku_kaijo_memo = []
def gyaku_kaijo(n):
if(len(gyaku_kaijo_memo) > n):
return gyaku_kaijo_memo[n]
if(len(gyaku_kaijo_memo) == 0):
gyaku_kaijo_memo.append(1)
while(len(gyaku_kaijo_memo) <= n):
gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
return gyaku_kaijo_memo[n]
def nCr(n,r):
if(n == r):
return 1
if(n < r or r < 0):
return 0
ret = 1
ret = ret * kaijo(n) % MOD
ret = ret * gyaku_kaijo(r) % MOD
ret = ret * gyaku_kaijo(n-r) % MOD
return ret
######Factorization######
def factorization(n):
arr = []
temp = n
for i in range(2, int(-(-n**0.5//1))+1):
if temp%i==0:
cnt=0
while temp%i==0:
cnt+=1
temp //= i
arr.append([i, cnt])
if temp!=1:
arr.append([temp, 1])
if arr==[]:
arr.append([n, 1])
return arr
#####MakeDivisors######
def make_divisors(n):
divisors = []
for i in range(1, int(n**0.5)+1):
if n % i == 0:
divisors.append(i)
if i != n // i:
divisors.append(n//i)
return divisors
#####MakePrimes######
def make_primes(N):
max = int(math.sqrt(N))
seachList = [i for i in range(2,N+1)]
primeNum = []
while seachList[0] <= max:
primeNum.append(seachList[0])
tmp = seachList[0]
seachList = [i for i in seachList if i % tmp != 0]
primeNum.extend(seachList)
return primeNum
#####GCD#####
def gcd(a, b):
while b:
a, b = b, a % b
return a
#####LCM#####
def lcm(a, b):
return a * b // gcd (a, b)
#####BitCount#####
def count_bit(n):
count = 0
while n:
n &= n -1
count += 1
return count
#####ChangeBase#####
def base_10_to_n(X, n):
if X//n:
return base_10_to_n(X//n, n)+[X%n]
return [X%n]
def base_n_to_10(X, n):
return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))
#####IntLog#####
def int_log(n, a):
count = 0
while n>=a:
n //= a
count += 1
return count
#############
# Main Code #
#############
N = I()
A = IL()
s = A[0]
count1 = max(0,1-s)
for i in range(1,N):
s += A[i]
if i%2:
if s>-1:
count1 += s+1
s = -1
else:
if s<1:
count1 += 1-s
s = 1
s = A[0]
count2 = max(0,s+1)
for i in range(1,N):
s += A[i]
if i%2==0:
if s>-1:
count2 += s+1
s = -1
else:
if s<1:
count2 += 1-s
s = 1
print(min(count1,count2))
|
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() {
ll N = 0;
cin >> N;
vector<ll> A(N, 0);
for (ll i = 0; i < N; i++) {
cin >> A.at(i);
}
ll ans = 0;
vector<ll> sum(N, 0);
sum.at(0) = A.at(0);
ll ansa = abs(A.at(0) + (abs(A.at(0)) / A.at(0)));
vector<ll> suma(N, 0);
suma.at(0) = -1 * (abs(A.at(0)) / A.at(0));
for (size_t i = 1; i < N; i++) {
sum.at(i) = sum.at(i - 1) + A.at(i);
if (sum.at(i) * sum.at(i - 1) < 0) {
continue;
} else {
ans += abs(sum.at(i) + (abs(A.at(i - 1)) / A.at(i - 1)));
sum.at(i) = -1 * (abs(A.at(i - 1)) / A.at(i - 1));
}
}
for (size_t i = 1; i < N; i++) {
suma.at(i) = suma.at(i - 1) + A.at(i);
if (suma.at(i) * suma.at(i - 1) < 0) {
continue;
} else {
ansa += abs(suma.at(i) + (abs(A.at(i - 1)) / A.at(i - 1)));
suma.at(i) = -1 * (abs(A.at(i - 1)) / A.at(i - 1));
}
}
cout << ans << endl;
cout << min(ans, ansa) << 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 | <?php
error_reporting(0);
$stdin = file_get_contents('php://stdin');
$line = explode("\n",$stdin);
$fi = 0;
$cnt = 0;
$list = array();
$key = new stdclass();
foreach($line as $l) {
if (strlen($l)==0) continue;
if ($fi == 0) {
$a = explode(" ",$l);
$key->A = $a;
$fi++;
continue;
}
if ($fi > 0) {
$a = explode(" ",$l);
$key->X[] = $a;
}
}
$cnt=0;
$prev=null;
$new=array();
foreach($key->X[0] as $v) {
if ($prev != null) {
//2回目以降処理
if ($prev > 0) {
//前の数が正なら、この数を負にする必要がある
if (($prev + $v) > 0) {
//ダメなので-1まで減らす
$wk = $prev + $v;
$cnt += $wk+1;
$prev = -1;
$new[]=$v-$cnt;
}
else {
$prev = $prev + $v;
$new[]=$v;
}
}
else {
//前はマイナスなのでプラスにする必要がある
if (($prev + $v) < 0) {
$wk = $prev + $v;
$cnt += 1-$wk;
$prev = 1;
$new[]=$v+$cnt;
}
else {
$prev = $prev + $v;
$new[]=$v;
}
}
}
else {
$prev = $v;
$new[]=$v;
}
}
$chk=0;
//合計が0になるかチェック
foreach($new as $v) {
$chk+=$v;
}
//ゼロなら最後にもう一度操作するので足す
if ($chk==0) $cnt++;
printf("%d\n",$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()))
if a[0] < 0:
for i in range(n):
a[i] *= -1
a_orig = a[:]
ans = 0
tot = [0 for i in range(n)]
tot[0] = a[0]
for i in range(1, n):
tot[i] = tot[i-1] + a[i]
if i % 2 == 0:
if tot[i] <= 0:
tot[i] = 1
a[i] = tot[i] - tot[i-1]
else:
if tot[i] >= 0:
tot[i] = -1
a[i] = tot[i] - tot[i-1]
for i in range(n):
ans += abs(a[i]-a_orig[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 | UNKNOWN |
def rec(ary, n, i, sum, cnt)
return cnt if i == n
if sum < 0
sum += ary[i]
if sum <= 0
diff = -sum+1
cnt += diff
sum += diff
end
elsif sum > 0
sum += ary[i]
if sum >= 0
diff = sum+1
cnt += diff
sum -= diff
end
elsif sum == 0 # never
if ary[i+1] > 0
sum -= 1
cnt += 1
else
sum += 1
cnt += 1
end
end
return rec(ary, n, i+1, sum, cnt)
end
# main
n = gets.to_i
ary = gets.split(' ').map(&:to_i)
puts rec(ary, n, 1, ary[0], 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 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) {
sum--;
} else {
sum++;
}
} else {
for (long long j = 1; j < n; j++) {
if (v[j] > 0) {
sum += pow((-1), j);
break;
} else if (v[j] < 0) {
sum += pow((-1), j + 1);
break;
}
}
}
cnt++;
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 | cpp | #include <bits/stdc++.h>
using namespace std;
const int N_MAX = 100000;
int N;
int a[N_MAX];
int All;
int sum[N_MAX];
int sum2[N_MAX];
int main() {
All = 0;
cin >> N;
for (int i = 0; i < N; i++) {
cin >> a[i];
All += a[i];
sum[i] = All;
sum2[i] = All;
}
int sigh = 1;
int ans1 = 0;
for (int i = 0; i < N; i++) {
if (sum[i] == 0) {
for (int j = i; j < N; j++) {
sum[j] += sigh;
}
ans1 += 1;
} else if (((sum[i]) / abs((sum[i]))) != sigh) {
ans1 += (abs(sum[i]) + 1);
int temp = sum[i];
for (int j = i; j < N; j++) {
sum[j] += (abs(temp) + 1) * sigh;
}
}
sigh *= -1;
}
sigh = -1;
int ans2 = 0;
for (int i = 0; i < N; i++) {
if (sum2[i] == 0) {
for (int j = i; j < N; j++) {
sum2[j] += sigh;
}
ans2 += 1;
} else if (((sum2[i]) / abs((sum2[i]))) != sigh) {
ans2 += (abs(sum2[i]) + 1);
int temp = sum2[i];
for (int j = i; j < N; j++) {
sum2[j] += (abs(temp) + 1) * sigh;
}
}
sigh *= -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;
using ll = long long int;
const ll INF = (1LL << 32);
const ll MOD = (ll)1e9 + 7;
const double EPS = 1e-9;
ll dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
ll dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
ll n;
ll solve(vector<ll> a) {
ll sum = a[0];
ll ans = 0;
for (ll i = (1); i < (n); i++) {
if (sum >= 0 and (sum + a[i]) >= 0) {
while (sum + a[i] != -1) {
a[i]--;
ans++;
}
} else if (sum < 0 and (sum + a[i]) < 0) {
while (sum + a[i] != 1) {
a[i]++;
ans++;
}
}
sum += a[i];
}
if (sum == 0) ans++;
return ans;
}
signed main() {
ios::sync_with_stdio(false);
cin >> n;
vector<ll> a;
for (ll i = 0; i < n; i++) {
ll x;
cin >> x;
a.push_back(x);
}
ll start = a[0];
auto ac = a;
ll fa1 = INF;
ll fa2 = INF;
if (a[0] >= 0) {
ll ans1 = solve(a);
ac[0] = -1;
ll ans2 = solve(ac);
ans2 += start + 1;
fa1 = min(ans1, ans2);
} else {
ll ans1 = solve(a);
ac[0] = 1;
ll ans2 = solve(ac);
ans2 += start + 1;
fa2 = min(ans1, ans2);
}
cout << min(fa1, fa2) << 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 int maxn = 1e5 + 5;
long long a[maxn];
int n, pos;
long long pre_sum, ans = 0;
bool slove() {
long long sum = a[1];
if (a[1] == 0) {
ans = 1;
for (int i = 2; i <= n; i++) {
if (a[i] > 0) {
if (i % 2 == 0) {
pre_sum = -1;
pos = 2;
return false;
} else {
pre_sum = 1;
pos = 2;
return false;
}
} else if (a[i] < 0) {
if (i % 2 == 0) {
pre_sum = 1;
pos = 2;
return false;
} else {
pre_sum = -1;
pos = 2;
return false;
}
}
}
} else {
for (int i = 2; i <= n; i++) {
if ((sum + a[i]) * sum >= 0) {
pos = i;
pre_sum = sum;
return false;
}
sum += a[i];
}
}
return true;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
if (slove())
printf("0");
else {
for (int i = pos; i <= n; i++) {
if ((pre_sum + a[i]) * pre_sum >= 0) {
if (pre_sum < 0) {
ans += abs(1 - pre_sum - a[i]);
pre_sum = 1;
} else if (pre_sum > 0) {
ans += abs(-1 - pre_sum - a[i]);
pre_sum = -1;
}
} else
pre_sum += a[i];
}
printf("%lld", 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;
const int INF = 0x3f3f3f3f;
int a[100010];
int main() {
int n;
while (scanf("%d", &n) != EOF) {
int sum = 0;
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
int tmp = -1;
for (int i = 0; i < n; i++) {
if (a[i] != 0) {
tmp = i;
break;
}
}
if (tmp != 0 && a[tmp] > 0) {
if (tmp % 2)
a[0] = -1;
else
a[0] = 1;
sum++;
} else if (tmp != 0 && a[tmp] < 0) {
if (tmp % 2)
a[0] = 1;
else
a[0] = -1;
sum++;
}
int oo = a[0], flag;
if (a[0] > 0)
flag = 1;
else if (a[0] < 0)
flag = -1;
for (int i = 1; i < n; i++) {
oo += a[i];
if (flag == 1) {
if (oo >= 0) {
sum += oo + 1;
oo = -1;
}
flag = -1;
} else if (flag == -1) {
if (oo <= 0) {
sum += 0 - oo + 1;
oo = 1;
}
flag = 1;
}
}
printf("%d\n", sum);
}
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.Text;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using static System.Console;
using static System.Math;
namespace AtCorder
{
public class Program
{
public static void Main(string[] args)
{
new Program().Solve(new ConsoleInput(Console.In, ' '));
}
public void Solve(ConsoleInput cin)
{
var n = cin.ReadInt;
var a = cin.ReadLongArray(n);
var ans = 0L;
var pre = a[0];
for(int i = 1; i < n; i++)
{
var now = pre + a[i];
if(pre * now < 0)
{
pre += a[i];
continue;
}
if(now >= 0)
{
ans += (now + 1);
a[i] -= (now + 1);
}
else if(now < 0)
{
ans += (1 - now);
a[i] += (1 - now);
}
pre += a[i];
}
WriteLine(ans);
}
public long C(int X, int Y)
{
if (Y == 0 || Y == X)
{
return 1;
}
if (X < Y)
{
return 0;
}
var Pascal = new long[X + 1, X + 1];
for (int i = 0; i <= X; i++)
{
Pascal[i, 0] = 1L;
Pascal[i, i] = 1L;
}
for (int i = 2; i <= X; i++)
{
for (int j = 1; j < i; j++)
{
Pascal[i, j] = Pascal[i - 1, j] + Pascal[i - 1, j - 1];
}
}
return Pascal[X, Y];
}
public class ConsoleInput
{
private readonly System.IO.TextReader _stream;
private char _separator = ' ';
private Queue<string> inputStream;
public ConsoleInput(System.IO.TextReader stream, char separator = ' ')
{
this._separator = separator;
this._stream = stream;
inputStream = new Queue<string>();
}
public string Read
{
get
{
if (inputStream.Count != 0) return inputStream.Dequeue();
string[] tmp = _stream.ReadLine().Split(_separator);
for (int i = 0; i < tmp.Length; ++i)
inputStream.Enqueue(tmp[i]);
return inputStream.Dequeue();
}
}
public string ReadLine { get { return _stream.ReadLine(); } }
public int ReadInt { get { return int.Parse(Read); } }
public long ReadLong { get { return long.Parse(Read); } }
public double ReadDouble { get { return double.Parse(Read); } }
public string[] ReadStrArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret; }
public int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret; }
public long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return 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 | java | import java.io.*;
import java.util.*;
public class Main{
static class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br = new BufferedReader(new InputStreamReader(System.in));
}
public String next(){
try{
while(st==null||!st.hasMoreElements()){
st = new StringTokenizer(br.readLine());
}
}catch(Exception e){
e.printStackTrace();
}
return st.nextToken();
}
public int nextInt(){
return Integer.parseInt(next());
}
public long nextLong(){
return Long.parseLong(next());
}
public double nextDouble(){
return Double.parseDouble(next());
}
public String nextLine(){
String s = "";
try{
s = br.readLine();
}catch(Exception e){
e.printStackTrace();
}
return s;
}
}
public static void main(String[] args){
FastReader in = new FastReader();
PrintWriter out = new PrintWriter(System.out);
int n= in.nextInt();
int[] a = new int[n];
int[] pre = new int[n];
for(int i=0;i<n;i++){
a[i] = in.nextInt();
if(i>0)
pre[i] = pre[i-1]+a[i];
else pre[i]=a[i];
}
int count=0;
for(int i=1;i<n;i++){
if(pre[i]*pre[i-1]>=0){
if(pre[i]>=0){
int temp = pre[i];
count+=(temp+1);
for(int j=i;j<n;j++)
pre[j]+=-(temp+1);
}
else{
int temp = pre[i];
count+=-(temp-1);
for(int j=i;j<n;j++){
pre[j]+=-(temp+1);
}
}
}
}
out.println(count);
out.flush();
out.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;
int n;
int flag[100005], k[100005];
long long a[100005], sum[100005], ans, b[100005], tot[100005], ant;
int main() {
int m = 0;
scanf("%d", &n);
scanf("%lld", &a[1]);
b[1] = a[1];
sum[1] = a[1];
tot[1] = sum[1];
if (sum[1] > 0) flag[1] = 1;
if (sum[1] < 0) flag[1] = 0;
if (sum[1] == 0) m = 1;
if (m == 0) {
for (int i = 2; i <= n; i++) {
scanf("%lld", &a[i]);
sum[i] = a[i] + sum[i - 1];
if (sum[i] > 0) flag[i] = 1;
if (sum[i] < 0) flag[i] = 0;
if (flag[i - 1] == 1) {
if (sum[i] >= 0) {
ans += sum[i] + 1;
sum[i] = -1;
flag[i] = 0;
}
} else {
if (sum[i] <= 0) {
ans += 1 - sum[i];
sum[i] = 1;
flag[i] = 1;
}
}
}
printf("%lld\n", ans);
} else {
flag[1] = 0;
ans = 1;
sum[1] = -1;
for (int i = 2; i <= n; i++) {
scanf("%lld", &a[i]);
b[i] = a[i];
sum[i] = a[i] + sum[i - 1];
if (sum[i] > 0) flag[i] = 1;
if (sum[i] < 0) flag[i] = 0;
if (flag[i - 1] == 1) {
if (sum[i] >= 0) {
ans += sum[i] + 1;
sum[i] = -1;
flag[i] = 0;
}
} else if (flag[i - 1] == 0) {
if (sum[i] <= 0) {
ans += 1 - sum[i];
sum[i] = 1;
flag[i] = 1;
}
}
}
k[1] = 1;
tot[1] = 1;
ant = 1;
for (int i = 2; i <= n; i++) {
tot[i] = b[i] + tot[i - 1];
if (tot[i] > 0) k[i] = 1;
if (tot[i] < 0) k[i] = 0;
if (k[i - 1] == 1) {
if (tot[i] >= 0) {
ant += tot[i] + 1;
tot[i] = -1;
k[i] = 0;
}
} else if (k[i - 1] == 0) {
if (tot[i] <= 0) {
ant = ans + 1 - tot[i];
tot[i] = 1;
k[i] = 1;
}
}
}
printf("%lld\n", min(ant, 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;
const long long int LINF = 1001002003004005006ll;
const int INF = 1001001001;
const int MOD = 1000000007;
int main() {
int N;
cin >> N;
vector<int> A(N);
for (int i = 0; i < (N); ++i) {
cin >> A[i];
}
vector<int> dp(N), sum(N);
int a = 0;
if (A[0] > 0) {
dp[0] = A[0];
} else {
dp[0] = 1;
a += 1 - A[0];
}
sum[0] = dp[0];
for (int i = 1; i < N; ++i) {
if (i % 2 == 0) {
if (sum[i - 1] + A[i] > 0) {
sum[i] = sum[i - 1] + A[i];
} else {
sum[i] = 1;
a += 1 - (sum[i - 1] + A[i]);
}
} else {
if (sum[i - 1] + A[i] < 0) {
sum[i] = sum[i - 1] + A[i];
} else {
sum[i] = -1;
a += 1 + (sum[i - 1] + A[i]);
}
}
}
int b = 0;
if (A[0] < 0) {
dp[0] = A[0];
} else {
dp[0] = -1;
b += 1 + A[0];
}
sum[0] = dp[0];
for (int i = 1; i < N; ++i) {
if (i % 2 == 1) {
if (sum[i - 1] + A[i] > 0) {
sum[i] = sum[i - 1] + A[i];
} else {
sum[i] = 1;
b += 1 - (sum[i - 1] + A[i]);
}
} else {
if (sum[i - 1] + A[i] < 0) {
sum[i] = sum[i - 1] + A[i];
} else {
sum[i] = -1;
b += 1 + (sum[i - 1] + A[i]);
}
}
}
cout << min(a, b) << 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 n;
cin >> n;
vector<long long> sum(n);
long long ans = 0;
for (long long i = (0); i < (long long)(n); i++) {
long long a;
cin >> a;
if (i == 0) {
sum[i] = a;
} else {
if (sum[i - 1] > 0 && sum[i - 1] + a < 0) {
sum[i] = a + sum[i - 1];
} else if (sum[i - 1] < 0 && sum[i - 1] + a > 0) {
sum[i] = a + sum[i - 1];
} else if (sum[i - 1] > 0 && sum[i - 1] + a >= 0) {
ans += (sum[i - 1] + a + 1);
sum[i] = -1;
} else if (sum[i - 1] < 0 && sum[i - 1] + a <= 0) {
ans += (1 - (sum[i - 1] + a));
sum[i] = 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())
ans=0
a=list(map(int,input().split()))
sum=a[0]
for i in range(1,n):
if (sum+a[i])*sum<0:
sum+=a[i]
else:
if sum+a[i]>=1:
ans+=abs(sum+a[i]+1)
a[i]-=sum+a[i]+1
sum=-1
elif sum+a[i]<=-1:
ans+=abs(sum+a[i]+1)
a[i]+=sum+a[i]
sum=1
else:
if sum<0:
ans+=1
a[i]+=1
sum=1
else:
ans+=1
a[i]-=1
sum=-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()))
ind=1
res=10**10+1
for isplus in (True,False):
cur=0
ans=0
for i in range(N):
cur+=A[i]
if isplus:
if cur<=0:
ans+=abs(cur-1)
cur=1
isplus=False
else:
if cur>=0:
ans+=abs(cur-(-1))
cur=-1
isplus=True
if res>ans:
res=ans
print(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 | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
long long res1 = 0, res2 = 0;
long long sum1[n], sum2[n];
if (a[0] < 0) {
sum1[0] = -a[0] + 1;
sum2[0] = a[0];
res1 += abs(-a[0] + 1);
} else if (a[0] > 0) {
sum1[0] = a[0];
sum2[0] = -a[0] - 1;
res2 += abs(-a[0] - 1);
} else if (a[0] == 0) {
sum1[0] = 1;
res1 += 1;
sum2[0] = -1;
res2 += 1;
}
for (int i = 1; i < n; i++) {
sum1[i] = sum1[i - 1] + a[i];
long long sum = sum1[i];
if (sum1[i] <= 0 && sum1[i - 1] < 0) {
sum1[i] += -sum + 1;
res1 += abs(-sum + 1);
} else if (sum1[i] >= 0 && sum1[i - 1] > 0) {
sum1[i] += -sum - 1;
res1 += abs(-sum - 1);
}
sum2[i] = sum2[i - 1] + a[i];
sum = sum2[i];
if (sum2[i] <= 0 && sum2[i - 1] < 0) {
sum2[i] += -sum + 1;
res2 += abs(-sum + 1);
} else if (sum2[i] >= 0 && sum2[i - 1] > 0) {
sum2[i] += -sum - 1;
res2 += abs(-sum - 1);
}
}
cout << min(res1, res2) << 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 = [int(a) for a in input().split()]
times = 0
previous = a[0]
for i in range(1, n):
if previous < 0:
if previous + a[i] > 0:
previous = previous + a[i]
else:
times += 1 - (previous + a[i])
previous = 1
elif previous > 0:
if previous + a[i] < 0:
previous = previous + a[i]
else:
times += abs(-1 - (previous + a[i]))
previous = -1
times2 = 0
if a[0] > 0:
times2 += abs(-1 - a[0])
previous = -1
else:
times2 += 1 - a[0]
previous = 1
for i in range(1, n):
if previous < 0:
if previous + a[i] > 0:
previous = previous + a[i]
else:
times2 += 1 - (previous + a[i])
previous = 1
elif previous > 0:
if previous + a[i] < 0:
previous = previous + a[i]
else:
times2 += abs(-1 - (previous + a[i]))
previous = -1
print(min(times, times2))
|
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;
__attribute__((constructor)) void initial() {
cin.tie(0);
ios::sync_with_stdio(false);
}
int main() {
long long N;
cin >> N;
vector<long long> a;
for (int i = 0; i < (N); i++) {
long long ai;
cin >> ai;
a.push_back(ai);
}
long long changeCount = 0;
long long sum = 0;
bool nextSumPositive = a[0] > 0;
if (a[0] == 0) {
changeCount++;
if (a[1] > 0) {
a[0] = -1;
} else {
a[0] = 1;
}
}
for (int i = 0; i < (N); i++) {
sum += a[i];
if (nextSumPositive) {
if (sum <= 0) {
long long change = -sum + 1;
changeCount += abs(change);
sum += change;
}
} else {
if (sum >= 0) {
long long change = -sum - 1;
changeCount += abs(change);
sum += change;
}
}
nextSumPositive = !nextSumPositive;
}
cout << changeCount << 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 | import math
n = int(input())
a = list(map(int, input().split()))
cnt = 0
x = []
x.append(a[0])
for i in range(n-1):
if x[i] > 0:
if x[i]+a[i+1] > -1:
cnt += math.fabs(x[i] + a[i+1]) + 1
x += [-1]
else:
x += [x[i] + a[i+1]]
else:
if x[i]+a[i+1] < +1:
cnt += math.fabs(x[i] + a[i+1]) + 1
x += [1]
else:
x += [x[i] + a[i+1]]
print(int(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 | import sys
input = sys.stdin.readline
n = int(input())
a = [int(i) for i in input().split()]
# i番目までの和が正(i: 奇数)
res1 = 0
cum = 0
for i in range(n):
cum += a[i]
if i % 2 == 1:
while cum <= 0:
cum += 1
res1 += 1
else:
while cum >= 0:
cum -= 1
res1 += 1
# i番目までの和が負(i: 奇数)
res2 = 0
cum = 0
for i in range(n):
cum += a[i]
if i % 2 == 0:
while cum <= 0:
cum += 1
res2 += 1
else:
while cum >= 0:
cum -= 1
res2 += 1
print(min(res1,res2)) |
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>
#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
//#define int long long
using bll = boost::multiprecision::cpp_int;
using ll = long long;
//constexpr int INF = 1e9;//INT_MAX=(1<<31)-1=2147483647
constexpr ll INF = (ll)1e18;//(1LL<<63)-1=9223372036854775807
constexpr ll MOD = (ll)1e9 + 7;
constexpr double EPS = 1e-9;
constexpr int dx[4]={1,0,-1,0};
constexpr int dy[4]={0,1,0,-1};
#define p(var) std::cout<<var<<std::endl
#define PI (acos(-1))
#define rep(i, n) for(ll i=0, i##_length=(n); i< i##_length; ++i)
#define repeq(i, n) for(ll i=1, i##_length=(n); i<=i##_length; ++i)
#define all(v) (v).begin(), (v).end()
#define uniq(v) (v).erase(unique((v).begin(), (v).end()), (v).end());
template<typename T> inline void pv(vector<T> v) { for(ll i=0, N=v.size(); i<N; i++) cout<< v[i] << (i==N-1 ? '\n' : ' '); }
template<typename T> inline T gcd(T a, T b) { return b ? gcd(b,a%b) : a; }
template<typename T> inline T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<typename T1, typename T2> inline T1 power(T1 x, T2 n){ return n ? power(x*x%MOD,n/2)*(n%2?x:1)%MOD : 1; }
template<typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template<typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }
template<typename T> class dvector : public std::vector<T> {
public:
dvector() : std::vector<T>() {}
explicit dvector(size_t n, const T& value = T()) : std::vector<T>(n,value) {}
dvector(const std::vector<T>& v) : std::vector<T>(v) {}
T& operator[](size_t n){ return this->at(n); }
};
template<typename T1, typename T2> ostream& operator<<(ostream& s, pair<T1, T2>& p) {return s << "(" << p.first << ", " << p.second << ")";}
template<typename T> ostream& operator<<(ostream& s, dvector<T>& v) {
for (int i = 0, len = v.size(); i < len; ++i){ s << v[i]; if (i < len - 1) s << "\t"; } return s; }
template<typename T> ostream& operator<<(ostream& s, dvector< dvector<T> >& vv) {
for (int i = 0, len = vv.size(); i < len; ++i){ s << vv[i] << endl; } return s; }
template<typename T1, typename T2> ostream& operator<<(ostream& s, map<T1, T2>& m) {
s << "{" << endl; for (auto itr = m.begin(); itr != m.end(); ++itr){ s << "\t" << (*itr).first << " : " << (*itr).second << endl; } s << "}" << endl; return s; }
template<typename T> ostream& operator<<(ostream& s, set<T>& se) {
s << "{ "; for (auto itr = se.begin(); itr != se.end(); ++itr){ s << (*itr) << "\t"; } s << "}" << endl; return s; }
template<typename T> ostream& operator<<(ostream& s, multiset<T>& se) {
s << "{ "; for (auto itr = se.begin(); itr != se.end(); ++itr){ s << (*itr) << "\t"; } s << "}" << endl; return s; }
#ifdef LOCAL_DEV
#define debug(var) std::cout<<#var" = "<<var<<std::endl
#else
#define debug(var)
#endif
#ifdef LOCAL_TEST
#define vector dvector
#endif
/*-----8<-----8<-----*/
signed main() {
ll N;
cin>>N;
vector<ll> a(N,0);
rep(i,N)cin>>a[i];
vector<ll> rui(N+1,0);
rep(i,N)rui[i+1]=rui[i]+a[i];
ll c,t=a[0]>0 ? 1 : -1;
if([&]{
rep(i,N-1){
if(t==1){
if(rui[i+2]>0)return false;
}else{
if(rui[i+2]<0)return false;
}
t*=-1;
}
return true;
}()){
p(0);return 0;
}
//+
t=0;
ll ansb=0;
if(rui[1]>0){
}else{
t+=-rui[1]+1;
ansb+=abs(-rui[1]+1);
}
c=-1;
for(ll i=1;i<N;i++){
ll tt=rui[i+1]+t;
if(c==1){
if(tt>0){
}else{
t+=-tt+1;
ansb+=abs(-tt+1);
}
}else{
if(tt>0){
t+=-tt-1;
ansb+=abs(-tt-1);
}else{
}
}
c*=-1;
}
//-
t=0;
ll ansc=0;
if(rui[1]>0){
t+=-rui[1]-1;
ansc+=abs(-rui[1]-1);
}else{
}
c=1;
for(ll i=1;i<N;i++){
ll tt=rui[i+1]+t;
if(c==1){
if(tt>0){
}else{
t+=-tt+1;
ansc+=abs(-tt+1);
}
}else{
if(tt>=0){
t+=-tt-1;
ansc+=abs(-tt-1);
}else{
}
}
c*=-1;
}
p(min(ansb,ansc));
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 | #include <bits/stdc++.h>
int main(void) {
int n;
long sum1 = 0;
long sum2 = 0;
long tmp;
long count = 0;
int a[100000];
char input[1000000];
int i = 0, j = 0;
int cp = 0, tcp = 0;
char tp[12];
tp[12] = '\0';
fgets(input, 1000000, stdin);
n = atoi(input);
fgets(input, 1000000, stdin);
for (i = 0; i < n; i++) {
while (input[cp] != ' ' && input[cp] != '\n') {
tp[tcp] = input[cp];
tcp++;
cp++;
}
tp[tcp] = '\0';
tcp = 0;
cp++;
a[i] = atoi(tp);
}
for (i = 0; i < n; i++) {
if (i % 2 == 0)
sum2 += a[i];
else
sum1 += a[i];
}
tmp = a[0];
if (sum1 == sum2) {
if (a[0] < 0) {
sum1++;
} else {
sum2++;
}
}
for (i = 1; i < n; i++) {
if (sum1 > sum2) {
if (i % 2 == 0) {
tmp += a[i];
while (tmp > -1) {
count++;
tmp--;
}
} else {
tmp += a[i];
while (tmp < 1) {
count++;
tmp++;
}
}
} else if (sum2 > sum1) {
if (i % 2 == 1) {
tmp += a[i];
while (tmp > -1) {
count++;
tmp--;
}
} else {
tmp += a[i];
while (tmp < 1) {
count++;
tmp++;
}
}
}
}
printf("%ld\n", count);
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 int INF = 1 << 29, MOD = 1e9 + 7;
int main() {
int n;
cin >> n;
long a[n];
cin >> a[0];
long ans = 0;
for (int i = 1; i < n; i++) {
int x;
cin >> x;
if (a[0] == 0) {
x > 0 ? a[0] = -1 : a[0] = 1;
ans++;
}
a[i] = a[i - 1] + x;
if (a[i] * a[i - 1] >= 0) {
if (a[i - 1] > 0) {
ans += a[i] + 1;
a[i] = -1;
} else {
ans += 1 - a[i];
a[i] = 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 | cpp | #include <bits/stdc++.h>
int inf = 1000000007;
using namespace std;
int main() {
int n;
cin >> n;
vector<int> data(n);
int ans = 0;
for (int i = 0; i < n; i++) {
cin >> data.at(i);
}
int64_t sum = data.at(0);
int64_t sump = sum;
for (int i = 1; i < n; i++) {
sump += data.at(i);
if (sum * sump >= 0) {
int c = sump;
if (c < 0) c *= -1;
c++;
ans += c;
if (sump > 0) {
data.at(i) -= c;
sump -= c;
} else {
data.at(i) += c;
sump += c;
}
}
sum += data.at(i);
}
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 | # -*- coding: utf-8 -*-
"""
https://abc059.contest.atcoder.jp/tasks/arc072_a
"""
import sys
from sys import stdin
input = stdin.readline
def check_sign(n):
if n > 0:
return 1
elif n < 0:
return -1
else:
return 0
def solve(A):
p_ans = 0
n_ans = 0
total = A[0]
prev_sign = check_sign(total)
# 最初をプラス側に振った場合の解
if prev_sign == 0:
p_ans += 1
total += 1
prev_sign = 1
for a in A[1:]:
total += a
sign = check_sign(total)
if sign == 0:
total -= prev_sign
p_ans += 1
elif prev_sign != sign:
prev_sign = sign
else:
p_ans += (abs(total) + 1)
if prev_sign < 0:
total = 1
prev_sign = 1
else:
total = -1
prev_sign = -1
# 最初をマイナス側に振った場合の解
if prev_sign == 0:
n_ans += 1
total -= 1
prev_sign = -1
for a in A[1:]:
total += a
sign = check_sign(total)
if sign == 0:
total -= prev_sign
n_ans += 1
elif prev_sign != sign:
prev_sign = sign
else:
n_ans += (abs(total) + 1)
if prev_sign < 0:
total = 1
prev_sign = 1
else:
total = -1
prev_sign = -1
return min(p_ans, n_ans)
def main(args):
n = int(input())
A = [int(x) for x in input().split()]
ans = solve(A)
print(ans)
if __name__ == '__main__':
main(sys.argv[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 | cpp | #include <bits/stdc++.h>
using namespace std;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const int MAX_N = int(1e5);
long long n, a[MAX_N], dp[MAX_N];
void solve() {
long long sum_diff = 0, ans = 0;
for (long long i = 0; i < (long long)(n - 1); i++) {
long long diff = 0;
dp[i + 1] += sum_diff;
if (dp[i] * dp[i + 1] > 0) {
if (dp[i + 1] > 0) {
diff = -1 - dp[i + 1];
sum_diff += diff;
dp[i + 1] = -1;
} else {
diff = 1 - dp[i + 1];
sum_diff += diff;
dp[i + 1] = 1;
}
}
if (dp[i + 1] == 0) {
sum_diff++, diff = 1;
dp[i + 1] = 1;
}
ans += abs(diff);
}
cout << ans << endl;
}
int main() {
cin >> n;
for (long long i = 0; i < (long long)(n); i++) {
cin >> a[i];
if (i == 0)
dp[0] = a[0];
else
dp[i] = dp[i - 1] + a[i];
}
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 | # vim: fileencoding=utf-8
def main():
n = int(input())
a = list(map(int, input().split()))
ans = 0
p = 0
q = 0
flg = 0
if a[0] > 0:
p = a[0]
flg = 1
else:
q = abs(a[0])
flg = -1
for i in a[1:]:
# print(i, p, q, ans)
if i > 0:
p += i
elif i < 0:
q += abs(i)
if flg == 1:
if p >= q:
t = p - q + 1
ans += t
q += t
flg = -1
elif flg == -1:
if q >= p:
t = q - p + 1
ans += t
p += t
flg = 1
print(ans)
if __name__ == "__main__":
main()
|
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
n = int(input())
a = list(map(int,input().split()))
cnt = 0
current_sum = a[0]#現在の操作後の値
for i in range(n-1):
if current_sum * (current_sum + a[i+1]) < 0:#符号が違う
current_sum += a[i+1]
cnt += 0#そのまま
elif current_sum < 0:
cnt += 1 - (current_sum + a[i+1])
current_sum = 1
else:
cnt += (current_sum + a[i+1]) +1
current_sum = -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 | cpp | #include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9, MOD = 1e9 + 7;
const double EPS = 1e-9, PI = 3.141592653589793;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
long long n, a[100001], seq, ans = 0, tmp;
cin >> n;
for (long long i = 0; i < n; i++) cin >> a[i];
seq = a[0];
for (long long i = 1; i < n; i++) {
if (seq > 0) {
seq += a[i];
if (seq >= 0) {
tmp = seq;
seq = -1;
ans += tmp + 1;
}
} else {
seq += a[i];
if (seq <= 0) {
tmp = seq;
seq = 1;
ans += abs(tmp) + 1;
}
}
}
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 | cpp | #include <bits/stdc++.h>
int a[100000 + 100], b[100000 + 100];
int main() {
int n;
long long cnt = 0;
scanf("%d", &n);
scanf("%d", &a[1]);
b[1] = a[1];
for (int i = 2; i <= n; i++) {
scanf("%d", &a[i]);
if (b[i - 1] < 0) {
if (b[i - 1] + a[i] > 0)
b[i] = b[i - 1] + a[i];
else {
b[i] = 1;
cnt += 1 - a[i] - b[i - 1];
}
} else if (b[i - 1] > 0) {
if (b[i - 1] + a[i] < 0)
b[i] = b[i - 1] + a[i];
else {
b[i] = -1;
cnt += b[i - 1] + a[i] + 1;
}
}
}
printf("%lld\n", cnt);
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;
int arr[n];
for (int i = 0; i < n; i++) cin >> arr[i];
int sum = arr[0];
int sum1, sum2;
int ans1 = 0, ans2 = 0;
if (sum > 0) {
sum1 = sum;
sum2 = -1;
ans1 = 0;
ans2 = sum + 1;
} else if (sum < 0) {
sum2 = sum;
sum1 = 1;
ans2 = 0;
ans1 = abs(sum) + 1;
} else if (sum == 0) {
sum1 = 1;
sum2 = -1;
ans2 = 1;
ans1 = 1;
}
for (int i = 1; i < n; i++) {
if (sum1 > 0) {
sum1 = sum1 + arr[i];
if (sum1 < 0)
continue;
else {
ans1 += sum1 + 1;
sum1 = -1;
}
} else {
sum1 = sum1 + arr[i];
if (sum1 > 0)
continue;
else {
ans1 += abs(sum1) + 1;
sum1 = 1;
}
}
}
for (int i = 1; i < n; i++) {
if (sum2 > 0) {
sum2 = sum2 + arr[i];
if (sum2 < 0)
continue;
else {
ans2 += sum2 + 1;
sum2 = -1;
}
} else {
sum2 = sum2 + arr[i];
if (sum2 > 0)
continue;
else {
ans2 += abs(sum2) + 1;
sum2 = 1;
}
}
}
if (ans1 < ans2)
cout << ans1;
else
cout << ans2;
cout << 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()))
ans1,ans2 = 0,0
res = A[0]
for i in range(1,N):
res += A[i]
if i%2==1:
if res >= 0:
ans1 += res+1
res = -1
else:
if res <= 0:
ans1 -= res-1
res = 1
for i in range(1,N):
res += A[i]
if i%2==1:
if res <= 0:
ans2 -= res-1
res = 1
else:
if res >= 0:
ans2 += res+1
res = -1
print(min(ans1,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>
#include <boost/range/irange.hpp>
#include <boost/range/adaptors.hpp>
using namespace std;
using namespace boost;
using namespace boost::adaptors;
using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long int;
int main() {
ll n;
cin >> n;
ll count{0}, s_now{0}, s_prev{0};
for (auto &&i: irange(0LL, n)){
ll a;
cin >> a;
s_now += a;
// cout << " new s: " << s_now << endl;
if (s_prev * s_now >= 0 && i != 0){
// cout << "kakikae because: " << s_prev * s_now << endl;
ll target = s_prev < 0 ? 1 : -1;
// cout << "from: " << s_now << " to: " << target << endl;
count += abs(target - s_now);
// cout << "count: " << count << endl;
s_now = target;
}
s_prev = s_now;
// cout << "old s: " << s_prev;
}
cout << count;
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 | n = gets.to_i
as = gets.split.map(&:to_i)
cnt1 = 0
sum = as[0]
n.times.with_index(1) do |_,i|
break if as[i].nil?
if sum < 0
if sum + as[i] > 0
sum += as[i]
next
else
x = 1 - sum - as[i]
cnt1 += x
sum = 1
end
elsif sum > 0
if sum + as[i] < 0
sum += as[i]
next
else
x = -1 - sum - as[i]
cnt1 += x.abs
sum = -1
end
end
end
cnt2 = 0
sum = as[0] * -1
n.times.with_index(1) do |_,i|
break if as[i].nil?
if sum < 0
if sum + as[i] > 0
sum += as[i]
next
else
x = 1 - sum - as[i]
cnt2 += x
sum = 1
end
elsif sum > 0
if sum + as[i] < 0
sum += as[i]
next
else
x = -1 - sum - as[i]
cnt2 += x.abs
sum = -1
end
end
end
puts [cnt1, cnt2].min
|
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 a[100010], n;
long long s, cnt, ans = 1e9;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
if (a[1] > 0)
s = a[1];
else
cnt = 1 - a[1], s = 1;
for (int i = 2; i <= n; ++i)
if (s * (s + a[i]) < 0)
s += a[i];
else {
cnt += abs(s + a[i]) + 1;
s = ((s < 0) << 1) - 1;
}
ans = cnt;
if (a[1] < 0)
cnt = 0, s = a[1];
else
cnt = a[1] - 1, s = -1;
for (int i = 2; i <= n; ++i)
if (s * (s + a[i]) < 0)
s += a[i];
else {
cnt += abs(s + a[i]) + 1;
s = ((s < 0) << 1) - 1;
}
printf("%lld\n", std::min(ans, cnt));
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<int> a(n);
for (int i = 0; i < (int)(n); i++) {
cin >> a.at(i);
}
int cntp = 0;
if (a.at(0) <= 0) {
cntp = -1 * a.at(0) + 1;
}
int sum = max(1, a.at(0));
for (int i = (1); i < (int)(n); i++) {
if (sum > 0) {
if (sum + a.at(i) >= 0) {
cntp += sum + a.at(i) + 1;
sum = -1;
} else {
sum += a.at(i);
}
} else {
if (sum + a.at(i) <= 0) {
cntp += -1 * (sum + a.at(i)) + 1;
sum = 1;
} else {
sum += a.at(i);
}
}
}
int cntm = 0;
if (a.at(0) >= 0) {
cntm = a.at(0) + 1;
}
int summ = min(-1, a.at(0));
for (int i = (1); i < (int)(n); i++) {
if (summ > 0) {
if (summ + a.at(i) >= 0) {
cntm += summ + a.at(i) + 1;
summ = -1;
} else {
summ += a.at(i);
}
} else {
if (summ + a.at(i) <= 0) {
cntm += -1 * (summ + a.at(i)) + 1;
summ = 1;
} else {
summ += a.at(i);
}
}
}
cout << min(cntp, cntm) << 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 sc=new Scanner(System.in);
int n=sc.nextInt();
long[] a=new long[n];
for(int i=0;i<n;i++)a[i]=sc.nextLong();
long sum=0;
long count=0;
for(int i=0;i<n-1;i++){
if(i==0){
if(a[i]==0){
for(int j=1;j<n;j++){
if(a[j]>0){
if(j%2==0){
a[i]+=1;
count++;
break;
}else{
a[i]-=1;
count++;
break;
}
}else if(a[j]<0){
if(j%2==0){
a[i]-=1;
count++;
break;
}else{
a[i]+=1;
count++;
break;
}
}
}
if(a[i]==0){
a[i]=1;
count++;
}
}
}
sum+=a[i];
if(sum>0){
if(sum+a[i+1]>=0){
count+=sum+a[i+1]+1;
a[i+1]-=sum+a[i+1]+1;
}
}else if(sum<0){
if(sum+a[i+1]<=0){
count+=-1*(sum+a[i+1])+1;
a[i+1]+=-1*(sum+a[i+1])+1;
}
}
}
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 | python3 | n = int(input())
A = list(map(int,input().split()))
ans = 0
x = 0
S = []
S.append(A[0])
for i in range(1,n):
if S[i - 1] == 0:
S[i - 1] += 1
x = A[i] + S[i - 1]
if S[i - 1] > 0:
if x >= 0:
x = - x - 1
ans += abs(x)
S.append(-1)
else:
S.append(x)
elif S[i - 1] < 0:
if x <= 0:
x = -x + 1
ans += abs(x)
S.append(1)
else:
S.append(x)
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()))
a = [A,A]
res = [0,0]
sum = 0
for check in range(2):
sum = 0
if check == 1:
if a[check][0] > 0:
temp = -1 - a[check][0]
a[check][0] += temp
res[check] += temp * -1
elif a[check][0] < 0:
temp = 1 - a[check][0]
a[check][0] += temp
res[check] += temp
if a[check][0] == 0:
if check == 0:
a[check][0] += 1
else:
a[check][0] -= 1
res[check] += 1
for i in range(n-1):
sum += a[check][i]
if sum * (sum + a[check][i+1]) >= 0:
if sum > 0:
temp = -1 - sum - a[check][i+1]
a[check][i+1] += temp
res[check] += temp * -1
else:
temp = 1 - sum - a[check][i+1]
a[check][i+1] += temp
res[check] += temp
print(min(res[0],res[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 | cpp | #include <bits/stdc++.h>
using namespace std;
typedef int _loop_int;
#define REP(i,n) for(_loop_int i=0;i<(_loop_int)(n);i++)
int main(){
int n; cin >> n;
int a[n]; REP(i,n) cin >> a[i];
long long sum = 0;
long long count = 0;
long long tmp = 0;
REP(i,n){
if(i%2==0){
sum += a[i];
if(sum<=0){
count += abs(sum)+1;
sum += abs(sum)+1;
}
}
if(i%2==1){
sum += a[i];
if(sum>=0){
count += abs(sum)+1;
sum -= abs(sum)+1;
}
}
// cout << a[i] << ' ';
}
// cout << endl;
int count2 = 0;
sum = 0;
REP(i,n){
if(i%2==1){
sum += a[i];
if(sum<=0){
count2 += abs(sum)+1;
sum += abs(sum)+1;
}
}
if(i%2==0){
sum += a[i];
if(sum>=0){
count2 += abs(sum)+1;
sum -= abs(sum)+1;
}
}
// cout << a[i] << ' ';
}
// cout << count << endl;
cout << min(count,count2) << 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 long long MOD = 1000000007;
const long long INF = -10000000000;
long long maxx(long long x, long long y, long long z) {
return max(max(x, y), z);
}
long long minn(long long x, long long y, long long z) {
return min(min(x, y), z);
}
long long gcd(long long x, long long y) {
if (x % y == 0)
return y;
else
return gcd(y, x % y);
}
long long lcm(long long x, long long y) { return x * (y / gcd(x, y)); }
int main() {
long long N;
cin >> N;
vector<long long> A(N);
for (long long i = 0; i < N; i++) cin >> A[i];
long long cnt = 0;
long long sum = A[0];
for (long long i = 1; i <= N - 1; i++) {
if (abs(sum * 2 + A[i]) < abs(sum) + abs(sum + A[i]))
sum += A[i];
else {
if (sum < 0) {
cnt += abs((-1) * (sum - 1 + A[i]));
sum = 1;
} else {
cnt += abs((-1) * (sum + 1 + A[i]));
sum = -1;
}
}
}
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 | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void) {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
int tmp1 = 0, tmp2 = 0;
int ans1 = 0, ans2 = 0;
for (int i = 1; i <= n; ++i) {
tmp1 += a[i - 1];
tmp2 += a[i - 1];
if (i % 2) {
if (tmp1 <= 0) {
ans1 += 1 - tmp1;
tmp1 = 1;
}
if (tmp2 >= 0) {
ans2 += 1 + tmp2;
tmp2 = -1;
}
} else {
if (tmp1 >= 0) {
ans1 += 1 + tmp1;
tmp1 = -1;
}
if (tmp2 <= 0) {
ans2 += 1 - tmp2;
tmp2 = 1;
}
}
}
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;
int count_operate(vector<int> a) {
int N = a.size();
vector<int> s(N, 0);
int retval = 0;
s.at(0) = a.at(0);
for (int i = 1; i < N; i++) {
s.at(i) = a.at(i) + s.at(i - 1);
if (s.at(i - 1) > 0 && s.at(i) >= 0) {
retval += abs(s.at(i)) + 1;
s.at(i) = -1;
} else if (s.at(i - 1) < 0 && s.at(i) <= 0) {
retval += abs(s.at(i)) + 1;
s.at(i) = 1;
}
}
return retval;
};
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
int retval;
if (a[0] == 0) {
a[0] = 1;
int retval_p = count_operate(a);
a[0] = -1;
int retval_m = count_operate(a);
retval = min(retval_m, retval_p);
} else {
retval = count_operate(a);
}
cout << retval << 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 unsigned long long MOD = 1000000000 + 7;
int main() {
int n;
cin >> n;
int cnt = 0;
int sum = 0;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
int s = a + sum;
if (a == 0 || s == 0) {
if (sum <= 0) {
cnt += 1 - s;
sum = 1;
} else {
cnt += 1 + s;
sum = 1;
}
} else if (sum < 0 && s < 0) {
cnt += 1 - s;
sum = 1;
} else if (sum > 0 && s > 0) {
cnt += 1 + s;
sum = -1;
} else {
sum = s;
}
}
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 | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
cin.sync_with_stdio(false);
int n;
cin >> n;
vector<ll> a(n);
ll sum = 0;
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];
}
}
if (sum == 0) {
count += 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()))
ans =0
for i in range(n-1):
if a[i]>0:
if sum(a[:i+2])<0:
pass
else:
while sum(a[:i+2])>=0:
a[i+1]-=1
ans+=1
else:
if sum(a[:i+2])>0:
pass
else:
while sum(a[:i+2])<=0:
a[i+1]+=1
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 | cpp | #include <bits/stdc++.h>
using namespace std;
const long long INFTY = 1001001001;
int main() {
int N;
cin >> N;
vector<long long> a(N + 1), b(N + 1);
for (int i = 0; i < N; ++i) {
cin >> a[i];
b[i] = a[i];
if (i != 0) {
a[i] += a[i - 1];
b[i] += b[i - 1];
}
}
long long ans = INFTY;
for (int sign = 0; sign < 2; ++sign) {
if (sign) {
for (int i = 0; i < N + 1; ++i) {
a[i] = b[i];
}
}
long long v = 0, count = 0;
for (int i = 0; i < N; ++i) {
if (sign) {
if (a[i] >= 0) {
count += abs(-a[i] - 1);
v -= abs(-a[i] - 1);
}
a[i + 1] += v;
} else {
if (a[i] <= 0) {
count += abs(-a[i] + 1);
v += abs(-a[i] + 1);
}
a[i + 1] += v;
}
sign = !(sign);
}
ans = min(ans, count);
}
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;
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < (int)(n); i++) cin >> a[i];
long long prevArraySum = a[0];
long long currentArraySum = a[0];
long long res = 0;
if (a[0] == 0) {
res = 1;
prevArraySum = -1;
currentArraySum = -1;
for (int i = (1); i < (n); ++i) {
if (prevArraySum > 0) {
currentArraySum = prevArraySum + a[i];
if (currentArraySum >= 0) {
res += abs(-1 - currentArraySum);
prevArraySum = -1;
} else {
prevArraySum = currentArraySum;
}
} else {
currentArraySum = prevArraySum + a[i];
if (currentArraySum <= 0) {
res += abs(1 - currentArraySum);
prevArraySum = 1;
} else {
prevArraySum = currentArraySum;
}
}
}
long long res1 = res;
res = 1;
prevArraySum = 1;
currentArraySum = 1;
for (int i = (1); i < (n); ++i) {
if (prevArraySum > 0) {
currentArraySum = prevArraySum + a[i];
if (currentArraySum >= 0) {
res += abs(-1 - currentArraySum);
prevArraySum = -1;
} else {
prevArraySum = currentArraySum;
}
} else {
currentArraySum = prevArraySum + a[i];
if (currentArraySum <= 0) {
res += abs(1 - currentArraySum);
prevArraySum = 1;
} else {
prevArraySum = currentArraySum;
}
}
}
res = min(res, res1);
} else {
for (int i = (1); i < (n); ++i) {
if (prevArraySum > 0) {
currentArraySum = prevArraySum + a[i];
if (currentArraySum >= 0) {
res += abs(-1 - currentArraySum);
prevArraySum = -1;
} else {
prevArraySum = currentArraySum;
}
} else {
currentArraySum = prevArraySum + a[i];
if (currentArraySum <= 0) {
res += abs(1 - currentArraySum);
prevArraySum = 1;
} else {
prevArraySum = currentArraySum;
}
}
}
}
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>
using namespace std;
int main() {
int N;
cin >> N;
long long int a[N];
for (int i = 0; i < N; i++) {
cin >> a[i];
}
long long int x = 0, y = 0;
long long int s = 0;
for (int i = 0; i < N; i++) {
s += a[i];
if (i % 2 == 1) {
if (s > 0) {
x += s + 1;
s = -1;
}
} else {
if (s < 0) {
x += 1 - s;
s = 1;
}
}
}
s = 0;
for (int i = 0; i < N; i++) {
s += a[i];
if (i % 2 == 0) {
if (s > 0) {
y += s + 1;
s = -1;
}
} else {
if (s < 0) {
y += 1 - s;
s = 1;
}
}
}
long long int ans = min(x, y);
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 f(vector<int> a, int sign) {
int ans = 0;
if (sign > 0 && a.at(0) <= 0) {
ans += -a.at(0) + 1;
a.at(0) += -a.at(0) + 1;
}
if (sign < 0 && a.at(0) >= 0) {
ans += a.at(0) + 1;
a.at(0) -= a.at(0) + 1;
}
long long sum = a.at(0);
for (int i = 1; i < a.size(); i++) {
if (sum > 0 && a.at(i) + sum >= 0) {
ans += a.at(i) + sum + 1;
a.at(i) -= a.at(i) + sum + 1;
}
if (sum < 0 && a.at(i) + sum <= 0) {
ans += -(a.at(i) + sum - 1);
a.at(i) += -(a.at(i) + sum - 1);
}
sum += a.at(i);
}
return ans;
}
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a.at(i);
}
cout << min(f(a, 1), f(a, -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 | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int sum = 0, num = 0;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
if (sum > 0) {
while (sum + a >= 0) {
a--;
num++;
}
} else if (sum < 0) {
while (sum + a <= 0) {
a++;
num++;
}
}
sum += a;
}
cout << num << 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 n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
bool plus = a[0] >= 0 ? true : false;
int cnt = 0;
int cnt2 = 0;
long long sum = 0;
long long sum2 = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
sum2 += a[i];
if (i % 2 == 0) {
if (sum <= 0) {
cnt += abs(sum) + 1;
sum = 1;
}
if (sum2 >= 0) {
cnt2 += sum2 + 1;
sum2 = -1;
}
} else {
if (sum2 <= 0) {
cnt2 += abs(sum2) + 1;
sum2 = 1;
}
if (sum >= 0) {
cnt += sum + 1;
sum = -1;
}
}
}
cout << min(cnt, cnt2) << 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 | n = gets.to_i
A = gets.split.map(&:to_i)
x = A[0]
answer = 0
for i in 0..n-2
s = x + A[i+1]
if x * s >= 0
if x < 0
answer = answer - s + 1
A[i+1] = A[i+1] - s + 1
else
answer = answer + s + 1
A[i+1] = A[i+1] - s - 1
end
end
x = x + A[i+1]
end
puts answer |
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 sequence(lis, first_value):
count = 0
now_sum = first_value
pre_sum = first_value
for num in lis[1:]:
now_sum += num
if now_sum == 0 or pre_sum * (now_sum) > 0:
count += int(abs(now_sum) + 1)
now_sum = -pre_sum/abs(pre_sum)
pre_sum = now_sum
return count
n = int(input())
a = [int(i) for i in input().split()]
if a[0] == 0:
print(min(sequence(a, 1), sequence(a, -1))+1)
else:
print(sequence(a, a[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 gcd(long long x, long long y) {
long long tmp = 0;
if (x < y) {
tmp = x;
x = y;
y = tmp;
}
while (y > 0) {
long long r = x % y;
x = y;
y = r;
}
return x;
}
long long lcm(long long x, long long y) { return x / gcd(x, y) * y; }
long long kaijo(long long k) {
long long sum = 1;
for (long long i = 1; i <= k; ++i) {
sum *= i;
sum %= 1000000000 + 7;
}
return sum;
}
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long c = 0;
long long sum = a[0];
for (int i = 1; i < n; i++) {
if (sum * (sum + a[i]) >= 0) {
int tmp = a[i];
a[i] = sum / abs(sum) * (-1) * (abs(sum) + 1);
c += abs(tmp - a[i]);
}
sum += a[i];
}
cout << c << 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>
#pragma GCC optimize("-O3")
using namespace std;
void _main();
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
_main();
}
const int inf = INT_MAX / 2;
const long long infl = 1LL << 60;
template <class T>
bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
enum PosiNega { POSITIVE = 0, NEGATIVE = 1 };
int solve(int N, int *a, PosiNega odd_posinega) {
long long ans = 0;
long long sum = 0;
PosiNega posi_nega = odd_posinega;
for (int i = 0; i < N; i++) {
sum += a[i];
if (POSITIVE == posi_nega) {
if (0 >= sum) {
ans += 1 - sum;
sum = 1;
}
posi_nega = NEGATIVE;
} else {
if (0 <= sum) {
ans += 1 + sum;
sum = -1;
}
posi_nega = POSITIVE;
}
}
return ans;
}
void _main() {
int N;
cin >> N;
int a[N];
for (int i = 0; i < N; i++) cin >> a[i];
long long ans = min(solve(N, a, POSITIVE), solve(N, a, NEGATIVE));
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() {
long N;
cin >> N;
long a[100001];
for (long i = 0; i < N; i++) {
cin >> a[i];
}
long total0 = 0;
long ops0 = 0;
for (int i = 0; i < N; i++) {
total0 += a[i];
if (total0 < 1) {
total0 = 1;
ops0 += 1 - a[i];
}
total0 = -total0;
}
long total1 = 0;
long ops1 = 0;
for (int i = 0; i < N; i++) {
total1 += a[i];
if (total1 > -1) {
total1 = -1;
ops1 += (a[i] + 1);
}
total1 = -total1;
}
printf("%d\n", min(ops0, ops1));
}
|
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 + 1, 0);
for (int i = 1; i <= n; i++) {
cin >> a[i];
a[i] += a[i - 1];
}
long long ans = 1LL << 50;
for (int k = -1; k <= 1; k += 2) {
int sign = k;
long long plus = 0, minus = 0;
for (int i = 1; i <= n; i++) {
if (a[i] + plus - minus > 0) {
if (sign == -1) minus += a[i] + plus - minus + 1;
} else if (a[i] + plus - minus < 0) {
if (sign == 1) plus += -(a[i] + plus - minus) + 1;
} else {
if (sign == -1)
minus += 1;
else if (sign == 1)
plus += 1;
}
sign *= -1;
}
if (ans > plus + minus) ans = plus + minus;
}
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 | n = int(input())
a = [int(i) for i in input().split()]
s = [0]*n
s[0] = a[0]
count = 0
for i in range(1,n):
s[i] = s[i-1] + a[i]
if s[i] == 0:
tmp = -s[i-1]//abs(s[i-1])
s[i] += tmp
count += abs(tmp)
elif s[i] * s[i-1] > 0:
tmp = (-s[i]) + (-s[i]//abs(s[i]))
s[i] += tmp
count += abs(tmp)
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 | UNKNOWN | n = gets.not_nil!.to_i
a = gets.not_nil!.split.map(&.to_i64)
ans = [0, 1].map do |i|
sum = 0
count = 0
n.times do |j|
sum += a[j]
if (i + j) % 2 == 0
if sum <= 0
count += -sum + 1
sum = 1
end
else
if sum >= 0
count += sum + 1
sum = -1
end
end
end
count
end.min
puts 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()))
s = a[0]
ans = 0
for i in range(n-1):
if s > 0:
s += a[i+1]
if s >= 0:
ans += abs(s) + 1
s = -1
"""
print(i)
print('s > 0')
print(ans)
print(s)
"""
elif s < 0:
s += a[i+1]
if s <= 0:
ans += abs(s) + 1
s = 1
"""
print(i)
print('s < 0')
print(ans)
print(s)
"""
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 a[1009];
int b[1009];
int main() {
int n, hh, sum = 0;
while (~scanf("%d", &n)) {
int s = 0;
for (int g = 0; g < n; g++) {
scanf("%d", &a[g]);
s += a[g];
b[g] = s;
}
int i;
for (i = 1; i < n; i++) {
if (b[i] > 0 && b[i - 1] > 0 || b[i] < 0 && b[i - 1] < 0 || b[i] == 0) {
hh = abs(b[i]) + 1;
if (b[i] < 0) {
int x = abs(b[i] + 1);
for (int j = i; j < n; j++) {
b[j] = b[j] + x;
}
} else if (b[i] > 0) {
int y = abs(b[i] + 1);
for (int t = i; t < n; t++) {
b[t] = b[t] - y;
}
} else if (b[i] == 0) {
if (b[i - 1] > 0) {
int c = 1;
for (int t = i; t < n; t++) {
b[t] = b[t] - c;
}
}
if (b[i - 1] < 0) {
int c = 1;
for (int t = i; t < n; t++) {
b[t] = b[t] + c;
}
}
}
sum += hh;
}
}
printf("%d\n", sum);
sum = 0;
}
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 = [int(x) for x in input().split()]
count = 0
sum_before = A[0]
for i in range(n):
if i == 0:
continue
sum_for_i = sum_before + A[i]
#print('[',i,']: before',sum_before,'after',sum_for_i, A)
if sum_for_i == 0 and sum_before > 0:
#print("case 1")
A[i] -= 1
count += 1
elif sum_for_i == 0 and sum_before <0:
#print("case 2")
A[i] += 1
count += 1
elif sum_before >0 and sum_for_i>0:
#print("case 3")
count += (abs(A[i])+1)
A[i] -= (abs(A[i])+1)
elif sum_before <0 and sum_for_i<0:
#print("case 4")
count += (abs(A[i])+1)
A[i] += (abs(A[i])+1)
#print('[',i,']: ',A, 'count', count)
sum_before += A[i]
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;
//型定義
typedef long long ll;
//定数
const int INF=1e+9;
const int MOD=1e+9+7;
//REPマクロ
#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define REP2(i,a,b) for(ll i=a;i<(ll)(b);i++)
#define REPD2(i,a,b) for(ll i=a;i>(ll)(b);i--)
// 多次元 vector 生成
template<class T>
vector<T> make_vec(size_t a){
return vector<T>(a);
}
template<class T, class... Ts>
auto make_vec(size_t a, Ts... ts){
return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
//vectorの扱い
#define ALL(x) (x).begin(),(x).end() //sortなどの引数省略
#define SIZE(x) ((ll)(x).size()) //size
#define MAX(x) *max_element(ALL(x)) //最大値
#define MIN(x) *min_element(ALL(x)) //最小値
int main(){
ll n;
cin>>n;
vector<ll> a(n);
ll sum=0;
ll ans=0;
REP(i,n) cin>>a[i];
REP(i,n){
ll tmp=sum;
sum+=a[i];
if(tmp>0&&sum>0){
ans+=(sum+1);
sum=-1;
}else if(tmp<0&&sum<0){
ans+=abs(sum-1);
sum=1;
}else if(sum==0){
ans++;
if(tmp<0){
sum++;
}else{
sum--;
}
}
}
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 int;
using ull = unsigned long long int;
using P = pair<ll, ll>;
using P3 = pair<P, int>;
using PP = pair<P, P>;
constexpr ll INF = 1LL << 60;
constexpr ll MOD = ll(1e9) + 7;
constexpr int di[] = {0, 1, 0, -1};
constexpr int dj[] = {1, 0, -1, 0};
constexpr int di8[] = {0, 1, 1, 1, 0, -1, -1, -1};
constexpr int dj8[] = {1, 1, 0, -1, -1, -1, 0, 1};
constexpr double EPS = 1e-9;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 1; i < n; i++) a[i] += a[i - 1];
ll s = 0, ans1 = 0, ans2 = 0;
for (int i = 0; i < n; i++) {
if (i % 2) {
if (a[i] + s <= 0) {
ans1 += abs(a[i] + s) + 1;
s = abs(a[i]) + 1;
}
} else {
if (a[i] + s >= 0) {
ans1 += abs(a[i] + s) + 1;
s = -(abs(a[i]) + 1);
}
}
}
s = 0;
for (int i = 0; i < n; i++) {
if (i % 2) {
if (a[i] + s >= 0) {
ans2 += abs(a[i] + s) + 1;
s = -(abs(a[i]) + 1);
}
} else {
if (a[i] + s <= 0) {
ans2 += abs(a[i] + s) + 1;
s = abs(a[i]) + 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;
int main() {
int N;
cin >> N;
long long A[N];
for (int i = 0; i < N; i++) cin >> A[i];
bool loop = true;
long long delta = 0;
while (loop) {
int sum[N];
bool sign = (A[0] > 0);
sum[0] = A[0];
loop = false;
for (int i = 1; i < N; i++) {
sum[i] = sum[i - 1] + A[i];
sign = !sign;
if (sign && sum[i] <= 0) {
int dd = 1 - sum[i];
delta += abs(dd);
A[i] = A[i] + dd;
sum[i] = 1;
loop = true;
} else if (!sign && sum[i] >= 0) {
int dd = -1 - sum[i];
delta += abs(dd);
A[i] = A[i] + dd;
sum[i] = -1;
loop = true;
}
}
}
cout << delta << 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;
void solve() {
long long n;
cin >> n;
vector<long long> a(n);
cin >> a[0];
long long pos = 0, ans = 0;
if (a[0] == 0) a[0]++, ans++;
if (a[0] > 0) pos = 1;
for (long long i = 1; i < n; i++) {
cin >> a[i];
a[i] += a[i - 1];
pos = 1 - pos;
if (pos == 1 && a[i] <= 0) ans += 1 - a[i], a[i] = 1;
if (pos == 0 && a[i] >= 0) ans += a[i] + 1, a[i] = -1;
}
cout << ans << '\n';
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
long long T = 1;
while (T--) solve();
}
|
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 | parseInt(x) = parse(Int, x)
function main()
n = readline() |> parseInt
a = map(parseInt, split(readline()))
b = Array{Int}(n)
b[1] = a[1]
k = 0
for i in 2:n
b[i] = a[i]+b[i-1]
if b[i]*b[i-1] >= 0
if b[i-1] < 0
k += abs(b[i]-1)
b[i] = 1
else
k += abs(b[i]+1)
b[i] = -1
end
end
end
c = Array{Int}(n)
l = 0
if a[1] > 0
c[1] = -1
l += abs(a[1]+1)
else
c[1] = 1
l += abs(a[1]-1)
end
for i in 2:n
c[i] = a[i]+c[i-1]
if c[i]*c[i-1] >= 0
if c[i-1] < 0
l += abs(c[i]-1)
c[i] = 1
else
l += abs(c[i]+1)
c[i] = -1
end
end
end
d = Array{Int}(n)
m = 0
if a[1] > 0
d[1] = 1
m += abs(a[1]-1)
else
d[1] = -1
m += abs(a[1]+1)
end
for i in 2:n
d[i] = a[i]+d[i-1]
if d[i]*d[i-1] >= 0
if d[i-1] < 0
m += abs(d[i]-1)
d[i] = 1
else
m += abs(d[i]+1)
d[i] = -1
end
end
end
print(min(k,l,m))
end
main() |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.