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 | cpp | #include <bits/stdc++.h>
using namespace std;
int n;
long long a[100010];
long long delta, ans1, ans2;
long long sum1, sum2;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
}
delta = 0LL;
ans1 = 0LL;
sum1 = a[1];
if (sum1 == 0) {
delta = 1LL;
ans1 = 1LL;
sum1 = 1;
}
for (int i = 2; i <= n; i++) {
sum2 = sum1 + a[i];
if (sum1 < 0 && sum2 <= 0) {
delta += 1 - sum2;
ans1 += 1 - sum2;
sum2 = 1;
} else if (sum1 > 0 && sum2 >= 0) {
delta += -1 - sum2;
ans1 += sum2 + 1;
sum2 = -1;
}
sum1 = sum2;
}
delta = 0LL;
ans2 = 0LL;
if (a[1] < 0) {
delta = 1 - a[1];
ans2 = 1 - a[1];
sum1 = 1;
} else {
delta = -1 - a[1];
ans2 = a[1] + 1;
sum1 = -1;
}
for (int i = 2; i <= n; i++) {
sum2 = sum1 + a[i];
if (sum1 < 0 && sum2 <= 0) {
delta += 1 - sum2;
ans2 += 1 - sum2;
} else if (sum1 > 0 && sum2 >= 0) {
delta += -1 - sum2;
ans2 += sum2 + 1;
}
sum1 = sum2;
}
printf("%lld\n", min(ans1, ans2));
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | # -*- coding: utf-8 -*-
"""
Created on Sat Sep 8 15:51:53 2018
@author: maezawa
"""
n = int(input())
a = list(map(int, input().split()))
sa = 0
cnt = 0
for i in range(0,n-1):
sa += a[i]
na = -sa//abs(sa)*(abs(sa)+1)
if abs(a[i+1]) > abs(na) and a[i+1]*na > 0:
continue
else:
cnt += abs(na-a[i+1])
a[i+1] = na
print(cnt)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | #
# abc059 c
#
import sys
from io import StringIO
import unittest
class TestClass(unittest.TestCase):
def assertIO(self, input, output):
stdout, stdin = sys.stdout, sys.stdin
sys.stdout, sys.stdin = StringIO(), StringIO(input)
resolve()
sys.stdout.seek(0)
out = sys.stdout.read()[:-1]
sys.stdout, sys.stdin = stdout, stdin
self.assertEqual(out, output)
def test_入力例_1(self):
input = """4
1 -3 1 0"""
output = """4"""
self.assertIO(input, output)
def test_入力例_2(self):
input = """5
3 -6 4 -5 7"""
output = """0"""
self.assertIO(input, output)
def test_入力例_3(self):
input = """6
-1 4 3 2 -5 4"""
output = """8"""
self.assertIO(input, output)
def resolve():
N = int(input())
A = list(map(int, input().split()))
ans = 0
s = 0
f = 0
for a in A:
if f == 1 and s+a >= 0:
ans += abs(s+a) + 1
s = -1
elif f == -1 and s+a <= 0:
ans += abs(s+a) + 1
s = 1
else:
s += a
f = s // abs(s)
print(ans)
if __name__ == "__main__":
# unittest.main()
resolve()
|
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 solve(asum, a, ans):
for i in range(1, n):
# print(asum)
asumplus = asum + a[i]
if asumplus == 0 and asum > 0:
asumplus -= 1
ans += 1
elif asumplus == 0 and asum < 0:
asumplus -= 1
ans += 1
elif asumplus > 0 and asum > 0:
ans += asumplus + 1
asumplus = -1
elif asumplus < 0 and asum < 0:
ans += -asumplus + 1
asumplus = 1
asum = asumplus
return ans
n = int(input())
a = list(map(int,input().split()))
asum = a[0]
if asum != 0:
ans = solve(asum,a,0)
else:
ans = min(solve(1,a,1),solve(-1,a,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;
int c[123456];
int solve(int n, int id) {
int a;
int sum = 0;
int res = 0;
for (int i = 0; i < n; ++i) {
if (id % 2 == 0) {
if (sum + c[i] <= 0) {
res += abs(sum + c[i] - 1);
sum = 1;
} else
sum += c[i];
} else {
if (sum + c[i] >= 0) {
res += abs(sum + c[i] - (-1));
sum = -1;
} else
sum += c[i];
}
id++;
}
return res;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> c[i];
}
int res = solve(n, 1);
res = min(res, solve(n, 2));
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 | UNKNOWN | #include <bits/stdc++.h>
long long int n;
long long int count(long long int a0) {
long long int a, i, S[2] = {}, C[2] = {};
S[0] = a0;
S[1] = a0;
for (i = 1; i < n; i++) {
scanf("%lld", &a);
S[0] += a;
S[1] += a;
if (i % 2 == 0) {
if (S[0] >= 0) {
C[0] += S[0] + 1;
S[0] = -1;
}
if (S[1] <= 0) {
C[1] += -1 * S[1] + 1;
S[1] = 1;
}
} else {
if (S[0] <= 0) {
C[0] += -1 * S[0] + 1;
S[0] = 1;
}
if (S[1] >= 0) {
C[1] += S[1] + 1;
S[1] = -1;
}
}
}
if (C[0] < C[1])
return C[0];
else
return C[1];
}
int main() {
long long int a0;
scanf("%lld %lld", &n, &a0);
printf("%lld\n", count(a0));
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 = INT_MAX;
const long long INFL = LLONG_MAX;
const long double pi = acos(-1);
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
long long xx(vector<long long> &v) {
long long s = 0;
long long ans = 0;
int n = int((v).size());
for (int i = 0; i < n; i++) {
if (!i)
s = v[0];
else {
if (s > 0) {
if (s + v[i] < 0) {
s += v[i];
continue;
}
long long x, y;
x = max(s + v[i] + 1, (long long)0);
ans += x;
if (x != 0) s = -1;
} else {
if (s + v[i] > 0) {
s += v[i];
continue;
}
long long x, y;
x = max(1 - (s + v[i]), (long long)0);
ans += x;
if (x != 0) s = 1;
}
}
}
return ans;
}
int main() {
ios_base::sync_with_stdio(0);
cout.precision(15);
cout << fixed;
cout.tie(0);
cin.tie(0);
int n;
cin >> n;
vector<long long> v(n);
for (int(i) = 0; (i) < (n); (i)++) cin >> v[i];
if (n == 1) {
if (v[0] != 0)
cout << 0 << '\n';
else
cout << 1 << '\n';
return 0;
}
long long ans = INFL;
if (v[0] == 0) {
v[0] += 1;
ans = min(ans, xx(v) + 1);
v[0] = -1;
ans = min(ans, xx(v) + 1);
} else if (v[0] > 0) {
ans = min(ans, xx(v));
v[0] = -1;
ans = min(ans, xx(v) + v[0] + 1);
} else {
ans = min(ans, xx(v));
v[0] = 1;
ans = min(ans, xx(v) + 1 - v[0]);
}
cout << ans << '\n';
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<long long int> A(N);
vector<long long int> V(N);
long long int t = 0LL;
for (int i = 0; i < N; i++) {
cin >> A[i];
t += A[i];
V[i] = t;
}
long long int l = 0LL, r = 0LL;
for (int i = 0; i < N - 1; i++) {
if (i % 2 == 0) {
if (V[i] > 0) {
continue;
} else {
l += 1 - V[i];
V[i] = 1;
V[i + 1] = V[i] + A[i + 1];
}
} else {
if (V[i] < 0) {
continue;
} else {
l += V[i] + 1;
V[i] = -1;
V[i + 1] = V[i] + A[i + 1];
}
}
}
for (int i = 0; i < N - 1; i++) {
if (i % 2 == 0) {
if (V[i] < 0) {
continue;
} else {
r += V[i] + 1;
V[i] = -1;
V[i + 1] = V[i] + A[i + 1];
}
} else {
if (V[i] > 0) {
continue;
} else {
l += 1 - V[i];
V[i] = 1;
V[i + 1] = V[i] + A[i + 1];
}
}
}
cout << min(l, r);
}
|
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 zero = 0;
int N;
cin >> N;
vector<int> sum(N,0);
int now;
cin >> now;
sum[0] = now;
for (int i=1; i<N; i++) {
cin >> now;
sum[i] = sum[i-1] + now;
}
long change = 0;
long ansp = 0;
int i = 0;
while (i<N) {
ansp += max(1-(sum[i]+change),zero);
change += max(1-(sum[i]+change),zero);
i++;
if (i==N) {
break;
}
ansp += max((sum[i]+change)+1,zero);
change -= max((sum[i]+change)+1,zero);
i++;
}
change = 0;
long ansm = 0;
i = 0;
while (i<N) {
ansm += max((sum[i]+change)+1,zero);
change -= max((sum[i]+change)+1,zero);
i++;
if (i==N) {
break;
}
ansm += max(1-(sum[i]+change),zero);
change += max(1-(sum[i]+change),zero)
i++;
}
cout << min(ansp,ansm) << 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) {
new Main().execute();
}
public void execute() {
Scanner sc = new Scanner(System.in);
final int n = sc.nextInt();
int ops = 0;
int[] sums = new int[n];
for (int i = 0; i < n; i++) {
int ai = sc.nextInt();
sums[i] = (i == 0) ? ai : sums[i - 1] + ai;
if (sums[i] == 0) {
ops++;
sums[i] = (sums[i - 1] > 0) ? -1 : 1;
} else if (i > 0 && sums[i - 1] * sums[i] > 0) {
if (sums[i] > 0) {
ops += sums[i] + 1;
sums[i] = -1;
} else if (sums[i] < 0) {
ops += 1 - sums[i];
sums[i] = 1;
}
}
}
System.out.println(ops);
sc.close();
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
ll n;
cin >> n;
vector<ll> a(n);
for (ll i = 0; i < n; i++) cin >> a[i];
ll sumodd = 0;
ll sumeven = 0;
ll ans = 0;
for (ll i = 0; i < n; i++) {
if (i % 2 == 0) {
sumodd += abs(a[i]);
if (sumodd <= sumeven) {
ans += sumeven - sumodd + 1;
sumodd += sumeven - sumodd + 1;
}
}
if (i % 2 != 0) {
sumeven += abs(a[i]);
if (sumeven <= sumodd) {
ans += sumodd - sumeven + 1;
sumeven += sumodd - sumeven + 1;
}
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | from numpy import sign
n = int(input())
A = [i for i in map(int,input().split())]
x = 0
ans = 0
if A[0] == 0:
A[0] = -1 * sign(A[1])
ans += 1
flag = 0
for i in A:
x += i
if flag == 0:
flag = 1
continue
if x == 0:
x += 1 * sign(i)
ans += 1
if sign(x) == sign(x-i):
diff = abs(x - (x-i))
x += -1*sign(x)*(diff + 1)
ans += diff + 1
if x == 0:
x += 1 * sign(i)
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;
int main()
{
int n,ans;
cin>>n;
vector<int>a(n);
for(int i=0;i<n;i++){
cin>>a.at(i);
}
for(int i=0;i<n-1;i++){
int p=0;
for(int j=i+1;j>=0;j--)p+=a.at(j);
while(a.at(i)*a.at(i+1)<0||p=0){
if(a.at(i)>0){
a.at(i+1)--;
ans++;
continue;
}
if(a.at(i)<0){
a.at(i+1)++;
ans++;
continue;
}
}
}
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 | import copy
N=int(input())
l=list(map(int, input().split())) #リスト入力
cp = copy.copy(l)
#c=0
for k in range(N-1):
if sum(l[:k+1])==0:
#c=c+1
if l[k+1]>0:
l[k+1]=l[k+1]+1
else:
l[k+1]=l[k+1]-1
if sum(l[:k])*sum(l[:k+1])>0:
if sum(l[:k+1])>0:
l[k]=l[k]-(sum(l[:k+1])-(-1))
#c=c+abs(sum(l[:k+1])-(-1))
else:
l[k]=l[k]+(1-sum(l[:k+1]))
#c=c+abs(1-sum(l[:k+1]))
if sum(l)==0:
c=c+1
l[-1]=l[-1]+1
print(sum([abs(l[n]-cp[n]) for n in range(N)])) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[100000];
for (int i = 0; i < (int)(n); i++) cin >> a[i];
bool zeroFlag = false;
long long costA = 0, costB = 0;
int sum = 0, prev = 0;
if (a[0] == 0) {
a[0] = 1;
costA += 1;
zeroFlag = true;
}
for (int i = 0; i < (int)(n); i++) {
prev = sum;
sum += a[i];
if (i == 0) continue;
if ((prev * sum) < 0) continue;
if (prev < 0) {
costA += abs(sum - 1);
sum = 1;
} else {
costA += abs(sum + 1);
sum = -1;
}
}
if (zeroFlag) {
a[0] = -1;
costB += 1;
} else {
int a0 = a[0];
a[0] = -1 * a[0] / abs(a[0]);
costB += (abs(a0) + 1);
}
sum = 0;
prev = 0;
for (int i = 0; i < (int)(n); i++) {
prev = sum;
sum += a[i];
if (i == 0) continue;
if ((prev * sum) < 0) continue;
if (prev < 0) {
costB += abs(sum - 1);
sum = 1;
} else {
costB += abs(sum + 1);
sum = -1;
}
}
cout << min(costA, costB) << 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 = input().split()
a = [int(i) for i in a]
out = 0
s = a[0]
for i in range(1, n):
is_plus = s > 0
s += a[i]
if s == 0:
out += 1
s = -1 if is_plus else 1
continue
if (s > 0) != is_plus:
continue
if s > 0:
out += s + 1
s = -1
else:
out += 1 - s
s = 1
print(out) |
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 = 1e9 + 7;
int main() {
int n;
cin >> n;
long long a[120000];
for (int i = 0; i < (n); i++) cin >> a[i];
long long total = 0;
long long total2 = 0;
long long count1 = 0;
long long count2 = 0;
for (int i = 0; i < (n - 2) / 2; i++) {
total += a[2 * i];
total2 += a[2 * i];
if (total >= 0) {
count1 += (abs(total) + 1);
total = -1;
}
if (total2 <= 0) {
count2 += (abs(total2) + 1);
total2 = 1;
}
total += a[2 * i + 1];
total2 += a[2 * i + 1];
if (total <= 0) {
count1 += (abs(total) + 1);
total = 1;
}
if (total2 >= 0) {
count2 += (abs(total2) + 1);
total2 = -1;
}
}
cout << min(count1, count2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<long long> A(N);
for (int i = 0; i < N; i++) {
cin >> A[i];
}
vector<long long> S1(N);
vector<long long> S2(N);
S1[0] = A[0];
S2[0] = A[0];
int cnt1 = 0;
int cnt2 = 0;
for (int i = 0; i < N; i++) {
if (i) {
S1[i] = S1[i - 1] + A[i];
S2[i] = S2[i - 1] + A[i];
}
if (!(i % 2)) {
if (S1[i] < 0) {
cnt1 += 1 - S1[i];
S1[i] = 1;
}
if (S2[i] > 0) {
cnt2 += S2[i] + 1;
S2[i] = -1;
}
} else {
if (S1[i] > 0) {
cnt1 += S1[i] + 1;
S1[i] = -1;
}
if (S2[i] < 0) {
cnt2 += 1 - S2[i];
S2[i] = 1;
}
}
}
cout << min(cnt1, cnt2) << 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.*;
// ABC 6-C
// http://abc006.contest.atcoder.jp/tasks/abc006_3
public class Main {
public static void main (String[] args) throws java.lang.Exception {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = in.nextInt();
}
int answer = 0;
if (nums[0] == 0) {
answer = solve(nums, 0, 0);
} else {
answer = solve(nums, nums[0], 1);
}
System.out.println(answer);
//
// long sum = 0;
// long answer = 0;
//
// for (int i = 0; i < n; i++) {
// int a = in.nextInt();
//
// if (sum < 0 && sum + a < 0) {
// answer += 1 + Math.abs(sum + a);
// sum = 1;
// } else if (sum > 0 && sum + a > 0) {
// answer += 1 + sum + a;
// sum = -1;
// } else if (sum + a == 0) {
// answer++;
// if (sum < 0) {
// sum = 1;
// } else {
// sum = -1;
// }
// } else {
// sum += a;
// }
// }
// System.out.println(answer);
}
public static long solve(int[] nums, long sum, int index) {
if (index == nums.length) {
return 0;
}
if (sum < 0 && sum + nums[index] < 0) {
return 1 + Math.abs(sum + nums[index]) + solve(nums, 1, index + 1);
} else if (sum > 0 && sum + nums[index] > 0) {
return 1 + sum + nums[index] + solve(nums, -1, index + 1);
} else if (sum + nums[index] == 0) {
return 1 + Math.min(solve(nums, 1, index + 1), solve(nums, -1, index + 1));
} else {
return solve(nums, sum + nums[index], index + 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 int mod = 1e9 + 7;
const double EPS = 1e-9;
const int INF = 1 << 29;
int a[100054];
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
int ans = 0;
int sum = a[1];
for (int i = 2; i <= n; ++i) {
if (sum < 0) {
int num = 1 - sum;
if (a[i] < num) ans += num - a[i], a[i] = num;
} else {
int num = -1 - sum;
if (a[i] > num) ans += a[i] - num, a[i] = num;
}
sum += a[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 | N = int(input())
a =list(map(int,input().split()))
a1=[0]*N
a2=[0]*N
s1 =a[0]
s2 =a[0]
#正の数から始まるパターン
c1 = 0
if s1 ==0:
s1 = 1
c1 = 1
elif s1 <0:
a1[0]=1
c1 = -s1+1
for i in range(1,N):
if s1*(s1+a[i])>=0 and s1>0:
a1[i] = -s1-1
c1 += abs(a[i]-a1[i])
elif s1*(s1+a[i])>=0 and s1<0:
a1[i] = -s1+1
c1 += abs(a1[i]-a[i])
else:
a1[i]=a[i]
s1 = s1+a1[i]
#負の数から始まるパターン
c2=0
if s2 ==0:
s2 = 1
c2 =1
elif s2 >0:
a2[0]=-1
c2 = s2+1
for i in range(1,N):
if s2*(s2+a[i])>=0 and s2>0:
a2[i] = -(s2+1)
c2 +=abs(a[i]-a2[i])
elif s2*(s2+a[i])>=0 and s2<0:
a2[i] = -s2+1
c2 +=abs(a2[i]-a[i])
else:
a2[i]=a[i]
s2 = s2+a2[i]
print(min(c1,c2)) |
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>
static int solve(const std::vector<int>& va, int initSum, int initCnt = 0) {
int sum = initSum;
int cnt = initCnt;
for (std::remove_reference<decltype(va)>::type::size_type i = 1;
i < va.size(); i++) {
auto nextSum = sum + va[i];
if (nextSum >= 0 && sum > 0) {
cnt += nextSum + 1;
sum = -1;
} else if (nextSum <= 0 && sum < 0) {
cnt += -nextSum + 1;
sum = 1;
} else {
sum = nextSum;
}
}
return cnt;
}
int main() {
std::cin.tie(nullptr);
std::ios::sync_with_stdio(false);
int n;
std::cin >> n;
std::vector<int> va(n);
for (auto&& e : va) {
std::cin >> e;
}
std::cout << std::min(solve(va, va[0]),
solve(va, va[0] > 0 ? -1 : 1, std::abs(va[0]) + 1))
<< std::endl;
return EXIT_SUCCESS;
}
|
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 getsign(long long int n) {
if (n > 0) {
return 1;
}
if (n < 0) {
return -1;
}
return -1;
}
long long int count(int sign0, long long a[], int n) {
long long int sum = 0;
long long int sign = sign0;
long long int count = 0;
for (int i = 0; i < n; ++i) {
sum += a[i];
if (getsign(sum) != sign) {
count += abs(sign - sum);
sum = sign;
}
sign = (sign * -1);
}
return count;
}
int main() {
int n;
cin >> n;
long long a[n];
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
cout << min(count(1, a, n), count(-1, a, n)) << 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 | p :: Int -> Int -> [Int] -> Int
p n x (y:[])
| x + y >= 0 = n+x+y+1
| otherwise = n
p n x (y:ys)
| x + y >= 0 = m (n+x+y+1) (-1) ys
| otherwise = m n (x+y) ys
m :: Int -> Int -> [Int] -> Int
m n x (y:[])
| x + y <= 0 = n-(x+y)+1
| otherwise = n
m n x (y:ys)
| x + y <= 0 = p (n-(x+y)+1) 1 ys
| otherwise = p n (x+y) ys
solve :: Int -> [Int] -> Int
solve n (x:xs) = min (m n x xs) (p n x xs)
main = getContents >>= print . solve 0 . map read . words . last . lines |
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[100000];
long getTotal(int n, int dir) {
long total{0}, sum{0};
for (int i{0}; i < n; ++i) {
sum += a[i];
if (dir > 0 && sum <= 0) {
total += -sum + 1;
sum = 1;
} else if (dir < 0 && sum >= 0) {
total += sum + 1;
sum = -1;
}
dir *= -1;
}
return total;
}
int main() {
int n;
scanf("%d", &n);
for (int i{0}; i < n; ++i) scanf("%d", &a[i]);
long try1 = getTotal(n, 1);
long try2 = getTotal(n, -1);
printf("%d\n", ((try1) < (try2) ? (try1) : (try2)));
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 check1(a):
sum = 0
ans = 0
for i in range(len(a)):
if(i % 2 == 0):
sum += a[i]
if(sum >= 0):
a[i] += -abs(sum)-1
sum += -abs(sum)-1
ans += abs(sum)+1
else:
sum += a[i]
if(sum <= 0):
a[i] += abs(sum)+1
sum += abs(sum)+1
ans += abs(sum)+1
return ans
def check2(a):
sum = 0
ans = 0
for i in range(len(a)):
if(i % 2 == 0):
sum += a[i]
if(sum <= 0):
a[i] += abs(sum)+1
sum += abs(sum)+1
ans += abs(sum)+1
else:
sum += a[i]
if(sum >= 0):
a[i] += -abs(sum)-1
sum += -abs(sum)-1
ans += abs(sum)+1
return ans
n = input()
b = input().split()
a = [int(b[i]) for i in range(len(b))]
a2 = list(a)
ans1 = check1(a)
ans2 = check2(a2)
print(ans1) if(ans1<ans2) else print(ans2) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n;
long long a[100004];
int main() {
scanf("%d", &n);
for (int i = (1); i <= (int)(n); ++i) scanf("%lld", &a[i]);
long long ans = 0;
if (!a[1]) ++ans;
for (int i = (2); i <= (int)(n); ++i) {
if (a[i - 1] > 0) {
if (a[i] + a[i - 1] < 0) {
a[i] += a[i - 1];
continue;
}
ans += abs(a[i] + 1 + a[i - 1]);
a[i] = -a[i] - 1;
} else {
if (a[i] + a[i - 1] > 0) {
a[i] += a[i - 1];
continue;
}
ans += abs(a[i] - 1 + a[i - 1]);
a[i] = 1 - a[i];
}
}
printf("%lld\n", ans);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int,input().split()))
s = a[0]
ans = 0
for e in a[1:]:
if s == 0:
s = 1
ans += 1
if (s+e)*s >= 0:
if s+e < 0:
ans += 1-(s+e)
s = 1
else:
ans += 1+(s+e)
s = -1
else:
s += e
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 | N=gets.to_i
list=gets.split(" ").map(&:to_i)
sum = 0
res = 0
if(list[0] == 0) then
temp = 0
N-1.times{ |i|
temp += list[i+1]
if(temp > 0)
res += 1
list[0] = 1
break
elsif(temp < 0)
res +=1
list[0] = -1
break
end
}
res+=1
list[0]=1
end
N.times{|i|
before_sum = sum
sum += list[i]
if (sum*before_sum> 0) then
if(sum > 0) then
res += (sum+1)
sum = -1
else
res += (-sum+1)
sum = 1
end
elsif sum*before_sum==0 then
if(before_sum < 0 )then
res += 1
sum = 1
elsif(before_sum > 0) then
sum = -1
res += 1
end
end
}
puts(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 | python3 | n = int(input())
a = list(map(int, input().split()))
l = len(a)
ans = 0
summary = a[0]
for i in range(1, l):
if(summary* (summary+ a[i])>= 0):
if(summary > 0):
ans+= a[i]+ summary+ 1
a[i] = -summary- 1
summary= -1
else:
ans+= -summary+ 1- a[i]
a[i] = -summary+ 1
summary= 1
else:
summary+= a[i]
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #![allow(non_snake_case)]
#![allow(dead_code)]
#![allow(unused_macros)]
#![allow(unused_imports)]
use std::str::FromStr;
use std::io::*;
use std::collections::*;
use std::cmp::*;
struct Scanner<I: Iterator<Item = char>> {
iter: std::iter::Peekable<I>,
}
macro_rules! exit {
() => {{
exit!(0)
}};
($code:expr) => {{
if cfg!(local) {
writeln!(std::io::stderr(), "===== Terminated =====")
.expect("failed printing to stderr");
}
std::process::exit($code);
}}
}
impl<I: Iterator<Item = char>> Scanner<I> {
pub fn new(iter: I) -> Scanner<I> {
Scanner {
iter: iter.peekable(),
}
}
pub fn safe_get_token(&mut self) -> Option<String> {
let token = self.iter
.by_ref()
.skip_while(|c| c.is_whitespace())
.take_while(|c| !c.is_whitespace())
.collect::<String>();
if token.is_empty() {
None
} else {
Some(token)
}
}
pub fn token(&mut self) -> String {
self.safe_get_token().unwrap_or_else(|| exit!())
}
pub fn get<T: FromStr>(&mut self) -> T {
self.token().parse::<T>().unwrap_or_else(|_| exit!())
}
pub fn vec<T: FromStr>(&mut self, len: usize) -> Vec<T> {
(0..len).map(|_| self.get()).collect()
}
pub fn mat<T: FromStr>(&mut self, row: usize, col: usize) -> Vec<Vec<T>> {
(0..row).map(|_| self.vec(col)).collect()
}
pub fn char(&mut self) -> char {
self.iter.next().unwrap_or_else(|| exit!())
}
pub fn chars(&mut self) -> Vec<char> {
self.get::<String>().chars().collect()
}
pub fn mat_chars(&mut self, row: usize) -> Vec<Vec<char>> {
(0..row).map(|_| self.chars()).collect()
}
pub fn line(&mut self) -> String {
if self.peek().is_some() {
self.iter
.by_ref()
.take_while(|&c| !(c == '\n' || c == '\r'))
.collect::<String>()
} else {
exit!();
}
}
pub fn peek(&mut self) -> Option<&char> {
self.iter.peek()
}
}
fn main() {
let cin = stdin();
let cin = cin.lock();
let mut sc = Scanner::new(cin.bytes().map(|c| c.unwrap() as char));
let n: usize = sc.get();
let a: Vec<i64> = sc.vec(n);
let mut p = 0;
let mut ans1 = 0;
for i in 0..n {
let mut s = p + a[i];
if i == 0 && s > 0 {
ans1 += s.abs()+1;
s += -s - 1;
} else if s == 0 {
s += if p > 0 { 1 } else { -1 };
ans1 += 1;
} else if s * p > 0 {
ans1 += s.abs()+1;
s += if s > 0 { -s - 1 } else { s + 1 };
}
p = s;
}
let mut p = 0;
let mut ans2 = 0;
for i in 0..n {
let mut s = p + a[i];
if i == 0 && s < 0 {
ans1 += s.abs()+1;
s += s + 1;
} else if s == 0 {
s += if p > 0 { 1 } else { -1 };
ans2 += 1;
} else if s * p > 0 {
ans2 += s.abs()+1;
s += if s > 0 { -s - 1 } else { s + 1 };
}
p = s;
}
println!("{}", 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 | python3 | # coding: utf-8
# hello worldと表示する
import sys
import numpy
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
from copy import deepcopy
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7
n=I()
seq=LI()
if seq[0]>0:
pm=-1
sm=seq[0]
count=0
for i in range(n-1):
sm=sm+seq[i+1]
if pm>0:
if sm<=0:
count+=1-sm
sm=1
pm=-1
else:
if sm>=0:
count+=1+sm
sm=-1
pm=1
print(count)
elif seq[0]<0:
pm=1
sm=seq[0]
count=0
for i in range(n-1):
sm=sm+seq[i+1]
if pm>0:
if sm<=0:
count+=1-sm
sm=1
pm=-1
else:
if sm>=0:
count+=1+sm
sm=-1
pm=1
print(count)
else:
pm=-1
sm=1
count=1
for i in range(n-1):
sm=sm+seq[i+1]
if pm>0:
if sm<=0:
count+=1-sm
sm=1
pm=-1
else:
if sm>=0:
count+=1+sm
sm=-1
pm=1
count1=count
pm=1
sm=-1
count=1
for i in range(n-1):
sm=sm+seq[i+1]
if pm>0:
if sm<=0:
count+=1-sm
sm=1
pm=-1
else:
if sm>=0:
count+=1+sm
sm=-1
pm=1
count2=count
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;
int co[100000];
int sum_u, sum_d;
long long ch_u, ch_d;
int n;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> co[i];
}
if (co[0] > 0) {
sum_u = -1;
ch_u = co[0] + 1;
sum_d = co[0];
ch_d = 0;
} else if (co[0] < 0) {
sum_u = co[0];
ch_u = 0;
sum_d = 1;
ch_d = 1 - co[0];
} else {
sum_u = -1;
ch_u = 1;
sum_d = 1;
ch_d = 1;
}
for (int i = 1; i < n; i++) {
sum_d += co[i];
sum_u += co[i];
if (i % 2 == 1 && sum_u >= 0) {
ch_u += sum_u + 1;
sum_u = -1;
} else if (i % 2 == 0 && sum_u <= 0) {
ch_u += 1 - sum_u;
sum_u = 1;
}
if (i % 2 == 1 && sum_d <= 0) {
ch_d += 1 - sum_d;
sum_d = 1;
} else if (i % 2 == 0 && sum_d >= 0) {
ch_d += sum_d + 1;
sum_d = -1;
}
}
if (ch_d >= ch_u)
cout << ch_u << "\n";
else {
cout << ch_d << "\n";
}
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
struct Setup {
Setup() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
}
} SETUP;
template <class T>
bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T>
bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
const long long INF = 1LL << 60;
signed main() {
long long n;
cin >> n;
vector<long long> v(n);
for (auto& a : v) {
cin >> a;
}
vector<long long> acc(n + 1);
acc[0] = 0;
long long ans = 0;
for (long long i = 0; i < n; i++) {
acc[i + 1] = acc[i] + v[i];
if (acc[i] < 0 && acc[i + 1] < 0) {
ans += abs(1 - acc[i + 1]);
acc[i + 1] = 1;
} else if (acc[i] > 0 && acc[i + 1] > 0) {
ans += abs(-1 - acc[i + 1]);
acc[i + 1] = -1;
} else if (acc[i + 1] == 0) {
if (acc[i] <= 0) {
ans++;
acc[i + 1]++;
} else if (acc[i] > 0) {
ans++;
acc[i + 1]--;
}
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N, ans = 0;
cin >> N;
int sum = 0;
for (int i = 0; i < N; i++) {
int a;
cin >> a;
int nowsum = a + sum;
if (i == 0)
sum += a;
else {
if ((nowsum < 0) != (sum < 0)) {
if (nowsum == 0) {
if (nowsum < 0)
nowsum--;
else
nowsum++;
ans++;
}
} else {
if (nowsum < 0) {
for (;;) {
if (nowsum > 0) break;
nowsum++;
ans++;
}
} else {
for (;;) {
if (nowsum < 0) break;
nowsum--;
ans++;
}
}
}
sum = nowsum;
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | import qualified Data.ByteString.Char8 as C
import Data.List
main = C.interact $ put . sol . get
get = unfoldr (C.readInt . C.dropWhile (==' ')) . last . C.lines
put = C.pack . show
sol = min <$> g (-1) <*> g 1
g a = fst . foldl' f (-1,a)
f (c,s) b
| s*s'<0 = (c,s')
| s>0 = (c+1+abs s',-1)
| s<0 = (c+1+abs s',1)
where
s' = s+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 | python3 | N = int(input())
A = list(map(int,input().split()))
ans = 0
s = A[0]
if s > 0:
flag = 1
elif s < 0:
flag = -1
elif A[1] < 0:
s = 1
flag = 1
ans += 1
else:
s = -1
flag = -1
ans += 1
for i in range(1,N):
s += A[i]
if flag == 1 and s >= 0:
ans += s + 1
s = -1
elif flag == -1 and s <= 0:
ans += 1 - s
s = 1
flag *= -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;
#define rep(i,n) for( int i = 0; i < n; i++ )
#define dump(x) cerr << #x << " = " << (x) << endl;
#define INF 1000000000000000000
int main(void)
{
int n;
cin >> n;
int a[n];
rep(i, n) cin >> a[i];
int sum[n] = {a[0]};
for( int i = 1; i < n; i++){
sum[i] = sum[i-1] + a[i];
}
int cost1 = 0;
int sign = 1;
int delta;
for( int i = 0; i < n; i++){
if( sum[i]*sign >= 0){
delta = sum[i]+sign;
cost1 += abs(delta);
for( int j = i; j < n; j++){
sum[j] -= delta;
}
}
sign = -sign;
}
sum[0] = a[0];
for( int i = 1; i < n; i++){
sum[i] = sum[i-1] + a[i];
}
int cost2 = 0;
sign = -1;
for( int i = 0; i < n; i++){
if( sum[i]*sign >= 0){
delta = sum[i]+sign;
cost2 += abs(delta);
for( int j = i; j < n; j++){
sum[j] -= delta;
}
}
sign = -sign;
}
cout << min(cost1,cost2);
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 MOD = 1e9 + 7;
const long long INF = 1e18;
signed main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
long long n, ans1 = 0, ans2 = 0, sum1 = 0, sum2 = 0;
cin >> n;
vector<long long> a(n);
for (long long i = 0; i < n; i++) {
cin >> a[i];
}
sum1 = a[0];
if (sum1 == 0) {
sum1 = (a[1] > 0 ? -1 : 1);
ans1++;
}
for (long long i = 1; i < n; i++) {
if (sum1 + a[i] == 0) {
ans1++;
sum1 = (a[i] > 0 ? 1 : -1);
} else if (sum1 > 0 && sum1 + a[i] < 0) {
sum1 += a[i];
} else if (sum1 < 0 && sum1 + a[i] > 0) {
sum1 += a[i];
} else if (sum1 > 0 && a[i] + sum1 > 0) {
ans1 += sum1 + a[i] + 1;
sum1 = -1;
} else {
ans1 += -a[i] - sum1 + 1;
sum1 = 1;
}
}
a[0] *= -1;
if (sum2 == 0) {
sum2 = (a[1] > 0 ? -1 : 1);
ans2++;
}
for (long long i = 1; i < n; i++) {
if (sum2 + a[i] == 0) {
ans2++;
sum2 = (a[i] > 0 ? 1 : -1);
} else if (sum2 > 0 && sum2 + a[i] < 0) {
sum2 += a[i];
} else if (sum2 < 0 && sum2 + a[i] > 0) {
sum2 += a[i];
} else if (sum2 > 0 && a[i] + sum2 > 0) {
ans2 += sum2 + a[i] + 1;
sum2 = -1;
} else {
ans2 += -a[i] - sum2 + 1;
sum2 = 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 | python3 | import numpy as np
n = int(input())
a = list(map(int,input().split()))
#print (a)
sum = 0
count = 0
p = 0
for i in range(n):
sum += a[i]
if i>0:
p = a[i]
if (sum - a[i]) > 0:
if sum >= 0:
a[i] = -(sum-a[i])-1
count += np.abs(p-a[i])
sum = -1
else:
if sum <= 0:
a[i] = -(sum-a[i])+1
count += np.abs(p-a[i])
sum = 1
#print (sum)
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;
int main() {
long long n;
cin >> n;
vector<long long> A(n), sum(n);
for (long long i = (0); i < (long long)(n); i++) cin >> A[i];
long long cnt1 = 0;
if (A[0] > 0) {
sum[0] = A[0];
} else {
sum[0] = 1;
cnt1 += abs(1 - A[0]);
}
for (long long i = (1); i < (long long)(n); i++) {
if (sum[i - 1] > 0) {
if (sum[i - 1] + A[i] < 0) {
sum[i] = sum[i - 1] + A[i];
} else {
sum[i] = -1;
cnt1 += abs(sum[i - 1] + A[i] - (-1));
}
} else {
if (sum[i - 1] + A[i] > 0) {
sum[i] = sum[i - 1] + A[i];
} else {
sum[i] = 1;
cnt1 += abs(1 - (sum[i - 1] + A[i]));
}
}
}
long long cnt2 = 0;
if (A[0] < 0) {
sum[0] = A[0];
} else {
sum[0] = 1;
cnt2 += abs(A[0] - (-1));
}
for (long long i = (1); i < (long long)(n); i++) {
if (sum[i - 1] > 0) {
if (sum[i - 1] + A[i] < 0) {
sum[i] = sum[i - 1] + A[i];
} else {
sum[i] = -1;
cnt2 += abs(sum[i - 1] + A[i] - (-1));
}
} else {
if (sum[i - 1] + A[i] > 0) {
sum[i] = sum[i - 1] + A[i];
} else {
sum[i] = 1;
cnt2 += abs(1 - (sum[i - 1] + A[i]));
}
}
}
long long cnt3 = 0;
if (A[n - 1] > 0) {
sum[n - 1] = A[n - 1];
} else {
sum[n - 1] = 1;
cnt3 += abs(1 - A[n - 1]);
}
for (long long i = n - 2; i >= 0; i--) {
if (sum[i + 1] > 0) {
if (sum[i + 1] + A[i] < 0) {
sum[i] = sum[i + 1] + A[i];
} else {
sum[i] = -1;
cnt3 += abs(sum[i + 1] + A[i] - (-1));
}
} else {
if (sum[i + 1] + A[i] > 0) {
sum[i] = sum[i + 1] + A[i];
} else {
sum[i] = 1;
cnt3 += abs(1 - (sum[i + 1] + A[i]));
}
}
}
long long cnt4 = 0;
if (A[n - 1] < 0) {
sum[n - 1] = A[n - 1];
} else {
sum[n - 1] = -1;
cnt4 += abs(A[n - 1] - (-1));
}
for (long long i = n - 2; i >= 0; i--) {
if (sum[i + 1] > 0) {
if (sum[i + 1] + A[i] < 0) {
sum[i] = sum[i + 1] + A[i];
} else {
sum[i] = -1;
cnt4 += abs(sum[i + 1] + A[i] - (-1));
}
} else {
if (sum[i + 1] + A[i] > 0) {
sum[i] = sum[i + 1] + A[i];
} else {
sum[i] = 1;
cnt4 += abs(1 - (sum[i + 1] + A[i]));
}
}
}
cout << min(cnt1, cnt2) << 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 mpow(int base, int exp);
void ipgraph(int m);
void dfs(int u, int par);
const int mod = 1000000007;
const int N = 3e5, M = N;
vector<int> g[N];
int a[N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int i, n, k, j;
cin >> n;
for (i = 0; i < n; i++) cin >> a[i];
long long ans = 0;
if (a[0] == 0) ans = 1, a[0] = 1;
if (a[1] > 0) a[0] = -1;
long long pre = a[0], to;
for (i = 1; 1 < n ? i < n : i > n; 1 < n ? i += 1 : i -= 1) {
to = pre + a[i];
long long need = abs(pre) + 1;
if (pre > 0) need = -need;
if (to == 0 or (pre > 0 and to > 0) or (pre < 0 and to < 0)) {
pre += need;
ans += abs(need - a[i]);
} else
pre = to;
}
cout << ans << endl;
return 0;
}
int mpow(int base, int exp) {
base %= mod;
int result = 1;
while (exp > 0) {
if (exp & 1) result = ((long long)result * base) % mod;
base = ((long long)base * base) % mod;
exp >>= 1;
}
return result;
}
void ipgraph(int m) {
int i, u, v;
for (i = 0; i < m; i++) {
cin >> u >> v;
u++, v++;
g[u - 1].push_back(v - 1);
g[v - 1].push_back(u - 1);
}
}
void dfs(int u, int par) {
for (int v : g[u]) {
if (v == par) continue;
dfs(v, u);
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
long long n;
cin >> n;
long long a[n];
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
long long cnt = a[0];
long long ans = 0ll;
long long p = -1ll;
if (a[0] < 0ll) p = 1ll;
for (int i = 1; i < n; ++i) {
cnt += a[i];
if (cnt * p <= 0ll) {
long long g = cnt * -1ll + p;
ans += (g * p);
cnt = cnt + g;
}
p *= -1ll;
}
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;
const long long INF = 1e10;
int main() {
long long n;
cin >> n;
vector<long long> a(n);
for (long long i = 0; i < n; i++) cin >> a[i];
vector<long long> b = a;
long long ans1 = 0;
long long ans2 = 0;
if (a[0] <= 0) {
ans1 += 1 - a[0];
a[0] = 1;
}
for (long long i = 1; i < n; i++) {
a[i] += a[i - 1];
if (a[i - 1] < 0 && a[i] <= 0) {
ans1 += 1 - a[i];
a[i] = 1;
}
if (a[i - 1] > 0 && a[i] >= 0) {
ans1 += a[i] + 1;
a[i] = -1;
}
}
if (b[0] >= 0) {
ans2 += a[0] + 1;
b[0] = -1;
}
for (long long i = 1; i < n; i++) {
b[i] += b[i - 1];
if (b[i - 1] < 0 && b[i] <= 0) {
ans2 += 1 - b[i];
b[i] = 1;
}
if (b[i - 1] > 0 && b[i] >= 0) {
ans2 += b[i] + 1;
b[i] = -1;
}
}
long long ans = min(ans1, ans2);
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | from itertools import accumulate
n = int(input())
a = [int(i) for i in input().split()]
ans = 0
da = 0
if a[0] == 0:
ans += 1
if a[1] >= 0:
a[0] = -1
da += -1
else:
a[0] = 1
da += 1
b = list(accumulate(a))
for i, (x, y) in enumerate(zip(b[:-1], b[1:])):
if (x+da)*(y+da) < 0:
continue
else:
y += da
ans += abs(y)+1
if y>=0:
da += -1-y
else:
da += 1-y
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 int INF = 0x7fffffff;
const int maxn = 1e5 + 10;
int a[maxn];
int n;
long long cal() {
long long t = a[0], ans = 0;
for (int i = 1; i < n; ++i) {
if (t < 0) {
t += a[i];
if (t <= 0) {
ans += 1 - t;
t = 1;
}
continue;
}
t += a[i];
if (t >= 0) {
ans += t + 1;
t = -1;
}
}
return ans;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < (n); ++i) {
scanf("%d", &a[i]);
}
long long ans = 0, ans1, ans2;
if (a[0] == 0) {
a[0] = 1;
ans1 = cal();
a[0] = -1;
ans2 = cal();
ans = min(ans1, ans2) + 1;
} else {
ans = cal();
}
printf("%lld\n", ans);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using pii = pair<int, int>;
using tiii = tuple<int, int, int>;
const ll mod = 1e9 + 7;
const int INF = (1 << 30) - 1;
const ll INFLL = (1LL << 62) - 1;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
template <class T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T>
inline int sgn(T n) {
if (n > 0)
return 1;
else if (n < 0)
return -1;
else
return 0;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
int main() {
int n;
cin >> n;
vector<ll> a(n);
for (int i = (0); i < ((n)); ++i) cin >> a[i];
ll ans = INFLL;
ll s = 0;
ll cnt = 0;
for (int i = (0); i < ((n)); ++i) {
s += a[i];
if (i % 2 == 0 && s <= 0) {
cnt += -s + 1;
s += -s + 1;
} else if (i % 2 != 0 && s >= 0) {
cnt += s + 1;
s -= s + 1;
}
cout << "s"
<< " = " << (s) << endl;
;
cout << "cnt"
<< " = " << (cnt) << endl;
;
}
ans = min(ans, cnt);
s = 0;
cnt = 0;
for (int i = (0); i < ((n)); ++i) {
s += a[i];
if (i % 2 == 0 && s >= 0) {
cnt += s + 1;
s -= s + 1;
} else if (i % 2 != 0 && s <= 0) {
cnt += -s + 1;
s += -s + 1;
}
}
ans = min(ans, cnt);
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 | import numpy as np
n = int(input())
a = list(map(int, input().split()))
c1 = 0
sum = a[0]
for i in range(1, n):
if not (np.sign(sum) != np.sign(sum + a[i]) and sum + a[i] != 0):
if sum > 0:
c1 += a[i] + sum + 1
sum = -1
else:
c1 += -a[i] - sum + 1
sum = 1
else:
sum += a[i]
c2 = abs(a[0]) + 1
sum = - a[0]
for i in range(1, n):
if not (np.sign(sum) != np.sign(sum + a[i]) and sum + a[i] != 0):
if sum > 0:
c2 += a[i] + sum + 1
sum = -1
else:
c2 += -a[i] - sum + 1
sum = 1
else:
sum += a[i]
print(min(c1, c2)) |
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
previous_sum = A[0] # total to i - 1
for i in range(1, N):
# if prev_sum is plus and A[i] is more minus than prev_sum, no action.
# if prev_sum is plus and minus of A[i] is larger than or equal to prev_sum.
if previous_sum > 0 and previous_sum + A[i] >= 0:
# the number that need to be subtracted to meet the requirements.
require_subtraction = previous_sum + A[i] + 1
# the number of wastes in meeting the requirement.
distance_from_ideal = previous_sum - 1
# restration to an ideal list.
A[i - 1] -= distance_from_ideal
previous_sum -= distance_from_ideal
ans += distance_from_ideal
# adjustment for the manupilation of the previous values.
require_subtraction -= distance_from_ideal
# correcting the current number.
A[i] -= require_subtraction
ans += require_subtraction
# if prev_sum is minus and A[i] is more plus than prev_sum, no action.
# if prev_sum is minus and minus of A[i] is more smaller than or equal to prev_sum.
elif previous_sum < 0 and previous_sum + A[i] <= 0:
# the number that need to be added to meet the requirements.
require_addition = -(previous_sum + A[i] - 1)
# the number of wastes in meeting the requirement.
distance_from_ideal = -1 - previous_sum
# restration to an ideal list.
A[i - 1] += distance_from_ideal
previous_sum += distance_from_ideal
ans += distance_from_ideal
# adjustment for the manupilation of the previous values.
require_addition -= distance_from_ideal
# correcting the current number.
A[i] += require_addition
ans += require_addition
previous_sum += A[i]
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.io.*;
import java.util.*;
public class Main{
static int n;
static long[] a;
static long count;
static long sum;
public static void main(String[] args) throws IOException{
MyReader r = new MyReader();
n = r.i();
a = r.ll();
sum = a[0];
count = 0;
if(a[0] == 0){
count = 1;
sum = 1;
solve();
long temp = count;
count = 1;
sum = -1;
solve();
count = Math.min(count, temp);
} else
solve();
println(count);
}
static void solve(){
for(int i = 1; i < n; i++){
if(sum < 0){
if(a[i]+sum <= 0){
count += -(a[i]+sum)+1;
sum = 1;
} else
sum = a[i]+sum;
} else{
if(a[i]+sum>=0){
count += a[i]+sum+1;
sum = -1;
} else
sum = a[i]+sum;
}
}
}
static void print(Object o){
System.out.print(o.toString());
}
static void println(Object o){
System.out.println(o.toString());
}
static int Int(String s){
return Integer.parseInt(s);
}
static long Long(String s){
return Long.parseLong(s);
}
static class MyReader extends BufferedReader{
MyReader(){
super(new InputStreamReader(System.in));
}
String s() throws IOException{
return readLine();
}
String[] ss() throws IOException{
return s().split(" ");
}
int i() throws IOException{
return Int(s());
}
int[] ii() throws IOException{
String[] ss = ss();
int size = ss.length;
int[] ii = new int[size];
for(int j = 0; j < size; j++) ii[j] = Integer.parseInt(ss[j]);
return ii;
}
long l() throws IOException{
return Long(s());
}
long[] ll() throws IOException{
String[] ss = ss();
int size = ss.length;
long[] ll = new long[size];
for(int j = 0; j < size; j++) ll[j] = Long.parseLong(ss[j]);
return ll;
}
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9;
const long long MOD = 1e9 + 7;
int main() {
long long n;
cin >> n;
vector<long long> a(n);
for (long long i = 0; i < n; ++i) {
cin >> a[i];
}
vector<long long> sum(n);
sum[0] = a[0];
for (long long i = 1; i < n; i++) {
sum[i] += sum[i - 1] + a[i];
}
long long even_sum = 0;
long long odd_sum = 0;
for (long long i = 0; i < n; ++i) {
if (i % 2 == 0 && sum[i] >= 0) {
even_sum += 1 + sum[i];
}
if (i % 2 == 1 && sum[i] <= 0) {
even_sum += 1 - sum[i];
}
}
for (long long i = 0; i < n; ++i) {
if (i % 2 == 0 && sum[i] <= 0) {
odd_sum += 1 - sum[i];
}
if (i % 2 == 1 && sum[i] >= 0) {
odd_sum += 1 + sum[i];
}
}
cout << min(odd_sum, even_sum) << 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 | import math
N = int(input())
a = [int(i) for i in input().split()]
num = 0
i = 0
if a[0] == 0:
while a[i] == 0:
i += 1
if i % 2 == 0:
a[0] = math.copysign(1, a[i])
else:
a[0] = math.copysign(1, -a[i])
num += 1
old = a[0]
sam = a[0]
for n in range(2):
num = 0
if n != 0:
sam = -a[0] - int(math.copysign(1, a[0]))
old = sam
for i in range(1, len(a)):
sam += a[i]
sam_sign = int(math.copysign(1, sam))
old_sign = int(math.copysign(1, old))
if sam_sign == old_sign or sam == 0:
num += abs(sam) + 1
a[i] = a[i] + (-old_sign)*(abs(sam)+1)
old += a[i]
sam = old
if n == 0:
num2 = num
print(min(num,num2))
|
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, cnt1 = 0;
for (int i = 0; i < n; i++) cin >> a[i];
sum = a[0];
if (sum < 0) {
cnt += abs(-1 - sum);
sum = -1;
} else {
cnt += abs(sum - 1);
sum = 1;
}
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 | cpp | #include <bits/stdc++.h>
using namespace std;
vector<int> v(1e5);
long long solve_pos(int n) {
long long sum = v[0];
long long ans = 0;
if (sum < 0) sum = 1, ans = abs(v[0]) + 1;
for (int i = 1; i < n; i++) {
if (sum < 0 && sum + v[i] > 0)
sum += v[i];
else if (sum > 0 && sum + v[i] < 0)
sum += v[i];
else if (sum < 0 && sum + v[i] <= 0)
ans += abs(sum + v[i]) + 1, sum = 1;
else if (sum > 0 && sum + v[i] >= 0)
ans += abs(sum + v[i]) + 1, sum = -1;
}
return ans;
}
long long solve_neg(int n) {
long long sum = v[0];
long long ans = 0;
if (sum > 0) sum = -1, ans = abs(v[0]) + 1;
for (int i = 1; i < n; i++) {
if (sum < 0 && sum + v[i] > 0)
sum += v[i];
else if (sum > 0 && sum + v[i] < 0)
sum += v[i];
else if (sum < 0 && sum + v[i] <= 0)
ans += abs(sum + v[i]) + 1, sum = 1;
else if (sum > 0 && sum + v[i] >= 0)
ans += abs(sum + v[i]) + 1, sum = -1;
}
return ans;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> v[i];
long long ans = solve_pos(n);
ans = min(ans, solve_neg(n));
cout << ans;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | def min_op ary, prev_sum, op_cnt
return op_cnt if ary.empty?
sum = prev_sum + ary[0]
diff = 0
if prev_sum < 0
diff = -sum + 1 if sum <= 0
else # prev_sum > 0
diff = -sum - 1 if sum >= 0
end
min_op(ary[1..-1], sum + diff, op_cnt + diff.abs)
end
N = gets.to_i
ary = gets.split.map(&:to_i)
cnt = 0
if ary[0] == 0
# プラスにするケース
cnt1 = min_op(ary[1..-1], 1, 1)
# マイナスにするケース
cnt2 = min_op(ary[1..-1], -1, 1)
# プラスとマイナスで操作数の小さいほうを採用
cnt = [cnt1, cnt2].min
else
cnt = min_op(ary[1..-1], ary[0], 0)
end
puts cnt |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n=int(input())
a=list(map(int,input().split()))
s=[0]*n
s[0]=a[0]
cnt=0
for i in range(1,n):
s[i]=a[i]+s[i-1]
num=0
if s[i]<0 and s[i-1]<0:
num+=1-s[i]
cnt+=num
a[i]+=num
s[i]=s[i-1]+a[i]
elif 0<s[i] and 0<s[i-1]:
num+=(-1-s[i])
cnt+=abs(num)
a[i]+=num
s[i]=s[i-1]+a[i]
if s[i]==0:
if s[i-1]<0:
s[i]+=1
cnt+=1
elif 0<s[i-1]:
s[i]-=1
cnt+=1
print(cnt) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
ans=0
if a[0]==0:
a[0]+=1
ans+=1
t=a[0]
if a[0]>0:
for i in range(1,n):
t+=a[i]
if i%2==1:
if t>=0:
ans+=abs(t+1)
t=-1
else:
if t<=0:
ans+=abs(t-1)
t=1
else:
for i in range(1,n):
t+=a[i]
if i%2==1:
if t<=0:
ans+=abs(t-1)
t=1
else:
if t>=0:
ans+=abs(t+1)
t=-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;
int main() {
int N;
cin >> N;
vector<int> a(N), S(N + 7);
for (int i = 0; i < N; i++) {
cin >> a[i];
}
int ans = 0;
S[0] = a[0];
if (S[0] == 0) {
for (int i = 0; i < N; i++) {
if (a[i] > 0) {
if (i % 2 == 0) {
S[0] = 1;
ans++;
break;
} else {
S[0] = -1;
ans++;
break;
}
} else if (a[i] < 0) {
if (i % 2 == 0) {
S[0] = -1;
ans++;
break;
} else {
S[0] = 1;
ans++;
break;
}
} else if (i == N - 1 && a[i] == 0) {
ans = (2 * N) - 1;
cout << ans << endl;
return 0;
}
}
}
for (int i = 1; i < N; i++) {
S[i] = S[i - 1] + a[i];
}
for (int i = 1; i < N; i++) {
if (S[i - 1] > 0 && S[i] >= 0) {
ans += abs(S[i]) + 1;
S[i] = -1;
if (i != N - 1) {
S[i + 1] = S[i] + a[i + 1];
}
} else if (S[i - 1] < 0 && S[i] <= 0) {
ans += abs(S[i]) + 1;
S[i] = 1;
if (i != N - 1) {
S[i + 1] = S[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>
using namespace std;
int solve(vector<int> vec) {
long long int n = vec.size();
long long int sum = vec[0];
int ans = 0;
for (long long int i = 1; i < n; i++) {
if (sum > 0) {
if (sum + vec[i] >= 0) {
ans += 1 + (sum + vec[i]);
sum = -1;
} else {
sum += vec[i];
}
} else if (sum < 0) {
if (sum + vec[i] <= 0) {
ans += 1 - (sum + vec[i]);
sum = 1;
} else {
sum += vec[i];
}
}
}
return ans;
}
int main() {
int n, Ans;
cin >> n;
vector<int> as;
for (int i = 0; i < n; i++) {
int t;
cin >> t;
as.push_back(t);
}
vector<int> as1 = as;
as1[0] = 1;
vector<int> as2 = as;
as2[0] = -1;
Ans = min(solve(as),
min(solve(as1) + abs(1 - as[0]), solve(as2) + abs(-1 - as[0])));
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 | import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
res = -max(0, ~a[0])
sm = 1
if a[0] > 1: sm = a[0]
for i in range(1, N):
if sm > 0:
res += max(-1, a[i] + sm) + 1
sm = min(-1, sm + a[i])
else:
res += -min(1, sm + a[i]) + 1
sm = max(1, sm + a[i])
#print(res, sm)
res2 = max(0, a[0] + 1)
sm = -1
if a[0] < -1: sm = a[0]
for i in range(1, N):
if sm > 0:
res2 += max(-1, a[i] + sm) + 1
sm = min(-1, sm + a[i])
else:
res2 += -min(1, sm + a[i]) + 1
sm = max(1, sm + a[i])
print(min(res, 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>
using namespace std;
using ll = long long;
double mod = 1e9 + 7;
char alphabet[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
int cnt(ll n) {
int c = 0;
while (true) {
if (n < 1) break;
n = n / 10;
c++;
}
return c;
}
int main() {
ll N = 0, answer = 0, sum1 = 0, sum2 = 0, tmp = 0;
int cnt1 = 0, cnt2 = 0;
cin >> N;
int *a = new int[N];
for (int i = 0; i < (int)N; ++i) cin >> a[i];
sum1 += a[0];
sum2 += a[0];
for (int i = 1; i < (int)N; ++i) {
sum1 += a[i];
sum2 += a[i];
if (i % 2 == 0) {
if (sum1 >= 0) {
cnt1 += abs(sum1) - 1;
sum1 -= abs(sum1) + 1;
}
if (sum2 <= 0) {
cnt2 += abs(sum2) + 1;
sum2 += abs(sum2) + 1;
}
} else {
if (sum1 <= 0) {
cnt1 += abs(sum1) + 1;
sum1 += abs(sum1) + 1;
}
if (sum2 >= 0) {
cnt2 += abs(sum2) - 1;
sum2 -= abs(sum2) + 1;
}
}
}
cout << min(cnt1, 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 | cpp | #include <bits/stdc++.h>
using namespace std;
template <typename T>
using vec = vector<T>;
using i32 = int_fast32_t;
using i64 = int_fast64_t;
using u32 = uint_fast32_t;
using u64 = uint_fast64_t;
using ll = long long;
using ld = long double;
using vi = vec<int_fast32_t>;
using vl = vec<int_fast64_t>;
using vld = vec<ld>;
using vii = vec<vi>;
using PII = pair<int_fast32_t, int_fast32_t>;
template <class T>
using maxheap = std::priority_queue<T>;
template <class T>
using minheap = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template <class T, class U>
inline bool chmax(T &a, const U &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T, class U>
inline bool chmin(T &a, const U &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
const ld Pi = std::acos(-1.0L);
constexpr ll infll = (1LL << 62) - 1;
constexpr int inf = (1 << 30) - 1;
const int mod = 1000000007;
signed main() {
int START_TIME = clock();
cin.tie(nullptr);
ios::sync_with_stdio(false);
i32 n;
cin >> n;
vi a(n);
for (int_fast32_t i = 0; i < ((int_fast32_t)(n)); i++) cin >> a[i];
i32 ans = 0, sum = a[0];
for (i32 i = 1; i < n; ++i) {
sum += a[i];
if (i & 1) {
if (sum > 0) continue;
ans += 1 - sum;
sum = 1;
} else {
if (sum < 0) continue;
ans += sum + 1;
sum = -1;
}
}
i32 ans2 = 0, sum2 = a[0];
for (i32 i = 1; i < n; ++i) {
sum2 += a[i];
if (!(i & 1)) {
if (sum2 > 0) continue;
ans2 += 1 - sum2;
sum2 = 1;
} else {
if (sum2 < 0) continue;
ans2 += sum2 + 1;
sum2 = -1;
}
}
cout << min(ans, ans2) << '\n';
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
i = 0
sum_before = 0
sum_after = 0
count = 0
while i < n:
sum_after = sum_before + a[i]
if sum_after * sum_before > 0 or sum_after == 0:
if sum_after < 0:
a[i] += abs(sum_after) + 1
elif sum_after > 0:
a[i] -= abs(sum_after) + 1
elif sum_before < 0:
a[i] += 1
else:
a[i] -= 1
count += abs(sum_after) + 1
sum_after = sum_before + a[i]
i += 1
sum_before = sum_after
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;
int main() {
int n;
cin >> n;
int count1 = 0;
int count2 = 0;
vector<long long> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
long long sum = a[0];
if (a[0] >= 0) {
for (int i = 1; i < n; i++) {
sum += a[i];
if (i % 2 == 1) {
while (sum >= 0) {
sum--;
count1++;
}
} else {
while (sum <= 0) {
sum++;
count1++;
}
}
}
} else {
for (int i = 1; i < n; i++) {
sum += a[i];
if (i % 2 == 0) {
while (sum >= 0) {
sum--;
count1++;
}
} else {
while (sum <= 0) {
sum++;
count1++;
}
}
}
}
if (a[0] >= 0) {
while (a[0] >= 0) {
a[0]--;
count2++;
}
} else {
while (a[0] <= 0) {
a[0]++;
count2++;
}
}
sum = a[0];
if (a[0] >= 0) {
for (int i = 1; i < n; i++) {
sum += a[i];
if (i % 2 == 1) {
while (sum >= 0) {
sum--;
count2++;
}
} else {
while (sum <= 0) {
sum++;
count2++;
}
}
}
} else {
for (int i = 1; i < n; i++) {
sum += a[i];
if (i % 2 == 0) {
while (sum >= 0) {
sum--;
count2++;
}
} else {
while (sum <= 0) {
sum++;
count2++;
}
}
}
}
if (count1 >= count2)
cout << count2 << endl;
else
cout << count1 << 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()))
if a[0] >= 0:
flag = 1
else:
flag = -1
ans = 0
dp = [0 for i in range(n)]
dp[0] = a[0]
for i in range(1, n):
dp[i] = dp[i - 1] + a[i]
if dp[i] == 0:
dp[i] = flag * -1
ans += 1
elif flag == 1 and dp[i] > 0:
ans += abs(-1 - dp[i])
dp[i] = -1
elif flag == -1 and dp[i] < 0:
ans += (1 - dp[i])
dp[i] = 1
flag *= -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 ll = long long;
using namespace std;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<ll> v(n, 0);
for (int i = (int)(0); i < (int)(n); i++) cin >> v[i];
vector<int> p1(n + 1, 1), p2(n + 1, 1);
for (int i = (int)(0); i < (int)(n + 1); i++) {
if (i % 2 == 0) p1[i] *= -1;
}
for (int i = (int)(0); i < (int)(n + 1); i++) {
if (i % 2 == 1) p2[i] *= -1;
}
priority_queue<int, vector<int>, greater<int> > pq;
vector<ll> sum_until(n + 1, 0);
int cnt;
cnt = 0;
for (int i = 1; i <= n; i++) {
sum_until[i] = sum_until[i - 1] + v[i - 1];
if (sum_until[i] * p1[i] < 0) {
int plus = abs(sum_until[i]);
cerr << "("
"i"
","
"plus * p1[i]"
"):("
<< i << "," << plus * p1[i] << ")" << endl;
cerr << "sum_until[i]"
":"
<< sum_until[i] << endl;
sum_until[i] += plus * p1[i] + p1[i];
cerr << "sum_until[i]"
":"
<< sum_until[i] << endl;
cnt += abs(plus * p1[i]) + 1;
} else if (sum_until[i] == 0) {
sum_until[i] = p1[i];
cnt += 1;
}
}
cerr << "sum_until"
":[ ";
for (auto macro_vi : sum_until) {
cerr << macro_vi << " ";
}
cerr << "]" << endl;
cerr << "cnt"
":"
<< cnt << endl;
pq.push(cnt);
p1 = p2;
cnt = 0;
for (int i = 1; i <= n; i++) {
cerr << "i"
":"
<< i << endl;
sum_until[i] = sum_until[i - 1] + v[i - 1];
if (sum_until[i] * p1[i] < 0) {
int plus = abs(sum_until[i]);
cerr << "("
"i"
","
"plus * p1[i]"
"):("
<< i << "," << plus * p1[i] << ")" << endl;
cerr << "sum_until[i]"
":"
<< sum_until[i] << endl;
sum_until[i] += plus * p1[i] + p1[i];
cerr << "sum_until[i]"
":"
<< sum_until[i] << endl;
cnt += abs(plus * p1[i]) + 1;
} else if (sum_until[i] == 0) {
sum_until[i] = p1[i];
cnt += 1;
}
}
pq.push(cnt);
cerr << "cnt"
":"
<< cnt << endl;
cerr << "sum_until"
":[ ";
for (auto macro_vi : sum_until) {
cerr << macro_vi << " ";
}
cerr << "]" << endl;
cout << pq.top() << 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> a(n);
for (int i = 0; i < n; i++) cin >> a.at(i);
int count = 0, keep = 0;
for (int i = 0; i < n; i++) {
keep += a.at(i);
if (i % 2 == 0) {
if (keep <= 0) {
count += 1 - keep;
keep = 1;
}
} else {
if (keep >= 0) {
count += keep + 1;
keep = -1;
}
}
}
int count2 = 0;
keep = 0;
for (int i = 0; i < n; i++) {
keep += a.at(i);
if (i % 2 == 0) {
if (keep >= 0) {
count2 += keep + 1;
keep = -1;
}
} else {
if (keep <= 0) {
count2 += 1 - keep;
keep = 1;
}
}
}
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;
int ans(int n, int *, int change);
int main() {
int n, A;
int a[110000], s[110000];
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
s[0] = a[0];
for (int i = 1; i < n; i++) s[i] = s[i - 1] + a[i];
int pAns, nAns;
pAns = ans(n, s, 1);
nAns = ans(n, s, -1);
printf("%d\n", min(pAns, nAns));
}
int ans(int n, int *s, int change) {
int Ans = 0;
int b[110000];
for (int i = 0; i < n; i++) b[i] = s[i];
for (int i = 0; i < n; i++) {
switch (change) {
case -1:
while (b[i] > -1) {
for (int j = i; j < n; j++) --b[j];
++Ans;
}
change *= -1;
break;
case 1:
while (b[i] < 1) {
for (int j = i; j < n; j++) ++b[j];
++Ans;
}
change *= -1;
break;
}
}
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 | python3 | n = int(input())
a = list(map(int, input().split()))
sm = a[0]
def f(sm):
ret = 0
for e in a[1:]:
if sm * (sm + e) < 0:
sm += e
continue
else:
if sm > 0:
a_mx = -sm - 1
ret += e - a_mx
sm += a_mx
else:
a_mn = -sm + 1
ret += a_mn - e
sm += a_mn
return ret
if sm > 0:
ans = min(f(sm), f(-1) + sm + 1)
elif sm < 0:
ans = min(f(sm), f(1) + sm + 1)
else:
ans = min(f(1), f(-1)) + 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;
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};
signed main() {
ios::sync_with_stdio(false);
ll n;
cin >> n;
vector<ll> a;
for (ll i = 0; i < n; i++) {
ll x;
cin >> x;
a.push_back(x);
}
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++;
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 | import numpy as np
n = int(input())
L = np.array([int(i) for i in input().split()])
if L[0] < 0:
L = -L
count = 0
s = L[0]
if L[0] == 0:
if L[1] > 0:
L[0] = -1
else:
L[0] = 1
count += 1
for i in range(n//2 -1):
s = s + L[2*i+1]
if s >= 0:
subt = s + 1
count += subt
s = s - count
s = s + L[2*i+2]
if s <= 0:
subt = s - 1
count -= subt
s = s + count
if n%2 == 0:
s = s + L[-1]
if s >= 0:
subt = s + 1
count += subt
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;
const int INF = 1e9 + 7, MOD = 1e9 + 7;
const long long LINF = 1e18;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int main() {
int n;
cin >> n;
int a[n], Sum1[n], Sum2[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
Sum1[i] = Sum2[i] = a[i];
if (i) {
Sum1[i] = Sum1[i - 1] + Sum1[i];
Sum2[i] = Sum1[i];
}
}
int sum1 = 0;
int sum2 = 0;
int tmp1 = 0, tmp2 = 0;
for (int i = 0; i < n; i++) {
Sum1[i] += tmp1;
Sum2[i] += tmp2;
if (i % 2 == 0) {
if (Sum1[i] >= 0) {
sum1 += 1 + Sum1[i];
tmp1 += -1 - Sum1[i];
}
if (Sum2[i] <= 0) {
sum2 += 1 + (-1 * Sum2[i]);
tmp2 += 1 - Sum2[i];
}
} else {
if (Sum1[i] <= 0) {
sum1 += 1 + (-1 * Sum1[i]);
tmp1 += 1 - Sum1[i];
}
if (Sum2[i] >= 0) {
sum2 += 1 + Sum2[i];
tmp2 += 1 - Sum2[i];
}
}
}
cout << min(sum1, sum2) << 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()))
def solve(A):
res = 0
pre_a = A[0]
for a in A[1:]:
a += pre_a
if a * pre_a < 0:
pre_a = a
continue
diff = - a + (-1)**(pre_a > 0)
res += abs(diff)
pre_a = a + diff
return res
if A[0] != 0:
ans = solve(A)
else:
A[0] = 1
ans = solve(A) + 1
A[0] = -1
ans = min(ans, solve(A) + 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;
using ll = long long;
const int INF = 1e9;
int main() {
int N;
cin >> N;
vector<int> A(N);
int a = 0, b = 0, sum = 0;
for (int(i) = 0; (i) < (N); ++(i)) cin >> A[i];
for (int(i) = 0; (i) < (N); ++(i)) {
sum += A[i];
if (i % 2 == 0 && sum <= 0)
a -= (sum - 1), sum = 1;
else if (i % 2 && sum >= 0)
a += (sum + 1), sum = -1;
}
sum = 0;
for (int(i) = 0; (i) < (N); ++(i)) {
sum += A[i];
if (i % 2 && sum <= 0)
b -= (sum - 1), sum = 1;
else if (i % 2 == 0 && sum >= 0)
b += (sum + 1), sum = -1;
}
cout << (a > b ? b : a) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; ++i) cin >> a[i];
long long sum = a[0];
long long ans = 0;
for (int i = 0; i < n - 1; ++i) {
if (sum < 0) {
if (a[i + 1] + sum > 0)
sum += a[i + 1];
else {
ans += (1 - (a[i + 1] + sum));
sum = 1;
}
} else {
if (sum == 0) {
ans++;
sum += 1;
}
if (a[i + 1] + sum < 0)
sum += a[i + 1];
else {
ans += abs(-1 - (a[i + 1] + sum));
sum = -1;
}
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, odd = 0, even = 0, temp = 0;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a.at(i);
for (int i = 0; i < n; i++) {
temp += a.at(i);
if ((i % 2 == 0 && temp < 0) || (i % 2 == 1 && temp > 0)) continue;
if (i % 2) {
odd += 1 - temp;
temp = 1;
} else {
odd += temp + 1;
temp = -1;
}
}
temp = 0;
for (int i = 0; i < n; i++) {
temp += a.at(i);
if ((i % 2 == 0 && temp > 0) || (i % 2 == 1 && temp < 0)) continue;
if (i % 2) {
even += temp + 1;
temp = -1;
} else {
even += 1 - temp;
temp = 1;
}
}
cout << min(odd, even) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | import Control.Monad
import Data.List
main = readLn >>= main'
where
main' n = getLine >>= print . solve n . fmap read . words
solve :: Int -> [Int] -> Int
solve c (x : xs)
| x /= 0 = fst $ foldl' ff (0, x) xs
| null zs = zeroCount c
| otherwise = fst $ foldl' ff (zeroCount $ length ys, negate z `div` abs z) zs
where
ff (acc, s) n
| s * next < 0 = (acc, next)
| s * next > 0 = (acc + abs next + 1 , negate s `div` abs s)
| otherwise = (acc + 1, negate s `div` abs s)
where
next = s + n
(ys, zs) = span (== 0) xs
z = head zs
zeroCount = pred . (2 *)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool amax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
inline bool amin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
struct aaa {
aaa() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
};
} aaaaaaa;
const int INF = 1001001001;
const long long LINF = 1001001001001001001ll;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;
const int dx[] = {1, 1, 0, -1, -1, -1, 0, 1},
dy[] = {0, 1, 1, 1, 0, -1, -1, -1};
signed main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0, i_len = n; i < i_len; ++i) {
cin >> a.at(i);
}
int cnt{};
int cnt2{};
int sum = 0;
for (int i = 0, i_len = n; i < i_len; ++i) {
sum += a.at(i);
if (i % 2 != 0) {
if (sum <= 0) {
cnt += abs(1 - sum);
sum = 1;
}
} else {
if (sum >= 0) {
cnt += abs(sum + 1);
sum = -1;
}
}
}
sum = 0;
for (auto i = 0; i != n; ++i) {
sum += a.at(i);
if (i % 2 == 0) {
if (sum <= 0) {
cnt2 += abs(1 - sum);
sum = 1;
}
} else {
if (sum >= 0) {
cnt2 += abs(sum + 1);
sum = -1;
}
}
}
cout << min(cnt, cnt2);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
# +, -, +, -, ...
sum = 0
count = 0
for i in range(n):
sum += a[i]
if i % 2 == 0:
if sum <= 0:
sum = 1
count += abs(sum) + 1
else:
if sum >= 0:
sum = -1
count += abs(sum) + 1
# -, +, -, +, ...
sum2 = 0
count2 = 0
for i in range(n):
sum2 += a[i]
if i % 2 == 0:
if sum2 >= 0:
sum2 = -1
count2 += abs(sum2) + 1
else:
if sum2 <= 0:
sum2 = 1
count2 += abs(sum2) + 1
print(min(count, 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 | UNKNOWN | def sign(i)
return 1 if i.positive? || i == 0
-1
end
def sequence(n, a)
b = [ a[0] ]
count = 0
(n-1).times do |i|
b[i+1] = b[i] + a[i+1]
if b[i+1] == 0
if b[i] > 0
a[i+1] -= 1
count += 1
else
a[i+1] += 1
count += 1
end
elsif sign(b[i]) == sign(b[i+1])
if b[i].positive?
count += (a[i+1] - (-b[i] - 1)).abs
a[i+1] = -b[i] - 1
b[i+1] = b[i] + a[i+1]
else
count += (a[i+1] - (-b[i] + 1)).abs
a[i+1] = -b[i] + 1
b[i+1] = b[i] + a[i+1]
end
end
end
count
end
n = gets.chomp.to_i
a = gets.chomp.split.map { |item| item.to_i }
a2 = a
pn_count = 0
if a[0] > 0
a2[0] = [ a[0] - a[0] - 1 ]
np_count = a[0].abs + 1
elsif a[0] < 0
a2[0] = [ a[0] - a[0] + 1 ]
np_count = a[0].abs + 1
else
a[0] = 1
pn_count = 1
a2[0] = -1
np_count = 1
end
pn_count = sequence(n, a)
np_count += sequence(n, a2)
puts pn_count < np_count ? pn_count : np_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())
arr = [int(x) for x in input().split()]
def exec(sign):
a = [x for x in arr]
res = 0
if a[0] == 0:
a[0] -= sign
res += 1
x = 0
for i in range(n-1):
x += a[i]
tmp = sign - (x + a[i+1])
if sign < 0:
tmp = min(tmp, 0)
else:
tmp = max(tmp, 0)
res += abs(tmp)
a[i+1] += tmp
sign *= (-1)
return res
print(min(exec(1), exec(-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, a[100000];
cin >> N;
for (int i = 0; i < N; ++i) cin >> a[i];
int counter = 0;
long long sum = 0;
if (a[0] >= 0) {
for (int i = 0; i < N; ++i) {
sum += a[i];
if (i % 2 == 0) {
while (sum <= 0) {
++sum;
++counter;
}
} else {
while (sum >= 0) {
--sum;
++counter;
}
}
}
} else {
for (int i = 0; i < N; ++i) {
sum += a[i];
if (i % 2 == 0) {
while (sum >= 0) {
--sum;
++counter;
}
} else {
while (sum <= 0) {
++sum;
++counter;
}
}
}
}
cout << counter << 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()]
ls1 = [1] + [-2 if i%2 == 0 else 2 for i in range(n-1)]
ls2 = [-1] + [2 if i%2 == 0 else -2 for i in range(n-1)]
ans1, ans2 = 0,0
for x, y in zip(a, ls1):
ans1 += abs(x-y)
for x, y in zip(a, ls2):
ans2 += abs(x-y)
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>
using namespace std;
int main() {
int n, total = 0, ans = 0;
cin >> n;
int t;
cin >> t;
total = t;
if (t > 0) {
for (int i = 1; i < n; i++) {
cin >> t;
total += t;
if (i % 2 == 1) {
if (total > 0) {
ans += total + 1;
total = -1;
}
} else {
if (total < 0) {
ans += -(total) + 1;
total = 1;
}
}
}
} else {
for (int i = 1; i < n; i++) {
cin >> t;
total += t;
if (i % 2 == 1) {
if (total < 0) {
ans += -(total) + 1;
total = 1;
}
} else {
if (total > 0) {
ans += total + 1;
total = -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 | java | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Pradyumn Agrawal coderbond007
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastReader in = new FastReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
static class TaskC {
public void solve(int testNumber, FastReader in, PrintWriter out) {
int n = in.nextInt();
int[] a = in.nextIntArray(n);
long[] sum = new long[n];
long ret = 0;
boolean flag = true;
for (int i = 0; i < n; i++) {
sum[i] += a[i];
if (i != 0) {
sum[i] += sum[i - 1];
if (sum[i] * sum[i - 1] >= 0) {
flag = false;
}
}
}
if (flag) {
out.println(0);
return;
}
Arrays.fill(sum, 0);
for (int i = 0; i < n; i++) {
sum[i] += a[i];
if (i != 0) sum[i] += sum[i - 1];
if (sum[i] == 0) {
if (i - 1 >= 0) {
if (sum[i - 1] > 0) {
sum[i] = -1;
ret++;
} else if (sum[i - 1] < 0) {
sum[i] = 1;
ret++;
}
}
} else {
if (sum[i] > 0) {
if (i - 1 >= 0 && sum[i - 1] > 0) {
ret += sum[i] + 1;
sum[i] = -1;
}
} else if (sum[i] < 0) {
if (i - 1 >= 0 && sum[i - 1] < 0) {
ret += -sum[i] + 1;
sum[i] = 1;
}
}
}
}
out.println(ret);
}
}
static class FastReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int pnumChars;
private FastReader.SpaceCharFilter filter;
public FastReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (pnumChars == -1) {
throw new InputMismatchException();
}
if (curChar >= pnumChars) {
curChar = 0;
try {
pnumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (pnumChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c == ',') {
c = read();
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int[] array = new int[n];
for (int i = 0; i < n; i++) {
array[i] = nextInt();
}
return array;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
|
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())
lista=list(map(int,input().split()))
updown=""
goukei=0
count=0
if lista[0]<0:
updown="up"
else:
updown="down"
goukei=goukei+lista[0]
print(goukei,count,updown)
for i in range(1,n):
if updown=="down":
if lista[i]+goukei<0:
goukei+=lista[i]
updown="up"
print(goukei,count,updown)
elif lista[i]+goukei>0:
count=count+abs(lista[i]+goukei)+1
goukei=-1
down="up"
print(goukei,count,updown)
elif updown=="up":
if lista[i]+goukei>0:
goukei+=lista[i]
updown="down"
print(goukei,count,updown)
elif lista[i]+goukei<0:
count=count+abs(lista[i]+goukei)+1
goukei=1
updown="down"
print(goukei,count,updown)
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 | python2 | # -*- coding:utf-8 -*-
n = int(raw_input())
numlist = (raw_input()).split(' ')
sumlist = [int(numlist[0])]
count = 0
for i in range(1, n):
sumlist.append(sumlist[i-1] + int(numlist[i]))
while (True):
if (sumlist[i-1] > 0 and sumlist[i] > 0): #i-1,i番目までのsumがともに正
#numlist[i] = int(numlist[i]) - 1
sumlist[i] -= 1
count += 1
elif (sumlist[i-1] < 0 and sumlist[i] < 0): #i-1,i番目までのsumがともに負
#numlist[i] = int(numlist[i]) + 1
sumlist[i] += 1
count += 1
elif (sumlist[i] == 0): #i番目までのsum=0
if (sumlist[i-1] > 0):
#numlist[i] = int(numlist[i]) - 1
sumlist[i] -= 1
if (sumlist[i-1] < 0):
#numlist[i] = int(numlist[i]) + 1
sumlist[i] += 1
count += 1
else:
break
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;
int main() {
int n, i, j, count, sum, x, bsum, s;
vector<int> a;
cin >> n;
a.resize(n);
cin >> a[0];
for (i = 1; i < n; i++) {
cin >> a[i];
}
count = 0;
sum = 0;
for (int i = 0; i < n - 1; i++) {
bsum = sum;
sum += a[i];
if (sum * (sum + a[i + 1]) >= 0) {
x = abs(sum + a[i + 1]) + 1;
s = abs(sum);
if (a[i] * a[i + 1] < 0) {
if (s - 1 > x) {
a[i] = a[i] > 0 ? a[i] - x : a[i] + x;
sum = bsum + a[i];
} else {
a[i] = a[i] > 0 ? a[i] - (s - 1) : a[i] + (s - 1);
sum = bsum + a[i];
s = x - (s - 1);
a[i + 1] = a[i + 1] > 0 ? a[i + 1] + s : a[i + 1] - s;
}
} else {
a[i + 1] = a[i + 1] > 0 ? -s - 1 : s + 1;
}
count += x;
}
}
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 | python3 | n = int(input())
a = list(map(int, input().split()))
s = 0
cnt = 0
for i in range(n):
s += a[i]
if i % 2 == 0:
if s <= 0:
cnt += abs(s) + 1
s = 1
else:
if s >= 0:
cnt += abs(s) + 1
s -= 1
ans1 = cnt
s = 0
cnt = 0
for i in range(n):
s += a[i]
if i % 2 != 0:
if s <= 0:
cnt += abs(s) + 1
s = 1
else:
if s >= 0:
cnt += abs(s) + 1
s = -1
ans2 = cnt
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>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> an(n);
for (int i = 0; i < n; ++i) {
cin >> an[i];
}
int cnt_min = INT_MAX;
for (int j = 0; j < 2; ++j) {
int sign = j == 0 ? -1 : 1;
int accum = an[0];
int cnt = 0;
if (accum * sign <= 0) {
auto x = sign - accum;
accum += x;
cnt += abs(x);
}
for (int i = 1; i < n; ++i) {
auto new_accum = accum + an[i];
if (new_accum * accum >= 0) {
int x = -sign - new_accum;
new_accum += x;
cnt += abs(x);
an[i] += x;
}
int new_sign = new_accum > 0 ? 1 : -1;
accum = new_accum;
assert(new_sign == -sign);
sign = new_sign;
}
if (cnt < cnt_min) {
cnt_min = cnt;
}
}
cout << cnt_min << 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 | import scala.io.StdIn
import scala.annotation.tailrec
object Main extends App {
val n = StdIn.readInt
val a = StdIn.readLine.split(" ").map(_.toInt)
val ans1 = a.tail./:(a.head,0)((acc,i) => {
val (bsum, bcnt) = acc
val sum = bsum + i
val cnt = if(bsum < 0 && sum < 0) 1 - sum
else if(bsum >= 0 && sum >= 0) -1 - sum
else if(sum == 0) if(bsum < 0) 1 else -1
else 0
println(sum + " " + cnt)
(sum+cnt, bcnt+cnt.abs)
})._2
val ans2 = a.tail./:(-a.head,a.head.abs+1)((acc,i) => {
val (bsum, bcnt) = acc
val sum = bsum + i
val cnt = if(bsum < 0 && sum < 0) 1 - sum
else if(bsum >= 0 && sum >= 0) -1 - sum
else if(sum == 0) if(bsum < 0) 1 else -1
else 0
println(sum + " " + cnt)
(sum+cnt, bcnt+cnt.abs)
})._2
println(math.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>
using namespace std;
int main() {
int N;
cin >> N;
vector<long long int> A(N);
for (int i = 0; i < N; i++) cin >> A[i];
vector<long long int> B(N);
B[0] = A[0];
for (int i = 1; i < N; i++) B[i] = B[i - 1] + A[i];
long long int ans = 0;
long long int base = 0;
for (int i = 1; i < N; i++) {
if ((B[i] + base) * (B[i - 1] + base) > 0) {
if (B[i] + base > 0) {
ans += abs(B[i] + base) + 1;
base -= abs(B[i] + base) + 1;
continue;
} else if (B[i] + base < 0) {
ans += abs(B[i] + base) + 1;
base += abs(B[i] + base) + 1;
continue;
}
}
if (B[i - 1] + base == 0) {
if (B[i] + base > 0) {
ans += 1;
base -= 1;
continue;
} else if (B[i] + base < 0) {
ans += 1;
base += 1;
continue;
}
}
if (i == N - 1 && B[i] + base == 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;
vector<long long> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long res = 0, cur = a[0];
for (int i = 1; i < n; i++) {
int s_prev = cur / abs(cur);
int s_cur = (cur + a[i] == 0 ? 0 : (cur + a[i]) / abs(cur + a[i]));
if (s_prev == s_cur || s_cur == 0) {
res += abs(cur + a[i]) + 1;
cur = (s_prev == -1 ? 1 : -1);
} else {
cur += a[i];
}
}
cout << res << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
int sum1 = 0;
int sum2 = 0;
int op1 = 0;
int op2 = 0;
cin >> n;
for (int i = 0; i < n; i++) {
int tmp;
cin >> tmp;
sum1 += tmp;
sum2 += tmp;
if (i % 2) {
if (sum1 >= 0) {
op1 += sum1 + 1;
sum1 = -1;
}
if (sum2 <= 0) {
op2 += -sum2 + 1;
sum2 = 1;
}
} else {
if (sum1 <= 0) {
op1 += -sum1 + 1;
sum1 = 1;
}
if (sum2 >= 0) {
op2 += sum2 + 1;
sum2 = -1;
}
}
}
if (op1 < op2)
cout << op1;
else
cout << op2;
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 a[100000];
int n;
int solve(int tmp, int cnt) {
for (int i = 1; i < n; ++i) {
int prev = tmp;
tmp += a[i];
if (prev > 0 && tmp >= 0) {
cnt += tmp + 1;
tmp = -1;
} else if (prev < 0 && tmp <= 0) {
cnt += abs(tmp) + 1;
tmp = 1;
}
}
return cnt;
}
int main() {
cin >> n;
for (int i = 0; i < (n); ++i) cin >> a[i];
int ans;
if (a[0] == 0)
ans = min(solve(1, 1), solve(-1, 1));
else if (a[0] > 0)
ans = min(solve(a[0], 0), solve(-1, a[0] + 1));
else
ans = min(solve(a[0], 0), solve(1, -a[0] + 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>
using namespace std;
long long calc(int MOD, vector<int> hairetu, int n) {
int result = 0;
int sum = 0;
for (int i = 0; i <= n - 1; i++) {
sum += hairetu[i];
if (i % 2 == MOD && sum <= 0) {
result += (1 - sum);
sum = 1;
} else if (i % 2 != MOD && sum >= 0) {
result += (sum + 1);
sum = -1;
}
}
return result;
}
int main() {
int num;
cin >> num;
vector<int> hairetu(num);
for (int i = 0; i < num; i++) {
cin >> hairetu[i];
}
cout << min(calc(0, hairetu, num), calc(1, hairetu, 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 | python3 | import numpy as np
n=int(input())
a=list(map(int,input().split()))
r=[0]
for i in range(n):
r.append(r[i]+a[i])
r.pop(0)
print(r)
q=[r[i] for i in range(n)]
pm=[1-2*(i%2) for i in range(n)]
mp=[1-2*((i+1)%2) for i in range(n)]
sum1,sum2=0,0
sousa1,sousa2=0,0
for i in range(n):
if np.sign(r[i]) != pm[i]:
sum1+=abs(pm[i]-r[i])
sousa1=pm[i]-r[i]
for j in range(n-i-1):
r[i+j+1]=r[i+j+1]+sousa1
for i in range(n):
if np.sign(q[i]) != mp[i]:
sum2+=abs(mp[i]-q[i])
sousa2=mp[i]-q[i]
for j in range(n-i-1):
q[i+j+1]=q[i+j+1]+sousa2
print(min(sum1,sum2)) |
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 ans = 0, c, n, count = 0, b = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> c;
if (i == 0) {
count = c;
if (c < 0) b = 1;
} else {
count += c;
if (b == 0) {
while (count >= 0) {
ans++;
count--;
}
b = 1;
} else {
while (count <= 0) {
ans++;
count++;
}
b = 0;
}
}
}
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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define MOD 1000000007
# define INF (1 < <29)
#define MODSET(d) if ((d) >= MOD) d %= MOD;
#define MODNEGSET(d) if ((d) < 0) d = ((d % MOD) + MOD) % MOD;
#define MODADDSET(d) if ((d) >= MOD) d -= MOD;
#define MODADDWHILESET(d) while ((d) >= MOD) d -= MOD;
//defines
#define FILE_IO freopen("in.txt","r",stdin); freopen("out.txt","w",stdout);
#define sc1(a,type) type a; cin>>a;
#define sc2(a,b,type) type a,b; cin>>a>>b;
#define sc3(a, b, c,type) type a,b,c; cin>>a>>b>>c;
#define sc4(a, b, c, d,type) type a ,b,c,d; cin>>a>>b>>c>>d;
#define nl cout<<"\n";
#define foreach(v, c) for(__typeof( (c).begin()) v = (c).begin(); v != (c).end(); ++v)
#define revforeach(v, c) for(__typeof( (c).rbegin()) v = (c).rbegin(); v != (c).rend(); ++v)
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);
#define re(i,b) for(int i=0;i<int(b);i++)
#define re1(i,b) for(int i=1;i<=int(b);i++)
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(),c.rend()
#define mpresent(container, element) (container.find(element) != container.end()) //for map,set..etc (returns true/false value)
#define vpresent(container, element) (find(all(container),element) != container.end()) //for vectors,strings,list,deque (returns true/false value)
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define ins insert
#define F first
#define S second
#define clr clear()
#define sz(x) ((int)x.size())
#define dt distance
#define test(t) int t; cin>>t; while(t--)
#define csb(i) __builtin_popcount(i)
#define csbll(i) __builtin_popcountll(i)
#define clz(x) __builtin_clz(x)
#define clzl(x) __builtin_clzl(x)
#define cp(x) __builtin_parity(x)
#define adv(v,num) advance(v,num)//used for lists and other structures that use iterators,when you can't access elements randomly ( iterator moves num positions)
#define mod 1000000007
#define MAX_ARR 1000000
#define v2d(rowsize,colsize,type,name) vector<vector<type>> name(rowsize,vector<type>(colsize));
#define digits_in(i) (ll)log10(i)+1 // gives no of digits in a number
#define sqr(x) (x)*(x)
//does not apply for i==0 , add an excetion contition for n==0 ( cust return count 1 for that inseted of using this function)
//typedef
typedef string str;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<str> vs;
typedef vector<char> vc;
typedef pair<int,int> pii;
typedef pair<str,int> psi;
typedef pair<int,str> pis;
typedef vector<pii> vii;
typedef map<int,int> mii;
typedef map<ll,ll> mll;
typedef map<str,int> msi;
typedef map<char,int> mci;
typedef map<int,str> mis;
typedef unordered_map<int,int> umii;
typedef unordered_map<str,int> umsi;
typedef unordered_map<int,str> umis;
typedef unordered_map<str,str> umss;
typedef unordered_map<char,int> umci;
typedef set<str> ss;
typedef set<int> si;
typedef unordered_set<str> uss;
typedef unordered_set<int> usi;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
// #ifndef ONLINE_JUDGE
// #include "debug.h"
// #else
// #define debug(args...)
// #endif
int main(){fastio
// #ifndef ONLINE_JUDGE
// FILE_IO
// #endif
vll v;
test(t){
int temp;cin>>temp;
v.pb(temp);
}
ll ct=0;
re(i,sz(v)-1){
// debug(v[i] ,v[i]+v[i+1]);
if( (v[i]<0 && v[i]+v[i+1]<0) || (v[i]>0 && v[i]+v[i+1]>0 )|| v[i]+v[i+1]==0 ){
if(v[i]+v[i+1]==0) ct+=1;
else ct+=llabs(v[i]+v[i+1])+1;
v[i+1]= v[i]>0?-1:1;
}
else{
v[i+1]+=v[i];
}
// debug(ct);
}
cout<<ct;
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 | import copy
n = int(input())
a = list(map(int,input().split()))
b = copy.copy(a)
#sum_a = []
#sum_b = []
ans_a = 0
ans_b = 0
for i in range(n):
if i%2==0:
if sum(a[:i+1])>0:
#sum_a.append(sum(a[:i+1]))
tmp = 0
else:
tmp = abs(sum(a[:i+1]))+1
a[i] += tmp
ans_a += tmp
#sum_a.append(sum(a[:i+1]))
else:
if sum(a[:i+1])<0:
#sum_a.append(sum(a[:i+1]))
tmp = 0
else:
tmp = abs(sum(a[:i+1]))+1
a[i] -= tmp
ans_a += tmp
#sum_a.append(sum(a[:i+1]))
if i%2==1:
if sum(b[:i+1])>0:
#sum_b.append(sum(b[:i+1]))
tmp = 0
else:
tmp = abs(sum(b[:i+1]))+1
b[i] += tmp
ans_b += tmp
#sum_b.append(sum(b[:i+1]))
else:
if sum(b[:i+1])<0:
#sum_b.append(sum(b[:i+1]))
tmp = 0
else:
tmp = abs(sum(b[:i+1]))+1
b[i] -= tmp
ans_b += tmp
#sum_b.append(sum(b[:i+1]))
#print(a)
#print(sum_a)
print(min(ans_a,ans_b)) |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.