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 <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <string>
#include <string.h>
#include <bitset>
#include <map>
#include <climits>
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)
{
// accum + x = sign
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)
{
// new_accum + x = sign
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;
// int accum = 0;
// int sign = 0;
// bool non_zero = true;
// int cnt = 0;
// for (int i = 0; i < n; ++i)
// {
// auto new_accum = accum + an[i];
// if (i == 0)
// {
// if (new_accum == 0)
// {
// int next_sign = an[1] > 0 ? 1 : -1;
// new_accum -= next_sign;
// ++cnt;
// an[0] = -next_sign;
// }
// accum = new_accum;
// sign = accum > 0 ? 1 : -1;
// }
// else
// {
// if (new_accum * accum >= 0)
// {
// // new_accum + x = sign
// // 2 + x = (-1) , x=-3
// 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;
// }
// }
// cout << cnt << endl;
return 0;
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll INF = LLONG_MAX;
using vc = vector<char>;
using vi = vector<int>;
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
ll I, O, T, J, L, S, Z;
cin >> I >> O >> T >> J >> L >> S >> Z;
ll ans = 0;
ans += O;
if (I % 2 == 1 && J % 2 == 1 && L % 2 == 0) {
ans += 3;
--I;
--J;
--L;
}
ans += I / 2 * 2;
ans += J / 2 * 2;
ans += L / 2 * 2;
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long N;
cin >> N;
vector<int> A(N);
for (long long i = 0; i < N; i++) cin >> A.at(i);
bool flag;
bool flag2 = false;
for (long long i = 0; i < N; i++) {
if (A.at(i) > 0) {
flag = true;
flag2 = true;
break;
} else if (A.at(i) < 0) {
flag = false;
flag2 = true;
break;
}
}
if (!flag2) {
cout << 0 << endl;
return 0;
}
long long ans = 0;
long long total = A.at(0);
for (long long i = 0; i < N - 1; i++) {
long long count = 0;
if (flag) {
if (total + A.at(i + 1) >= 0) {
count = -1 - total - A.at(i + 1);
ans += abs(count);
A.at(i + 1) = A.at(i + 1) + count;
}
total += A.at(i + 1);
flag = false;
} else if (!flag) {
if (total + A.at(i + 1) <= 0) {
count = 1 - total - A.at(i + 1);
ans += abs(count);
A.at(i + 1) = A.at(i + 1) + count;
}
total += A.at(i + 1);
flag = true;
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n, count = 0;
cin >> n;
vector<long long> v(n), ans(n);
for (size_t i = 0; i < n; i++) {
cin >> v[i];
}
for (size_t i = 0; i < n; i++) {
if (i == 0) {
ans[i] = v[i];
} else {
if (ans[i - 1] < 0 && ans[i - 1] + v[i] > 0 ||
ans[i - 1] > 0 && ans[i - 1] + v[i] < 0) {
ans[i] = ans[i - 1] + v[i];
} else if (ans[i - 1] + v[i] == 0) {
if (ans[i - 1] < 0) {
count++;
ans[i] = 1;
} else {
count++;
ans[i] = -1;
}
} else {
count += abs(ans[i - 1] + v[i]) + 1;
if (ans[i - 1] + v[i] < 0) {
ans[i] = 1;
} else if (ans[i - 1] + v[i] > 0) {
ans[i] = -1;
}
}
}
}
cout << count << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; ++i) cin >> a[i];
int sign = (a[0] > 0) - (a[0] < 0);
int s = a[0];
int ans = 0;
for (int i = 1; i < n; ++i) {
while (abs(a[i]) <= abs(s)) {
++ans;
a[i] -= sign;
}
s += a[i];
sign *= -1;
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
s1 = []
s2 = []
for i in range(n):
s1.append(sum(a[:i+1]))
s2.append(sum(a[:i+1]))
ans1 = 0 #偶数インデックスが正
for i in range(n):
if i % 2 == 0:
if s1[i] <= 0:
x = abs(s1[i]) + 1
ans1 += x
for j in range(i, n):
s1[j] += x
else:
continue
else:
if s1[i] >= 0:
x = abs(s1[i]) + 1
ans1 += x
for j in range(i,n):
s1[j] -= x
ans2 = 0 #偶数インデックスが負
for i in range(n):
if i % 2 == 1:
if s2[i] <= 0:
x = abs(s2[i]) + 1
ans2 += x
for j in range(i, n):
s2[j] += x
else:
continue
else:
if s2[i] >= 0:
x = abs(s2[i]) + 1
ans2 += x
for j in range(i,n):
s2[j] -= x
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() {
long long n, ci, counter1, counter2;
bool isPositive;
cin >> n;
vector<long long> a(n);
for (int(i) = (0); (i) <= (n - 1); ++(i)) cin >> a[i];
ci = a[0];
counter1 = counter2 = 0;
isPositive = true;
for (int(i) = (1); (i) <= (n - 1); ++(i)) {
if (ci == 0) {
ci += 1;
++counter1;
}
ci += a[i];
if (isPositive && ci > 0) {
counter1 += abs(ci) + 1;
ci -= abs(ci) + 1;
} else if (!isPositive && ci < 0) {
counter1 += abs(ci) + 1;
ci += abs(ci) + 1;
} else if (ci == 0) {
if (isPositive) {
--ci;
++counter1;
} else {
++ci;
++counter1;
}
}
isPositive = !isPositive;
}
ci = a[0];
isPositive = false;
for (int(i) = (1); (i) <= (n - 1); ++(i)) {
if (ci == 0) {
ci -= 1;
++counter2;
}
ci += a[i];
if (isPositive && ci > 0) {
counter2 += abs(ci) + 1;
ci -= abs(ci) + 1;
} else if (!isPositive && ci < 0) {
counter2 += abs(ci) + 1;
ci += abs(ci) + 1;
} else if (ci == 0) {
if (isPositive) {
--ci;
++counter2;
} else {
++ci;
++counter2;
}
}
isPositive = !isPositive;
}
cout << min(counter1, counter2) << 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;
long long seq(vector<long long> &a, long long sum, int beg, long long count) {
long long prev;
while (beg < a.size()) {
prev = sum;
sum += a[beg];
if ((sum ^ prev) >= 0LL || sum == 0LL) {
if (prev > 0) {
count += sum + 1;
sum = -1;
} else {
count += 1 - sum;
sum = 1;
}
}
beg++;
}
return count;
}
int main(int argc, char *argv[]) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
long long res;
if (a[0] == 0)
res = max(seq(a, 1, 1, 1), seq(a, -1, 1, 1));
else
res = seq(a, a[0], 1, 0);
std::cout << res << std::endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int signs[2] = {-1, 1};
long long cnt[2] = {0, 0};
for (int i = 0; i < 2; i++) {
long long sum = 0;
int sign = signs[i];
for (int j = 0; j < n; j++) {
sum += a[j];
cout << sum << "->";
if (sum == 0) {
sum += sign;
cnt[i]++;
} else if (sum * sign < 0) {
cnt[i] = cnt[i] + abs(sum) + 1;
sum = sum + sum * (-1) + sign;
}
cout << sum << " " << cnt[i] << endl;
sign *= -1;
}
cout << endl;
}
cout << (cnt[0] < cnt[1] ? cnt[0] : cnt[1]) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
A = list(map(int, input().split()))
sm=A[0]
cnt=0
if A[0]>0:
for i in range(1,N):
sm+=A[i]
if i%2==1:
if sm>=0:
cnt+=sm+1
sm=-1
else:
if sm<=0:
cnt+=sm*-1+1
sm=1
elif A[0]<0:
for i in range(1,N):
sm+=A[i]
if i%2==1:
if sm<=0:
cnt+=sm*-1+1
sm=1
else:
if sm>=0:
cnt+=sm+1
sm=-1
elif A[0]==0:
a=0
for i in range(1,N):
if A[i]==0:
a=i
else:
break
if A[a+1]>0:
sm=-1
for i in range(a+1,N):
sm+=A[i]
if i%2==1:
if sm>=0:
cnt+=sm+1
sm=-1
else:
if sm<=0:
cnt+=sm*-1+1
sm=1
cnt+=2*a+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 | import numpy as np
dummyn = int(input())
a = list(map(int, input().split()))
b = []
for i in range(len(a)):
b.append(sum(a[:i+1]))
b_np = np.array(b)
ans = 0
for i in range(len(b_np)-1):
if b_np[i] * b_np[i+1] >= 0 and b_np[i] <= 0 and b_np[i] <= b_np[i+1]:
ans += (1 - b_np[i+1])
b_np[i+1:] += (1 - b_np[i+1])
elif b_np[i] * b_np[i+1] >= 0 and b_np[i] < 0 and b_np[i] > b_np[i+1]:
ans += (1 - b_np[i])
b_np[i:] += (1 - b_np[i])
elif b_np[i] * b_np[i+1] >= 0 and b_np[i] >= 0 and b_np[i] >= b_np[i+1]:
ans += (b_np[i+1] + 1)
b_np[i+1:] -= (b_np[i+1] + 1)
elif b_np[i] * b_np[i+1] >= 0 and b_np[i] > 0 and b_np[i] < b_np[i+1]:
ans += (b_np[i] + 1)
b_np[i:] -= (b_np[i] + 1)
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
count = 0
sum_ = 0
for i in range(n):
if sum_ * (sum_+a[i]) >=0 and i!=0:
if sum_ > 0:
count += sum_+a[i]+1
a[i] = -sum_-1
elif sum_ < 0:
count += abs(sum_+a[i])+1
a[i] = -sum_+1
sum_ += a[i]
print(count) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <bits/stdc++.h>
int main() {
long long int n, i, a[100000], sum, count = 0, flag = 0;
scanf("%lld", &n);
for (i = 0; i < n; i++) {
scanf("%lld", &a[i]);
if (a[0] == 0 && a[i] != 0 && flag == 0) flag = i;
}
if (flag != 0) {
if ((a[flag] > 0 && flag % 2 == 0) || (a[flag] < 0 && flag % 2 == 1))
a[0] = 1;
else
a[0] = -1;
count++;
}
for (i = 0; i < n; i++) {
if (i == 0)
sum = a[0];
else {
if (sum > 0 && sum + a[i] >= 0) {
count += 1 + sum + a[i];
a[i] = -1 * sum - 1;
sum = -1;
} else if (sum < 0 && sum + a[i] <= 0) {
count += 1 - sum - a[i];
a[i] = -1 * sum + 1;
sum = 1;
} else if (sum + a[i] == 0) {
if (sum > 0)
a[i]--;
else if (sum < 0)
a[i]++;
count++;
sum += a[i];
} else
sum += a[i];
}
}
printf("%lld\n", count);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
A = map(int, input().split())
sum_b = 0
cnt_b = 0
sum_c = 0
cnt_c = 0
for i,a in enumerate(A):
sum_b += a
if (sum_b > 0) != ((i % 2) > 0):
cnt_b += abs(sum_b) + 1
sum_b = 1 if ((i % 2) > 0) else -1
sum_c += a
if (sum_c >= 0) == ((i % 2) > 0):
cnt_c += abs(sum_c) + 1
sum_c = -1 if ((i % 2) > 0) else 1
print(min(cnt_b, cnt_c))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long sum[220220];
int main() {
int n;
while (~scanf("%d", &n)) {
long long x;
memset(sum, 0, sizeof(sum));
for (int i = 1; i <= n; i++) {
scanf("%lld", &x);
sum[i] = sum[i - 1] + x;
}
for (int i = 1; i <= n; i++) printf("sum[%d] is %lld\n", i, sum[i]);
int cnt = 0;
for (int i = 1; i < n; i++) {
if (sum[i] == 0 || sum[i] + cnt == sum[i + 1]) cnt++;
}
if (sum[n] + cnt == 0) cnt++;
printf("%d\n", cnt);
}
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using st = string;
using db = double;
using vll = vector<long long>;
using vvll = vector<vll>;
using vst = vector<st>;
using vchar = vector<char>;
ll mod = 1000000007;
int main() {
ll n;
cin >> n;
vll a(n);
for (auto& i : a) cin >> i;
vll a1 = a, a2 = a;
ll ans1 = 0, ans2 = 0;
ll pointer = 0;
ll sum = 0;
while (pointer < n) {
sum += a1.at(pointer);
if (pointer % 2 == 0) {
if (sum > 0)
pointer++;
else {
ans1 += -1 * sum + 1;
a1.at(pointer) += -1 * sum + 1;
sum += -1 * sum + 1;
pointer++;
}
} else {
if (sum < 0)
pointer++;
else {
ans1 += sum + 1;
a1.at(pointer) -= sum + 1;
sum += sum + 1;
pointer++;
}
}
}
pointer = 0, sum = 0;
while (pointer < n) {
sum += a2.at(pointer);
if (pointer % 2 == 1) {
if (sum > 0)
pointer++;
else {
ans2 += -1 * sum + 1;
a2.at(pointer) += -1 * sum + 1;
sum += -1 * sum + 1;
pointer++;
}
} else {
if (sum < 0)
pointer++;
else {
ans2 += sum + 1;
a2.at(pointer) -= sum + 1;
sum += sum + 1;
pointer++;
}
}
}
cout << min(ans1, ans2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long MOD = (1e+9) + 7;
const long long INF = (long long)1000000007 * 1000000007;
const long double eps = 1e-11;
const long double pi = acos(-1.0);
int dp[500001][4];
int main() {
int n;
cin >> n;
long long a[100000];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long sum = 0;
bool f = true;
long long cnt = 0;
if (a[0] != 0) {
if (a[0] < 0) f = false;
sum = a[0];
for (int i = 1; i <= n - 1; i++) {
sum += a[i];
if (f) {
if (sum >= 0) {
cnt += sum + (long long)1;
sum = -1;
}
f = !f;
} else {
if (sum <= 0) {
cnt += (long long)1 - sum;
sum = 1;
}
f = !f;
}
}
cout << cnt << endl;
} else {
long long mi = INF;
cnt = 1;
sum = 1;
for (int i = 1; i <= n - 1; i++) {
sum += a[i];
if (f) {
if (sum >= 0) {
cnt += sum + (long long)1;
sum = -1;
}
f = !f;
} else {
if (sum <= 0) {
cnt += (long long)1 - sum;
sum = 1;
}
f = !f;
}
}
mi = min(mi, cnt);
cnt = 1;
sum = -1;
f = false;
for (int i = 1; i <= n - 1; i++) {
sum += a[i];
if (f) {
if (sum >= 0) {
cnt += sum + (long long)1;
sum = -1;
}
f = !f;
} else {
if (sum <= 0) {
cnt += (long long)1 - sum;
sum = 1;
}
f = !f;
}
}
mi = min(mi, cnt);
cout << mi << 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 int check(long int sum, long int ans, vector<int> T, int N, bool pre_pm) {
for (int i = 0; i < N; i++) {
if (pre_pm) {
sum += T.at(i);
if (0 <= sum) {
ans += abs(sum + 1);
sum -= abs(sum + 1);
}
pre_pm = false;
} else {
sum += T.at(i);
if (sum <= 0) {
ans += abs(sum + 1);
sum += abs(sum + 1);
}
pre_pm = true;
}
}
return ans;
}
int main() {
int N;
vector<int> T;
cin >> N;
for (int i = 0; i < N; i++) {
int tmp;
cin >> tmp;
T.push_back(tmp);
}
long int ans = 0;
long int sum = 0;
bool pre_pm;
cout << min(check(sum, ans, T, N, true), check(sum, ans, T, N, false))
<< 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 a[100010];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int count = 0;
int c;
int ans1 = 0;
int old_a;
for (int i = 0; i < n; i++) {
c = count + a[i];
if (count > 0 && c >= 0) {
ans1 += c - (-1);
a[i] -= c - (-1);
c = count + a[i];
if (c == 0) {
a[i]--;
}
} else if (count < 0 && c <= 0) {
ans1 += 1 - c;
a[i] += 1 - c;
c = count + a[i];
if (c == 0) {
a[i]++;
}
}
count += a[i];
}
cout << ans1 << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long int LINF = 2000000000000000000ll;
const int INF = 1000000100;
const long long int MOD = 1e9 + 7;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < (n); ++i) cin >> a[i];
int ans = INF;
int ans_t, total, sign;
for (int i = 0; i < (2); ++i) {
ans_t = 0;
total = 0;
if (i == 0)
sign = true;
else
sign = false;
for (int i = 0; i < (n); ++i) {
total += a[i];
if (sign) {
while (total >= 0) {
total--;
ans_t++;
}
} else {
while (total <= 0) {
total++;
ans_t++;
}
}
sign = 1 - sign;
}
ans = min(ans, ans_t);
}
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>
long long a[100000 + 100], b[100000 + 100];
int main() {
int n;
long long cnt = 0;
scanf("%d", &n);
scanf("%lld", &a[1]);
b[1] = a[1];
for (int i = 2; i <= n; i++) {
scanf("%lld", &a[i]);
if (b[i - 1] < 0) {
if (b[i - 1] + a[i] > 0)
b[i] = b[i - 1] + a[i];
else {
b[i] = 1;
cnt += 1 - a[i] - b[i - 1];
}
} else if (b[i - 1] > 0) {
if (b[i - 1] + a[i] < 0)
b[i] = b[i - 1] + a[i];
else {
b[i] = -1;
cnt += b[i - 1] + a[i] + 1;
}
}
}
printf("%lld\n", cnt);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int 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;
if (a[2] > 0)
a[1] = -1;
else
a[1] = 1;
}
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] = -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;
}
}
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;
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < (int)(n); i++) cin >> a[i];
int res1 = 0, res2 = 0;
int sum1 = 0, sum2 = 0;
for (int i = 0; i < (int)(n); i++) {
sum1 += a[i];
if (i % 2 == 1 && sum1 <= 0) {
res1 += (1 - sum1);
sum1 = 1;
} else if (i % 2 == 0 && sum1 >= 0) {
res1 += (1 + sum1);
sum1 = -1;
}
}
for (int i = 0; i < (int)(n); i++) {
sum2 += a[i];
if (i % 2 == 1 && sum2 >= 0) {
res2 += (1 + sum2);
sum2 = -1;
} else if (i % 2 == 0 && sum2 <= 0) {
res2 += (1 - sum2);
sum2 = 1;
}
}
cout << min(res1, res2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int,input().split()))
ans = 0
for i in range(n):
if i == 0:
if a[i] == 0:
f = "+"
a[i] = 1
elif a[0] > 0:
f = "+"
elif a[0] < 0:
f = "-"
else:
if f == "+":
if a[i] + sum(a[:i]) > 0:
c = -1 - sum(a[:i])
ans += abs(c - a[i])
a[i] = c
f = "-"
else:
if a[i] + sum(a[:i]) == 0:
a[i] -= 1
ans += 1
f = "-"
elif f == "-":
if a[i] + sum(a[:i]) < 0:
c = 1 - sum(a[:i])
ans += abs(c - a[i])
a[i] = c
f = "+"
else:
if a[i] + sum(a[:i]) == 0:
a[i] += 1
ans += 1
f = "+"
#print(a)
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long INF = 2e18;
const long long MOD = 1e9 + 7;
long long N;
long long a[100010];
long long Calc() {
long long ret = 0;
long long sum = a[0];
long long preSum;
for (long long i = 1; i < N; i++) {
preSum = sum;
sum += a[i];
if (sum == 0 || ((preSum < 0) ^ (sum > 0))) {
if (preSum > 0) {
ret += abs(-1 - sum);
sum = -1;
} else {
ret += abs(1 - sum);
sum = 1;
}
}
}
return ret;
}
int main() {
cin >> N;
for (long long i = 0; i < N; i++) cin >> a[i];
if (a[0] == 0) {
a[0] = 1;
long long tmp = Calc();
a[0] = -1;
cout << min(tmp, Calc()) << endl;
return 0;
}
cout << Calc() << 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())
src = list(map(int,input().split()))
cum1 = cum2 = src[0]
ans1 = ans2 = 0
for i,a in enumerate(src[1:]):
cum1 += a
cum2 += a
if i%2:
if cum1 >= 0:
ans1 += cum1+1
cum1 = -1
if cum2 <= 0:
ans2 += 1-cum2
cum2 = 1
else:
if cum1 <= 0:
ans1 += 1-cum1
cum1 = 1
if cum2 >= 0:
ans2 += cum2+1
cum2 = -1
print(min(ans1,ans2))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int INF = (long long int)(1 << 30) - 1;
const long long int INFl = (long long int)9223372036854775807;
const int MAX = 10000;
const long long int MOD = (long long int)1e9 + 7;
long long int gcd(long long int a, long long int b) {
return b ? gcd(b, a % b) : a;
}
long long int lcm(long long int a, long long int b) {
return a / gcd(a, b) * b;
}
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int n;
int a[100100];
int b[100100];
long long int sum[100100];
long long int sumb[100100];
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
b[i] = a[i];
}
sum[0] = a[0];
sumb[0] = a[0];
for (int i = 1; i < n; i++) {
sum[i] = sum[i - 1] + a[i];
sumb[i] = sum[i];
}
long long int ansa = 0, ansb = 0;
long long int ta = 0;
for (int i = 0; i < n; i++) {
sum[i] += ta;
if (i % 2 == 0) {
if (sum[i] <= 0) {
int t = -1 * sum[i] + 1;
ta = t;
ansa += abs(t);
}
} else {
if (sum[i] >= 0) {
int t = sum[i] + 1;
ta -= t;
ansa += abs(t);
}
}
}
ta = 0;
for (int i = 0; i < n; i++) {
sumb[i] += ta;
if (i % 2 == 1) {
if (sumb[i] <= 0) {
int t = -1 * sumb[i] + 1;
ta = t;
ansb += abs(t);
}
} else {
if (sumb[i] >= 0) {
int t = sumb[i] + 1;
ta -= t;
ansb += abs(t);
}
}
}
cout << ((ansa) < (ansb) ? (ansa) : (ansb)) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long a[100010];
int main() {
int n, i, j, k;
long long s, x, y, s1, s2;
while (scanf("%d", &n) != EOF) {
for (i = 0; i < n; i++) scanf("%lld", &a[i]);
if (a[0] != 0) {
s = 0;
x = a[0];
for (i = 1; i < n; i++) {
y = x;
x = x + a[i];
if (x < 0 && y > 0) continue;
if (y < 0 && x > 0) continue;
if (y < 0) {
s = s - x + 1;
x = 1;
} else if (y > 0) {
s = s + x + 1;
x = -1;
}
}
printf("%lld\n", s);
continue;
} else {
s1 = 1;
x = 1;
for (i = 1; i < n; i++) {
y = x;
x = x + a[i];
if (x < 0 && y > 0) continue;
if (y < 0 && x > 0) continue;
if (y < 0) {
s1 = s1 - x + 1;
x = 1;
} else if (y > 0) {
s1 = s1 + x + 1;
x = -1;
}
}
s2 = 1;
x = -1;
for (i = 1; i < n; i++) {
y = x;
x = x + a[i];
if (x < 0 && y > 0) continue;
if (y < 0 && x > 0) continue;
if (y < 0) {
s2 = s2 - x + 1;
x = 1;
} else if (y > 0) {
s2 = s2 + x + 1;
x = -1;
}
}
s = min(s1, s2);
printf("%lld\n", s);
continue;
}
}
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int ans = 0;
int sum = 0;
for (int i = 0; i < n; i++) {
if (i == 0) {
sum = a[i];
continue;
}
if (sum > 0) {
if (sum + a[i] >= 0) {
while (sum + a[i] > -1) {
a[i]--;
ans++;
}
}
} else {
if (sum + a[i] <= 0) {
while (sum + a[i] < 1) {
a[i]++;
ans++;
}
}
}
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 | java | import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.*;
public class Main {
static InputStream is;
static PrintWriter out;
static String INPUT = "";
static void solve() {
int n = ni();
int[] a = na(n);
int[] sum = new int[n];
sum[0] = a[0];
int total = 0;
for (int i = 1; i < n; i++) {
int cur = a[i];
if (sum[i - 1] < 0) {
if (cur + sum[i - 1] > 0) {
sum[i] = cur + sum[i - 1];
} else {
total += 1 - (cur + sum[i - 1]);
sum[i] = 1;
}
} else {
if (cur + sum[i - 1] >= 0) {
total += (cur + sum[i - 1]) + 1;
sum[i] = -1;
} else {
sum[i] = cur + sum[i - 1];
}
}
}
System.out.println(total);
}
public static void main(String[] args) throws Exception {
long S = System.currentTimeMillis();
is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
solve();
out.flush();
long G = System.currentTimeMillis();
tr(G-S+"ms");
}
private static boolean eof() {
if(lenbuf == -1)return true;
int lptr = ptrbuf;
while(lptr < lenbuf)if(!isSpaceChar(inbuf[lptr++]))return false;
try {
is.mark(1000);
while(true){
int b = is.read();
if(b == -1){
is.reset();
return true;
}else if(!isSpaceChar(b)){
is.reset();
return false;
}
}
} catch (IOException e) {
return true;
}
}
private static byte[] inbuf = new byte[1024];
static int lenbuf = 0, ptrbuf = 0;
private static int readByte() {
if(lenbuf == -1)throw new InputMismatchException();
if(ptrbuf >= lenbuf){
ptrbuf = 0;
try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
if(lenbuf <= 0)return -1;
}
return inbuf[ptrbuf++];
}
private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
private static int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
private static double nd() { return Double.parseDouble(ns()); }
private static char nc() { return (char)skip(); }
private static String ns() {
int b = skip();
StringBuilder sb = new StringBuilder();
while(!(isSpaceChar(b))){
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private static char[] ns(int n) {
char[] buf = new char[n];
int b = skip(), p = 0;
while(p < n && !(isSpaceChar(b))){
buf[p++] = (char)b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private static char[][] nm(int n, int m) {
char[][] map = new char[n][];
for(int i = 0;i < n;i++)map[i] = ns(m);
return map;
}
private static int[] na(int n) {
int[] a = new int[n];
for(int i = 0;i < n;i++)a[i] = ni();
return a;
}
private static int ni() {
int num = 0, b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private static long nl() {
long num = 0;
int b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); }
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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 init1 = if(a.head < 0) 1 - a.head else 0
val ans1 = a.tail./:(a.head + init1, init1.abs)((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 init2 = if(a.head < 0) 0 else -1 - a.head
val ans2 = a.tail./:(a.head + init2, init2.abs)((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 keta(int num) {
int ans = 0;
int rem;
for (int i = 4; i >= 0; i--) {
rem = pow(10, i);
ans += (num / rem);
num = num % rem;
}
return ans;
}
int main() {
int n;
cin >> n;
vector<int64_t> ar(n);
for (int i = 0; i < n; i++) {
cin >> ar[i];
}
int ans = 0;
int cum = ar[0];
for (int i = 1; i < n; i++) {
int next = cum + ar[i];
if (cum > 0 && next >= 0) {
ans += abs(next - (-1));
cum = -1;
} else if (cum < 0 && next <= 0) {
ans += abs(next - 1);
cum = 1;
} else {
cum = next;
}
}
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 | {-# OPTIONS_GHC -O2 -funbox-strict-fields #-}
{-# OPTIONS_GHC -fno-warn-unused-imports #-}
{-# OPTIONS_GHC -fno-warn-missing-signatures #-}
{-# OPTIONS_GHC -fno-warn-unused-binds #-}
{-# OPTIONS_GHC -fno-warn-incomplete-patterns #-}
{-# OPTIONS_GHC -fno-warn-name-shadowing #-}
{-# LANGUAGE OverloadedStrings #-}
-- {-# LANGUAGE FlexibleContexts #-}
-- {-# LANGUAGE MultiWayIf #-}
-- {-# LANGUAGE BangPatterns #-}
-- {-# LANGUAGE ViewPatterns #-}
-- {-# LANGUAGE TupleSections #-}
import System.IO hiding (char8)
import Control.Applicative
import Control.Monad
import Data.List
import Data.Tuple
import Data.Int
import Data.Char
import Data.Function (on)
import Data.Ord (comparing)
import Data.Monoid (mappend)
import Data.Array
-- import Data.Array.Unboxed
-- import Data.Array.IArray
-- import Data.Array.ST
-- import Data.Array.MArray
-- import Data.Array.Unsafe
-- import Data.Array.Base(unsafeRead, unsafeWrite)
-- import Data.Array.IO
import Data.Ix
import Data.Maybe
-- import Data.Monoid hiding ((<>))
import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString.Lazy.Char8 as BL
import Data.ByteString.Builder
-- import Data.Graph
-- import Data.Vector.Unboxed ((//), (++), (!), (!?))
-- import qualified Data.Vector.Unboxed as U
-- import Data.IntSet (IntSet)
-- import qualified Data.IntSet as IntSet
-- import Data.IntMap.Strict (IntMap)
-- import qualified Data.IntMap.Strict as IntMap
-- import Data.Sequence ((|>), (<|), (><),ViewR(..), ViewL(..), Seq)
-- import qualified Data.Sequence as Seq
-- import Data.Foldable (toList, minimumBy)
-- import Debug.Trace
main = solve <$ getInt1 <*> getIntegers >>= print
solve = solve' 0 0 . scanl (+) 0 where
solve' _ ans [_] = ans
solve' acc ans (x:x2:xs)
| x2+acc == 0 = solve' (acc-signum x) (ans+1) (x2+acc-signum x:xs)
| signum x == signum (x2+acc) = solve' (acc+d) (ans+abs d) (x2+acc+d:xs)
| otherwise = solve' acc ans (x2+acc:xs)
where
d = - signum (x2+acc) * (abs (x2+acc) + 1)
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
getIntegers :: IO [Integer]
getIntegers = readIntegers <$> BS.getLine
getInt1 :: IO Int
getInt1 = readInt1 <$> BS.getLine
getInt2 :: IO (Int, Int)
getInt2 = readInt2 <$> BS.getLine
getInt3 :: IO (Int, Int, Int)
getInt3 = readInt3 <$> BS.getLine
getInts :: IO [Int]
getInts = readInts <$> BS.getLine
getIntN :: Int -> IO [Int]
getIntN n = map readInt1 <$> replicateM n BS.getLine
format :: Show a => Maybe a -> IO ()
format Nothing = putStrLn "NO"
format (Just a) = putStrLn "YES" >> print a
-- [1,2,3] -> 1 2 3
putInts :: [Int] -> IO ()
-- putInts [] = return ()
-- putInts xs = BL.putStrLn . toLazyByteString . foldl1 mappend . intersperse (char8 ' ') $ map intDec xs
putInts = putStrLn . unwords . map show
readInt1 :: BS.ByteString -> Int
readInt1 = fst . fromJust . BS.readInt
readInt2 :: BS.ByteString -> (Int,Int)
readInt2 = toTuple . readInts
readInt3 :: BS.ByteString -> (Int,Int,Int)
readInt3 = toTriple . readInts
readInts :: BS.ByteString -> [Int]
readInts = map readInt1 . BS.words
readInt641 :: BS.ByteString -> Int64
readInt641 = fromIntegral . fst . fromJust . BS.readInteger
readInt642 :: BS.ByteString -> (Int64,Int64)
readInt642 = toTuple . readInt64s
readInt643 :: BS.ByteString -> (Int64,Int64,Int64)
readInt643 = toTriple . readInt64s
readInt64s :: BS.ByteString -> [Int64]
readInt64s = map readInt641 . BS.words
readInteger1 :: BS.ByteString -> Integer
readInteger1 = fst . fromJust . BS.readInteger
readInteger2 :: BS.ByteString -> (Integer,Integer)
readInteger2 = toTuple . readIntegers
readInteger3 :: BS.ByteString -> (Integer,Integer,Integer)
readInteger3 = toTriple . readIntegers
readIntegers :: BS.ByteString -> [Integer]
readIntegers = map readInteger1 . BS.words
toTuple :: [a] -> (a, a)
toTuple [x, y] = (x, y)
toTriple :: [a] -> (a, a, a)
toTriple [x, y, z] =(x, y, z)
fromTuple :: (a, a) -> [a]
fromTuple (x, y) = [x, y]
fromTriple :: (a, a, a) -> [a]
fromTriple (x, y, z) = [x, y, z]
-- if not applying, use "id"
applyTuple :: (a -> a') -> (b -> b') -> (a, b) -> (a', b')
applyTuple f g (x, y) = (f x, g y)
applyTriple :: (a -> a') -> (b -> b') -> (c -> c') -> (a, b, c) -> (a', b', c')
applyTriple f g h (x, y, z) = (f x, g y, h z)
-- @since 4.8.0.0
sortOn' :: Ord b => (a -> b) -> [a] -> [a]
sortOn' f =
map snd . sortBy (comparing fst) . map (\x -> let y = f x in y `seq` (y, x))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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 N = 123456;
long long a[N];
signed main() {
long long n;
scanf("%lld", &n);
for (long long i = 0; i < n; i++) {
scanf("%lld", &a[i]);
}
long long h = 0;
long long s = a[0];
long long q = 0;
if (s > 0)
q = 1;
else
q = 0;
for (long long i = 1; i < n; i++) {
s += a[i];
if (s == 0) {
if (q == 0) {
h++;
a[i]++;
q = 1;
} else {
h++;
a[i]--;
q = 1;
}
} else if (s < 0) {
long long foo = abs(s);
if (q == 0) {
h += (foo + 1);
a[i] += (foo + 1);
s += (foo + 1);
q = 1;
} else {
q = 0;
}
} else {
long long foo = (s);
if (q == 1) {
h += (foo + 1);
a[i] -= (foo + 1);
s -= (foo + 1);
q = 0;
} else {
q = 1;
}
}
}
printf("%lld\n", h);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <bits/stdc++.h>
int main() {
int n;
scanf("%d", &n);
long long a[n];
for (int i = 0; i < n; i++) scanf("%lld", a + i);
long long initial_plus = ((-1 - a[0]) > (0) ? (-1 - a[0]) : (0));
long long initial_minus = ((1 + a[0]) > (0) ? (1 + a[0]) : (0));
long long sum = 0;
sum = a[0] + initial_plus;
for (int i = 1; i < n; i++) {
if ((sum + a[i]) * sum >= 0ll) {
if (sum < 0ll) {
initial_plus += 1 - sum - a[i];
sum = 1;
} else {
initial_plus += sum + a[i] + 1;
sum = -1;
}
} else {
sum += a[i];
}
}
sum = a[0] - initial_minus;
for (int i = 1; i < n; i++) {
if ((sum + a[i]) * sum >= 0ll) {
if (sum < 0ll) {
initial_minus += 1 - sum - a[i];
sum = 1;
} else {
initial_minus += sum + a[i] + 1;
sum = -1;
}
} else {
sum += a[i];
}
}
printf("%lld\n",
((initial_plus) > (initial_minus) ? (initial_minus) : (initial_plus)));
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 itertools
def main():
N = int(input())
A = tuple(map(int, input().split()))
a = list(itertools.accumulate(A))
even, odd = 0, 0
for i, v in enumerate(a):
if i % 2:
odd += v
else:
even += v
if even > odd:
func = (plus, minus)
else:
func = (minus, plus)
cnt = 0
add_ = 0
for i, v in enumerate(a):
ret = func[i%2](v + add_)
add_ += ret
cnt += abs(ret)
print(cnt)
def plus(n):
if n <= 0:
x = 1 - n
return x
else:
return 0
def minus(n):
if n >= 0:
x = -1 - n
return x
else:
return 0
main()
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
typedef vector<vector<int> > vii;
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
long long n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < (int)n; i++) cin >> a[i];
long long sum = a[0], op_cnt = 0, op_cnt1 = 0, op_cnt2 = 0;
for (int i = (int)1; i < (int)n; i++) {
if (sum < 0 && sum + a[i] <= 0) {
op_cnt += (0 - sum - a[i]) + 1;
sum = 1;
} else if (sum >= 0 && sum + a[i] >= 0) {
op_cnt += sum + a[i] + 1;
sum = -1;
} else
sum += a[i];
}
sum = 1;
op_cnt1 += abs(a[0] - 1);
for (int i = (int)1; i < (int)n; i++) {
if (sum < 0 && sum + a[i] <= 0) {
op_cnt1 += (0 - sum - a[i]) + 1;
sum = 1;
} else if (sum >= 0 && sum + a[i] >= 0) {
op_cnt1 += sum + a[i] + 1;
sum = -1;
} else
sum += a[i];
}
sum = -1;
op_cnt2 = abs(a[0] + 1);
for (int i = (int)1; i < (int)n; i++) {
if (sum < 0 && sum + a[i] <= 0) {
op_cnt2 += (0 - sum - a[i]) + 1;
sum = 1;
} else if (sum >= 0 && sum + a[i] >= 0) {
op_cnt2 += sum + a[i] + 1;
sum = -1;
} else
sum += a[i];
}
cout << min(op_cnt, min(op_cnt1, op_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;
int main() {
int n, i, j;
vector<int> a;
int sum, count, count2;
cin >> n;
a.resize(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
count = 0;
count2 = 0;
sum = 0;
if (a[0] <= 0) {
sum = 1;
count += abs(a[0]) + 1;
} else {
sum = a[0];
}
for (int i = 0; i < n - 1; i++) {
if (sum + a[i + 1] == 0) {
count++;
if (sum < 0)
sum = 1;
else
sum = -1;
} else if (sum < 0 && (sum + a[i + 1]) < 0) {
j = 1 - sum;
count += j - a[i + 1];
sum = 1;
} else if (sum > 0 && (sum + a[i + 1]) > 0) {
j = -1 - sum;
count += abs(j - a[i + 1]);
sum = -1;
} else {
sum += a[i + 1];
}
}
if (a[0] >= 0) {
sum = -1;
count2 += abs(a[0]) + 1;
} else {
sum = a[0];
}
for (int i = 0; i < n - 1; i++) {
if (sum + a[i + 1] == 0) {
count2++;
if (sum < 0)
sum = 1;
else
sum = -1;
} else if (sum < 0 && (sum + a[i + 1]) < 0) {
j = 1 - sum;
count2 += j - a[i + 1];
sum = 1;
} else if (sum > 0 && (sum + a[i + 1]) > 0) {
j = -1 - sum;
count2 += abs(j - a[i + 1]);
sum = -1;
} else {
sum += a[i + 1];
}
}
cout << min(count, count2);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
long long sum;
cin >> sum;
long long delta = 0;
for (int i = 1; i < N; ++i) {
long long temp;
cin >> temp;
if (sum > 0 && sum + temp >= 0) {
delta += sum + temp + 1;
sum = -1;
} else if (sum < 0 && sum + temp <= 0) {
delta += 1 - (sum + temp);
sum = 1;
} else {
sum += temp;
}
}
cout << delta;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
sum = a[0]
change = 0
for i in range(1, n):
val = 0
tempsum = sum + a[i]
if sum < 0 and tempsum <= 0:
val = 1 - tempsum
if sum > 0 and tempsum >= 0:
val = -1 - tempsum
if sum == 0 and a[i] > 0:
val = -1
if sum == 0 and a[i] < 0:
val = 1
sum = tempsum + val
change += abs(val)
print(change)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.next());
ArrayList<Integer> a = new ArrayList<>();
for(int i=0; i<n; i++){
a.add(Integer.parseInt(sc.next()));
}
int ans1 = 0;
int sum1 = 0;
int sign1 = 1;
for(int i=0; i<n; i++){
sum1 += a.get(i);
if (sum1 * sign1 <= 0){
ans1 += Math.abs(sum1) + 1;
sum1 = sign1;
}
sign1 *= -1;
}
int ans2 = 0;
int sum2 = 0;
int sign2 = -1;
for(int i=0; i<n; i++){
sum2 += a.get(i);
if (sum2 * sign2 <= 0){
ans2 += Math.abs(sum2) + 1;
sum2 = sign2;
}
sign2 *= -1;
}
System.out.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 | UNKNOWN | n = gets.to_i
as = gets.split(' ').map { |e| e.to_i }
x = 0
bs = []
as.each { |e|
x += e
bs << x
}
# p bs
memo = 0
ans = 0
for i in (1..(n - 1))
a, b = bs[i - 1], bs[i]
b += memo
if a >= 0 && b >= 0
d = b + 1
memo -= d
ans += d
elsif a <= 0 && b <= 0
d = -1 * b + 1
memo += d
ans += d
end
end
ans += 1 if bs[n-1] == 0
puts ans |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<long long> L(N);
for (int i = 0; i < N; i++) {
cin >> L.at(i);
}
long long v = 0, res = 0, le = 0;
bool change_flag = true;
for (int i = 0; i < N; i++) {
if (i == 0) {
v = L.at(i);
} else {
if (v > 0 && v + L.at(i) >= 0) {
le = -1 - v - L.at(i);
L.at(i) -= abs(le);
v += L.at(i);
res += -le;
le = 0;
} else if (v < 0 && v + L.at(i) <= 0) {
le = 1 - v - L.at(i);
L.at(i) += abs(le);
v += L.at(i);
res += le;
le = 0;
} else {
v += L.at(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 | python3 | n=int(input())
a=list(map(int,input().split()))
cnt1,cnt2,sm1,sm2=0,0,0,0
for i,val in enumerate(a):#+
sm1+=val
if i%2 ==0:
if sm1<1:
cnt1+=1-sm1
sm1=1
else:
if sm1>-1:
cnt1+=1+sm1
sm1=-1
for i,val in enumerate(a):#-
sm2+=val
if i%2 ==0:
if sm2>-1:
cnt2+=1+sm2
sm2=-1
else:
if sm2<1:
cnt2+=1-sm2
sm2=1
print(min(cnt1,cnt2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long up = 0;
long long down = 0;
long long cu = 0;
long long cd = 0;
for (int i = 0; i < n; i++) {
long long t;
cin >> t;
cu += t;
cd += t;
if (i % 2 == 0 && cu <= 0) {
up += abs(cu - 1);
cu = 1;
} else if (i % 2 == 1 && cu > 0) {
up += abs(cu + 1);
cu = -1;
}
if (i % 2 == 1 && cd <= 0) {
down += abs(cd - 1);
cd = 1;
} else if (i % 2 == 0 && cd > 0) {
down += abs(cd + 1);
cd = -1;
}
}
cout << min(up, down) << 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 n;
long long a[100000];
long long sum[100000];
bool eqsig(long long a, long long b) {
if (a < 0 && b > 0) return true;
if (a > 0 && b < 0) return true;
return false;
}
int solve() {
int ans = 0;
for (int i = 0; i < n - 1; i++) {
sum[i + 1] = sum[i] + a[i + 1];
if (eqsig(sum[i], sum[i + 1])) continue;
if (sum[i] < 0) {
ans -= (sum[i + 1] - 1);
sum[i + 1] = 1;
}
if (sum[i] > 0) {
ans += (sum[i + 1] + 1);
sum[i + 1] = -1;
}
}
return ans;
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
sum[0] = a[0];
int aa, bb;
if (a[0] == 0) {
sum[0] = 1;
aa = solve() + 1;
sum[0] = -1;
bb = solve() + 1;
} else if (a[0] > 0) {
aa = solve();
sum[0] = -1;
bb = solve() + a[0] + 1;
} else if (a[0] < 0) {
aa = solve();
sum[0] = 1;
bb = solve() - (a[0] - 1);
}
cout << min(aa, bb) << 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>
int N;
std::vector<long long int> A;
bool bxor(bool a, bool b) { return !a != !b; }
long long int solve(bool f) {
long long int sum = 0;
long long int ans = 0;
for (long i = 0; i < (N); ++i) {
sum += A[i];
if (!bxor(bxor(f, i % 2 == 0), sum > 0)) {
long long int dist = bxor(f, i % 2 == 0) ? (-1) : 1;
if (sum == 0) {
ans += 1;
sum = dist;
} else if (dist != (sum / std::abs(sum))) {
ans += std::abs(sum - dist);
sum = dist;
}
}
}
return ans;
}
int main() {
std::cin >> N;
A.resize(N);
for (long i = 0; i < (N); ++i) {
std::cin >> A[i];
}
std::cout << std::min(solve(true), solve(false)) << std::endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
const int LG = 21;
const int FN = 400005;
const long long MOD = 1e9 + 7;
const long long INF = 1e9;
const long long INFLL = 1e18;
using namespace std;
int cx[4] = {-1, 0, 1, 0};
int cy[4] = {0, -1, 0, 1};
long long inq(long long x, long long y) {
if (!y) return 1 % MOD;
long long l = inq(x, y / 2);
if (y % 2) return l * l % MOD * x % MOD;
return l * l % MOD;
}
long long rev(long long x) { return inq(x, MOD - 2); }
bool __precomputed_combinatorics = 0;
vector<long long> __fact, __ufact, __rev;
void __precompute_combinatorics() {
__precomputed_combinatorics = 1;
__fact.resize(FN);
__ufact.resize(FN);
__rev.resize(FN);
__rev[1] = 1;
for (int i = 2; i < FN; i++)
__rev[i] = MOD - __rev[MOD % i] * (MOD / i) % MOD;
__fact[0] = 1, __ufact[0] = 1;
for (int i = 1; i < FN; i++)
__fact[i] = __fact[i - 1] * i % MOD,
__ufact[i] = __ufact[i - 1] * __rev[i] % MOD;
}
long long fact(int x) {
if (!__precomputed_combinatorics) __precompute_combinatorics();
return __fact[x];
}
long long cnk(int n, int k) {
if (k < 0 || k > n) return 0;
if (!__precomputed_combinatorics) __precompute_combinatorics();
return __fact[n] * __ufact[n - k] % MOD * __ufact[k] % MOD;
}
int Root(int x, vector<int> &root) {
if (x == root[x]) return x;
return root[x] = Root(root[x], root);
}
void Merge(int v, int u, vector<int> &root, vector<int> &sz) {
v = Root(v, root), u = Root(u, root);
if (v == u) return;
if (sz[v] < sz[u]) {
sz[u] += sz[v];
root[v] = u;
} else {
sz[v] += sz[u];
root[u] = v;
}
}
int ok(int x, int n) { return 0 <= x && x < n; }
const int N = 400000;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n;
cin >> n;
vector<int> a(n);
for (int(i) = 0; (i) != (n); (i)++) cin >> a[i];
long long p = 0, cur;
long long r1 = 0, r2 = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
cur = p + a[i];
if (cur <= 0) r1 += abs(1 - cur), cur = 1;
} else {
cur = p + a[i];
if (cur >= 0) r1 += abs(cur + 1), cur = 1;
}
p = cur;
}
p = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 1) {
cur = p + a[i];
if (cur <= 0) r2 += abs(1 - cur), cur = 1;
} else {
cur = p + a[i];
if (cur >= 0) r2 += abs(cur + 1), cur = 1;
}
p = cur;
}
cout << min(r1, r2);
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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>
void drop(const T &x) {
cout << x << endl;
exit(0);
}
void solve() {
int n;
cin >> n;
int a;
vector<int> sum(2), cnt(2);
for (int i = 0; i < n; ++i) {
cin >> a;
for (int j : {0, 1}) {
sum[j] += a;
int d = 1 - (i + j) % 2 * 2;
if (sum[j] * d <= 0) {
cnt[j] += abs(d - sum[j]);
sum[j] = d;
}
}
}
cout << min(cnt[0], cnt[1]) << '\n';
return;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int T = 1;
while (T--) solve();
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
int n;
cin >> n;
int a[100010];
for (int i = 0; i < n; ++i) cin >> a[i];
int cur1 = 0;
int cur2 = 0;
int ans1 = 0;
int ans2 = 0;
for (int i = 0; i < n; ++i) {
cur1 += a[i];
if (i % 2 == 0) {
if (cur1 <= 0) {
ans1 = ans1 - cur1 + 1;
cur1 = 1;
}
}
if (i % 2 == 1) {
if (cur1 >= 0) {
ans1 = ans1 + cur1 + 1;
cur1 = -1;
}
}
}
for (int i = 0; i < n; ++i) {
cur2 += a[i];
if (i % 2 == 1) {
if (cur2 <= 0) {
ans2 = ans2 - cur2 + 1;
cur2 = 1;
}
}
if (i % 2 == 0) {
if (cur2 >= 0) {
ans2 = ans2 + cur2 + 1;
cur2 = -1;
}
}
cout << cur2 << endl;
}
cout << ans1 << ' ' << ans2 << endl;
cout << min(ans1, ans2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import numpy as np
n = int(input())
L = np.array([int(i) for i in input().split()])
count = 0
s = L[0]
#if L[0] == 0:
# if L[1] > 0:
# L[0] = -1
# else:
# L[0] = 1
# count += 1
print(L)
loopnum = n//2
if n%2 == 0:
loopnum -= 1
#+-+-...
for i in range(loopnum):
s = s + L[2*i+1]
if s >= 0:
subt = s + 1
count += subt
s = s - subt
s = s + L[2*i+2]
if s <= 0:
subt = s - 1
count -= subt
s = s - subt
if n%2 == 0:
s = s + L[-1]
if s >= 0:
subt = s + 1
count += subt
cand1 = count
count = 0
s = L[0]
#-+-+...
for i in range(loopnum):
s = s + L[2*i+1]
if s <= 0:
subt = s - 1
count -= subt
s = s - subt
s = s + L[2*i+2]
if s >= 0:
subt = s + 1
count += subt
s = s - subt
if n%2 == 0:
s = s + L[-1]
if s <= 0:
subt = s - 1
count -= subt
cand2 = count
#print(cand1)
#print(cand2)
print(min(cand1,cand2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
cnt=0
for i in range(1,n):
# 条件満たすまでループ
for _ in range(3):
#print(a)
now_tmp = sum(a[:i])
next_tmp = sum(a[:i+1])
#print(i, now_tmp, next_tmp)
# 符号が逆転していればOK かつ 現在までの総和が0でない
# 異なる符号を掛けるとマイナスになる
if now_tmp * next_tmp <0 and now_tmp !=0:
break
else:
# 現在の合計がマイナスの場合
if now_tmp < 0:
a[i] += next_tmp+1
cnt +=abs(next_tmp+1)
# 現在の合計がプラスの場合
elif now_tmp > 0 :
a[i] += -next_tmp-1
cnt +=abs(next_tmp+1)
# 現在の合計が0の場合
elif now_tmp == 0 :
# 1個前がプラスの場合、
if sum(a[:i-1]) > 0:
a[i] += -next_tmp+1
cnt +=abs(next_tmp+1)
# 1個前がマイナスの場合
else:
a[i] += next_tmp+1
cnt +=abs(next_tmp+1)
print(cnt) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n;
int a[100001];
int main() {
int cul;
int ans = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
cul = a[0];
for (int i = 1; i < n; i++) {
if (cul > 0) {
while ((a[i] + cul) >= 0) {
ans++;
a[i]--;
}
cul += a[i];
} else {
while ((a[i] + cul) <= 0) {
ans++;
a[i]++;
}
cul += a[i];
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | 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 ans1 = 0, ans2 = 0, sum = 0;
for (int i = 0; i < N; i++) {
sum += a.at(i);
if (i % 2 == 0 && sum <= 0) {
ans1 += 1 - sum;
sum = 1;
} else if (i % 2 == 1 && sum >= 0) {
ans1 += sum + 1;
sum = -1;
}
}
sum = 0;
for (int i = 0; i < N; i++) {
sum += a.at(i);
if (i % 2 == 0 && sum >= 0) {
ans2 += sum + 1;
sum = -1;
} else if (i % 2 == 1 && sum <= 0) {
ans2 += 1 - sum;
sum = 1;
}
}
int ans = min(ans1, ans2);
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 | def c(ints):
for i in range(len(ints)):
if ints[i] != 0:
sig = 1 if ints[i] > 0 else -1
sig_ = -sig
total = ints[i]
total_ = -sig
mov = i
mov_ = abs(total) + 1
if i > 0:
mov += 1 + i - 1
mov_ += 2 + i - 1
j = i
break
if i == len(ints) - 1:
return i * 2 + 1
for i_ in ints[j+1:]:
tmp = total + i_
tmp_ = total_ + i_
if tmp == 0:
mov +=1
tmp = -sig
elif sig * tmp > 0:
mov += abs(tmp) + 1
tmp = -sig
if tmp_ == 0:
mov_ +=1
tmp_ = -sig_
elif sig_ * tmp_ > 0:
mov_ += abs(tmp_) + 1
tmp_ = -sig_
sig *= -1
total = tmp
sig_ *= -1
total_ = tmp_
return min(mov, mov_)
_ = input()
inp = input()
inp = inp.split(' ')
inp = [int(i_) for i_ in inp]
print(c(inp)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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 | #C
N = int(input())
A = list(map(int,input().split()))
SUM = 0
ans = 0
flag = 0 #0+,1-
for i in range(N):
if i == 0:
SUM = A[i]
if SUM == 0:
SUM+=1
ans+=1
elif SUM < 0:
flag = 1
else:
SUM += A[i]
if flag == 0:
if SUM >= 0:
ans += SUM+1
SUM = -1
flag = 1
else:
if SUM <= 0:
ans += 1-SUM
SUM = 1
flag = 0
#print(ans,SUM)
MIN = ans
ans = 0
flag = 1 #0+,1-
for i in range(N):
if i == 0:
SUM = A[i]
if SUM == 0:
SUM-=1
ans+=1
elif SUM > 0:
flag = 0
else:
SUM += A[i]
if flag == 0:
if SUM >= 0:
ans += SUM+1
SUM = -1
flag = 1
else:
if SUM <= 0:
ans += 1-SUM
SUM = 1
flag = 0
#print(ans,SUM)
ans = min(MIN,ans)
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;
constexpr int MOD = 1000000007;
using long long = long long;
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;
}
void print(const std::vector<int> &v) {
std::for_each(v.begin(), v.end(), [](int x) { std::cout << x << " "; });
std::cout << std::endl;
}
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (long long i = 0; i < (long long)n; i++) {
cin >> a[i];
}
long long res = (1ll << 60);
long long ans = 0LL;
if (a[0] >= 0) {
long long s = a[0];
for (int i = 1; i < n; i++) {
s += a[i];
if (i % 2 == 1) {
if (s >= 0) {
ans += s + 1;
s = -1;
}
} else {
if (s <= 0) {
ans += -s + 1;
s = 1;
}
}
}
res = min(res, ans);
} else if (a[0] <= 0) {
ans = 0;
long long s = a[0];
for (int i = 1; i < n; i++) {
s += a[i];
if (i % 2 == 0) {
if (s >= 0) {
ans += s + 1;
s = -1;
}
} else {
if (s <= 0) {
ans += -s + 1;
s = 1;
}
}
}
res = min(res, ans);
}
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 | python3 | n = int(input())
a = list(map(int, input().split()))
sum = a[0]
count = 0
if a[0] == 0:
if a[1] > 0:
sum -= 1
count += 1
elif a[1] < 0:
sum += 1
count += 1
for i in range(1, n):
tmp = sum
sum += a[i]
while tmp > 0 and sum >= 0:
sum -= 1
count += 1
while tmp < 0 and sum <= 0:
sum += 1
count += 1
print(count)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import sys
from itertools import accumulate
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007
def solve(A):
ans = 0
s = A[0]
for a in A[1:]:
prev, s = s, s + a
if prev > 0 and s >= 0:
ans += s + 1
s = -1
if prev < 0 and s <= 0:
ans += -s + 1
s = 1
return ans
def main():
N, *A = map(int, read().split())
a0 = A[0]
ans1 = 0
if A[0] <= 0:
ans1 = -A[0] + 1
A[0] = 1
ans1 = solve(A)
A[0] = a0
ans2 = 0
if A[0] >= 0:
ans2 = A[0] + 1
A[0] = -1
ans2 += solve(A)
print(min(ans1, ans2))
return
if __name__ == '__main__':
main()
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void) {
int n;
cin >> n;
vector<int> a(n);
cin >> a[0];
for (int i = 1; i < n; i++) {
cin >> a[i];
a[i] += a[i - 1];
}
if (a[0] < 0) {
for (int i = 0; i < n; i++) {
a[i] *= -1;
}
}
int ans = 0, def = 0;
if (a[0] == 0) {
ans++;
def++;
}
for (int i = 1; i < n; i++) {
if (i % 2 == 0 && a[i] + def <= 0) {
ans += 1 - (a[i] + def);
def += 1 - (a[i] + def);
} else if (i % 2 == 1 && a[i] + def >= 0) {
ans += a[i] + def + 1;
def -= a[i] + def + 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 a[100000], b[100001];
int main() {
long long n;
cin >> n;
for (long long i = 0; i < n; i++) {
cin >> a[i];
b[i] += a[i];
b[i + 1] = b[i];
}
long long sum = 0, sum2 = 0;
if (b[0] == 0) {
int i = 0;
while (b[i] == 0 && i < n) i++;
if (i % 2 == 0) {
b[0] = 1;
sum2 = 1;
} else {
b[0] = -1;
sum2 = -1;
}
sum++;
}
for (long long i = 1; i < n; i++) {
b[i] += sum2;
if (b[i] == 0) {
if (b[i - 1] > 0) {
sum2--;
sum++;
b[i] = -1;
} else {
sum2++;
sum++;
b[i] = 1;
}
} else {
if (b[i - 1] > 0 && b[i] > 0) {
sum2 -= (b[i] + 1);
sum += (b[i] + 1);
b[i] = -1;
} else if (b[i] < 0 && b[i - 1] < 0) {
sum2 += (0 - b[i] + 1);
sum += (0 - b[i] + 1);
b[i] = 1;
}
}
}
cout << sum << endl;
cin >> 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<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int ans = 0;
int sum = 0;
for (int i = 0; i < n; i++) {
if (i == 0) {
sum = a[i];
continue;
}
if (sum > 0) {
if (sum + a[i] >= 0) {
ans += sum + a[i] + 1;
a[i] -= sum + a[i] + 1;
}
} else {
if (sum + a[i] <= 0) {
ans -= sum + a[i] - 1;
a[i] -= sum + a[i] - 1;
}
}
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 | 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 sum = a[0];
long long ans = 0;
for (int i = 1; i < n; i++) {
long long sum1 = sum + a[i];
if (sum1 == 0 || sum / abs(sum) == sum1 / abs(sum1)) {
ans += abs(sum1 + sum / abs(sum));
sum1 = sum / abs(sum) * -1;
}
sum = sum1;
}
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())
l=list(map(int,input().split()))
s=l[0]
c=0
if not s:
s+=1
c+=1
for i in l[1:]:
if (s+i)*s>=0:
b=s
s=s//abs(s)*(-1)
c+=abs((b+i)-s)
else:
s+=i
print(c)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
using static System.Console;
using static System.Convert;
class Program
{
static void Main(string[] args)
{
var length = ToInt32(ReadLine());
var nums = Array.ConvertAll(ReadLine().Split(' '), long.Parse);
var r = 0;
var sum = nums[0];
if (sum == 0) { sum++; r++; }
var result = GetResult(nums, sum,length)+r;
var result2 = GetResult(nums,-sum,length)+r;
WriteLine(Math.Min(result,result2));
}
private static long GetResult(long[] nums,long sum,int length)
{
long result = 0;
var lastSum = sum;
for (var i = 1; i < length; i++)
{
sum += nums[i];
while (!IsDifferentSign(lastSum, sum) || sum == 0)
{
if (!IsDifferentSign(lastSum, sum))
{ result += Math.Abs(sum) + 1; sum = lastSum > 0 ? -1 : 1; }
if (sum == 0) { sum = lastSum > 0 ? --sum : ++sum; result++; }
}
lastSum = sum;
}
return result;
}
private static bool IsDifferentSign(long lastSum,long sum)
{
return (lastSum > 0 && sum < 0) || (lastSum < 0 && sum > 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 vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using mii = map<int, int>;
using pqls = priority_queue<long long>;
using pqlg = priority_queue<long long, vector<long long>, greater<long long>>;
using mll = map<long long, long long>;
using pll = pair<long long, long long>;
using sll = set<long long>;
long long divup(long long a, long long b);
long long kaijou(long long i);
long long P(long long n, long long k);
long long C(long long n, long long k);
long long GCD(long long a, long long b);
long long LCM(long long a, long long b);
bool prime(long long N);
double distance(vector<long long> p, vector<long long> q, long long n);
void press(vector<long long> &v);
void ranking(vector<long long> &v);
void erase(vector<long long> &v, long long i);
void unique(vector<long long> &v);
void printv(vector<long long> v);
vector<ll> keta(ll x);
long long modpow(long long a, long long n, long long mod);
long long modinv(long long a, long long mod);
vector<long long> inputv(long long n);
vector<long long> yakusuu(int n);
map<long long, long long> soinsuu(long long n);
vector<vector<long long>> maze(long long i, long long j, vector<string> &s);
vector<long long> eratos(long long n);
set<long long> eraset(long long n);
long long divup(long long a, long long b) {
long long x = abs(a);
long long y = abs(b);
long long z = (x + y - 1) / y;
if ((a < 0 && b > 0) || (a > 0 && b < 0))
return -z;
else if (a == 0)
return 0;
else
return z;
}
long long kaijou(long long i) {
if (i == 0) return 1;
long long j = 1;
for (long long k = 1; k <= i; k++) {
j *= k;
}
return j;
}
long long P(long long n, long long k) {
if (n < k) return 0;
long long y = 1;
for (long long i = 0; i < k; i++) {
y *= (n - i);
}
return y;
}
long long C(long long n, long long k) {
if (n < k) return 0;
return P(n, k) / kaijou(k);
}
long long GCD(long long a, long long b) {
if (a < b) swap(a, b);
long long d = a % b;
if (d == 0) {
return b;
}
return GCD(b, d);
}
long long LCM(long long a, long long b) { return (a / GCD(a, b)) * b; }
bool prime(long long N) {
if (N == 1) {
return false;
}
if (N < 0) return false;
long long p = sqrt(N);
for (long long i = 2; i <= p; i++) {
if (N % i == 0) {
return false;
}
}
return true;
}
double distance(vector<long long> p, vector<long long> q, long long n) {
double x = 0;
for (long long i = 0; i < n; i++) {
x += pow((p.at(i) - q.at(i)), 2);
}
return sqrt(x);
}
void press(vector<long long> &v) {
long long n = v.size();
vector<long long> w(n);
map<long long, long long> m;
for (auto &p : v) {
m[p] = 0;
}
long long i = 0;
for (auto &p : m) {
p.second = i;
i++;
}
for (long long i = 0; i < n; i++) {
w.at(i) = m[v.at(i)];
}
v = w;
return;
}
void ranking(vector<long long> &v) {
long long n = v.size();
map<long long, long long> m;
long long i;
for (i = 0; i < n; i++) {
m[v.at(i)] = i;
}
vector<long long> w(n);
i = 0;
for (auto &p : m) {
v.at(i) = p.second;
i++;
}
return;
}
void erase(vector<long long> &v, long long i) {
long long n = v.size();
if (i > n - 1) return;
for (long long j = i; j < n - 1; j++) {
v.at(j) = v.at(j + 1);
}
v.pop_back();
return;
}
void unique(vector<long long> &v) {
long long n = v.size();
set<long long> s;
long long i = 0;
while (i < n) {
if (s.count(v.at(i))) {
erase(v, i);
n--;
} else {
s.insert(v.at(i));
i++;
}
}
return;
}
void printv(vector<long long> v) {
cout << "{ ";
for (auto &p : v) {
cout << p << ",";
}
cout << "}" << endl;
}
vector<ll> keta(ll x) {
if (x == 0) return {0};
ll n = log10(x) + 1;
vll w(n, 0);
for (ll i = 0; i < n; i++) {
ll p;
p = x % 10;
x = x / 10;
w[n - 1 - i] = p;
}
return w;
}
long long modpow(long long a, long long n, long long mod) {
long long res = 1;
while (n > 0) {
if (n & 1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
long long modinv(long long a, long long mod) { return modpow(a, mod - 2, mod); }
vector<long long> inputv(long long n) {
vector<long long> v(n);
for (long long i = 0; i < n; i++) {
cin >> v[i];
}
return v;
}
vector<long long> yakusuu(long long n) {
vector<long long> ret;
for (long long i = 1; i <= sqrt(n); ++i) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n) {
ret.push_back(n / i);
}
}
}
sort(ret.begin(), ret.end());
return ret;
}
map<long long, long long> soinsuu(long long n) {
map<long long, long long> m;
long long p = sqrt(n);
while (n % 2 == 0) {
n /= 2;
if (m.count(2)) {
m[2]++;
} else {
m[2] = 1;
}
}
for (long long i = 3; i * i <= n; i += 2) {
while (n % i == 0) {
n /= i;
if (m.count(i)) {
m[i]++;
} else {
m[i] = 1;
}
}
}
if (n != 1) m[n] = 1;
return m;
}
vector<vector<long long>> maze(ll i, ll j, vector<string> &s) {
ll h = s.size();
ll w = s[0].size();
queue<vector<long long>> q;
vector<vector<long long>> dis(h, vll(w, -1));
q.push({i, j});
dis[i][j] = 0;
while (!q.empty()) {
auto v = q.front();
q.pop();
if (v[0] > 0 && s[v[0] - 1][v[1]] == '.' && dis[v[0] - 1][v[1]] == -1) {
dis[v[0] - 1][v[1]] = dis[v[0]][v[1]] + 1;
q.push({v[0] - 1, v[1]});
}
if (v[1] > 0 && s[v[0]][v[1] - 1] == '.' && dis[v[0]][v[1] - 1] == -1) {
dis[v[0]][v[1] - 1] = dis[v[0]][v[1]] + 1;
q.push({v[0], v[1] - 1});
}
if (v[0] < h - 1 && s[v[0] + 1][v[1]] == '.' && dis[v[0] + 1][v[1]] == -1) {
dis[v[0] + 1][v[1]] = dis[v[0]][v[1]] + 1;
q.push({v[0] + 1, v[1]});
}
if (v[1] < w - 1 && s[v[0]][v[1] + 1] == '.' && dis[v[0]][v[1] + 1] == -1) {
dis[v[0]][v[1] + 1] = dis[v[0]][v[1]] + 1;
q.push({v[0], v[1] + 1});
}
}
return dis;
}
long long modC(long long n, long long k, long long mod) {
if (n < k) return 0;
long long p = 1, q = 1;
for (long long i = 0; i < k; i++) {
p = p * (n - i) % mod;
q = q * (i + 1) % mod;
}
return p * modinv(q, mod) % mod;
}
long long POW(long long a, long long n) {
long long res = 1;
while (n > 0) {
if (n & 1) res = res * a;
a = a * a;
n >>= 1;
}
return res;
}
vector<long long> eratos(long long n) {
if (n < 2) return {};
vll v(n - 1);
for (long long i = 0; i < n - 1; i++) {
v[i] = i + 2;
}
ll i = 0;
while (i < n - 1) {
ll p = v[i];
for (ll j = i + 1; j < n - 1; j++) {
if (v[j] % p == 0) {
v.erase(v.begin() + j);
n--;
}
}
i++;
}
v.resize(n - 1);
return v;
}
set<long long> eraset(long long n) {
set<long long> s;
vll v = eratos(n);
for (auto &t : v) {
s.insert(t);
}
return s;
}
vll line(ll x1, ll y1, ll x2, ll y2) {
vector<ll> v(3);
v[0] = y1 - y2;
v[1] = x2 - x1;
v[2] = -x1 * (y1 - y2) + y1 * (x1 - x2);
return v;
}
double dis(vll v, ll x, ll y) {
double s = sqrt(v[0] * v[0] + v[1] * v[1]);
return (double)abs(v[0] * x + v[1] * y + v[2]) / s;
}
ll const mod = 1e9 + 7;
int main() {
ll n;
cin >> n;
auto a = inputv(n);
auto b = a;
ll l = 0;
ll res = 0;
for (long long i = 0; i < n; i++) {
if (l == 0) {
if (a[0] == 0) {
for (long long j = 0; j < n; j++) {
if (a[j] != 0) {
a[0] = a[j] / abs(a[j]);
if (j % 1) a[0] *= (-1);
res++;
break;
}
}
}
if (!a[0]) {
a[0] = 1;
res++;
}
l += a[0];
} else if (l < 0) {
if (a[i] < -l + 1) {
res += -l + 1 - a[i];
a[i] = -l + 1;
l = 1;
} else {
l += a[i];
}
} else if (l > 0) {
if (a[i] > -l - 1) {
res += abs(a[i] - (-l - 1));
a[i] = -l - 1;
l = -1;
} else {
l += a[i];
}
}
}
a = b;
ll L = 0;
ll res2 = 0;
for (long long i = 0; i < n; i++) {
if (L == 0) {
if (a[0] == 0) {
for (long long j = 0; j < n; j++) {
if (a[j] != 0) {
a[0] = a[j] / abs(a[j]);
if (j % 1) a[0] *= (-1);
res2++;
break;
}
}
}
if (!a[0]) {
a[0] = 1;
res2++;
}
L += a[0];
L *= (-1);
L = L / abs(L);
} else if (L < 0) {
if (a[i] < -L + 1) {
res2 += -L + 1 - a[i];
a[i] = -L + 1;
L = 1;
} else {
L += a[i];
}
} else if (L > 0) {
if (a[i] > -L - 1) {
res2 += abs(a[i] - (-L - 1));
a[i] = -L - 1;
L = -1;
} else {
L += a[i];
}
}
}
cout << min(res, res2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n=int(input())
a=list(map(int,input().split()))
import sys
sum=a[0]
cnt=0
if a[0]==0:
sum+=1
cnt+=1
for i in range(1,n):
if sum<0:
z=sum+a[i]
if z>0:
sum=z
elif z<0:
cnt+=(1-z)
sum=1
else:
if a[i-1]>0:
sum=-1
cnt+=1
elif a[i-1]<0:
sum=1
cnt+=1
elif sum>0:
z=sum+a[i]
if z>=0:
cnt+=(z+1)
sum=-1
elif z<0:
sum=z
cnt_plus=cnt
sum=-1
cnt=1
for i in range(1,n):
if sum<0:
z=sum+a[i]
if z>0:
sum=z
elif z<0:
cnt+=(1-z)
sum=1
else:
if a[i-1]>0:
sum=-1
cnt+=1
elif a[i-1]<0:
sum=1
cnt+=1
elif sum>0:
z=sum+a[i]
if z>=0:
cnt+=(z+1)
sum=-1
elif z<0:
sum=z
cnt_sbst=cnt
print(min(cnt_plus,cnt_sbst))
sys.exit()
for i in range(1,n):
if sum<0:
z=sum+a[i]
if z>0:
sum=z
elif z<0:
cnt+=(1-z)
sum=1
else:
if a[i-1]>0:
sum=-1
cnt+=1
elif a[i-1]<0:
sum=1
cnt+=1
elif sum>0:
z=sum+a[i]
if z>=0:
cnt+=(z+1)
sum=-1
elif z<0:
sum=z
print(cnt)
# 6
# 0 0 -1 3 5 0
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
#pragma GCC optimize("-O3")
using namespace std;
void _main();
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
_main();
}
const int inf = INT_MAX / 2;
const long long infl = 1LL << 60;
template <class T>
bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
enum PosiNega { POSITIVE = 0, NEGATIVE = 1 };
int solve(int N, int *a, PosiNega odd_posinega) {
int ans = 0;
int sum = 0;
PosiNega posi_nega = odd_posinega;
for (int i = 0; i < N; i++) {
sum += a[i];
if (POSITIVE == posi_nega) {
if (0 >= sum) {
ans += 1 - sum;
sum = 1;
}
posi_nega = NEGATIVE;
} else {
if (0 <= sum) {
ans += 1 + sum;
sum = -1;
}
posi_nega = POSITIVE;
}
}
return ans;
}
void _main() {
int N;
cin >> N;
int a[N];
for (int i = 0; i < N; i++) cin >> a[i];
int candidate1 = solve(N, a, POSITIVE);
int candidate2 = solve(N, a, NEGATIVE);
int ans = (candidate1 < candidate2) ? candidate1 : candidate2;
cout << ans << "\n";
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void) {
int64_t n;
int64_t i;
int64_t sum;
bool default_flag;
bool plus_flag, minus_flag;
int64_t ope_count;
cin >> n;
vector<int64_t> a(n);
for (i = 0; i < n; i++) {
cin >> a.at(i);
}
sum = 0;
ope_count = 0;
default_flag = true;
plus_flag = false;
minus_flag = false;
for (i = 0; i < n; i++) {
sum += a.at(i);
if (default_flag == true) {
default_flag = false;
if (sum > 0) {
plus_flag = true;
} else if (sum < 0) {
minus_flag = true;
} else if (sum == 0) {
ope_count++;
if (a.at(i + 1) <= 0) {
sum++;
plus_flag = true;
} else if (a.at(i + 1) > 0) {
sum--;
minus_flag = true;
}
}
} else if (plus_flag == true) {
while (sum >= 0) {
ope_count++;
sum--;
}
plus_flag = false;
minus_flag = true;
} else if (minus_flag == true) {
while (sum <= 0) {
ope_count++;
sum++;
}
plus_flag = true;
minus_flag = false;
}
}
cout << ope_count << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
i = input()
i = i.split()
for item in range(len(i)):
i[item] = int(i[item])
if i[0] > 0:
signN = 'neg'
else:
signN = 'pos'
signM = None
tot = 0
count = 0
for x in range(len(i)):
tot += i[x]
if tot > 0:
signM = 'pos'
elif tot < 0:
signM = 'neg'
elif tot == 0:
signM = 'z'
if signN != signM and signM != 'z':
signN = signM
elif signN == 'pos' and signM == 'pos':
count += (tot + 1)
tot -= tot
tot -= 1
signM = 'neg'
elif signN == 'neg' and signM == 'neg':
count -= (tot - 1)
tot = 1
signM = 'pos'
if tot == 0:
count += 1
if signN == 'neg':
tot += 1
signM = 'pos'
elif signN == 'pos':
tot -= 1
signM = 'neg'
signN = signM
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>
int main(){
int n,chk;
long long ans=0;
scanf("%d %d",&n,&m);
vector<int> a(n);
for (auto&e:a) scanf("%d"&e);
chk=a[0];
for (int i=1;i<n;i++) {
if (i%2) {
chk+=a[i];
if (chk>=0) {
ans+=chk+1;
chk=-1;
}
} else {
chk+=a[i];
if (chk<=0) {
ans+=-1*chk+1;
chk=1;
}
}
}
print("%lld",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;
int a[100005];
int pos[100005];
int neg[100005];
int main() {
int n, sp, sn, addp, addn;
sp = 0;
sn = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
pos[i] = pos[max(i - 1, 0)] + a[i];
neg[i] = neg[max(i - 1, 0)] + a[i];
if (i % 2 == 0) {
if (pos[i] <= 0) {
sp += abs(pos[i]) + 1;
pos[i] = 1;
}
if (neg[i] >= 0) {
sn += abs(neg[i]) + 1;
neg[i] = -1;
}
} else {
if (pos[i] >= 0) {
sp += abs(pos[i]) + 1;
pos[i] = -1;
}
if (neg[i] <= 0) {
sn += abs(neg[i]) + 1;
neg[i] = 1;
}
}
}
cout << min(sn, sp);
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | /* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] input = new int[n];
int[] result = new int[n];
int even = 0;
int odd = 0;
boolean sign = true; //正=true, 負=false
for(int i = 0; i < n; i++) {
input[i] = sc.nextInt();
if(i % 2 == 0) {
even += input[i];
} else {
odd += input[i];
}
}
if(even > odd) {
sign = true;
} else {
sign = false;
}
//System.out.println(Arrays.toString(input));
//System.out.println(sign + "");
//System.out.println(counting(input, result, 0, 0, sign));
counting(input, result, 0, 0, sign);
}
public static void counting(int[] input, int[] result, int count, int index, boolean sign) {
if(index > 0) {
result[index] = result[index - 1] + input[index];
} else {
result[index] = input[index];
}
if(sign) {
if(result[index] <= 0) {
count += Math.abs(result[index]) + 1;
result[index] = result[index] + Math.abs(result[index]) + 1;
}
sign = false;
} else {
if(result[index] >= 0) {
count += Math.abs(result[index]) + 1;
result[index] = result[index] - Math.abs(result[index]) - 1;
}
sign = true;
}
if(index < result.length - 1) {
counting(input, result, count, index+1, sign);
} else {
System.out.println(count);
}
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
enum State { Plus, Minus, Zero };
State GetState(long long sum) {
State state;
if (sum > 0)
state = Plus;
else if (sum == 0)
state = Zero;
else
state = Minus;
return state;
}
int main() {
int n;
cin >> n;
vector<long long> a(n);
cin >> a[0];
unsigned long long count = 0;
State state = GetState(a[0]);
if (state == Zero) {
a[0] = 1;
state = Plus;
count++;
}
long long sum = a[0];
for (int i = 1; i < n; i++) {
cin >> a[i];
State nextState = GetState(sum + a[i]);
switch (nextState) {
case Plus:
if (state == Plus) {
long long bf_a = a[i];
a[i] = -1 - sum;
count += abs(a[i] - bf_a);
nextState = Minus;
}
break;
case Minus:
if (state == Minus) {
long long bf_a = a[i];
a[i] = 1 - sum;
count += abs(a[i] - bf_a);
nextState = Plus;
}
break;
case Zero:
if (state == Plus) {
long long bf_a = a[i];
a[i] = -1 - sum;
count += abs(a[i] - bf_a);
nextState = Minus;
} else if (state == Minus) {
long long bf_a = a[i];
a[i] = 1 - sum;
count += abs(a[i] - bf_a);
nextState = Plus;
}
default:
break;
}
sum += a[i];
state = nextState;
}
if (sum == 0) count++;
cout << count << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import numpy as np
n = int(input())
a = list(map(int, input().split()))
cum_a = [0 for _ in range(n)]
cum_a[0] = a[0]
for i in range(n-1):
cum_a[i+1] = cum_a[i] + a[i+1]
cum_a = np.array(cum_a)
count = 0
flag = 0
diff = 0
def r_flag(n):
if n > 0:
return 1
elif n < 0:
return -1
else:
return 0
flag = r_flag(cum_a[0])
for i in range(1, len(cum_a)):
tmp_flag = r_flag(cum_a[i])
if tmp_flag * flag == -1:
flag = tmp_flag
elif tmp_flag * flag == 1:
if tmp_flag == 1:
count += abs(-1-(cum_a[i]))
diff += -1-(cum_a[i]+diff)
cum_a[i:] += diff
flag = -1
else:
count += abs(1-(cum_a[i]))
diff += 1-(cum_a[i]+diff)
cum_a[i:] += diff
flag = 1
else:
try:
next_flag = r_flag(cum_a[i+1]-diff)
if next_flag == 1:
diff -= 1
cum_a[i:] -= 1
count += 1
else:
diff += 1
cum_a[i:] += 1
count += 1
except:
diff += 1
count += 1
print(count)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a_p = list(map(int,input().split()))
a_n = a_p
#positive
if a[0] <= 0:
counter_p = 0
for i in range(n):
print(a_p,counter_p)
sum_ai = sum(a_p[:i+1])
if i % 2 == 0:
if sum_ai <= 0:
a_p[i] += -sum_ai + 1
counter_p += -sum_ai + 1
else:
if sum_ai >= 0:
a_p[i] += -sum_ai - 1
counter_p += sum_ai + 1
print(counter_p)
else:
#negative
counter_n = 0
for i in range(n):
print(a_n,counter_n)
sum_ai = sum(a_n[:i+1])
if i % 2 == 1:
if sum_ai <= 0:
a_n[i] += -sum_ai + 1
counter_n += -sum_ai + 1
else:
if sum_ai >= 0:
a_n[i] += -sum_ai - 1
counter_n += sum_ai + 1
print(counter_n)
print(min(counter_n,counter_p)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
long long chk1, chk2, ans1 = 0, ans2 = 0;
scanf("%d", &n);
vector<int> a(n);
for (auto& e : a) scanf("%d", &e);
chk1 = a[0];
chk2 = a[0];
for (int i = 1; i < n; i++) {
if (i % 2) {
chk1 += a[i];
chk2 += a[i];
if (chk1 >= 0) {
ans1 += chk1 + 1;
chk1 = -1;
}
if (chk2 <= 0) {
ans2 += -1 * chk2 + 1;
chk2 = 1;
}
} else {
chk1 += a[i];
chk2 += a[i];
if (chk1 <= 0) {
ans1 += -1 * chk1 + 1;
chk1 = 1;
}
if (chk2 >= 0) {
ans2 += chk2 + 1;
chk2 - 1;
}
}
}
if (ans1 >= 0 && ans2 >= 0)
printf("%lld\n", min(ans1, ans2));
else if (ans1 >= 0)
printf("%lld\n", ans1);
else if (ans2 >= 0)
printf("%lld\n", ans2);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void) {
int n;
cin >> n;
int* a = new int[n];
bool flg = false;
long long sum = 0;
long long cnt = 0;
for (int i = 0; i < n; ++i) {
cin >> a[i];
if (i == 0) {
if (a[0] <= 0)
flg = false;
else
flg = true;
}
sum += a[i];
if (flg == false && i % 2 == 0 && sum >= 0) {
while (sum >= 0) {
--sum;
++cnt;
}
} else if (flg == false && i % 2 == 1 && sum <= 0) {
while (sum <= 0) {
++sum;
++cnt;
}
} else if (flg == true && i % 2 == 0 && sum <= 0) {
while (sum <= 0) {
++sum;
++cnt;
}
} else if (flg == true && i % 2 == 1 && sum >= 0) {
while (sum >= 0) {
--sum;
++cnt;
}
}
}
cout << cnt << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | package main
import (
"bufio"
"fmt"
"math"
"os"
"strconv"
)
const pi = math.Pi
var mod int = pow(10, 9) + 7
var Umod uint64 = 1000000007
var ans int64
func main() {
reader.Split(bufio.ScanWords)
n, _ := strconv.Atoi(read())
a := make([]int, n)
for i := 0; i < n; i++ {
a[i], _ = strconv.Atoi(read())
}
sum := make([]int64, n)
sum[0] = int64(a[0])
for i := 1; i < n; i++ {
sum[i] += int64(a[i]) + sum[i-1]
if 0 < sum[i-1] && 0 <= sum[i] {
// NGパターン
ans += sum[i] + 1
sum[i] = -1
} else if sum[i-1] < 0 && sum[i] <= 0 {
// NGパターン
ans += 1 - sum[i]
sum[i] = 1
}
}
fmt.Println(ans)
}
/* ---------------------------------------- */
var reader = bufio.NewScanner(os.Stdin)
func read() string {
reader.Scan()
return reader.Text()
}
func lcm(x, y int) int {
return (x / gcd(x, y)) * y
}
func gcd(x, y int) int {
if x%y == 0 {
return y
} else {
r := x % y
return gcd(y, r)
}
}
var fac [1000000]int
var finv [1000000]int
var inv [1000000]int
func combination_init() {
fac[0], fac[1] = 1, 1
finv[0], finv[1] = 1, 1
inv[1] = 1
// invは a^(-1) mod p
// pをaで割ることを考える
// p/a*(a) + p%a = p
// p/a*(a) + p%a = 0 (mod p)
// -p%a = p/a*(a) (mod p)
// -p%a *a^(-1)= p/a (mod p)
// a^(-1)= p/a * (-p%a)^(-1) (mod p)
// a^(-1) =
for i := 2; i < 1000000; i++ {
fac[i] = fac[i-1] * i % mod
inv[i] = mod - inv[mod%i]*(mod/i)%mod
finv[i] = finv[i-1] * inv[i] % mod
}
}
func combination(x, y int) int {
if x < y {
return 0
}
if fac[0] != 1 {
combination_init()
}
return fac[x] * (finv[y] * finv[x-y] % mod) % mod
//return fac[x] / (fac[y] * fac[x-y])
}
func permutation(x, y int) int {
if x < y {
return 0
}
if fac[0] != 1 {
combination_init()
}
return fac[x] * (finv[x-y] % mod) % mod
//return fac[x] / fac[x-y]
}
func max(x ...int) int {
var res int = x[0]
for i := 1; i < len(x); i++ {
res = int(math.Max(float64(x[i]), float64(res)))
}
return res
}
func min(x ...int) int {
var res int = x[0]
for i := 1; i < len(x); i++ {
res = int(math.Min(float64(x[i]), float64(res)))
}
return res
}
func pow(x, y int) int { return int(math.Pow(float64(x), float64(y))) }
func abs(x int) int { return int(math.Abs(float64(x))) }
func floor(x int) int { return int(math.Floor(float64(x))) }
func ceil(x int) int { return int(math.Ceil(float64(x))) }
type SortBy [][]int
func (a SortBy) Len() int { return len(a) }
func (a SortBy) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a SortBy) Less(i, j int) bool { return a[i][0] < a[j][0] }
type PriorityQueue []int
func (h PriorityQueue) Len() int { return len(h) }
func (h PriorityQueue) Less(i, j int) bool { return h[i] < h[j] }
func (h PriorityQueue) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *PriorityQueue) Push(x interface{}) { *h = append(*h, x.(int)) }
func (h *PriorityQueue) Pop() interface{} {
old := *h
n := len(old)
x := old[n-1]
*h = old[0 : n-1]
return x
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | # -*- coding: utf-8 -*-
"""
Created on Sat Sep 8 15:51:53 2018
@author: maezawa
"""
def f(n, a0, cnt, sa, sign):
a = a0[:]
if a[0] == 0:
a[0] = 1
cnt += 1
if sign == -1:
a[0] = -a[0]
cnt += 2*abs(a[0])
for i in range(n-1):
sa += a[i]
na = -sa//abs(sa)*(abs(sa)+1)
if abs(a[i+1]) > abs(na) and a[i+1]*na > 0:
continue
else:
cnt += abs(na-a[i+1])
a[i+1] = na
return cnt
n = int(input())
a = list(map(int, input().split()))
sa = 0
cnt = 0
cnt0 = f(n, a, 0, 0, -1)
cnt1 = f(n, a, 0, 0, 1)
cnt = min([cnt0,cnt1])
print(cnt)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<long long int> A(N);
for (int i = 0; i < N; i++) cin >> A[i];
long long unsigned int cnt, cnt1, cnt2 = 0;
for (int i = 0; i < N; i++) {
if (i % 2 == 0) {
cnt1 += abs(1 - A[i]);
} else {
cnt1 += abs(-1 - A[i]);
}
}
for (int i = 0; i < N; i++) {
if (i % 2 == 1) {
cnt2 += abs(1 - A[i]);
} else {
cnt2 += abs(-1 - A[i]);
}
}
cnt = min(cnt1, cnt2);
cout << cnt;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | gets
xs = gets.split(' ').map(&:to_i)
def flip_sign(v)
v > 0 ? -1 : 1
end
puts xs.inject([0,0]){|r,v|
sum = r.first
count = r.last
sign = sum > 0
if sum == 0
sum = v
else
sum += v
if sign == (sum > 0)
count += sum.abs + 1
sum = flip_sign(sum)
end
end
[sum, count]
}.last
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
cnt_1 = 0
base = 0
#奇数番目が正
for i in range(n):
if ((base + a[i] >= 0) and (i % 2 == 1)) or ((base + a[i] <= 0) and (i % 2 == 0)):
cnt_1 += (1 + abs(base + a[i]))
base += (1 + abs(base + a[i])) * (-1) ** i #調整後のa[0]~a[i]の和
else:
base += a[i] #調整後のa[0]~a[i]の和
cnt_2 = 0
base = 0
for i in range(n):
if ((base + a[i] <= 0) and (i % 2 == 1)) or ((base + a[i] >= 0) and (i % 2 == 0)):
#奇数番目が負
cnt_2 += (1 + abs(base + a[i]))
base -= (1 + abs(base + a[i])) * (-1) ** i #調整後のa[0]~a[i]の和
else:
base += a[i] #調整後のa[0]~a[i]の和
print(min(cnt_1, cnt_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;
const long long INF = (long long)1 << 60;
int main() {
int N;
cin >> N;
int a[N];
for (int i = 0; i < (int)(N); i++) {
cin >> a[i];
}
int ans = 0;
int sum = 0;
for (int i = 0; i < (int)(N); i++) {
sum += a[i];
if (sum == 0) {
if (a[i] > 0) {
a[i]++;
sum++;
ans++;
} else {
a[i]--;
sum--;
ans++;
}
}
}
sum = 0;
for (int i = 0; i < (int)(N - 1); i++) {
sum += a[i];
if (sum * (sum + a[i + 1]) > 0) {
if (sum + a[i + 1] < 0) {
a[i + 1] += abs(1 - (sum + a[i + 1]));
ans += abs(1 - (sum + a[i + 1]));
} else {
a[i + 1] += abs(-1 - (sum + a[i + 1]));
ans += abs(-1 - (sum + 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 | python3 | N = int(input())
a = [int(x) for x in input().split()]
A = a[0]
asum = [A]
if A == 0:
ansp,ansm,tempp,tempm = 1,1,1,-1
elif A < 0:
ansp,ansm,tempp,tempm = -A + 1, 0, -A + 1, 0
else:
ansp,ansm,tempp,tempm = 0, A2 + 1, 0, -A2 - 1
for i in range(1,N):
A = asum[i-1] + a[i]
asum += [A]
A1 = A + tempp
if A1 == 0:
ansp += 1
tempp += ((i%2)*2-1)
elif i%2==0 and A1 < 0:
ansp += -A1 + 1
tempp += -A1 + 1
elif i%2!=0 and A1 > 0:
ansp += A1 + 1
tempp += -A1 - 1
A2 = A + tempm
if A2 == 0:
ansm += 1
tempm += -((i%2)*2-1)
elif i%2!=0 and A2 < 0:
ansm += -A2 + 1
tempm += -A2 + 1
elif i%2==0 and A2 > 0:
ansm += A2 + 1
tempm += -A2 - 1
print(min(ansp,ansm)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
int a[100010];
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int sa = 0, ans0 = 0, ans1 = 0;
for (int i = 0; i < n; i++) {
sa += a[i];
if (i % 2 == 0) {
if (sa >= 0) {
ans0 += sa - (-1);
sa = -1;
}
} else {
if (sa <= 0) {
ans0 += 1 - sa;
sa = 1;
}
}
}
sa = 0;
for (int i = 0; i < n; i++) {
sa += a[i];
if (i % 2 == 1) {
if (sa >= 0) {
ans1 += sa - (-1);
sa = -1;
}
} else {
if (sa <= 1) {
ans1 += 1 - sa;
sa = 1;
}
}
}
cout << min(ans0, ans1) << 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;
template <typename T>
void showvector(vector<T> v) {
for (T x : v) cout << x << " ";
cout << "\n";
}
template <typename T>
void showvector1(vector<T> v) {
long long int n = v.size();
for (long long int i = 1; i <= n - 1; i++) cout << v[i] << "\n";
}
template <typename T>
void showset(set<T> s) {
for (T x : s) cout << x << " ";
cout << "\n";
}
template <class T>
void showvectorpair(vector<T> v) {
for (auto it = v.begin(); it != v.end(); it++)
cout << it->first << " " << it->second << "\n";
cout << "\n";
}
template <typename T, typename P>
void showmap(map<T, P> m) {
for (auto it = m.begin(); it != m.end(); it++)
cout << it->first << " " << it->second << "\n";
cout << "\n";
}
template <typename T>
bool comp(T a, T b) {
return (a > b);
}
template <class T>
bool comppair(T a, T b) {
if (a.first == b.first) return (a.second > b.second);
return (a.first > b.first);
}
bool sameparity(long long int a, long long int b) { return (a % 2 == b % 2); }
bool difparity(long long int a, long long int b) { return !(a % 2 == b % 2); }
bool isprime(long long int x) {
if (x <= 1) return false;
for (long long int i = 2; i <= sqrt(x); i++) {
if (x % i == 0) return false;
}
return true;
}
bool iseven(long long int x) { return !(x % 2); }
bool isodd(long long int x) { return (x % 2); }
void vfun() {
long long int n, k;
cin >> n;
vector<long long int> v(n);
for (long long int i = 0; i < n; i++) cin >> v[i];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long int test = 1;
while (test--) {
long long int n;
cin >> n;
vector<long long int> v(n);
for (long long int i = 0; i < n; i++) cin >> v[i];
long long int sum = v[0], psum = v[0], cnt = 0;
if (v[0] == 0) {
cnt = 1;
if (v[1] > 0)
sum = psum = -1;
else
sum = psum = 1;
}
for (long long int i = 1; i <= n - 1; i++) {
sum += v[i];
if (psum > 0) {
if (sum >= 0) {
cnt += (sum + 1);
sum = -1;
}
} else {
if (sum <= 0) {
cnt += (abs(sum) + 1);
sum = 1;
}
}
psum = sum;
}
cout << cnt << "\n";
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
class Program{
static int n;
static long ans;
static int[] s;
static void Main(){
n=int.Parse(Console.ReadLine());
s=Array.ConvertAll(Console.ReadLine().Split(),int.Parse);
if(s[0]==0){s[0]=fu(1);ans++;}
for(int i=1;i<n;i++){
s[i]+=s[i-1];
if(s[i]==0){s[i]=-Math.Sign(s[i-1]);ans++;}
else if(Math.Sign(s[i])==Math.Sign(s[i-1])){
ans+=Math.Abs(s[i])+1;
s[i]=-Math.Sign(s[i]);
}
}
Console.WriteLine("{0}",ans);
}
static int fu(int z){
if(z==n){return 1;}
else if(s[z]==0){s[z]=fu(z+1);ans++;}
return s[z]<0?1:-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 MAXN = 100010;
int n;
long long a[MAXN];
long long s[MAXN];
void solve() {
s[0] = a[0];
for (int i = 1; i < n; i++) {
s[i] = s[i - 1] + a[i];
}
long long cnt = 0;
long long carry = 0;
for (int i = 1; i < n; i++) {
if ((s[i] + carry) * s[i - 1] >= 0) {
cnt += abs(s[i] + carry) + 1;
if (s[i - 1] < 0) {
carry += abs(s[i]) + 1;
s[i] = 1;
} else {
carry -= abs(s[i]) + 1;
s[i] = -1;
}
} else {
s[i] += carry;
}
}
cout << cnt << endl;
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
solve();
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n = 0;
cin >> n;
int* all = new int[n];
for (int i = 0; i < n; i++) cin >> all[i];
int ans = 0;
int sum = 0;
int last = 0;
if (all[0] > 0)
last = -1;
else if (all[0] < 0)
last = 1;
else {
last = -1;
all[0] = 1;
ans++;
}
for (int i = 0; i < n; i++) {
sum += all[i];
if (last == 1) {
if (sum >= 0) {
ans += sum + 1;
sum = -1;
}
last = -1;
} else if (last == -1) {
if (sum <= 0) {
ans += -1 * sum + 1;
sum = 1;
}
last = 1;
}
}
cout << ans;
system("pause");
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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())
numbers = list(map(int, input().split()))
counter = 0
sum_i_n = 0
sum_i_n_1 = numbers[0]
for i in range(len(numbers) - 1):
# sum_i_n = sum(numbers[:i + 1])
# sum_i_n_1 = sum(numbers[:i + 2])
sum_i_n += numbers[i]
sum_i_n_1 += numbers[i + 1]
if sum_i_n == 0:
numbers[i] += 1
sum_i_n += 1
sum_i_n_1 += 1
counter += 1
if sum_i_n * sum_i_n_1 > 0:
sub = abs(sum_i_n_1) + 1
counter += sub
if sum_i_n_1 > 0:
numbers[i + 1] -= sub
sum_i_n_1 -= sub
else:
numbers[i + 1] += sub
sum_i_n_1 += sub
if sum(numbers) == 0:
counter += 1
print(counter)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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 a[100000];
int n;
void solve() {
long long ans = 0;
long long sum0 = a[0];
long long sum1;
for (int i = 1; i < n; i++) {
sum1 = sum0 + a[i];
if (sum1 * sum0 < 0) {
} else if (sum1 * sum0 > 0) {
ans += abs(sum1) + 1;
sum1 = -1 * sum1 / abs(sum1);
} else {
ans++;
sum1 = -1 * sum0 / abs(sum0);
}
sum0 = sum1;
}
cout << ans << endl;
return;
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
solve();
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
import numpy as np
na1 = np.array(a).cumsum()
na2 = np.array(a).cumsum()
cnt1 = 0
hoge1 = 0
cnt2 = 0
hoge2 = 0
for i in range(1, n):
na1[i] += hoge1
na2[i] += hoge2
if(i % 2 == 0 and na1[i] <= 0):
delta1 = abs(na1[i]) + 1
cnt1 = cnt1 + delta1
hoge1 += delta1
elif(i % 2 == 1 and na1[i] >= 0):
delta1 = abs(na1[i]) + 1
cnt1 = cnt1 + delta1
hoge1 -= delta1
if(i % 2 == 1 and na2[i] <= 0):
delta2 = abs(na2[i]) + 1
cnt2 = cnt2 + delta2
hoge2 += delta2
elif(i % 2 == 0 and na2[i] >= 0):
delta2 = abs(na2[i]) + 1
cnt2 = cnt2 + delta2
hoge2 -= delta2
else:
na1[i]
print(min([cnt1,cnt2]))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <bits/stdc++.h>
int in(void) {
int i;
scanf("%d", &i);
return i;
}
long long llin(void) {
long long i;
scanf("%lld", &i);
return i;
}
void chin(char s[]) { scanf("%s", s); }
void print(int a) { printf("%d\n", a); }
void llprint(long long a) { printf("%lld\n", a); }
void print2(int a, int b) { printf("%d %d\n", a, b); }
long long max(long long a, long long b) { return a > b ? a : b; }
long long min(long long a, long long b) { return a < b ? a : b; }
int main(void) {
long long n = llin(), a[100000], i, ans = 0, sum = 0;
for (i = 0; i < n; i++) {
a[i] = llin();
}
for (i = 0; i < n; i++) {
sum += a[i];
if (sum == 0) {
if (sum - a[i] < 0) {
sum = 1;
ans++;
} else if (sum - a[i] > 0) {
sum = -1;
ans++;
} else if (a[i + 1] > 0) {
sum = -1;
ans++;
} else {
sum = 1;
ans++;
}
} else if (sum - a[i] < 0 && sum < 0) {
ans += fabs(1 - sum);
sum = 1;
} else if (sum - a[i] > 0 && sum > 0) {
ans += fabs(1 + sum);
sum = -1;
}
}
llprint(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;
const int INF = (1 << 30) - 1;
const long long LINF = (1LL << 62) - 1;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {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;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < (n); i++) cin >> a[i];
int num = max(a[0], 1), ans = max(0, 1 - a[0]);
for (int i = 1; i < n; i++) {
if (i % 2) {
ans += max(num + a[i] + 1, 0);
num = min(-1, num + a[i]);
} else {
ans += max(1 - (num + a[i]), 0);
num = max(1, num + a[i]);
}
}
num = min(a[0], -1);
int cnt = max(0, a[0] + 1);
for (int i = 1; i < n; i++) {
if (i % 2) {
cnt += max(1 - (num + a[i]), 0);
num = max(1, num + a[i]);
} else {
cnt += max(num + a[i] + 1, 0);
num = min(-1, num + a[i]);
}
}
chmin(ans, cnt);
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 n;
int flag[100005], k[100005];
long long a[100005], sum[100005], ans, b[100005], tot[100005], ant;
int main() {
int m = 0;
scanf("%d", &n);
scanf("%lld", &a[1]);
b[1] = a[1];
sum[1] = a[1];
tot[1] = sum[1];
if (sum[1] > 0) flag[1] = 1;
if (sum[1] < 0) flag[1] = 0;
if (sum[1] == 0) m = 1;
if (m == 0) {
for (int i = 2; i <= n; i++) {
scanf("%lld", &a[i]);
sum[i] = a[i] + sum[i - 1];
if (sum[i] > 0) flag[i] = 1;
if (sum[i] < 0) flag[i] = 0;
if (flag[i - 1] == 1) {
if (sum[i] >= 0) {
ans += sum[i] + 1;
sum[i] = -1;
flag[i] = 0;
}
} else {
if (sum[i] <= 0) {
ans += 1 - sum[i];
sum[i] = 1;
flag[i] = 1;
}
}
}
printf("%lld\n", ans);
} else {
ans = 1;
flag[1] = 0;
for (int i = 2; i <= n; i++) {
scanf("%lld", &a[i]);
b[i] = a[i];
sum[i] = a[i] + sum[i - 1];
if (sum[i] > 0) flag[i] = 1;
if (sum[i] < 0) flag[i] = 0;
if (flag[i - 1] == 1) {
if (sum[i] >= 0) {
ans += sum[i] + 1;
sum[i] = -1;
flag[i] = 0;
}
} else if (flag[i - 1] == 0) {
if (sum[i] <= 0) {
ans += 1 - sum[i];
sum[i] = 1;
flag[i] = 1;
}
}
}
k[1] = 1;
ant = 1;
for (int i = 2; i <= n; i++) {
tot[i] = b[i] + tot[i - 1];
if (tot[i] > 0) k[i] = 1;
if (tot[i] < 0) k[i] = 0;
if (k[i - 1] == 1) {
if (tot[i] >= 0) {
ant += tot[i] + 1;
tot[i] = -1;
k[i] = 0;
}
} else if (k[i - 1] == 0) {
if (tot[i] <= 0) {
ant += 1 - tot[i];
tot[i] = 1;
k[i] = 1;
}
}
}
printf("%lld\n", min(ant, ans));
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <bits/stdc++.h>
int main() {
int a, sum[1000000], cnt[2] = {}, b, n;
scanf("%d", &n);
scanf("%d", &a);
sum[0] = a;
for (int i = 1; i < n; i++) {
scanf("%d", &a);
sum[i] = sum[i - 1] + a;
}
b = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0 && sum[i] + b >= 0) {
cnt[0] += sum[i] + b + 1;
b -= sum[i] + b + 1;
} else if (i % 2 == 1 && sum[i] + b <= 0) {
cnt[0] -= sum[i] + b;
cnt[0]++;
b += 1 - (sum[i] + b);
}
}
b = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0 && sum[i] + b <= 0) {
cnt[1] -= sum[i] + b;
cnt[1]++;
b += 1 - (sum[i] + b);
} else if (i % 2 == 1 && sum[i] + b >= 0) {
cnt[1] += sum[i] + b + 1;
b -= sum[i] + b + 1;
}
}
if (cnt[0] < cnt[1])
printf("%d\n", cnt[0]);
else
printf("%d\n", cnt[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 | UNKNOWN | #!/usr/bin/env ruby
#
n = STDIN.gets.to_i
a = STDIN.gets.split(' ').map{|x| x.to_i}
s = Array.new(n,0)
output = 0
if a[0] == 0
output += 1
s[0] = (a[1] > 0) ? -1 : 1
else
s[0] = a[0]
end
1.upto(n-1) do |i|
s[i] = s[i-1] + a[i]
if s[i] == 0
change = 1
s[i] = (s[i-1] > 0) ? -1 : 1
else
if (s[i-1] > 0) ^ (s[i] > 0)
change = 0
else
change = s[i].abs+1
s[i] = (s[i-1] > 0) ? -1: 1
end
end
output += change
end
puts output
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | # -*- coding: utf-8 -*-
# 整数の入力
n=int(input())
a=input().split()
counter=0
S=int(a[0])
# 出力
for i in range(1,n):
if S<0 and S+int(a[i])<=0:
counter+=-S-int(a[i])+1
a[i]=-S+1
elif S>0 and S+int(a[i])>=0:
counter+=S+int(a[i])+1
a[i]=-S-1
S+=int(a[i])
print(counter) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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())
nList = list(map(int,input().split()))
gokei = nList[0]
count = 0
for i in range(1,n):
gokei_tmp =gokei+ nList[i]
if (gokei < 0 and gokei_tmp > 0) or (gokei > 0 and gokei_tmp < 0):
gokei = gokei_tmp
else:
count += abs(gokei_tmp)
gokei_tmp -= gokei_tmp
if gokei > 0:
gokei_tmp -= 1
else:
gokei_tmp += 1
count += 1
gokei = gokei_tmp
print(count)
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.