Search is not available for this dataset
name
stringlengths 2
88
| description
stringlengths 31
8.62k
| public_tests
dict | private_tests
dict | solution_type
stringclasses 2
values | programming_language
stringclasses 5
values | solution
stringlengths 1
983k
|
---|---|---|---|---|---|---|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> S(N + 1);
for (int i = 1; i <= N; ++i) {
cin >> S[i];
S[i] += S[i - 1];
}
int ians = (1 << 30);
for (int j = -1; j <= 1; j += 2) {
vector<int> S_(S);
int ans = 0;
int add = 0;
int sign = j;
for (int i = 1; i <= N; ++i) {
S_[i] += add;
int sign_i = ((S_[i] >> 31) << 1) + 1;
if (sign_i == sign) {
ans += abs(-sign_i - S_[i]);
add += -sign_i - S_[i];
S_[i] = -sign_i;
} else if (S_[i] == 0) {
ans += 1;
add += -sign;
S_[i] += -sign;
}
sign = -sign;
}
ians = min(ans, ians);
}
cout << ians << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = [int(x) for x in input().split()]
for i in range(n-1):
if a[i] != 0:
if i == 0:
break
else:
sign = a[i] // abs(a[i])
if i % 2 == 1:
a[0] -= sign
else:
a[0] += sign
res = 0
x = 0
for i in range(n-1):
x += a[i]
sign = (x // abs(x)) * (-1)
tmp = sign - (x + a[i+1])
if sign < 0:
tmp = min(tmp, 0)
else:
tmp = max(tmp, 0)
res += abs(tmp)
a[i+1] += tmp
print(res) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int a[100010];
int sign(int n) {
if (n > 0) return 1;
if (n < 0) return -1;
return 0;
}
int main(void) {
int n;
int res = 0;
int si, d;
int s = 0;
cin >> n;
for (long long i = 0; i < n; i++) cin >> a[i];
for (long long i = 0; i < n; i++) {
if (a[i] != 0) {
si = sign(a[i]) * ((i % 2 == 0) ? 1 : -1);
break;
}
}
for (long long i = 0; i < n; i++) {
s += a[i];
if (sign(s) != si) {
d = si - s;
res += abs(d);
s += d;
}
si = -si;
}
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 | python2 | n=int(raw_input())
a=map(int,raw_input().split(' '))
c=0
for i in range(n):
s=sum(a[0:i+1])
if s==0:
if i==n-1:
a[i]+=1
c+=1
elif a[i+1]>=0:
a[i]-=1
c+=1
else:
a[i]+=1
c+=1
if i==(n-1): break
s=sum(a[0:i+1])
n_s=s+a[i+1]
if s*n_s>0:
if s>=0:
a[i+1]-=(s+1)
c+=(s+1)
else:
a[i+1]+=(s+1)
c+=(s+1)
print c |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import itertools
def sign(num):
if num < 0:
return -1
elif num > 0:
return 1
else:
return 0
N = input()
a_i = list(map(int, input().split()))
a_sum = [a_i[0]]
for i, a in enumerate(a_i[1:]):
i += 1
a_sum.append(a_sum[-1]+a)
signs = [1, -1]
for i, sum_i in enumerate(a_sum):
if sum_i != 0 and i%2 == 0:
signs = [sign(sum_i), -sign(sum_i)]
break
elif sum_i != 0 and i%2 == 1:
signs = [-sign(sum_i), sign(sum_i)]
break
a_sum = 0
changes = 0
for i, a in enumerate(a_i):
a_sum += a
if sign(a_sum) != signs[i%2]:
changes += abs(a_sum) + 1
a_sum = signs[i%2]
print(changes)
#
# for i, sum_i in enumerate(a_sum):
# if i == 0:
# signs = [sign(sum_i), -sign(sum_i)]
# elif sign(sum_i) != signs[i%2]:
# a_sum[i:] = [num + (abs(sum_i) + 1) * signs[i%2] for num in a_sum[i:]]
# changes += abs(sum_i) + 1
# # print(a_sum)
# print(changes)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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 = 1; i < N; i++) {
if (pre_pm) {
sum += T.at(i);
while (0 <= sum) {
sum--;
ans++;
}
pre_pm = false;
} else {
sum += T.at(i);
while (sum <= 0) {
sum++;
ans++;
}
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;
sum = T.at(0);
if (0 <= sum) {
pre_pm = true;
long int tmp1 = check(sum, ans, T, N, pre_pm);
pre_pm = false;
long int tmp2 = check(-1, 1 + sum, T, N, pre_pm);
cout << min(tmp1, tmp2) << endl;
} else {
pre_pm = false;
long int tmp1 = check(sum, ans, T, N, pre_pm);
pre_pm = true;
long int tmp2 = check(1, 1 + sum, T, N, pre_pm);
cout << min(tmp1, tmp2) << endl;
}
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long ans1 = (a[0] > 0) ? 0 : -a[0] + 1;
long long sum = (a[0] > 0) ? a[0] : 1;
for (int i = 1; i < n; i++) {
if (sum * (sum + a[i]) < 0) {
sum += a[i];
} else {
ans1 += abs(sum + a[i]) + 1;
sum = (sum > 0) ? -1 : 1;
}
}
long long ans2 = (a[0] < 0) ? 0 : -a[0] + 1;
sum = (a[0] < 0) ? a[0] : -1;
for (int i = 1; i < n; i++) {
if (sum * (sum + a[i]) < 0) {
sum += a[i];
} else {
ans2 += abs(sum + a[i]) + 1;
sum = (sum > 0) ? -1 : 1;
}
}
cout << min(ans1, ans2) << endl;
return;
}
int main(int argc, char const* argv[]) {
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;
using ll = long long;
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
int ans1 = 0;
int sum = 0;
for (int i = 0; i < n; i++) {
int cur = a[i];
if (i % 2) {
if (sum + cur >= 0) cur = -(sum + 1);
} else {
if (sum + cur == 0)
cur++;
else if (sum + cur < 0)
cur = -sum + 1;
}
sum += cur;
ans1 += abs(a[i] - cur);
}
int ans2 = 0;
sum = 0;
for (int i = 0; i < n; i++) {
int cur = a[i];
if (i % 2 == 0) {
if (sum + cur >= 0) cur = -(sum + 1);
} else {
if (sum + cur == 0)
cur++;
else if (sum + cur < 0)
cur = -sum + 1;
}
sum += cur;
ans2 += abs(a[i] - cur);
}
cout << min(ans1, ans2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using std::cin;
using std::cout;
using std::endl;
using std::min;
using std::vector;
static int solve(int N, vector<int> &a) {
int count_a = 0;
int count_b = 0;
int sum_a = 0;
int sum_b = 0;
int sign_a = 1;
int sign_b = -1;
for (int n = 0; n < N; n++) {
int val = a[n];
sum_a += val;
sum_b += val;
if (sum_a * sign_a <= 0) {
count_a += 1 - (sum_a * sign_a);
sum_a = sign_a;
}
if (sum_b * sign_b <= 0) {
count_b += 1 - (sum_b * sign_b);
sum_b = sign_b;
}
sign_a = -sign_a;
sign_b = -sign_b;
}
return min(count_a, count_b);
}
int main() {
int N;
cin >> N;
vector<int> a(N);
for (int n = 0; n < N; n++) {
cin >> a[n];
}
cout << solve(N, a) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < (int)(n); i++) {
cin >> a[i];
}
vector<int> copy_a = a;
int count = 0;
int sum = 0;
if (a[0] != 0) {
for (int i = 0; i < n - 1; i++) {
sum += a[i];
if (sum < 0 && sum + a[i + 1] <= 0) {
int na = 1 - sum;
count += abs(na - a[i + 1]);
a[i + 1] = na;
}
if (sum > 0 && sum + a[i + 1] >= 0) {
int na = -1 - sum;
count += abs(na - a[i + 1]);
a[i + 1] = na;
}
}
} else if (a[0] == 0) {
a[0] = 1;
int count1 = 1;
for (int i = 0; i < n - 1; i++) {
sum += a[i];
if (sum < 0 && sum + a[i + 1] <= 0) {
int na = 1 - sum;
count1 += abs(na - a[i + 1]);
a[i + 1] = na;
}
if (sum > 0 && sum + a[i + 1] >= 0) {
int na = -1 - sum;
count1 += abs(na - a[i + 1]);
a[i + 1] = na;
}
}
copy_a[0] = -1;
int count2 = 1;
int sum2 = 0;
for (int i = 0; i < n - 1; i++) {
sum2 += copy_a[i];
if (sum2 < 0 && sum2 + copy_a[i + 1] <= 0) {
int na = 1 - sum2;
count2 += abs(na - copy_a[i + 1]);
copy_a[i + 1] = na;
}
if (sum2 > 0 && sum2 + copy_a[i + 1] >= 0) {
int na = -1 - sum2;
count2 += abs(na - copy_a[i + 1]);
copy_a[i + 1] = na;
}
}
count = min(count1, count2);
}
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 | cpp | #include <bits/stdc++.h>
using namespace std;
bool posi(long long x) { return x > 0; }
int main() {
int N;
cin >> N;
vector<long long> a(N);
for (auto &i : a) cin >> i;
long long ans = 0, tmp = 0;
long long sum = a[0];
for (int i = 1; i < N; i++) {
if ((!(posi(sum) ^ posi(sum + a[i]))) || sum + a[i] == 0) {
tmp += abs(sum + a[i]) + 1;
sum = (sum > 0) ? -1 : 1;
} else
sum += a[i];
}
ans = tmp;
tmp = abs(a[0]) + 1;
sum = (a[0] > 0) ? -1 : 1;
for (int i = 1; i < N; i++) {
if ((!(posi(sum) ^ posi(sum + a[i]))) || sum + a[i] == 0) {
tmp += abs(sum + a[i]) + 1;
sum = (sum > 0) ? -1 : 1;
} else
sum += a[i];
}
ans = min(ans, tmp);
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()))
ansp = 0
sums = A[0]
if sums == 0:
ansp += 1
sums += 1
for i in range(1,n-1):
sums += A[i]
if i%2 == 1:
if sums < 0:
pass
else:
ansp += abs(-1-sums)
sums = -1
else:
if sums > 0:
pass
else:
ansp += abs(1 - sums)
sums = 1
if (n-1)%2 == 0:
if A[n-1] > 0:
sums += A[n-1]
pass
else:
ansp += abs(1-A[n])
sums += 1
else:
if A[n-1] < 0:
sums += A[n-1]
pass
else:
ansp += abs(-1-A[n-1])
sums -= 1
if sums == 0:
ansp += 1
sums = A[0]
ansm = 0
if sums == 0:
ansm += 1
sums -= 1
for i in range(1,n-1):
sums += A[i]
if i%2 == 0:
if sums < 0:
pass
else:
ansm += abs(-1-sums)
sums = -1
else:
if sums > 0:
pass
else:
ansm += abs(1 - sums)
sums = 1
if (n-1)%2 == 1:
if A[n-1] > 0:
sums += A[n-1]
pass
else:
ansm += abs(1-A[n-1])
sums += 1
else:
if A[n-1] < 0:
sums += A[n-1]
pass
else:
ansm += abs(-1-A[n-1])
sums -= 1
if sums == 0:
ansm += 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 | java | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++) {
a[i]=sc.nextInt();
}
//偶数を正とする
int counter=0;
int sum=0;
for(int i=0;i<n;i++) {
sum+=a[i];
if(i%2==0 && sum<=0) {
//-aだったら+1の操作をa回で0になり
//正にするならそこからさらに+1
counter+=Math.abs(sum)+1;
sum=1;
}else if(i%2==1 && sum>=0){
//+aだったらa回の-1と1回の-1でマイナス
counter+=sum+1;
sum=-1;
}
}
int counter1=0;
int sum1=0;
//奇数を正
for(int i=0;i<n;i++) {
sum1+=a[i];
if(i%2==0 && sum1>=0) {
//+aだったらa回の-1と1回の-1でマイナス
counter1+=sum1+1;
sum1=-1;
}else if(i%2==1 && sum1>=0){
//-aだったら+1の操作をa回で0になり
//正にするならそこからさらに+1
counter1+=Math.abs(sum1)+1;
sum1=1;
}
}
System.out.println(Math.max(counter,counter1));
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int t;
vector<int> answer(2, 0);
int sumi;
bool flag = true;
cin >> t;
vector<int> A(t);
for (int i = 0; i < t; i++) {
cin >> A[i];
}
for (int j = 0; j < 2; j++) {
for (int i = 0; i < t; i++) {
sumi += A[i];
if (sumi == 0) {
answer[j] += 1;
if (flag) {
sumi = -1;
} else {
sumi = 1;
}
} else if (sumi > 0 == flag) {
answer[j] += abs(sumi) + 1;
if (sumi > 0) {
sumi = -1;
} else {
sumi = 1;
}
}
flag = !flag;
}
flag = false;
}
cout << min(answer[0], answer[1]) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int n;
cin >> n;
ll a[n];
for (int i = 0; i < n; i++) cin >> a[i];
ll ans = 4e18;
if (a[0] < 0)
for (int i = 0; i < n; i++) a[i] *= -1;
ll sum = a[0], now = 0;
for (int i = 0; i < n - 1; i++) {
if (i % 2) {
if (sum + a[i + 1] < 0) {
now += 1 - (sum + a[i + 1]);
sum = 1;
} else {
sum += a[i + 1];
}
} else {
if (sum + a[i + 1] >= 0) {
now += sum + a[i + 1] + 1;
sum = -1;
} else {
sum += a[i + 1];
}
}
}
ans = min(ans, now);
sum = -1, now = a[0] + 1;
for (int i = 0; i < n - 1; i++) {
if (i % 2 == 0) {
if (sum + a[i + 1] <= 0) {
now += 1 - a[i + 1] - sum;
sum = 1;
} else {
sum += a[i + 1];
}
} else {
if (sum + a[i + 1] >= 0) {
now += sum + a[i + 1] + 1;
sum = -1;
} else {
sum += a[i + 1];
}
}
}
ans = min(ans, now);
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];
vector<long long> sums(n);
long long ans = 0;
sums[0] = a[0];
for (int i = 1; i < n; i++) {
sums[i] = sums[i - 1] + a[i];
if (sums[i - 1] > 0 && sums[i] >= 0) {
ans += a[i] + (sums[i - 1] + 1);
a[i] = -(sums[i - 1] + 1);
sums[i] = -1;
} else if (sums[i - 1] < 0 && sums[i] <= 0) {
ans += (-a[i]) + (-sums[i - 1] + 1);
a[i] = (-sums[i - 1] + 1);
sums[i] = 1;
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i) cin >> a[i];
int sum = 0;
int res = 0;
if (a[0] > 0) {
for (int i = 0; i < n; ++i) {
sum += a[i];
while (i % 2 == 0 && sum <= 0) {
++sum;
++res;
}
while (i % 2 != 0 && sum >= 0) {
--sum;
++res;
}
}
}
if (a[0] < 0) {
for (int i = 0; i < n; ++i) {
sum += a[i];
while (i % 2 == 0 && sum >= 0) {
--sum;
++res;
}
while (i % 2 != 0 && sum <= 0) {
++sum;
++res;
}
}
}
cout << res << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int ans = 0;
vector<int> inp;
vector<int> comp;
int num1;
cin >> num1;
comp.push_back(num1);
if (num1 >= 0) {
for (int i = 0; i < n - 1; i++) {
int num;
cin >> num;
if (i % 2 == 0) {
if (num + comp[i] < 0) {
comp.push_back(num + comp[i]);
} else {
comp.push_back(-1);
ans += max(num, comp[i]) + 1 + min(num, comp[i]);
}
} else {
if (num + comp[i] > 0) {
comp.push_back(num + comp[i]);
} else {
comp.push_back(1);
ans += abs(min(num, comp[i])) + 1 - max(num, comp[i]);
}
}
}
} else {
for (int i = 0; i < n - 1; i++) {
int num;
cin >> num;
if (i % 2 != 0) {
if (num + comp[i] < 0) {
comp.push_back(num + comp[i]);
} else {
comp.push_back(-1);
ans += max(num, comp[i]) + 1 + min(num, comp[i]);
}
} else {
if (num + comp[i] > 0) {
comp.push_back(num + comp[i]);
} else {
comp.push_back(1);
ans += abs(min(num, comp[i])) + 1 - max(num, comp[i]);
}
}
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.*;
public class Main {
static long ans;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int [] num = new int [N];
long sum = 0;
ans=0;
for(int i=0; i<N; i++){
num[i] = sc.nextInt();
sum+=num[i];
if(i!=0)sum=function(i,num,sum);
}
System.out.println(ans);
}
static long function(int i, int[]num, long sum){
int a = sign((sum-num[i]));
int b = sign(sum);
int t = a*b;
if(t==0){
ans++;
if(a>0){
return -1;
}else{
return 1;
}
}else if(t<0){
return sum;
}else{
ans+=(Math.abs(sum)+1);
if(sum>0){
return -1;
}else{
return 1;
}
}
}
static int sign(long A){
if(A>0){
return 1;
}else if(A<0){
return -1;
}else{
return 0;
}
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.Scanner;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
new Main().solve();
}
void solve() {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long[] a = new long[n];
long A = 0;
long ANS = Long.MAX_VALUE;
long ans = 0;
long q = 1;
a[0] = sc.nextLong();
A = a[0];
for (int i = 1; i < n; i++) {
a[i] = sc.nextLong();
A += a[i];
q = q * -1;
if (A <= 0 && q == 1) {
ans += Math.abs(A - 1);
A += Math.abs(A - 1);
} else if (A >= 0 && q == -1) {
ans += Math.abs(A + 1);
A -= Math.abs(A + 1);
}
}
ANS = ans;
A = a[0];
ans = 0;
q = -1;
for (int i = 1; i < n; i++) {
A += a[i];
q = q * -1;
if (A <= 0 && q == 1) {
ans += Math.abs(A - 1);
A += Math.abs(A - 1);
} else if (A >= 0 && q == -1) {
ans += Math.abs(A + 1);
A -= Math.abs(A + 1);
}
}
ANS = Math.min(ANS, ans);
q = -1;
System.out.println(ANS);
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.ArrayList;
import java.util.Scanner;
public class ABC {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
// 整数の入力
int n = sc.nextInt();
int a[] = new int[n];
for(int i = 0;i<a.length;i++)
{
a[i] = sc.nextInt();
}
int b[] = new int[n];
b[0] = a[0];
int count = 0;
for(int i = 0;i<a.length-1;i++)
{
b[i+1] = b[i] + a[i+1];
if(b[i+1]*b[i]>0)
{
count += Math.abs(b[i+1])+1;
if(b[i+1]>0) b[i+1]=-1;
else b[i+1] = 1;
}
else if(b[i+1] == 0)
{
if(b[i]>0) b[i+1]=-1;
else b[i+1] = 1;
count++;
}
}
System.out.println(count);
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ull = uint64_t;
using ll = int64_t;
using PII = pair<int, int>;
using VI = vector<int>;
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto& x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
int main() {
ios::sync_with_stdio(false), cin.tie(0);
int N;
cin >> N;
VI V(N);
for (int _n = N, i = 0; i < _n; ++i) cin >> V[i];
if (V[0]) {
ll sum = V[0];
ll ans = 0;
for (int i = (1), _b = (N - 1); i <= _b; ++i) {
ll nsum = sum + V[i];
ll target = (ll)-1 * (sum / abs(sum));
if (nsum == 0) {
ans += abs(target - nsum);
sum = target;
} else {
ll nsign = nsum / abs(nsum);
if (nsign == target) {
sum = nsum;
continue;
} else {
ans += abs(target - nsum);
sum = target;
}
}
}
cout << ans << endl;
} else {
ll ans1 = 1;
ll sum = 1;
for (int i = (1), _b = (N - 1); i <= _b; ++i) {
ll nsum = sum + V[i];
ll target = (ll)-1 * (sum / abs(sum));
if (nsum == 0) {
ans1 += abs(target - nsum);
sum = target;
} else {
ll nsign = nsum / abs(nsum);
if (nsign == target) {
sum = nsum;
continue;
} else {
ans1 += abs(target - nsum);
sum = target;
}
}
}
ll ans2 = 1;
sum = -1;
for (int i = (1), _b = (N - 1); i <= _b; ++i) {
ll nsum = sum + V[i];
ll target = (ll)-1 * (sum / abs(sum));
if (nsum == 0) {
ans2 += abs(target - nsum);
sum = target;
} else {
ll nsign = nsum / abs(nsum);
if (nsign == target) {
sum = nsum;
continue;
} else {
ans2 += abs(target - nsum);
sum = target;
}
}
}
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 ll = long long;
using namespace std;
const double pi = acos(-1.0);
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
void print(vector<ll> vec) {
for (int i = 0; i < ((ll)(vec).size()); i++) {
if (i) cout << " ";
cout << vec[i];
}
cout << "\n";
}
ll dp[100005];
int main() {
ll n;
cin >> n;
vector<ll> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
dp[0] = a[0];
ll ans = 0;
for (int i = 1; i < n; i++) {
if (dp[i - 1] < 0) {
if (dp[i - 1] + a[i] > 0) {
dp[i] = dp[i - 1] + a[i];
} else {
ll ai = 1 - dp[i - 1];
ans += abs(ai - a[i]);
dp[i] = 1;
}
}
if (dp[i - 1] > 0) {
if (dp[i - 1] + a[i] < 0) {
dp[i] = dp[i - 1] + a[i];
} else {
ll ai = -1 - dp[i - 1];
ans += abs(ai - a[i]);
dp[i] = -1;
}
}
}
cout << "\n";
cout << ans << "\n";
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python2 | # -*- coding: utf-8 -*-
n = input()
a = map(int, raw_input().split())
asum = [0]*len(a)
asum[0] = a[0]
cnt = 0
for i in range(len(a)-1):
asum[i+1] = a[i+1] + asum[i]
if(asum[i]*asum[i+1]>=0):
if(asum[i+1]>=0):
cnt += asum[i+1]+1
asum[i+1] = -1
else:
cnt += (-1)*asum[i+1]+1
asum[i+1] = 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 main() {
int t;
vector<int> answer(2, 0);
int sumi;
bool flag = true;
cin >> t;
vector<int> A(t);
for (int i = 0; i < t; i++) {
cin >> A[i];
}
for (int j = 0; j < 2; j++) {
for (int i = 0; i < t; i++) {
sumi += A[i];
if (sumi == 0) {
answer[i] += 1;
if (flag) {
sumi = -1;
} else {
sumi = 1;
}
} else if (sumi > 0 == flag) {
answer[i] += abs(sumi) + 1;
if (sumi > 0) {
sumi = -1;
} else {
sumi = 1;
}
}
flag = !flag;
}
flag = false;
}
cout << min(answer[0], answer[1]) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <ctype.h>
#include <stdint.h>
#include <string.h>
#include <wchar.h>
#include <math.h>
#define N_MAX (100)
#define P_MAX (100)
#define DP_ARRAY_SIZE (N_MAX * P_MAX / 32 + 1)
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define ABS(a) ((a) < 0 ? -(a) : (a))
#define ABSS(a, b) ((a) > (b) ? (a) - (b) : (b) - (a))
int compare_sz_asc(const void* a, const void* b) {
return *((size_t*)a) < *((size_t*)b) ? -1 : 1;
}
int compare_sz_desc(const void* a, const void* b) {
return *((size_t*)a) > * ((size_t*)b) ? -1 : 1;
}
int compare_i64_asc(const void* a, const void* b) {
return *((int64_t*)a) < *((int64_t*)b) ? -1 : 1;
}
int compare_i64_desc(const void* a, const void* b) {
return *((int64_t*)a) > * ((int64_t*)b) ? -1 : 1;
}
int compare_c_asc(const void* a, const void* b) {
return *((char*)a) < *((char*)b) ? -1 : 1;
}
int compare_c_desc(const void* a, const void* b) {
return *((char*)a) > * ((char*)b) ? -1 : 1;
}
static size_t powSz(const size_t base, const size_t exp) {
if (exp == 0) {
return 1;
}
if (exp == 1) {
return base;
}
if (exp % 2 == 0) {
return powSz(base * base, exp / 2);
}
else {
return base * powSz(base, exp - 1);
}
}
static size_t comb(const size_t n, const size_t r) {
size_t result = 1;
for (size_t i = 0; i < r; i++) {
result *= n - i;
result /= i + 1;
}
return result;
}
static uint64_t combU64(const uint64_t n, const uint64_t r) {
uint64_t result = 1;
for (uint64_t i = 0; i < r; i++) {
result *= n - i;
result /= i + 1;
}
return result;
}
static size_t gcdZu(size_t m, size_t n) {
size_t temp;
while (m % n != 0) {
temp = n;
n = m % n;
m = temp;
}
return n;
}
static uint64_t gcdU64(uint64_t m, uint64_t n)
{
uint64_t temp;
while (m % n != 0) {
temp = n;
n = m % n;
m = temp;
}
return n;
}
static int64_t a[100000];
int main(void) {
size_t n;
scanf("%zu\n", &n);
for (size_t i = 0; i < n; i++) {
scanf("%"PRId64, &a[i]);
}
size_t cnt[2] = { 0,0 };
int64_t base[2] = { 1,-1 };
int64_t sum = 0;
for (size_t i = 0; i < n; i++) {
sum += a[i];
cnt[0] = (size_t)ABSS(base[0], sum);
cnt[1] = (size_t)ABSS(base[1], sum);
base[0] = -base[0];
base[1] = -base[1];
}
printf("%zu", MIN(cnt[0], cnt[1]));
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import copy
n = int(input())
a = [int(i) for i in input().split()]
b=a.copy()
s0p = a[0]
s0n = b[0]
countp = 0
countn = 0
if s0p<=0:
while s0p<=0:
s0p+=1
countp+=1
if s0n>=0:
while s0n>=0:
s0n-=1
countn+=1
for i in range(1,n):
s1 = s0p+a[i]
"""print("i",i,"a[i]",a[i],"s0p",s0p,"s1",s1,"countp",countp)"""
if s0p*s1>=0:
if s1>0:
a[i]-=(abs(s1)+1)
countp+=(abs(s1)+1)
elif s1<0:
a[i]+=(abs(s1)+1)
countp+=(abs(s1)+1)
elif s1==0:
if s0p>0:
a[i]-=1
countp+=1
elif s0p<0:
a[i]+=1
countp+=1
s0p += a[i]
for i in range(1,n):
s1 = s0n+b[i]
"""print("i",i,"b[i]",b[i],"s0n",s0n,"s1",s1,"countn",countn)"""
if s0n*s1>=0:
if s1>0:
b[i]-=(abs(s1)+1)
countn+=(abs(s1)+1)
elif s1<0:
b[i]+=(abs(s1)+1)
countn+=(abs(s1)+1)
elif s1==0:
if s0n>0:
b[i]-=1
countn+=1
elif s0n<0:
b[i]+=1
countn+=1
s0n += b[i]
print(countp if countp<=countn else(countn))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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, a, b, s[100000], ans, ans1;
int main(void) {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> s[i];
}
for (int i = 0; i < n; i++) {
a += s[i];
if (i % 2 == 0) {
if (a <= 0) {
ans += -a + 1;
a = 1;
}
} else {
if (a >= 0) {
ans += a + 1;
a = -1;
}
}
}
a = 0;
ans1 = ans;
ans = 0;
for (int i = 0; i < n; i++) {
a += s[i];
if (i % 2 == 1) {
if (a <= 0) {
ans += -a + 1;
a = 1;
}
} else {
if (a >= 0) {
ans += a + 1;
a = -1;
}
}
}
cout << min(ans, ans1) << 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 | using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AtCoder
{
class Code3
{
static void Main(string[] args)
{
string s1 = Console.ReadLine();
string s2 = Console.ReadLine();
Console.WriteLine(funcMain(s1,s2));
}
static private string funcMain(string arg1, string arg2)
{
long ret = 0;
long sum = 0;
foreach (string buf in arg2.Split())
{
if (sum == 0)
sum = int.Parse(buf);
else
{
if (sum > 0)
{
sum += int.Parse(buf);
if (sum >= 0)
{
ret += sum + 1;
sum = -1;
}
}
else
{
sum += int.Parse(buf);
if (sum <= 0)
{
ret += (sum * -1) + 1; // 絶対値の関数探すのがめんどくさかった
sum = 1;
}
}
}
}
return ret.ToString();
}
static private void test()
{
string arg1, arg2;
arg1 = "4";
arg2 = "1 -3 1 0";
Console.WriteLine("4" == funcMain(arg1, arg2));
arg1 = "5";
arg2 = "3 -6 4 -5 7";
Console.WriteLine("0" == funcMain(arg1, arg2));
arg1 = "6";
arg2 = "-1 4 3 2 -5 4";
Console.WriteLine("8" == funcMain(arg1, arg2));
Console.ReadKey();
}
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, ansa = 0, ansb = 0, suma = 0, sumb = 0;
cin >> n;
for (int i = 0; i < (n); i++) {
int a, b;
cin >> b;
a = b;
if (i % 2 == 0) {
if (suma + a <= 0) {
ansa = 1 - a - suma;
suma = 1;
}
if (sumb + b >= 0) {
ansb = sumb + b + 1;
sumb = -1;
}
} else {
if (suma + a >= 0) {
ansa = suma + a + 1;
suma = 1;
}
if (sumb + b <= 0) {
ansb = 1 - b - sumb;
sumb = -1;
}
}
}
cout << min(ansa, ansb) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | 'use strict'
let input = require("fs").readFileSync("/dev/stdin", "utf8");
let Nums = input.split('\n');
let amount = Nums[0]*1;
let arr = Nums[1].split(" ").map(x => x*1);
let sum = 0;
let ans = 0;
// 一番最初の正負判定フラグ
let isInitPlus = arr[0] > 0 ? true : false;
for(let i = 0; i < amount; i++){
// 和
sum += arr[i];
if((sum >= 0) != isInitPlus){
ans += Math.abs(sum) + 1;
sum = isInitPlus == true? 1 : -1;
}
isInitPlus =!isInitPlus;
}
// 和が0になってしまった時の処理
if(sum == 0){
ans += 1;
}
console.log(ans); |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> arr(n);
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
int aux;
int sol = 0;
if (arr[0] == 0) {
if (arr[1] > 0) {
arr[0] = -1;
} else {
arr[0] = 1;
}
sol++;
}
int acum = arr[0];
bool sign = acum < 0 ? false : true;
for (int i = 1; i < n; i++) {
aux = acum + arr[i];
if (sign) {
if (aux >= 0) {
sol += abs(acum + 1 + arr[i]);
arr[i] = -(acum + 1);
}
sign = false;
} else {
if (aux <= 0) {
sol += abs(acum - 1 + arr[i]);
arr[i] = -(acum - 1);
}
sign = true;
}
acum = acum + arr[i];
}
cout << sol << "\n";
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};
int ny, nx;
using namespace std;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long m, long long n) {
if ((0 == m) || (0 == n)) return 0;
return ((m / gcd(m, n)) * n);
}
long long llpow(long long x, long long y) {
long long ans = 1;
for (int i = 0, i_len = (y); i < i_len; ++i) ans *= x;
return ans;
}
int ctoi(char c) {
if (c >= '0' && c <= '9') {
return c - '0';
}
return 0;
}
class UnionFind {
public:
vector<long long> par;
vector<long long> siz;
UnionFind(long long sz_) : par(sz_), siz(sz_, 1LL) {
for (long long i = 0; i < sz_; ++i) par[i] = i;
}
void init(long long sz_) {
siz.assign(sz_, 1LL);
par.resize(sz_);
for (long long i = 0; i < sz_; ++i) par[i] = i;
}
long long root(long long x) {
while (par[x] != x) {
x = par[x] = par[par[x]];
}
return x;
}
bool merge(long long x, long long y) {
x = root(x);
y = root(y);
if (x == y) return false;
if (siz[x] < siz[y]) swap(x, y);
siz[x] += siz[y];
par[y] = x;
return true;
}
bool issame(long long x, long long y) { return root(x) == root(y); }
long long size(long long x) { return siz[root(x)]; }
};
template <class T>
inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T>
inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0, i_len = (n); i < i_len; ++i) cin >> a[i];
int odd = 0, even = 0;
bool flag = false;
long long total = 0, res = 0;
for (int i = 0, i_len = (n); i < i_len; ++i) {
total += a[i];
if (i % 2 != 0) {
if (total >= 0) {
even += total + 1;
total = -1;
}
} else {
if (total <= 0) {
even += -total + 1;
total = 1;
}
}
}
for (int i = 0, i_len = (n); i < i_len; ++i) {
res += a[i];
if (i % 2 == 0) {
if (res >= 0) {
odd += res + 1;
res = -1;
}
} else {
if (res <= 0) {
odd += -res + 1;
res = 1;
}
}
}
cout << min(odd, even) << 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 array[n];
for (int i = 0; i < n; i++) {
cin >> array[i];
}
long long answer = 0;
long long sum = 0;
for (int i = 0; i < n; i++) {
if (sum == 0)
sum += array[0];
else if (sum < 0) {
if (sum + array[i] > 0) {
sum += array[i];
} else {
answer += abs((-1) * sum + 1 - array[i]);
sum = 1;
}
} else {
if (sum + array[i] < 0) {
sum += array[i];
} else {
answer += abs((-1) * sum - 1 - array[i]);
sum = -1;
}
}
}
cout << answer << 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 | object Main {
def main(args: Array[String]): Unit = {
solve
}
def solve(): Unit = {
val sc = new java.util.Scanner(System.in)
val n = sc.nextInt
sc.nextLine
val a = sc.nextLine.split(" ").map(_.toInt).toList
println(a)
var prevSum = a(0)
var opeCount = 0
for (i <- 1 until n) {
val currSum = prevSum + a(i)
if (prevSum < 0 && currSum < 0) {
opeCount += math.abs(currSum) + 1
prevSum = 1
} else if (prevSum > 0 && currSum > 0) {
opeCount += math.abs(currSum) + 1
prevSum = -1
} else {
if (currSum == 0) {
opeCount += 1
if (prevSum < 0) {
prevSum = 1
} else {
prevSum = -1
}
} else {
prevSum = prevSum + a(i)
}
}
}
println(opeCount)
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007, INF = 1111111111;
const double EPS = 1e-9;
int main() {
cin.tie(0);
int n;
cin >> n;
vector<long long> a(n);
for (int i = (int)(0); i < (int)(n); ++i) cin >> a[i];
for (int i = (int)(1); i < (int)(n); ++i) a[i] = a[i - 1] + a[i];
for (const auto &ele : a) ((void)0);
long long tmp = 0, ans = 0, prev_sum = a[0];
for (int i = (int)(1); i < (int)(n); ++i) {
if (prev_sum > 0 && a[i] + tmp >= 0) {
ans += a[i] + tmp + 1;
tmp -= a[i] + tmp + 1;
}
if (prev_sum < 0 && a[i] + tmp <= 0) {
ans -= a[i] + tmp - 1;
tmp -= a[i] + tmp - 1;
}
prev_sum *= -1;
}
cout << ans;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int MaxN = 1e5;
bool flag, ok;
long long sum, ans, anv;
int n;
int a[MaxN + 5], b[MaxN + 5];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
b[i] = a[i];
}
sum = a[1];
if (a[1] < 0) flag = 1, ok = 1;
for (int i = 2; i <= n; i++) {
if (flag == 1) {
if (sum + a[i] <= 0) {
long long ant = sum + a[i];
int t = a[i];
a[i] = 1 - sum;
ans += (a[i] - t);
sum += a[i];
} else
sum += a[i];
flag = 0;
} else {
if (sum + a[i] >= 0) {
long long ant = sum + a[i];
int t = a[i];
a[i] = -1 - sum;
ans += (t - a[i]);
sum += a[i];
} else
sum += a[i];
flag = 1;
}
}
int tr = b[1];
if (ok)
b[1] = 1, flag = 0;
else
b[1] = -1, flag = 1;
anv += (abs(b[1] - tr));
sum = b[1];
for (int i = 2; i <= n; i++) {
if (flag == 1) {
if (sum + b[i] <= 0) {
long long ant = sum + b[i];
int t = b[i];
b[i] = 1 - sum;
anv += (b[i] - t);
sum += b[i];
} else
sum += b[i];
flag = 0;
} else {
if (sum + b[i] >= 0) {
long long ant = sum + b[i];
int t = b[i];
b[i] = -1 - sum;
anv += (t - b[i]);
sum += b[i];
} else
sum += b[i];
flag = 1;
}
}
printf("%lld\n", min(ans, anv));
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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> as(n);
for (int i = 0; i < n; i++) {
cin >> as[i];
}
int kotae1 = 0;
int sum1 = 0;
int kotae2 = 0;
int sum2 = 0;
for (int i = 0; i < n; i++) {
sum1 += as[i];
if (i % 2 == 0) {
if (sum1 <= 0) {
kotae1 += (1 - sum1);
sum1 = 1;
}
} else {
if (sum1 >= 0) {
kotae1 += (sum1 - (-1));
sum1 = -1;
}
}
}
for (int i = 0; i < n; i++) {
sum2 += as[i];
if (i % 2 == 1) {
if (sum2 <= 0) {
kotae2 += (1 - sum2);
sum2 = 1;
}
} else {
if (sum2 >= 0) {
kotae2 += (sum2 - (-1));
sum2 = -1;
}
}
}
int kotae = 0;
kotae = kotae1 < kotae2 ? kotae1 : kotae2;
cout << kotae << 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 | n = gets.to_i
arr = gets.chomp.split(" ").map(&:to_i)
$count = [0,0]
flg = true
2.times do |j|
tmp_arr = Marshal.load(Marshal.dump(arr))
sum = tmp_arr[0] + tmp_arr[1]
if sum == 0
if flg
tmp_arr[1] -= 1
else
tmp_arr[1] += 1
end
$count[j] += 1
end
if flg
if sum > 0
tmp_arr[1] -= sum+1
$count[j] += sum+1
end
else
if sum < 0
tmp_arr[1] += sum+1
$count[j] += sum+1
end
end
sum = tmp_arr[0] + tmp_arr[1]
(2...tmp_arr.size).each do |i|
diff = sum + tmp_arr[i]
# puts %(sum : #{sum})
# puts %(diff : #{diff})
if sum > 0
if diff >= 0
tmp_arr[i] -= diff.abs+1
$count[j] += diff.abs+1
end
else
if diff <= 0
tmp_arr[i] += diff.abs+1
$count[j] += diff.abs+1
end
end
sum += tmp_arr[i]
# p sum
# p tmp_arr
end
flg = false
end
#p $count
#p arr
puts $count.min |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = [int(i) for i in input().split()]
a1 = a.copy()
cnt1 = 0
sum1 = 0
if a1[0] == 0:
a1[0] = 1
cnt1 +=1
a2 = a.copy()
cnt2 = 0
sum2 = 0
if a2[0] == 0:
a2[0] = -1
cnt2 +=1
for i in range(n-1):
sum1 = sum(a1[:i+1])
if sum1 > 0 and (sum1+a1[i+1]) >= 0:
cnt1 += abs(((sum1 * -1)-1) - a1[i+1])
a1[i+1] = ((sum1 * -1)-1)
elif sum1 < 0 and (sum1 + a1[i+1]) <= 0:
cnt1 += abs(abs(sum1)+1 - a1[i+1])
a1[i+1] = abs(sum1)+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;
vector<int> a(N);
for (int i = 0; i < N; i++) cin >> a[i];
int count = 0;
if (a[0] == 0) {
for (int i = 1; i < N; i++) {
if (a[i] > 0) {
a[0] = -1;
break;
} else if (a[i] < 0) {
a[0] = 1;
break;
}
}
count++;
}
int sum = a[0];
for (int i = 1; i < N; i++) {
if (sum * a[i] < 0 && abs(sum) < abs(a[i])) {
sum += a[i];
} else {
if (sum > 0) {
count += a[i] + sum + 1;
sum = -1;
} else {
count += 1 - sum - a[i];
sum = 1;
}
}
}
cout << count << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; ++i) cin >> a[i];
long long sum = a[0], befsum = a[0];
long long ans = 0;
for (int i = 1; i < n; ++i) {
sum += a[i];
if (sum == 0) {
ans += 1;
if (befsum > 0)
sum = -1;
else
sum = 1;
} else if (sum * befsum > 0) {
if (sum > 0) {
ans += sum + 1;
sum = -1;
} else {
ans += -sum + 1;
sum = 1;
}
}
befsum = sum;
}
long long tmp = abs(a[0]) + 1;
if (a[0] > 0) {
sum = -1;
befsum = -1;
} else {
sum = 1;
befsum = 1;
}
for (int i = 1; i < n; ++i) {
sum += a[i];
if (sum == 0) {
tmp += 1;
if (befsum > 0)
sum = -1;
else
sum = 1;
} else if (sum * befsum > 0) {
if (sum > 0) {
tmp += sum + 1;
sum = -1;
} else if (sum < 0) {
tmp += -sum + 1;
sum = 1;
}
}
befsum = sum;
}
ans = min(ans, tmp);
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 sum, ans, n, i;
int a[100005];
int main() {
cin >> n;
for (i = 1; i <= n; i++) {
cin >> a[i];
}
ans = 0;
sum = 0;
for (i = 1; i <= n; i++) {
if (a[i] == 0) {
sum++;
} else
break;
}
if (sum != 0) {
for (i = sum; i > 1; i--) {
if (a[i + 1] > 0) {
a[i] = -1;
} else {
a[i] = 1;
}
ans++;
}
if (a[2] > 0) {
a[1] = -2;
ans += 2;
} else {
a[1] = 2;
ans += 2;
}
}
sum = a[1];
for (i = 2; i <= n; i++) {
if (sum == 0) {
if (a[i - 1] > 0) {
ans++;
sum--;
} else {
ans++;
sum++;
}
}
if (sum > 0) {
if (a[i] + sum >= 0) {
ans += a[i] + sum + 1;
sum = -1;
} else {
sum += a[i];
}
} else {
if (a[i] + sum <= 0) {
ans += abs(a[i] + sum) + 1;
sum = 1;
} else {
sum += a[i];
}
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | import qualified Data.Vector.Unboxed as VU
import qualified Data.ByteString.Char8 as B
import Data.Char
solve :: VU.Vector Int -> Int -> Int
solve vec n
| VU.length vec == 2 && VU.sum vec == 0 = 1
| VU.length vec == 2 && VU.sum vec /= 0 = 0
| otherwise = minimum $ map fst [f, g]
where
t = VU.take 2 vec
d = VU.drop 2 vec
f = VU.foldl' step (fst $ init t) d
g = VU.foldl' step (snd $ init t) d
init :: VU.Vector Int -> ((Int, Int), (Int, Int))
init vec
| a + b == 0 = ((1, 1), (1, negate 1))
| a + b > 0 = ((0, a + b), (1 + a + b, negate 1))
| otherwise = ((0, a + b), (abs (1 - (a + b)), 1))
where
a = VU.head vec
b = VU.last vec
step :: (Int, Int) -> Int -> (Int, Int)
step (res, acc) x
| acc + x == 0 = (res + (x + 1) , negate (signum acc))
| (signum acc) /= signum (acc + x) = (res, acc + x)
| otherwise =
let
aim = negate $ signum acc
y = aim - (acc + x)
in
(res + abs y, aim)
main = do
n <- readLn :: IO Int
as <- VU.unfoldrN n (B.readInt . B.dropWhile isSpace) <$> B.getLine
print $ solve as 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) {
long long n;
cin >> n;
bool lastPalus = true;
long long a[n];
for (long long i = 0; i < n; i++) {
cin >> a[i];
}
long long sum = 0;
long long ans = 0;
for (long long i = 0; i < n; i++) {
if (sum == 0) {
if (a[i] == 0) {
sum += a[i + 1] > 0 ? -1 : 1;
ans++;
} else {
sum += a[i];
}
} else if (sum > 0) {
if (sum + a[i] >= 0) {
ans += abs(sum + a[i]) + 1;
sum = -1;
} else {
sum += a[i];
}
} else {
if (sum + a[i] <= 0) {
ans += abs(sum + a[i]) + 1;
sum = 1;
} else {
sum += a[i];
}
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | fun main(args: Array<String>) {
val n = readLine()!!.toInt()
val l = readLine()!!.split(" ").map { it.toInt() }
var s_pls = 0
var s_mns = 0
var tmp = 0
var pls_cnt = 0
var mns_cnt = 0
l.forEach {
tmp += 1
s_pls += it
s_mns += it
if (tmp % 2 == 0){
while(s_pls <= 0){
pls_cnt += 1
s_pls += 1
}
while(s_mns >= 0){
mns_cnt += 1
s_mns -= 1
}
}
else {
while(s_pls >= 0){
pls_cnt += 1
s_pls -= 1
}
while(s_mns <= 0){
mns_cnt += 1
s_mns += 1
}
}
}
println(Math.min(Math.abs(pls_cnt), Math.abs(mns_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;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, x, n) for (int i = x; i < (n); i++)
#define all(n) begin(n), end(n)
struct cww
{
cww()
{
ios::sync_with_stdio(false);
cin.tie(0);
}
} star;
const long long INF = numeric_limits<long long>::max();
typedef long long ll;
typedef vector<int> vint;
typedef vector<char> vchar;
typedef vector<vector<int>> vvint;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef unsigned long long ull;
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;
}
template <typename T>
vector<T> make_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts>
auto make_v(size_t a, Ts... ts)
{
return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type
fill_v(T &t, const V &v) { t = v; }
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type
fill_v(T &t, const V &v)
{
for (auto &e : t)
fill_v(e, v);
}
template <typename Set>
struct Monoid
{
using Op = function<Set(Set, Set)>;
const Op f;
const Set e;
Monoid(const Op F, const Set E) : f(F), e(E)
{
}
};
template <typename Set>
struct SegmentTree
{
using Op = function<Set(Set, Set)>;
Monoid<Set> M;
Set e;
int size; //一番下以外の区間たち全部の数
vector<Set> seg;
SegmentTree(int n, const Monoid<Set> &M) : M(M)
{
size = 1;
while (size < n)
{
size <<= 1;
}
e = M.e;
seg.assign(2 * size, e);
}
void set(int k, const Set &x)
{
seg[k + size] = x;
}
void build()
{
for (int k = size - 1; k > 0; k--)
{
seg[k] = M.f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
void update(int k, const Set &x)
{
k += size;
seg[k] = x;
while (k >>= 1)
{
seg[k] = M.f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
Set query(int a, int b) //区間[a,b)に対して演算したやつを返す
{
Set L = e, R = e;
for (a += size, b += size; a < b; a >>= 1, b >>= 1) //ループ一回ごとにa,bは親に向かう
{
if (a & 1) //a%2==1なら
{
L = M.f(L, seg[a++]);
}
if (b & 1)
{
R = M.f(seg[--b], R);
}
}
return M.f(L, R);
}
};
int main()
{
int n;
cin >> n;
vint a(n), s(n + 1);
Monoid<int> M([](int a, int b) { return a + b; }, 0);
SegmentTree<int> sg(n, M), sg2(n, M);
rep(i, n)
{
cin >> a[i];
s[i + 1] = s[i] + a[i];
sg.update(i, a[i]);
sg2.update(i, a[i]);
}
int prev = 1, ans = 0;
rep(i, n)
{
int sum = sg.query(0, i + 1);
if (sum == 0)
{
if (prev < 0)
{
sg.update(i, sg.query(i, i + 1) + 1);
}
else
{
sg.update(i, sg.query(i, i + 1) - 1);
}
if (prev != 0)
prev *= -1;
else
prev = 1;
ans++;
continue;
}
else
{
int sgn = sum / abs(sum);
if (prev == sgn)
{
int cnt = -sgn - sum;
ans += abs(cnt);
sg.update(i, sg.query(i, i + 1) + cnt);
}
prev *= -1;
}
}
prev = -1;
int ans2 = 0;
rep(i, n)
{
int sum = sg2.query(0, i + 1);
if (sum == 0)
{
if (prev < 0)
{
sg2.update(i, sg2.query(i, i + 1) + 1);
}
else
{
sg2.update(i, sg2.query(i, i + 1) - 1);
}
if (prev != 0)
prev *= -1;
else
prev = 1;
ans2++;
continue;
}
else
{
int sgn = sum / abs(sum);
if (prev == sgn)
{
int cnt = -sgn - sum;
ans2 += abs(cnt);
sg2.update(i, sg2.query(i, i + 1) + cnt);
}
prev *= -1;
}
}
cout << min(ans, ans2);
return 0;
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, d, a = 0, b = 0, cnta = 0, cntb = 0, i, ans;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &d);
a += d;
b += d;
if (i % 2) {
if (a > -1) {
cnta += a + 1;
a = -1;
}
if (b < 1) {
cntb += 1 - b;
b = 1;
}
} else {
if (b > -1) {
cntb += b + 1;
b = -1;
}
if (a < 1) {
cnta += 1 - a;
a = 1;
}
}
}
ans = (cnta < cntb) ? cnta : cntb;
printf("%d\n", 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(void) {
long long n, ans = 0;
scanf("%lld", &n);
long long a[n], sum[n];
scanf("%lld", &a[0]);
sum[0] = a[0];
for (long long i = 1; i < n; i++) {
scanf("%lld", &a[i]);
sum[i] = a[i] + sum[i - 1];
if (sum[i - 1] < 0) {
if (sum[i] == 0) {
sum[i]++;
ans++;
} else if (sum[i] < 0) {
ans += (llabs(sum[i]) + 1);
sum[i] += (llabs(sum[i]) + 1);
}
} else {
if (sum[i] == 0) {
sum[i]--;
ans++;
} else if (sum[i] > 0) {
ans += (llabs(sum[i]) + 1);
sum[i] -= (llabs(sum[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;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a.at(i);
}
long long sum1 = a.at(0);
long long sum2 = a.at(0);
long long op1 = 0;
long long op2 = 0;
if (sum1 < 0) {
sum1 = 1;
op1 += -1 * a.at(0) + 1;
}
if (sum2 > 0) {
sum2 = -1;
op2 += a.at(0) + 1;
}
for (int j = 1; j < n; j++) {
if (sum1 > 0) {
sum1 += a.at(j);
if (sum1 >= 0) {
op1 += (sum1 + 1);
sum1 = -1;
}
} else {
sum1 += a.at(j);
if (sum1 <= 0) {
op1 += (-1 * sum1 + 1);
sum1 = 1;
}
}
if (sum2 > 0) {
sum2 += a.at(j);
if (sum2 >= 0) {
op2 += (sum2 + 1);
sum2 = -1;
}
} else {
sum2 += a.at(j);
if (sum2 <= 0) {
op2 += (-1 * sum2 + 1);
sum2 = 1;
}
}
}
cout << (op1 > op2 ? op2 : op1) << 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(void) {
int n;
cin >> n;
vector<int> v(n);
for (int i = 0; i < n; i++) cin >> v[i];
int op = 0;
int prev_sum = 0;
for (int i = 0; i < n; i++) {
int new_sum = prev_sum + v[i];
if (i == 0)
prev_sum = new_sum;
else if (prev_sum >= 0 && new_sum >= 0) {
op += new_sum + 1;
prev_sum = -1;
} else if (prev_sum <= 0 && new_sum <= 0) {
op += -new_sum + 1;
prev_sum = 1;
} else
prev_sum = new_sum;
}
cout << op << 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 MAX_N = 1 << 17;
using namespace std;
long long int dy[] = {0, 0, 1, -1, 0};
long long int dx[] = {1, -1, 0, 0, 0};
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;
}
long long int gcd(long long int a, long long int b) {
return b ? gcd(b, a % b) : a;
}
struct aaa {
aaa() {
cin.tie(0);
ios::sync_with_stdio(0);
cout << fixed << setprecision(20);
};
} aaaaaaa;
signed main() {
long long int n;
std::cin >> n;
std::vector<long long int> a;
for (long long int(i) = 0, i_len = (n); (i) < i_len; (i)++) {
long long int temp;
std::cin >> temp;
a.push_back(temp);
}
long long int sum = 0;
long long int out = 0;
long long int outt = 0;
sum = a[0];
for (long long int i = 1; i < n; i++) {
if (sum < 0) {
if (a[i] + sum > 0) {
sum = a[i] + sum;
} else {
out += abs(sum + a[i]) + 1;
sum = 1;
}
} else {
if (a[i] + sum < 0) {
sum = a[i] + sum;
} else {
out += abs(sum + a[i]) + 1;
sum = -1;
}
}
}
std::cout << out << 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 | 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 + i
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 | cpp | #include <bits/stdc++.h>
bool N(int a) { return (a > 0); }
int main() {
int a, n;
int ans = 0;
int sum = 0;
std::cin >> n;
std::cin >> a;
sum += a;
for (int i = 0; i < n - 1; ++i) {
std::cin >> a;
if (N(sum + a) == N(sum)) {
int hoge;
if (N(sum + a))
hoge = -1 - (sum + a);
else
hoge = 1 - (sum + a);
ans += std::abs(hoge);
sum = sum + a + hoge;
} else
sum += a;
}
std::cout << ans << std::endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | # -*- coding: utf-8 -*-
"""
Created on Sat Sep 8 15:51:53 2018
@author: maezawa
"""
n = int(input())
a = list(map(int, input().split()))
sa = 0
cnt = 0
for i in range(0,n-1):
sa += a[i]
na = -sa//abs(sa)*(abs(sa)+1)
if abs(a[i+1]) > abs(na) and a[i+1]*na >= 0:
continue
cnt += abs(na-a[i+1])
a[i+1] = na
print(cnt)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | 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]++;
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 | cpp | #include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9 + 7;
const int MAX = 1e6;
int status(int a) {
if (a < 0) return 1;
return 0;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long int n, cnt = 0, last = -1, a, f = 0, sum = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a;
if (!i) {
last = a;
sum = a;
f = status(a) ^ 1;
} else {
if (status(last) == status(a)) {
cnt += abs(a);
a = 0;
}
sum += a;
if (status(sum) != f) {
cnt += abs(sum) + 1;
if (f == 1)
sum = -1;
else
sum = 1;
} else if (sum == 0) {
cnt++;
if (f == 1)
sum = -1;
else
sum = 1;
}
if (!a) a = sum;
f ^= 1;
last = a;
}
}
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;
void sum(int *N, long long *S, int n);
int main() {
int *N;
long long *S;
long long count_eve = 0, count_odd = 0, n;
int j = 0, k = 0;
cin >> n;
N = new int[n];
S = new long long[n];
for (int i = 0; i < n; i++) {
cin >> N[i];
}
sum(N, S, n);
int del1 = 0, del2 = 0;
while (j != n) {
if (j % 2 == 0 && S[j] + del1 <= 0) {
count_eve += abs(S[j] + del1) + 1;
del1 += abs(S[j] + del1) + 1;
} else if (j % 2 == 1 && S[j] + del1 >= 0) {
count_eve += abs(S[j] + del1) + 1;
del1 += -abs(S[j] + del1) - 1;
}
j++;
}
sum(N, S, n);
while (k != n) {
if (k % 2 == 0 && S[k] + del2 >= 0) {
count_odd += abs(S[k] + del2) + 1;
del2 += -abs(S[k] + del2) - 1;
} else if (k % 2 == 1 && S[k] + del2 <= 0) {
count_odd += abs(S[k] + del2) + 1;
del2 += abs(S[k] + del2) + 1;
}
k++;
}
cout << min(count_eve, count_odd) << endl;
delete[] N;
delete[] S;
return 0;
}
void sum(int *N, long long *S, int n) {
S[0] = N[0];
for (int i = 1; i < n; i++) S[i] = S[i - 1] + N[i];
}
void add(int *S, int n, int del, int k) {
for (int i = k; i < n + 1; i++) S[i] += del;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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 ll INF = 1LL << 60;
int main() {
ll n;
cin >> n;
ll a[n];
for (int i = 0; i < (int)(n); i++) {
cin >> a[i];
}
ll sum = a[0];
ll ans = 0;
for (int i = 1; i < n; i++) {
ll tmp_sum = sum + a[i];
cout << "debug sum: " << sum << endl;
cout << "debug tmp_sum: " << tmp_sum << endl;
if (sum > 0) {
while (tmp_sum >= 0) {
--tmp_sum;
++ans;
}
} else if (sum < 0) {
while (tmp_sum <= 0) {
++tmp_sum;
++ans;
}
}
sum = tmp_sum;
}
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(i) for i in input().split()]
ans = 0
tmp = a[0]
if a[0] == 0:
tmp = 1
ans += 1
for i in range(1,n):
#print(tmp,ans)
if tmp > 0:
if tmp + a[i] >= 0:
ans += tmp + a[i] + 1
tmp = -1
else:
tmp += a[i]
else:
if tmp + a[i] <= 0:
ans += abs(tmp + a[i]) + 1
tmp = 1
else:
tmp += a[i]
#print(ans)
ans2 = 0
if a[0] > 0:
ans2 += a[0]+1
tmp = 1
elif a[0] < 0:
ans2 += -a[0]+1
else:
tmp = -1
ans2 += 1
for i in range(1,n):
#print(tmp,ans)
if tmp > 0:
if tmp + a[i] >= 0:
ans2 += tmp + a[i] + 1
tmp = -1
else:
tmp += a[i]
else:
if tmp + a[i] <= 0:
ans2 += abs(tmp + a[i]) + 1
tmp = 1
else:
tmp += a[i]
print(min(ans,ans2))
exit()
ans2 = 0
tmp = -a[0]
ans2 += abs(a[0])*2
for i in range(1,n):
#print(tmp,ans)
if tmp > 0:
if tmp + a[i] >= 0:
ans2 += tmp + a[i] + 1
tmp = -1
else:
tmp += a[i]
else:
if tmp + a[i] <= 0:
ans2 += abs(tmp + a[i]) + 1
tmp = 1
else:
tmp += a[i]
print(min(ans,ans2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | l = int(input())
n = [int(i) for i in input().split()]
count = 0
nsum = n[0]
for i in range(1, l):
while(True):
if (n[i-1] * n[i] >= 0) or (nsum * (nsum + n[i]) >= 0):
if n[i-1] < 0:
count += 1
n[i] += 1
else:
count += 1
n[i] -= 1
else:
nsum += n[i]
break
print(count)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include<bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define ll long long
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define set0(a) memset ((a), 0 , sizeof(a))
#define set1(a) memset((a),-1,sizeof (a))
#define pi pair<int, int>
#define ps pair<string, string>
#define pl pair<long, long>
#define pll pair<long long, long long>
#define vll vector<long long>
#define vl vector<long>
#define vi vector<int>
#define vs vector<string>
#define vps vector< ps >
#define vpi vector< pi >
#define vpl vector< pl >
#define vpll vector< pll >
#define flash ios_base::sync_with_stdio(false); cin.tie(NULL);
#define tc(t) for(long long l=0;l<t;l++)
#define rep(i,s,n,d) for(long long i=s;i<n;i=i+d)
bool sortbysec(const pll &a,
const pll &b)
{
return (a.second < b.second);
}
void func(void)
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
}
int main(){
ll n;
cin>>n;
ll a[n];
rep(i,0,n,1){
cin>>a[i];
}
ll count1=0;
if(a[0]==0){
if(a[1]>0){
a[0]=1;
}
else a[0]=-1;
count1++;
}
ll sum[n]={};
sum[0]=a[0];
rep(i,1,n,1){
sum[i]=sum[i-1]+a[i];
}
ll sum1=a[0];
rep(i,1,n,1){
if(sum1*(sum1+a[i])>=0){
ll d=1;
if(sum1<0){
d=1;
}else{
d=-1;
}
int dif=abs(sum1+a[i]-d);
count1=count1+dif;
sum1=d;
}
else{
sum1=sum1+a[i];
}
}
cout<<count1<<endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | # vim: fileencoding=utf-8
def calc(a, flg):
ans = 0
cursol = a[0]
if flg == 1:
if cursol <= 0:
cursol = 1
ans = abs(cursol) + 1
elif flg == -1:
if cursol >= 0:
cursol = -1
ans = abs(cursol) + 1
for i in a[1:]:
t = cursol + i
if cursol > 0:
if t >= 0:
ans += t + 1
cursol = -1
else:
cursol = t
elif cursol < 0:
if t <= 0:
ans += abs(t) + 1
cursol = 1
else:
cursol = t
return ans
def main():
n = int(input())
a = list(map(int, input().split()))
ans = min(calc(a, 1), calc(a, -1))
print(ans)
if __name__ == "__main__":
main()
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int sign, n, a, cnt = 0, acm = 0, ans = 0;
cin >> n;
int A[n];
for (int i = 0; i < n; i++) cin >> A[i];
sign = 1;
for (int i = 0; i < n; i++) {
if ((acm + A[i]) * sign < 0) {
acm += A[i];
sign *= -1;
} else {
cnt += abs(acm + A[i]) + 1;
acm = -1 * sign;
sign *= -1;
}
}
ans = cnt;
sign = -1;
acm = 0;
cnt = 0;
for (int i = 0; i < n; i++) {
if ((acm + A[i]) * sign < 0) {
acm += A[i];
sign *= -1;
} else {
cnt += abs(acm + A[i]) + 1;
acm = -1 * sign;
sign *= -1;
}
}
ans = min(ans, cnt);
cout << ans << "\n";
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N, count = 0;
cin >> N;
vector<long int> A(N);
for (int i = 0; i < N; i++) cin >> A[i];
int su = A[0];
bool plus = A[0] > 0;
for (int i = 1; i < N; i++) {
plus = !plus;
su += A[i];
if (plus) {
if (su <= 0) {
count += abs(su) + 1;
su = 1;
}
} else {
if (su >= 0) {
count += abs(su) + 1;
su = -1;
}
}
}
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 | cpp | #include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("03")
#pragma GCC optimize("unroll-loops")
using namespace std;
constexpr int INF = 1 << 30;
constexpr long long LINF = 1LL << 60;
constexpr long long mod = 1e9 + 7;
constexpr int NIL = -1;
template <class T>
inline bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
inline bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
int n;
const int MX = 1e5 + 7;
long long a[MX];
long long b[MX];
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> n;
for (int i = (0); i <= (n - 1); i++) cin >> a[i];
for (int i = (1); i <= (n - 1); i++) a[i] += a[i - 1];
for (int i = (0); i <= (n - 1); i++) b[i] = a[i];
long long total = 0;
long long cnt1 = 0;
long long cnt2 = 0;
for (int i = (0); i <= (n - 1); i++) {
a[i] += total;
if (i % 2 == 0) {
if (a[i] > 0)
continue;
else {
cnt1 += 1LL - a[i];
total += 1LL - a[i];
}
} else {
if (a[i] < 0)
continue;
else {
cnt1 += a[i] + 1LL;
total -= a[i] + 1LL;
}
}
}
total = 0;
for (int i = (0); i <= (n - 1); i++) {
b[i] += total;
if (i % 2 == 1) {
if (b[i] > 0)
continue;
else {
cnt2 += 1LL - b[i];
total += 1LL - b[i];
}
} else {
if (b[i] < 0)
continue;
else {
cnt2 += b[i] + 1LL;
total -= b[i] + 1LL;
}
}
}
cout << min(cnt1, cnt2) << "\n";
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | def resolve():
# 整数 1 つ
n = int(input())
# 整数複数個
# a, b = map(int, input().split())
# 整数 N 個 (改行区切り)
# N = [int(input()) for i in range(N)]
# 整数 N 個 (スペース区切り)
A = list(map(int, input().split()))
# 整数 (縦 H 横 W の行列)
# A = [list(map(int, input().split())) for i in range(H)]
sumi = 0
cnt = 0
for i in range(n-1):
sumi += A[i]
suminext = sumi + A[i+1]
if sumi * suminext < 0:
continue
else:
change = abs(suminext) +1
cnt += change
if sumi < 0:
A[i+1] = A[i+1] + change
else:
A[i+1] = A[i+1] - change
print(cnt)
resolve() |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
long long a[n];
for (int i = 0; i < n; i++) cin >> a[i];
long long first_plus = 0;
long long count_plus = 0;
long long first_minus = 0;
long long count_minus = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
first_plus += a[i];
first_minus += a[i];
if (first_plus <= 0) {
count_plus += 1 - first_plus;
first_plus += count_plus;
}
if (first_minus >= 0) {
count_minus += 1 + first_minus;
first_minus -= count_minus;
}
}
if (i % 2 == 1) {
first_plus += a[i];
first_minus += a[i];
if (first_plus >= 0) {
count_plus += 1 + first_plus;
first_plus -= count_plus;
}
if (first_minus <= 0) {
count_minus += 1 - first_minus;
first_minus += count_minus;
}
}
}
if (count_plus <= count_minus)
cout << count_plus << endl;
else
cout << count_minus << 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()))
r_even = 0 # 偶数桁を+にする場合の値
a_even = a[:]
sum_even = 0
for i in range(len(a)):
sum_even += a_even[i]
if i % 2 == 0:
if sum_even <= 0:
r_even += 1 - sum_even
a_even[i] += 1 - sum_even
sum_even += 1 - sum_even
else:
if sum_even > 0:
r_even += sum_even + 1
a_even[i] -= sum_even + 1
sum_even -= sum_even + 1
r_odd = 0 #奇数桁を+にする場合の値
a_odd = a[:]
sum_odd = 0
for i in range(len(a)):
sum_odd += a_odd[i]
if i % 2 != 0:
if sum_odd <= 0:
r_odd += 1 - sum_odd
a_odd[i] += 1 - sum_odd
sum_odd += 1 - sum_odd
else:
if sum_odd > 0:
r_odd += sum_odd + 1
a_odd[i] -= sum_odd + 1
sum_odd -= sum_odd + 1
print(min(r_odd, r_even))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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 change_num(long long p[], int N) {
long long res = 0;
long long sum = p[0];
for (int i = 1; i < N; i++) {
if (sum * (sum + p[i]) < 0) {
sum += p[i];
continue;
}
if (sum > 0 && sum + p[i] >= 0) {
sum += p[i];
while (sum >= 0) {
res++;
sum--;
}
continue;
}
if (sum < 0 && sum + p[i] <= 0) {
sum += p[i];
while (sum <= 0) {
res++;
sum++;
}
continue;
}
}
return res;
}
int main() {
int N;
cin >> N;
long long a[N];
for (int i = 0; i < N; i++) cin >> a[i];
long long ans = 0;
long long sum = a[0];
if (a[0] == 0) {
long long plus_ans = 0;
a[0] = 1;
plus_ans = change_num(a, N) + 1;
long long minus_ans = 0;
a[0] = -1;
minus_ans = change_num(a, N) + 1;
if (plus_ans < minus_ans) {
ans = plus_ans;
} else {
ans = minus_ans;
}
} else {
ans = change_num(a, N);
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 30;
const long long LINF = 1LL << 50;
const int NIL = -1;
const int MAX = 10000;
const int mod = 1000000007;
const double pi = 3.141592653589;
int main() {
int N;
cin >> N;
vector<int> a(N);
for (int i = 0; i < N; i++) cin >> a[i];
int sum1 = 0, sum2 = 0;
int cost1 = 0, cost2 = 0;
for (int i = 0; i < N; i++) {
if (i % 2 == 0) {
if (sum1 + a[i] <= 0) {
cost1 += 1 - sum1 - a[i];
sum1 = 1;
} else {
sum1 += a[i];
}
if (sum2 + a[i] >= 0) {
cost2 += abs(-1 - sum2 - a[i]);
sum2 = -1;
} else {
sum2 += a[i];
}
} else {
if (sum1 + a[i] >= 0) {
cost1 += abs(-1 - sum1 - a[i]);
sum1 = -1;
} else {
sum1 += a[i];
}
if (sum2 + a[i] <= 0) {
cost2 += 1 - sum2 - a[i];
sum2 = 1;
} else {
sum2 += a[i];
}
}
}
cout << min(cost1, cost2) << '\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;
long long a[100005], dp[100005];
cin >> n;
long long sum = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
dp[i] = sum;
}
long long diff = 0, ans = 0;
for (int i = 1; i < n; i++) {
if (dp[i] + diff == 0) {
if (dp[i - 1] + diff < 0) diff++, ans++;
if (dp[i - 1] + diff > 0) diff--, ans++;
continue;
}
if ((dp[i - 1] + diff) / llabs(dp[i - 1] + diff) ==
(dp[i] + diff) / llabs(dp[i] + diff)) {
if (dp[i] + diff >= 0) {
ans += llabs(dp[i] + diff) + 1;
diff -= llabs(dp[i] + diff) + 1;
} else {
ans += llabs(dp[i] + diff) + 1;
diff += llabs(dp[i] + diff) + 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;
const int INF = 100000000;
int dx[] = {0, 1, -1, 0, 1, -1, 1, -1};
int dy[] = {1, 0, 0, -1, 1, -1, -1, 1};
int main() {
int n;
cin >> n;
vector<int> v(n + 1, 0), w(n + 1, 0);
for (int i = 1; i <= n; i++) cin >> v[i];
int ans = 0;
for (int i = 1; i <= n; i++) {
w[i] = w[i - 1] + v[i];
if (w[i - 1] > 0 && w[i] > 0) {
int tmp = w[i] + 1;
ans += tmp;
v[i] -= tmp;
w[i] = -1;
} else if (w[i - 1] < 0 && w[i] < 0) {
int tmp = -w[i] + 1;
ans += tmp;
v[i] += tmp;
w[i] = 1;
} else if (w[i] == 0 && w[i - 1] < 0) {
ans++;
v[i]++;
w[i]++;
} else if (w[i] == 0 && w[i - 1] > 0) {
ans++;
v[i]--;
w[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;
int a[100000];
for (int i = 0; i < n; i++) cin >> a[i];
int sum;
sum = 0;
int ret1 = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 0 && sum <= 0) {
ret1 += abs(sum) + 1;
sum = 1;
} else if (i % 2 == 1 && sum >= 0) {
ret1 += abs(sum) + 1;
sum = -1;
}
}
sum = 0;
int ret2 = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 0 && sum >= 0) {
ret2 += abs(sum) + 1;
sum = -1;
} else if (i % 2 == 1 && sum <= 0) {
ret2 += abs(sum) + 1;
sum = 1;
}
}
cout << min(ret1, ret2) << 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.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws Exception {
// Your code here!
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String[] str_a = br.readLine().split(" ");
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = Integer.parseInt(str_a[i]);
}
int sum = 0;
int count = 0;
sum += a[0];
if (sum == 0) {
a[0]++;
sum++;
count++;
}
for (int i = 1; i < n; i++) {
sum += a[i];
if (i % 2 == (a[0]>0?1:0)) {
if (sum >= 0) {
count += sum + 1;
sum = -1;
}
}
else {
if (sum <= 0) {
count += 1 - sum;
sum = 1;
}
}
}
System.out.println(count);
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
vector<long long> a(n);
for (long long i = 0; i < n; i++) {
cin >> a.at(i);
}
long long ans = 0;
long long sum = a.at(0);
if (a.at(0) > 0) {
for (long long i = 1; i < n; i++) {
sum += a.at(i);
if (i % 2 == 0 && sum <= 0) {
ans += 1 - sum;
sum = 1;
} else if (i % 2 == 1 && sum >= 0) {
ans += sum + 1;
sum = -1;
}
}
} else if (a.at(0) < 0) {
for (long long i = 1; i < n; i++) {
sum += a.at(i);
if (i % 2 == 0 && sum >= 0) {
ans += sum + 1;
sum = -1;
} else if (i % 2 == 1 && sum <= 0) {
ans += 1 - sum;
sum = 1;
}
}
} else if (a.at(0) == 0) {
long long ans1 = 1;
long long ans2 = -1;
long long sum1 = 1;
long long sum2 = -1;
for (long long i = 1; i < n; i++) {
sum1 += a.at(i);
if (i % 2 == 0 && sum1 <= 0) {
ans1 += 1 - sum1;
sum1 = 1;
} else if (i % 2 == 1 && sum1 >= 0) {
ans1 += sum1 + 1;
sum1 = -1;
}
}
for (long long i = 1; i < n; i++) {
sum2 += a.at(i);
if (i % 2 == 0 && sum2 >= 0) {
ans2 += sum2 + 1;
sum2 = -1;
} else if (i % 2 == 1 && sum2 <= 0) {
ans2 += 1 - sum2;
sum2 = 1;
}
}
ans = min(ans1, ans2);
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int s[maxn];
long long ans[maxn];
int main() {
int n, j;
cin >> n;
long long sum = 0;
for (int i = 1; i <= n; i++) {
cin >> s[i];
}
for (int i = 1; i < n; i++) {
ans[i] = ans[i - 1] + s[i];
if (ans[i] > 0) {
if (s[i + 1] >= 0) {
sum += (s[i + 1] + ans[i] + 1);
s[i + 1] = -(ans[i] + 1);
} else {
if (abs(s[i + 1]) > ans[i]) {
} else {
sum += (s[i + 1] + ans[i] + 1);
s[i + 1] = -(ans[i] + 1);
}
}
} else if (ans[i] == 0) {
sum++;
ans[i]++;
} else if (ans[i] < 0) {
if (s[i + 1] > 0) {
if (abs(ans[i]) < s[i + 1]) {
} else {
sum += (1 - ans[i] - s[i + 1]);
s[i + 1] = -ans[i] + 1;
}
} else {
sum += (1 - ans[i] - s[i + 1]);
s[i + 1] = -ans[i] + 1;
}
}
}
cout << sum << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | def solve():
n = int(input())
A = map(int, input().split())
total = 0
count = 0
for a in A:
if total > 0 and a < 0:
if total + a >= 0:
count += total + a + 1
total = -1
else:
total += a
elif total > 0 and a >= 0:
count += total + a + 1
total = -1
elif total < 0 and a > 0:
if total + a <= 0:
count += -(total + a) + 1
total = 1
else:
total += a
elif total < 0 and a <= 0:
count += -total - a + 1
total = 1
else:
total += a
return count
if __name__ == '__main__':
print(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>
const long long INF = 1000000007;
using namespace std;
using Graph = vector<vector<int>>;
template <class T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
int solve() {
int n;
cin >> n;
vector<long long> v(n), sum(n);
for (int i = 0; i < (n); ++i) cin >> v[i];
for (int i = 0; i < (n); ++i) {
if (i)
sum[i] += sum[i - 1] + v[i];
else
sum[i] += v[i];
}
long long ans = 10000000000;
long long up = 0, cnt = 0;
for (int i = 0; i < (n); ++i) {
if (i % 2 == 0) {
if (sum[i] + up < 0)
continue;
else {
cnt += sum[i] + up + 1;
up -= sum[i] + up + 1;
}
} else {
if (sum[i] + up > 0)
continue;
else {
cnt += abs(sum[i] + up) + 1;
up += abs(sum[i] + up) + 1;
}
}
}
ans = cnt;
up = 0;
cnt = 0;
for (int i = 0; i < (n); ++i) {
if (i % 2 == 0) {
if (sum[i] + up < 0)
continue;
else {
cnt += sum[i] + up + 1;
up -= sum[i] + up + 1;
}
} else {
if (sum[i] + up > 0)
continue;
else {
cnt += abs(sum[i] + up) + 1;
up += abs(sum[i] + up) + 1;
}
}
}
cout << min(cnt, ans) << endl;
return 0;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
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;
const int mod = 1e9 + 7;
const double EPS = 1e-9;
const int INF = 1 << 29;
long long int a[100054];
long long int b[100054];
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i], b[i] = a[i];
long long int ans = 0;
long long int sum = a[1];
for (int i = 2; i <= n; ++i) {
if (sum < 0) {
long long int num = 1 - sum;
if (a[i] < num) ans += num - a[i], a[i] = num;
} else {
long long int num = -1 - sum;
if (a[i] > num) ans += a[i] - num, a[i] = num;
}
sum += a[i];
}
long long int ans1 = b[1] + 1;
sum = (b[1] > 0 ? -1 : 1);
for (int i = 2; i <= n; ++i) {
if (sum < 0) {
long long int num = 1 - sum;
if (b[i] < num) ans1 += num - b[i], b[i] = num;
} else {
long long int num = -1 - sum;
if (b[i] > num) ans1 += b[i] - num, b[i] = num;
}
sum += b[i];
}
cout << min(ans, 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 | python3 |
import sys
input = sys.stdin.readline
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
# A = [ int(input()) for _ in range(N) ]
##############################
N = int(input())
A = list(map(int, input().split()))
count = 0
summary = A[0]
for i in range(1, N):
# print(summary)
# 次はマイナス?
if summary > 0:
# 条件を満たしてる?
if (summary + A[i]) < 0:
summary += A[i]
else:
# 必要な数
diff = -1-summary-A[i]
count += abs(diff)
summary = -1
else:
# 次はプラス
if (summary + A[i]) > 0:
summary += A[i]
else:
# 必要な数
diff = 1-summary-A[i]
count += abs(diff)
summary = 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=list(map(int,input().split()))
hugo=0
wa=a[0]
ans=0
for i in range(1,n):
# print(wa)
if wa>0:
if wa+a[i]<0:
wa+=a[i]
else:
ans+=abs(wa+a[i])+1
wa=-1
else:
if wa+a[i]>0:
wa+=a[i]
else:
ans+=abs(wa+a[i])+1
wa=1
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const int inf = 1e9;
const long long INF = 1LL << 60;
const double PI = 3.1415926535897932;
int main() {
int n;
cin >> n;
long long a[n];
for (int i = 0; i < (int)(n); i++) cin >> a[i];
long long tmp = 0;
long long sum = 0;
long long ans = INF;
for (int i = 0; i < (int)(n); i++) {
if (i == 0) {
if (a[i] < 0) {
tmp += 1 - a[i];
sum = 1;
} else {
sum += a[i];
}
} else {
if (i % 2 == 0) {
long long x = a[i];
if (a[i] + sum <= 0) {
x = 1 - sum;
tmp += x - a[i];
}
sum += x;
} else {
long long x = a[i];
if (a[i] + sum >= 0) {
x = -1 - sum;
tmp += a[i] - x;
}
sum += x;
}
}
}
ans = min(ans, tmp);
tmp = 0;
sum = 0;
for (int i = 0; i < (int)(n); i++) {
if (i == 0) {
if (a[i] > 0) {
tmp += abs(-1 - a[i]);
sum += -1;
} else {
sum += a[i];
}
} else {
if (i % 2 == 1) {
long long x = a[i];
if (a[i] + sum <= 0) {
x = 1 - sum;
tmp += x - a[i];
}
sum += x;
} else {
long long x = a[i];
if (a[i] + sum >= 0) {
x = -1 - sum;
tmp += a[i] - x;
}
sum += x;
}
}
}
ans = min(ans, tmp);
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import sys
import copy
import math
from _bisect import *
from collections import *
from operator import itemgetter
from math import factorial
"""
from fractions import gcd
def lcm(x, y):
return (x * y) // gcd(x, y)
"""
stdin = sys.stdin
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()
n = ni()
li = na()
ans = [0, 0]
s = li[0]
for j in range(2):
code = j
for i in range(n - 1):
code = 1 - code
if code:
if s + li[i + 1] > 0:
s += li[i + 1]
else:
ans[j] += abs(1 - s)
s = 1
else:
if s + li[i + 1] < 0:
s += li[i + 1]
else:
ans[j] += abs(- 1 - s)
s = -1
print(min(ans))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
sm = a[0]
def f(sm):
ret = 0
for e in a[1:]:
if sm * (sm + e) < 0:
sm += e
continue
else:
if sm > 0:
a_mx = -sm - 1
ret += e - a_mx
sm += a_mx
else:
a_mn = -sm + 1
ret += a_mn - e
sm += a_mn
return ret
if sm != 0:
ans = f(sm)
else:
ans = min(f(1), f(-1)) + 1
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0;i<n;i++)
#define erep(i,n) for(int i = 0;i<=n;i++)
#define rep1(i,n) for(int i = 1;i<n;i++)
#define erep1(i,n) for(int i = 1;i<=n;i++)
typedef long long ll;
#define vint vector<int>
#define vstring vector<string>
#define vll vector<ll>
#define vbool vector<bool>
#define INF 100000000
ll gcm(ll a,ll b);
ll lcm(ll a,ll b);
ll fac(ll a);
int main(){
ll n;
cin >> n;
ll nowsum = 0;
ll ans = 0;
ll sum = 0;
bool plus = false;
bool minus = false;
vll A(n);
rep(i,n){
cin >> A[i];
sum += A[i];
rep(i,n){
nowsum += A[i];
if(i == 0){
if(nowsum == 0){
if(A[1] > 0){
ans = A[i] - 1;
nowsum = (-1)*abs(A[i]-1);
minus = true;
}
else if(A[1] < 0){
ans = A[i] + 1;
nowsum = abs(A[i]-1);
plus = true;
}
else{
if(sum >= 0){
ans = 1;
nowsum = 1;
plus = true;
}
else{
ans = 1;
nowsum = -1;
minus = true;
}
}
else if(nowsum >= 0) plus = true;
else minus = true;
}
else{
if(plus){
if(i%2 == 0){
//minusだったらだめ
if(nowsum <= 0){
ans += abs(nowsum -A[i] - 1) - A[i];
nowsum = 1;
}
}
else{
//plusだったらだめ
if(nowsum >= 0){
ans += A[i] + abs(nowsum -A[i] + 1);
nowsum = -1;
}
}
}
else if(minus){
if(i%2 == 0){
//plusだったらだめ
if(nowsum >= 0){
ans += A[i] + abs(nowsum -A[i] + 1);
nowsum = -1;
}
}
else{
//minusだったらだめ
if(nowsum <= 0){
ans += abs(nowsum -A[i] - 1) - A[i];
nowsum = 1;
}
}
//cout << nowsum << " " << ans << endl;
}
}
}
cout << ans << endl;
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
long long *a;
cin >> n;
a = new long long[n];
long long ans = 0;
long long sum = 0;
for (int i = 0; i < n; i++) cin >> a[i];
int flag = -1;
if (a[0] > 0)
flag = 1;
else if (a[0] == 0)
flag = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (flag > 0 || flag == 0) {
if (sum <= 0) {
while (sum <= 0) {
sum++;
ans++;
}
}
flag = -1;
} else if (flag < 0) {
if (sum >= 0) {
while (sum >= 0) {
sum--;
ans++;
}
}
flag = 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() {
int 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];
}
int long long sum = 0, sum2 = 0;
if (b[0] == 0) {
long long 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>
#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
//#define int long long
using bll = boost::multiprecision::cpp_int;
using ll = long long;
//constexpr int INF = 1e9;//INT_MAX=(1<<31)-1=2147483647
constexpr ll INF = (ll)1e18;//(1LL<<63)-1=9223372036854775807
constexpr ll MOD = (ll)1e9 + 7;
constexpr double EPS = 1e-9;
constexpr int dx[4]={1,0,-1,0};
constexpr int dy[4]={0,1,0,-1};
#define p(var) std::cout<<var<<std::endl
#define PI (acos(-1))
#define rep(i, n) for(ll i=0, i##_length=(n); i< i##_length; ++i)
#define repeq(i, n) for(ll i=1, i##_length=(n); i<=i##_length; ++i)
#define all(v) (v).begin(), (v).end()
#define uniq(v) (v).erase(unique((v).begin(), (v).end()), (v).end());
template<typename T> inline void pv(vector<T> v) { for(ll i=0, N=v.size(); i<N; i++) cout<< v[i] << (i==N-1 ? '\n' : ' '); }
template<typename T> inline T gcd(T a, T b) { return b ? gcd(b,a%b) : a; }
template<typename T> inline T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<typename T1, typename T2> inline T1 power(T1 x, T2 n){ return n ? power(x*x%MOD,n/2)*(n%2?x:1)%MOD : 1; }
template<typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template<typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }
template<typename T> class dvector : public std::vector<T> {
public:
dvector() : std::vector<T>() {}
explicit dvector(size_t n, const T& value = T()) : std::vector<T>(n,value) {}
dvector(const std::vector<T>& v) : std::vector<T>(v) {}
T& operator[](size_t n){ return this->at(n); }
};
template<typename T1, typename T2> ostream& operator<<(ostream& s, pair<T1, T2>& p) {return s << "(" << p.first << ", " << p.second << ")";}
template<typename T> ostream& operator<<(ostream& s, dvector<T>& v) {
for (int i = 0, len = v.size(); i < len; ++i){ s << v[i]; if (i < len - 1) s << "\t"; } return s; }
template<typename T> ostream& operator<<(ostream& s, dvector< dvector<T> >& vv) {
for (int i = 0, len = vv.size(); i < len; ++i){ s << vv[i] << endl; } return s; }
template<typename T1, typename T2> ostream& operator<<(ostream& s, map<T1, T2>& m) {
s << "{" << endl; for (auto itr = m.begin(); itr != m.end(); ++itr){ s << "\t" << (*itr).first << " : " << (*itr).second << endl; } s << "}" << endl; return s; }
template<typename T> ostream& operator<<(ostream& s, set<T>& se) {
s << "{ "; for (auto itr = se.begin(); itr != se.end(); ++itr){ s << (*itr) << "\t"; } s << "}" << endl; return s; }
template<typename T> ostream& operator<<(ostream& s, multiset<T>& se) {
s << "{ "; for (auto itr = se.begin(); itr != se.end(); ++itr){ s << (*itr) << "\t"; } s << "}" << endl; return s; }
#ifdef LOCAL_DEV
#define debug(var) std::cout<<#var" = "<<var<<std::endl
#else
#define debug(var)
#endif
#ifdef LOCAL_TEST
#define vector dvector
#endif
/*-----8<-----8<-----*/
signed main() {
ll N;
cin>>N;
vector<ll> a(N,0);
rep(i,N)cin>>a[i];
vector<ll> rui(N+1,0);
rep(i,N)rui[i+1]=rui[i]+a[i];
ll c,t=a[0]>0 ? 1 : -1;
if([&]{
rep(i,N-1){
if(t==1){
if(rui[i+2]>0)return false;
}else{
if(rui[i+2]<0)return false;
}
t*=-1;
}
return true;
}()){
p(0);return 0;
}
//+
t=0;
ll ansb=0;
if(rui[1]>0){
}else{
t+=-rui[1]+1;
ansb+=abs(-rui[1]+1);
}
c=-1;
for(ll i=1;i<N;i++){
ll tt=rui[i+1]+t;
if(c==1){
if(tt>0){
}else{
t+=-tt+1;
ansb+=abs(-tt+1);
}
}else{
if(tt>=0){
t+=-tt-1;
ansb+=abs(-tt-1);
}else{
}
}
c*=-1;
}
//-
t=0;
ll ansc=0;
if(rui[1]>0){
t+=-rui[1]-1;
ansc+=abs(-rui[1]-1);
}else{
}
c=1;
for(ll i=1;i<N;i++){
ll tt=rui[i+1]+t;
if(c==1){
if(tt>0){
}else{
t+=-tt+1;
ansc+=abs(-tt+1);
}
}else{
if(tt>=0){
t+=-tt-1;
ansc+=abs(-tt-1);
}else{
}
}
c*=-1;
}
p(min(ansb,ansc));
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
# map(int, input().split())
n = int(input())
A = list(map(int, input().split()))
acc = [0] * n
acc[0] = A[0]
for i in range(1, n):
acc[i] = acc[i - 1] + A[i]
ans = 0
if acc[0] == 0:
acc[0] += 1
ans += 1
cur = acc[0]
x = 0
for i in range(1, n):
acc[i] += x
if cur > 0:
if acc[i] >= 0:
ans += acc[i] + 1
x -= acc[i] + 1
acc[i] = -1
else:
if acc[i] <= 0:
ans += abs(acc[i]) + 1
x += abs(acc[i]) + 1
acc[i] = 1
cur = acc[i]
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int64_t> vec(N);
for (int i = 0; i < N; i++) {
cin >> vec.at(i);
}
int64_t ans = 0;
int64_t x = vec.at(0);
for (int i = 1; i < N; i++) {
if ((vec.at(i) + x) * x >= 0) {
if (x > 0) {
ans += abs(vec.at(i) + 1 + x);
vec.at(i) -= (vec.at(i) + 1 + x);
}
if (x < 0) {
ans += abs(1 - x - vec.at(i));
vec.at(i) += (1 - x - vec.at(i));
}
}
x = vec.at(i) + x;
}
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<long long> a(n), tot(n);
for (int i = 0; i < (n); ++i) cin >> a[i];
tot[0] = a[0];
for (int i = 0; i < (n - 1); ++i) tot[i + 1] += tot[i] + a[i + 1];
long long ans = 1LL << 60, now = 0;
long long wa = 0;
int p;
if (tot[0] != 0) {
p = tot[0] / abs(tot[0]);
for (int i = 0; i < (n); ++i) {
tot[i] += wa;
if (p == 1) {
if (tot[i] <= 0) {
wa += abs(tot[i]) + 1;
now += abs(tot[i]) + 1;
}
} else {
if (tot[i] >= 0) {
wa -= abs(tot[i]) + 1;
now += abs(tot[i]) + 1;
}
}
p *= -1;
}
ans = min(ans, now);
} else {
now = 1;
wa = 1;
p = 1;
for (int i = 0; i < (n); ++i) {
tot[i] += wa;
if (p == 1) {
if (tot[i] <= 0) {
wa += abs(tot[i]) + 1;
now += abs(tot[i]) + 1;
}
} else {
if (tot[i] >= 0) {
wa -= abs(tot[i]) + 1;
now += abs(tot[i]) + 1;
}
}
p *= -1;
}
ans = min(ans, now);
now = 1;
wa = -1;
p = -1;
for (int i = 0; i < (n); ++i) {
tot[i] += wa;
if (p == 1) {
if (tot[i] <= 0) {
wa += abs(tot[i]) + 1;
now += abs(tot[i]) + 1;
}
} else {
if (tot[i] >= 0) {
wa -= abs(tot[i]) + 1;
now += abs(tot[i]) + 1;
}
}
p *= -1;
}
ans = min(ans, now);
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long a[100001];
for (int i = 0; i < n; ++i) cin >> a[i];
int cnt1 = 0;
int cnt2 = 0;
long long sum = 0;
for (int i = 0; i < n; ++i) {
sum += a[i];
if (i % 2 == 0) {
while (sum <= 0) {
sum++;
cnt1++;
}
} else {
while (sum >= 0) {
sum--;
cnt1++;
}
}
}
sum = 0;
for (int i = 0; i < n; ++i) {
sum += a[i];
if (i % 2 == 1) {
while (sum <= 0) {
sum++;
cnt2++;
}
} else {
while (sum >= 0) {
sum--;
cnt2++;
}
}
}
cout << min(cnt1, cnt2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n, *A = map(int, open(0).read().split())
B = [A[0]]
C = [A[0]]
D = [0]
for i in range(1, n):
if (C[i-1] + A[i]) * C[i-1] < 0:
B.append(A[i])
C.append(C[i-1] + A[i])
D.append(D[i-1])
else:
if C[i-1] > 0:
B.append(-(C[i-1]+1))
C.append(-1)
D.append(C[i-1]+1 + A[i])
elif C[i-1] < 0:
B.append(-C[i-1]+1)
C.append(1)
D.append(-C[i-1]+1 + A[i])
print(A)
print(B)
print(C)
print(D) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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(int 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<int> a(n);
cin >> a[0];
int count = 0;
State state = GetState(a[0]);
if (state == Zero) {
a[0] = 1;
state = Plus;
count++;
}
int sum = a[0];
cout << a[0] << " " << sum << endl;
for (int i = 1; i < n; i++) {
cin >> a[i];
State nextState = GetState(sum + a[i]);
switch (nextState) {
case Plus:
if (state == Plus) {
int bf_a = a[i];
a[i] = -1 - sum;
count += abs(a[i] - bf_a);
nextState = Minus;
}
break;
case Minus:
if (state == Minus) {
int bf_a = a[i];
a[i] = 1 - sum;
count += abs(a[i] - bf_a);
nextState = Plus;
}
break;
case Zero:
if (state == Plus) {
int bf_a = a[i];
a[i] = -1 - sum;
count += abs(a[i] - bf_a);
nextState = Minus;
} else if (state == Minus) {
int 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 | cpp | #include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9 + 7;
const int MAX = 1e6;
int arr[MAX];
int status(int a) {
if (a < 0)
return 1;
else if (a > 0)
return 0;
else
return 2;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long int n, cnt = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
if (!arr[0]) arr[0] = 1;
for (int i = 1; i < n; i++) {
if (status(arr[i - 1]) == status(arr[i])) {
cnt += abs(arr[i] * 2);
arr[i] *= -1;
}
}
long long int sum = arr[0], f = 0;
if (arr[0] < 0)
f = 1;
else
f = 0;
for (int i = 1; i < n; i++) {
f ^= 1;
sum += arr[i];
if (status(sum) != f) {
cnt += abs(sum) + 1;
if (f)
sum = -1;
else
sum = 1;
}
}
cout << cnt << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import numpy as np
n = int(input())
a = [int(_) for _ in input().split()]
ans = 0
zeroflag = True
zerocnt = 0
tmp = 0
for i in a:
if zeroflag and i == 0:
zerocnt += 1
elif zeroflag and i != 0:
zeroflag = False
if zerocnt != 0 and abs(i) == 1:
ans += 1
tmp += np.sign(i)*2
else:
tmp = i
else:
if np.sign(tmp+i) == np.sign(tmp) or np.sign(tmp+i) == 0:
ans += abs((tmp+i) + np.sign(tmp))
tmp = -np.sign(tmp)
else:
tmp += i
if zerocnt != 0:
ans += 2*zerocnt - 1
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long curr, ans = 0;
cin >> curr;
for (int i = 1; i < n; ++i) {
long long t;
cin >> t;
if (curr > 0 && (long long)(t + curr) < 0) {
curr += t;
continue;
}
if (curr < 0 && (long long)(t + curr) > 0) {
curr += t;
continue;
} else {
if (curr > 0 && (long long)(t + curr) > 0) {
long long t1 = -1L - (curr);
long long t2 = t1 - t;
ans += abs(t2);
curr = -1L;
continue;
}
if (curr < 0 && (long long)(t + curr) < 0) {
long long t1 = 1 - (curr);
long long t2 = t1 - t;
ans += abs(t2);
curr = 1L;
continue;
}
if (!((long long)(t + curr))) {
if (curr > 0) {
++ans;
curr = -1L;
} else if (curr < 0) {
++ans;
curr = 1L;
}
}
}
}
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()))
acc = 0
cnt = 0
flag = 1 if a[0] > 0 else -1
for i in a:
acc += i
if (flag == 1 and flag > acc) or (flag == -1 and flag < acc) or i == 0:
cnt += abs(flag - acc)
acc = flag
flag *= -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 main() {
int N;
cin >> N;
vector<long long int> A(N);
for (int i = 0; i < N; i++) cin >> A[i];
vector<long long int> B(N);
B[0] = A[0];
for (int i = 1; i < N; i++) B[i] = B[i - 1] + A[i];
long long int ans = 0;
long long int base = 0;
for (int i = 1; i < N; i++) {
if ((B[i] + base) * (B[i - 1] + base) > 0) {
if (B[i] + base > 0) {
ans += abs(B[i] + base) + 1;
base -= abs(B[i] + base) + 1;
continue;
} else if (B[i] + base < 0) {
ans += abs(B[i] + base) + 1;
base += abs(B[i] + base) + 1;
continue;
}
}
if (i == 1 && B[i - 1] + base == 0) {
if (B[i] + base > 0) {
ans++;
base -= 1;
} else {
ans++;
base += 1;
}
}
if (i < N - 1) {
if (B[i] + base == 0) {
if (B[i - 1] + base > 0) {
ans += 1;
base -= 1;
continue;
} else if (B[i - 1] + base < 0) {
ans += 1;
base += 1;
continue;
}
}
} else {
if (B[i] + base == 0) ans++;
}
}
cout << ans << endl;
return 0;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.