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, i, sum, count;
cin >> N;
vector<int> input(N);
for (i = 0; i < N; i++) {
cin >> input.at(i);
}
sum = 0;
count = 0;
sum = input.at(0);
if (input.at(0) >= 0) {
for (i = 1; i < N; i++) {
if (i % 2 == 1) {
sum += input.at(i);
if (sum >= 0) {
while (sum >= 0) {
sum--;
count++;
}
}
}
if (i % 2 == 0) {
sum += input.at(i);
if (sum < 0) {
while (sum <= 0) {
sum++;
count++;
}
}
}
}
}
if (input.at(0) < 0) {
for (i = 1; i < N; i++) {
if (i % 2 == 1) {
sum += input.at(i);
if (sum <= 0) {
while (sum <= 0) {
sum++;
count++;
}
}
}
if (i % 2 == 0) {
sum += input.at(i);
if (sum >= 0) {
while (sum >= 0) {
sum--;
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;
int n;
long long a[100005];
void solver() {
long long cnt = 0;
long long sum = a[0];
for (int i = 1; i <= n; ++i) {
if (sum > 0) {
if (sum + a[i] >= 0) {
cnt += abs(sum - a[i]) + 1;
sum = -1;
} else {
sum = sum + a[i];
}
} else if (sum < 0) {
if (sum + a[i] <= 0) {
cnt += abs(sum + a[i]) + 1;
sum = 1;
} else {
sum = sum + a[i];
}
}
}
cout << cnt << endl;
}
int main() {
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
solver();
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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 chk, ans = 0;
scanf("%d", &n);
vector<int> a(n);
for (auto& e : a) scanf("%d", &e);
chk = a[0];
if (chk > 0) {
for (int i = 1; i < n; i++) {
if (i % 2) {
chk += a[i];
if (chk >= 0) {
ans += chk + 1;
chk = -1;
}
} else {
chk += a[i];
if (chk <= 0) {
ans += abs(chk) + 1;
chk = 1;
}
}
}
} else {
for (int i = 1; i < n; i++) {
if (i % 2) {
chk += a[i];
if (chk <= 0) {
ans += abs(chk) + 1;
chk = 1;
}
} else {
chk += a[i];
if (chk >= 0) {
ans += chk + 1;
chk = -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 | python3 | n = int(input())
a = list(map(int, input().split()))
L = [0 for _ in range(n)]
L[0] = a[0]
for i in range(1, n):
L[i] = L[i-1] + a[i]
delay = 0
all_over = 0
sign = (a[0] > 0) - (a[0] < 0)
for i in range(1, n):
L[i] += delay
if L[i] <= 0 and sign == -1:
delay += 1 - L[i]
all_over += 1 - L[i]
L[i] = 1
elif L[i] >= 0 and sign == 1:
delay -= L[i] + 1
all_over += L[i] + 1
L[i] = -1
sign *= -1
print(all_over) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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 a[n];
for (int i = 0; i < n; i++) cin >> a[i];
int t;
if (a[0] >= 0)
t = 1;
else
t = -1;
int sum = a[0];
int ans = 0;
for (int i = 1; i < n; i++) {
int sum2 = sum + a[i];
if (sum > 0 && sum2 > 0) {
ans += sum2 + 1;
sum = -1;
} else if (sum < 0 && sum2 < 0) {
ans += -sum2 + 1;
sum = 1;
} else if (sum2 == 0) {
ans++;
if (sum < 0)
sum = 1;
else
sum = -1;
} else
sum = sum2;
}
cout << ans;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long ans = 0;
if (a[0] == 0) {
a[0] = 1;
long long total = a[0];
long long temp;
long long ans1 = 0;
ans1++;
for (int i = 1; i < n; i++) {
temp = a[i];
if (total > 0) {
if (total + a[i] >= 0) {
a[i] = -(total + 1);
}
total += a[i];
ans1 += abs(a[i] - temp);
a[i] = temp;
} else if (total < 0) {
if (total + a[i] <= 0) {
a[i] = (-total + 1);
}
total += a[i];
ans1 += abs(a[i] - temp);
a[i] = temp;
}
}
a[0] = -1;
total = a[0];
temp;
long long ans2 = 0;
ans2++;
for (int i = 1; i < n; i++) {
temp = a[i];
if (total > 0) {
if (total + a[i] >= 0) {
a[i] = -(total + 1);
}
total += a[i];
ans2 += abs(a[i] - temp);
a[i] = temp;
} else if (total < 0) {
if (total + a[i] <= 0) {
a[i] = (-total + 1);
}
total += a[i];
ans2 += abs(a[i] - temp);
a[i] = temp;
}
total += a[i];
}
ans = min(ans1, ans2);
} else {
long long total = a[0];
long long temp;
for (int i = 1; i < n; i++) {
temp = a[i];
if (total > 0) {
if (total + a[i] >= 0) {
a[i] = -(total + 1);
}
ans += abs(a[i] - temp);
} else if (total < 0) {
if (total + a[i] <= 0) {
a[i] = (-total + 1);
}
ans += abs(a[i] - temp);
}
total += a[i];
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long INF = (1LL << 62);
long long N;
vector<long long> A, W;
long long S[100002] = {INF * (-1)};
long long dp[100002] = {0};
void calcDP(int n) {
if (n == 1) {
if (W[1] != 0) {
dp[1] = 0;
} else {
dp[1] = 1;
if (W[2] <= 0) {
W[1] = 1;
} else {
W[1] = -1;
}
S[1] = W[1];
}
return;
} else {
S[n] = S[n - 1] + W[n];
if (S[n - 1] * S[n] < 0) {
dp[n] = dp[n - 1];
} else {
dp[n] = dp[n - 1] + abs(0 - S[n - 1] - W[n]) + 1;
W[n] = 0 - S[n - 1] - (abs(S[n - 1]) / S[n - 1]);
S[n] = S[n - 1] + W[n];
}
return;
}
}
int main(int argc, char* argv[]) {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> N;
W.push_back(0);
S[0] = 0;
for (int i = 1; i <= N; i++) {
long long a;
cin >> a;
A.push_back(a);
W.push_back(a);
if (i == 1) {
S[1] = a;
} else {
S[i] = S[i - 1] + a;
}
}
for (int i = 1; i <= N; i++) {
calcDP(i);
}
printf("%lld\n", dp[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 | java |
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
StringTokenizer str = new StringTokenizer(br.readLine(), " ");
long[] A = new long[n];
for(int i = 0; i < n ; i++) {
A[i] = Integer.parseInt(str.nextToken());
}
int c1 = func(A,1,n);
int c2 = func(A,-1,n);
System.out.println(Math.min(c1,c2));
} catch (IOException e) {
System.out.println("error");
}
}
static int func(long[] A,int flg,int n){
long sum = 0;
int c = 0;
for(int i = 0; i < n; i++){
sum += A[i];
if(flg == 1){ //次は負
if(sum >= 0){
c += (sum + 1);
sum = -1;
}
flg = -1;
}
else{ //次は正
if(sum <= 0){
c += 1+ (-sum);
sum = 1;
}
flg = 1;
}
}
return c;
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, ansa = 0, ansb = 0, suma = 0, sumb = 0;
cin >> n;
for (int i = 0; i < (n); i++) {
int c;
cin >> c;
if (i % 2 == 0) {
if (suma + c <= 0) {
ansa += 1 - c - suma;
suma = 1;
}
if (sumb + c >= 0) {
ansb += sumb + c + 1;
sumb = -1;
}
} else {
if (suma + c >= 0) {
ansa += suma + c + 1;
suma = -1;
}
if (sumb + c <= 0) {
ansb += 1 - c - 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 | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
bool is_diff(ll a, ll b) {
if ((a > 0 && b < 0) || (a < 0 && b > 0))
return true;
else
return false;
}
ll f(vector<ll> a, ll a0, ll n) {
ll ans = 0;
ll total = a0;
for (ll i = 0; i < n - 1; i++) {
ll next_total = total + a[i + 1];
if (next_total == 0) {
if (total > 0)
next_total = -1;
else
next_total = 1;
ans++;
} else {
if (is_diff(total, next_total))
total = next_total;
else {
if (next_total > 0) {
total = -1;
ans += next_total + 1;
} else {
total = 1;
ans += (-next_total) + 1;
}
}
}
}
return ans;
}
int main() {
ll n;
cin >> n;
vector<ll> a(n);
for (ll i = 0; i < n; i++) cin >> a[i];
ll ans;
if (a[0] != 0)
ans = f(a, a[0], n);
else
ans = min(f(a, 1, n) + 1, f(a, -1, n) + 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;
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T>
inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
template <class T>
inline T sqr(T x) {
return x * x;
}
const double EPS = 1e-10;
const double PI = acos(-1.0);
const long long INF = 1000000007;
template <class T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
int main(void) {
int N;
cin >> N;
vector<long long> a(N);
for (int i = (0); i < (N); ++i) cin >> a[i];
vector<long long> s(N);
s[0] = a[0];
for (int i = (0); i < (N - 1); ++i) s[i + 1] = s[i] + a[i + 1];
long long offset = 0, ans = 0;
for (int i = (0); i < (N - 1); ++i) {
if ((s[i] + offset) * (s[i + 1] + offset) >= 0) {
ans += abs(s[i + 1] + offset) + 1;
if (s[i] + offset > 0)
offset -= s[i + 1] + offset + 1;
else
offset += -(s[i + 1] + offset - 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 ll = long long;
using namespace std;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<ll> v(n, 0);
for (int i = (int)(0); i < (int)(n); i++) cin >> v[i];
vector<int> p1(n + 1, 1), p2(n + 1, 1);
for (int i = (int)(0); i < (int)(n + 1); i++) {
if (i % 2 == 0) p1[i] *= -1;
}
for (int i = (int)(0); i < (int)(n + 1); i++) {
if (i % 2 == 1) p2[i] *= -1;
}
priority_queue<int, vector<int>, greater<int> > pq;
vector<ll> sum_until(n + 1, 0);
int cnt;
cnt = 0;
for (int i = 1; i <= n; i++) {
sum_until[i] = sum_until[i - 1] + v[i - 1];
if (sum_until[i] * p1[i] < 0) {
int plus = abs(sum_until[i]);
cerr << "("
"i"
","
"plus * p1[i]"
"):("
<< i << "," << plus * p1[i] << ")" << endl;
cerr << "sum_until[i]"
":"
<< sum_until[i] << endl;
sum_until[i] += plus * p1[i] + p1[i];
cerr << "sum_until[i]"
":"
<< sum_until[i] << endl;
cnt += abs(plus * p1[i]) + 1;
}
}
cerr << "cnt"
":"
<< cnt << endl;
pq.push(cnt);
p1 = p2;
cnt = 0;
for (int i = 1; i <= n; i++) {
cerr << "i"
":"
<< i << endl;
sum_until[i] = sum_until[i - 1] + v[i - 1];
if (sum_until[i] * p1[i] < 0) {
int plus = abs(sum_until[i]);
cerr << "("
"i"
","
"plus * p1[i]"
"):("
<< i << "," << plus * p1[i] << ")" << endl;
cerr << "sum_until[i]"
":"
<< sum_until[i] << endl;
sum_until[i] += plus * p1[i] + p1[i];
cerr << "sum_until[i]"
":"
<< sum_until[i] << endl;
cnt += abs(plus * p1[i]) + 1;
} else if (sum_until[i] == 0) {
sum_until[i] = p1[i];
cnt += 1;
}
}
pq.push(cnt);
cerr << "cnt"
":"
<< cnt << endl;
cout << pq.top() << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java |
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Main main = new Main();
main.run();
}
public void run() {
Scanner sc = new Scanner(System.in);
int n= sc.nextInt();
int sum[]=new int[n];
sum[0]=sc.nextInt();
boolean evenPlus = true;
if(sum[0]<0) evenPlus=false;
int ans = 0;
for(int i=1; i<n; i++) {
int a = sc.nextInt();
if(i%2==0) {
if(evenPlus) {
if(sum[i-1]+a <= 0) {
ans += Math.abs(sum[i-1]+a)+1;
sum[i]=1;
} else {
sum[i]=sum[i-1]+a;
}
} else {
if(sum[i-1]+a >= 0) {
ans += Math.abs(sum[i-1]+a)+1;
sum[i]=-1;
} else {
sum[i]=sum[i-1]+a;
}
}
} else {
if(!evenPlus) {
if(sum[i-1]+a <= 0) {
ans += Math.abs(sum[i-1]+a)+1;
sum[i]=1;
} else {
sum[i]=sum[i-1]+a;
}
} else {
if(sum[i-1]+a >= 0) {
ans += Math.abs(sum[i-1]+a)+1;
sum[i]=-1;
} else {
sum[i]=sum[i-1]+a;
}
}
}
}
System.out.println(ans);
sc.close();
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> vec;
int a;
for (int i = 0; i < n; i++) {
cin >> a;
vec.emplace_back(a);
}
int ans = 0;
int wa = vec[0];
for (int i = 1; i < n; i++) {
if (vec[i - 1] > 0) {
while (wa + vec[i] >= 0) {
ans++;
vec[i]--;
}
wa += vec[i];
}
if (vec[i - 1] < 0) {
while (wa + vec[i] <= 0) {
ans++;
vec[i]++;
}
wa += vec[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 | python2 | # -*- coding: utf-8 -*-
n = input()
a = map(int, raw_input().split())
b = a
asum = [0]*len(a)
bsum = [0]*len(a)
asum[0] = a[0]
bsum[0] = b[0]
cnt_a = 0
cnt_b = 0
if(a[0]==0):
asum[0] = 1
cnt_a = 1
bsum[0] = -1
cnt_b = 1
for i in range(len(a)-1):
asum[i+1] = a[i+1] + asum[i]
if(i%2==0 and asum[i+1]>=0):
cnt_a += asum[i+1]+1
asum[i+1] = -1
elif(i%2==1 and asum[i+1]<=0):
cnt_a += (-1)*asum[i+1]+1
asum[i+1] = 1
for i in range(len(b)-1):
bsum[i+1] = b[i+1] + bsum[i]
if(i%2==0 and bsum[i+1]<=0):
cnt_b += (-1)*bsum[i+1]+1
bsum[i+1] = 1
elif(i%2==1 and bsum[i+1]>=0):
cnt_b += bsum[i+1]+1
bsum[i+1] = -1
print(min(cnt_a,cnt_b)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
A = [int(i) for i in input().split()]
ff = -1
for i in range(n):
if A[i] != 0:
ff = i
break
if A[0] != 0:
ans = 0
S = A[0]
f = A[0]//abs(A[0])
else:
if ff == -1:
ans = 1
S[0] = 1
f = 1
else:
if ff % 2 == 0:
ans = 1
S[0] = 1
f = 1
else:
ans = 1
S[0] = -1
f = -1
for a in A[1:]:
S += a
if S == 0:
ans += 1
S = -f
else:
if S/abs(S) != f*(-1):
ans += abs(S)+1
S = -f
f *= -1
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long total = a[0], ans = 0, ans1 = 0, ans2 = 0;
bool flg1 = false, flg2 = false;
if (total <= 0) {
ans += abs(total - 1);
total = 1;
}
for (int i = 1; i < n; i++) {
if (total < 0) {
flg1 = false;
} else {
flg1 = true;
}
if (flg1) {
total += a[i];
if (total >= 0) {
ans1 += total + 1;
total = -1;
}
} else {
total += a[i];
if (total <= 0) {
ans1 += abs(total - 1);
total = 1;
}
}
}
total = a[0];
if (total >= 0) {
ans += abs(total + 1);
total = -1;
}
for (int i = 1; i < n; i++) {
if (total < 0) {
flg1 = false;
} else {
flg1 = true;
}
if (flg1) {
total += a[i];
if (total >= 0) {
ans2 += total + 1;
total = -1;
}
} else {
total += a[i];
if (total <= 0) {
ans2 += abs(total - 1);
total = 1;
}
}
}
cout << min(ans1, ans2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | n=gets.chomp.to_i
a=gets.split.map(&:to_i)
a.map!{|e| -e} if a[0]<0
s, m=a[0], 0
(1...n).each do |i|
s+=a[i]
if i%2==0
if s<=0
m+=(-s+1)
s=1
end
else
if s>=0
m+=s+1
s=-1
end
end
end
s, m2=-1, a[0]+1
(1...n).each do |i|
s+=a[i]
if i%2==0
if s>=0
m2+=s+1
s=-1
end
else
if s<=0
m2+=(-s+1)
s=1
end
end
end
puts [m, m2].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())
seq = [int(x) for x in input().split()]
a_sum = seq[0]
op = 0
for a in seq[1:]:
tmp = a_sum + a
if tmp * a_sum < 0:
a_sum = tmp
elif a_sum < 0:
while a_sum * (a_sum + a) >= 0:
a += 1
op += 1
a_sum += a
elif a_sum > 0:
while a_sum * (a_sum + a) >= 0:
a -= 1
op += 1
a_sum += a
print(op)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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, int* S, int n);
void add(int* S, int n, int del, int k);
int main() {
int *N, *S;
int count_eve = 0, count_odd = 0, n;
int j = 0, k = 0;
cin >> n;
N = new int[n];
S = new int[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]) + 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, int* 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;
int main() {
int n;
cin >> n;
vector<long long> 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 | python3 | import sys
input = sys.stdin.readline
n = int(input())
a_list = list(map(int, input().split()))
cnt = 0
a_sum = a_list[0]
if a_sum > 0:
flag = 1
elif a_sum == 0:
flag = 1
cnt += 1
else:
flag = -1
for a in a_list[1:]:
a_sum += a
if flag == 1:
if a_sum >= 0:
cnt += a_sum+1
a_sum = -1
flag = -1
else:
if a_sum <= 0:
cnt += abs(a_sum)+1
a_sum = 1
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 | UNKNOWN | import sequtils, strutils, algorithm, math, future, sets, tables, hashes, intsets
let read = iterator : string {.closure.} = (while true : (for s in stdin.readLine.split : yield s))
var
n = read().parseint
a = newseqwith(n, read().parseint)
acm1 = newseqwith(n + 1, 0)
acm2 = newseqwith(n + 1, 0)
cur = 0
ans1 = 0
ans2 = 0
for i in 0 ..< n:
acm1[i + 1] = acm1[i] + a[i]
acm2[i + 1] = acm2[i] + a[i]
if acm1[1] == 0:
ans1 = 1
acm1[1] = 1
cur = 1
for i in 2 .. n:
if acm1[i - 1] + cur < 0 and acm1[i] + cur <= 0:
ans1 += abs(1 - acm1[i] + cur)
cur += abs(1 - acm1[i])
acm1[i] = 1
elif acm1[i - 1] + cur > 0 and acm1[i] + cur >= 0:
ans1 += abs(-1 - acm1[i] + cur)
cur -= abs(-1 - acm1[i])
acm1[i] = -1
if acm2[1] == 0:
ans2 = 1
acm2[1] = -1
cur = -1
else:
if acm2[1] + cur > 0:
ans2 += abs(-1 - acm2[1])
cur -= abs(-1 - acm2[1])
acm2[1] = -1
else:
ans2 += abs(1 - acm2[1])
cur += abs(1 - acm2[1])
acm1[1] = 1
for i in 2 .. n:
if acm2[i - 1] + cur < 0 and acm2[i] + cur <= 0:
ans2 += abs(1 - acm2[i] + cur)
cur += abs(1 - acm2[i])
acm2[i] = 1
elif acm2[i - 1] + cur > 0 and acm2[i] + cur >= 0:
ans2 += abs(-1 - acm2[i] + cur)
cur -= abs(-1 - acm2[i])
acm2[i] = -1
echo min(ans1, ans2)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int a[100010];
int main() {
int n;
while (scanf("%d", &n) != EOF) {
long long sum = 0;
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
int tmp = -1;
for (int i = 0; i < n; i++) {
if (a[i] != 0) {
tmp = i;
break;
}
}
if (tmp != 0 && a[tmp] > 0) {
if (tmp % 2)
a[0] = -1;
else
a[0] = 1;
sum++;
} else if (tmp != 0 && a[tmp] < 0) {
if (tmp % 2)
a[0] = 1;
else
a[0] = -1;
sum++;
}
long long oo = a[0], flag;
if (a[0] > 0)
flag = 1;
else if (a[0] < 0)
flag = -1;
for (int i = 1; i < n; i++) {
oo += a[i];
if (flag == 1) {
if (oo >= 0) {
sum += oo + 1;
oo = -1;
}
flag = -1;
} else if (flag == -1) {
if (oo <= 0) {
sum += 0 - oo + 1;
oo = 1;
}
flag = 1;
}
}
printf("%lld\n", sum);
}
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(int argc, const char* argv[]) {
long long n, x;
long long cnt;
vector<long long> v, sum;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> x;
v.push_back(x);
sum.push_back(0);
}
sum[0] = v[0];
cnt = 0;
for (int i = 1; i < n; i++) {
sum[i] = sum[i - 1] + v[i];
if (sum[i - 1] >= 0 && sum[i] >= 0) {
cnt += sum[i - 1] + 1 + v[i];
v[i] = sum[i - 1] * (-1) - 1;
sum[i] = -1;
} else if (sum[i - 1] < 0 && sum[i] < 0) {
cnt += sum[i - 1] * (-1) + 1 - v[i];
v[i] = sum[i - 1] * (-1) + 1;
sum[i] = 1;
} else if (sum[i] == 0 && sum[i - 1] > 0) {
cnt++;
v[i]--;
sum[i] = -1;
} else if (sum[i] == 0 && sum[i - 1] < 0) {
cnt++;
v[i]++;
sum[i] = 1;
}
}
cout << cnt << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> vector;
long long temp;
for (int i = 0; i < n; i++) {
cin >> temp;
vector.push_back(temp);
}
long long answer = 0;
long long sum = 0;
for (int i = 0; i < n; i++) {
if (sum == 0)
sum += vector[0];
else if (sum < 0) {
if (sum + vector[i] >= 0) {
sum += vector[i];
} else {
answer += abs((-1) * sum + 1 - vector[i]);
sum = 1;
}
} else {
if (sum + vector[i] <= 0) {
sum += vector[i];
} else {
answer += abs((-1) * sum - 1 - vector[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 | cpp | #include <bits/stdc++.h>
using namespace std;
const int MAX = 100005;
int main() {
int n, tmp;
long long ans1{0};
long long ans2{0};
cin >> n;
long long A[MAX], B[MAX];
long long cum_sum = 0;
for (int i = 0; i < n; i++) {
cin >> tmp;
cum_sum += tmp;
A[i] = cum_sum;
B[i] = cum_sum;
}
tmp = 0;
if (A[0] <= 0) {
tmp = -A[0] + 1;
ans1 += tmp;
for (int i = 0; i < n; i++) {
A[i] += tmp;
}
}
for (int i = 0; i < n - 1; i++) {
if (A[i] > 0) {
if (A[i + 1] >= 0) {
tmp = (A[i + 1] + 1);
ans1 += tmp;
for (int j = i + 1; j < n; j++) A[j] -= tmp;
}
} else if (A[i] < 0) {
if (A[i + 1] <= 0) {
tmp = (-A[i + 1] + 1);
ans1 += tmp;
for (int j = i + 1; j < n; j++) A[j] += tmp;
}
}
}
tmp = 0;
if (B[0] >= 0) {
tmp = B[0] + 1;
ans2 += tmp;
for (int i = 0; i < n; i++) {
A[i] -= tmp;
}
}
for (int i = 0; i < n - 1; i++) {
if (B[i] > 0) {
if (B[i + 1] >= 0) {
tmp = (B[i + 1] + 1);
ans2 += tmp;
for (int j = i + 1; j < n; j++) B[j] -= tmp;
}
} else if (B[i] < 0) {
if (B[i + 1] <= 0) {
tmp = (-B[i + 1] + 1);
ans2 += tmp;
for (int j = i + 1; j < n; j++) B[j] += tmp;
}
}
}
long long ans = (ans1 > ans2 ? ans2 : ans1);
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a.at(i);
}
long long sumi = a.at(0), val1 = 0;
long long val2 = val1;
for (int i = 1; i < n; i++) {
if (i % 2 == 1) {
if (sumi + a.at(i) < 0)
sumi += a.at(i);
else {
val1 += (sumi + a.at(i) + 1);
sumi = -1;
}
} else {
if (sumi + a.at(i) > 0)
sumi += a.at(i);
else {
val1 += (abs(sumi + a.at(i)) + 1);
sumi = 1;
}
}
}
for (int i = 1; i < n; i++) {
if (i % 2 == 1) {
if (sumi + a.at(i) > 0)
sumi += a.at(i);
else {
val2 += (abs(sumi + a.at(i)) + 1);
sumi = 1;
}
} else {
if (sumi + a.at(i) < 0)
sumi += a.at(i);
else {
val2 += (sumi + a.at(i) + 1);
sumi = -1;
}
}
}
cout << min(val1, val2) << 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()))
cur = A[0]
ans = 0
for a in A[1:]:
if cur > 0:
if a + cur >= 0:
ans += abs(-1 - (a + cur))
cur = -1
else:
cur = a + cur
elif cur < 0:
if a + cur <= 0:
ans += abs(1 - (a + cur))
cur = 1
else:
cur = a + cur
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;
void answer1() {
cin.tie(0);
ios_base::sync_with_stdio(false);
int n;
cin >> n;
vector<int> a(n);
for (int& a_i : a) {
cin >> a_i;
}
long long count = 0;
long long sum = 0;
long long count2 = 0;
long long sum2 = 0;
bool is_positive = a.at(0) > 0;
for (int i = 0; i < a.size(); i++) {
sum += a.at(i);
sum2 += a.at(i);
if (is_positive) {
if (sum <= 0) {
long long diff = 1 - sum;
count += diff;
sum += diff;
}
if (sum2 >= 0) {
long long diff = 1 + sum2;
count2 += diff;
sum2 -= diff;
}
} else {
if (sum >= 0) {
long long diff = 1 + sum;
count += diff;
sum -= diff;
}
if (sum2 <= 0) {
long long diff = 1 - sum2;
count2 += diff;
sum2 += diff;
}
}
is_positive = !is_positive;
}
cout << min(count, count2) << endl;
}
void answer2() {
cin.tie(0);
ios_base::sync_with_stdio(false);
int n;
cin >> n;
vector<int> a(n);
for (int& a_i : a) {
cin >> a_i;
}
long long count = 0;
long long sum = 0;
long long count2 = 0;
long long sum2 = 0;
for (int i = 0; i < a.size(); i++) {
sum += a.at(i);
sum2 += a.at(i);
if (i % 2 == 0) {
if (sum <= 0) {
long long diff = 1 - sum;
count += diff;
sum = 1;
}
if (sum >= 0) {
long long diff = 1 + sum;
count += diff;
sum = -1;
}
} else {
if (sum >= 0) {
long long diff = 1 + sum;
count += diff;
sum = -1;
}
if (sum <= 0) {
long long diff = 1 - sum;
count += diff;
sum = 1;
}
}
}
cout << min(count, count2) << endl;
}
int main() { answer2(); }
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import copy
n = int(input())
a = list(map(int, input().split()))
def judge_pm(a,b):
if a*b<0:
return True
else:
return False
a1 = copy.deepcopy(a)
tmp_sum = a1[0]
operate_num1 = 0
for i in range(1, n):
if judge_pm(tmp_sum, tmp_sum+a1[i]):
pass
elif tmp_sum<0:
tmp_operate_num = - tmp_sum + 1 - a1[i]
operate_num1 += tmp_operate_num
a1[i] += tmp_operate_num
else:
tmp_operate_num = tmp_sum + 1 + a1[i]
operate_num1 += tmp_operate_num
a1[i] -= tmp_operate_num
tmp_sum += a1[i]
a2 = copy.deepcopy(a)
operate_num2 = 0
if a2[0]<0:
operate_num2 += 1 - a2[0]
a2[0] += operate_num2
else:
operate_num2 += a2[0] + 1
a2[0] -= operate_num2
tmp_sum = a2[0]
for i in range(1, n):
if judge_pm(tmp_sum, tmp_sum+a2[i]):
pass
elif tmp_sum<0:
tmp_operate_num = - tmp_sum + 1 - a2[i]
operate_num2 += tmp_operate_num
a2[i] += tmp_operate_num
else:
tmp_operate_num = tmp_sum + 1 + a2[i]
operate_num2 += tmp_operate_num
a2[i] -= tmp_operate_num
tmp_sum += a2[i]
print(min(operate_num1, operate_num2))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int inf = 999999999;
const double pi = acos(-1);
int a[100005] = {};
int main() {
int n, ans = 0, wa = 0;
cin >> n;
for (int i = (0); i < (int)(n); i++) cin >> a[i];
wa = a[0];
for (int i = (1); i < (int)(n); i++) {
if (wa >= 0) {
int tes = wa + a[i];
if (tes < 0) {
wa = tes;
} else {
ans += -(-1 - tes);
wa = -1;
}
} else {
int tes = wa + a[i];
if (tes > 0) {
wa = tes;
} else {
ans += 1 - tes;
wa = 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;
template <class T>
void cout_vec(const vector<T> &vec) {
for (auto itr : vec) cout << itr << ' ';
cout << endl;
}
const long long mod = 1e9 + 7;
const long long inf = 1e15;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<int> a(n);
int cnt1 = 0, cnt2 = 0, sum1 = 0, sum2 = 0;
for (long long i = 0; i < n; i++) {
cin >> a[i];
sum1 += a[i], sum2 += a[i];
if (i % 2) {
if (sum1 <= 0) {
cnt1 += 1 - sum1;
sum1 = 1;
}
if (sum2 >= 0) {
cnt2 += abs(sum2 + 1);
sum2 = -1;
}
} else {
if (sum2 <= 0) {
cnt2 += 1 - sum2;
sum2 = 1;
}
if (sum1 >= 0) {
cnt1 += abs(sum1 + 1);
sum1 = -1;
}
}
}
cout << min(cnt1, cnt2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | a = int(input())
b = list(map(int, input().split()))
num = 0
k = b[0] + b[1]
if k == 0:
if b[0] >= 0:
k -= 1
else:
k += 1
num += 1
for i in range(a-2):
if k > 0:
k += b[i+2]
if k > -1:
num += k +1
k = -1
else:
k += b[i+2]
if k < 1:
num += -k + 1
k = 1
print(num) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 |
def culc(a, plus, ans):
sum = a[0]
for i in range(1, len(a)):
plus = not(plus)
sum += a[i]
if plus:
if sum <= 0:
ans += abs(sum) + 1
sum = 1
else:
if sum >= 0:
ans += abs(sum) + 1
sum = -1
return ans
n = int(input())
a = list(map(int, input().split()))
plus = None
ans = 0
if a[0] == 0:
ans += 1
a[0] = 1
plus = True
elif a[0] > 0:
plus = True
else:
plus = False
ans1 = culc(a, plus, ans)
a[0] = a[0] // abs(a[0]) * -1
ans2 = abs(a[0]) + 1
ans2 = culc(a, not(plus), abs(a[0]) + 1 - ans)
print(min(ans1, ans2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<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];
int ans = 0;
int base = 0;
for (int i = 1; i < N; i++) {
if ((B[i] + base) * (B[i - 1] + base) > 0) {
if (B[i] + base > 0) {
if (B[i] + base > B[i - 1] + base) {
ans += abs(B[i - 1] + base) + 1;
base -= abs(B[i - 1] + base) + 1;
} else {
ans += abs(B[i] + base) + 1;
base -= abs(B[i] + base) + 1;
}
continue;
} else if (B[i] + base < 0) {
if (B[i] + base < B[i - 1] + base) {
ans += abs(B[i - 1] + base) + 1;
base += abs(B[i - 1] + base) + 1;
} else {
ans += abs(B[i] + base) + 1;
base += abs(B[i] + base) + 1;
}
continue;
}
}
if (B[i - 1] + base == 0) {
if (B[i] + base > 0) {
ans += 1;
base -= 1;
continue;
} else if (B[i] + base < 0) {
ans += 1;
base += 1;
continue;
}
}
if (i == N - 1 && B[i] + base == 0) ans++;
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | object Main {
def main(args: Array[String]): Unit = {
solve2
}
def solve2(): Unit = {
val sc = new java.util.Scanner(System.in)
val n = sc.nextInt
val a = new Array[Int](n)
for(i <- 0 until n){
a(i) = sc.nextInt
}
var sum = 0L
var opeCount1 = 0L
// 正、負、正、負(偶数インデックスが正)とする
for (i <- 0 until n) {
sum = sum + a(i)
if (i % 2 == 0) {
if (sum <= 0) {
opeCount1 += -sum + 1
sum = 1
}
} else {
if (sum > 0) {
opeCount1 += sum + 1
sum = -1
}
}
}
sum = 0L
var opeCount2 = 0L
// 負、正、負、正(奇数インデックスが正)とする
for (i <- 0 until n) {
sum = sum + a(i)
if (i % 2 != 0) {
if (sum <= 0) {
opeCount2 += -sum + 1
sum = 1
}
} else {
if (sum > 0) {
opeCount2 += sum + 1
sum = -1
}
}
}
println(math.min(opeCount1, opeCount2))
}
def solve(): Unit = {
val sc = new java.util.Scanner(System.in)
val n = sc.nextInt
val a = new Array[Int](n)
for(i <- 0 until n){
a(i) = sc.nextInt
}
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;
using ll = long long;
template <class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
int main() {
int n;
cin >> n;
vector<ll> a(n);
for (int i = (0); i < (n); ++i) cin >> a[i];
ll sum = a[0];
ll ans = 0;
for (int i = (1); i < (n); ++i) {
sum += a[i];
if (sum * (sum - a[i]) < 0)
;
else {
if (sum == 0) {
if (sum - a[i] < 0) {
sum = 1;
ans++;
} else {
ans++;
sum = -1;
}
} else if (sum > 0) {
ans += sum + 1;
sum = -1;
} else {
ans += -sum + 1;
sum = 1;
}
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.Scanner;
class Main {
int n;
int[] a;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Main m = new Main(sc);
m.solve();
sc.close();
}
Main(Scanner sc) {
n = sc.nextInt();
a = new int[n];
for(int i=0;i<n;i++){
a[i] = sc.nextInt();
}
}
void solve() {
long cnt1 = (a[0]>0)?0:(Math.abs(a[0])+1);
int sign = 1;
long sum = (a[0]>0)?a[0]:1;
for(int i=1;i<n;i++){
sum += a[i];
if(sum*sign>=0){
cnt1 += Math.abs(sum) + 1;
sum = -sign;
}
sign *= -1;
}
System.out.println(cnt1);
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
int sum = 0, ans1 = 0, ans2 = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 0) {
if (sum <= 0) {
ans1 += 1 - sum;
sum = 1;
}
} else {
if (sum >= 0) {
ans1 += 1 + sum;
sum = -1;
}
}
}
sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 != 0) {
if (sum <= 0) {
ans2 += 1 - sum;
sum = 1;
}
} else {
if (sum >= 0) {
ans2 += 1 + sum;
sum = -1;
}
}
}
cout << min(ans1, ans2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
const int INF = 1e9;
const int MOD = 1e9 + 7;
using LL = long long;
const LL LINF = 1e18;
using namespace std;
class Edge {
public:
int from, to, value;
Edge(int a, int b, int c) {
from = a;
to = b;
value = c;
}
Edge(int a, int b) {
from = a;
to = b;
}
};
int main() {
int(N);
cin >> (N);
vector<int> vec;
for (int a = 0; a < (N); ++a) {
int(b);
cin >> (b);
vec.push_back(b);
}
int ans1 = 0, ans2 = 0, n1 = 0, n2 = 0;
for (int a = 0; a < N; a++) {
n1 += vec.at(a);
n2 += vec.at(a);
if (a % 2 == 0) {
if (n1 >= 0) {
ans1 += abs(n1 - (-1));
n1 -= abs(n1 - (-1));
}
if (n2 <= 0) {
ans2 += abs(n2 - 1);
n2 += abs(n2 - 1);
}
} else {
if (n2 >= 0) {
ans2 += abs(n2 - (-1));
n2 -= abs(n2 - (-1));
}
if (n1 <= 0) {
ans1 += abs(n1 - 1);
n1 += abs(n1 - 1);
}
}
}
cout << (min(ans1, ans2)) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; ++i) cin >> a.at(i);
long long cnt = 0, cnt2 = 0, sum = a.at(0);
if (a.at(0) >= 0)
for (int i = 1; i < n; i++) {
if (i % 2 == 1) {
while (a.at(i) >= 0 || sum + a.at(i) >= 0) {
cnt++;
a.at(i)--;
}
sum += a.at(i);
} else {
while (a.at(i) < 0 || sum + a.at(i) <= 0) {
cnt++;
a.at(i)++;
}
sum += a.at(i);
}
}
else {
for (int i = 1; i < n; i++) {
if (i % 2 == 1) {
while (a.at(i) < 0 || sum + a.at(i) <= 0) {
cnt++;
a.at(i)++;
}
sum += a.at(i);
} else {
while (a.at(i) >= 0 || sum + a.at(i) >= 0) {
cnt++;
a.at(i)--;
}
sum += a.at(i);
}
}
}
cout << cnt << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | 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(s * (-1) + 1 - li[i + 1])
s = 1
else:
if s + li[i + 1] < 0:
s += li[i + 1]
else:
ans[j] += abs(s * (-1) - 1 - li[i + 1])
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 | cpp | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int a[maxn];
int main() {
int n;
long long sum;
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
sum = a[0];
long long cnt = 0;
if (sum == 0) {
int i = 1;
while (a[i] == 0 && i < n) i++;
if (i == n) {
cnt = (n - 1) * 2 + 1;
printf("%lld\n", cnt);
return 0;
}
if (a[i] > 0 && i & 1 == 1) {
sum = -1;
cnt++;
} else if (a[i] > 0 && i & 1 == 0) {
sum = 1;
cnt++;
} else if (a[i] < 0 && i & 1 == 1) {
sum = 1;
cnt++;
} else if (a[i] < 0 && i & 1 == 0) {
sum = -1;
cnt++;
}
}
for (int i = 1; i < n; i++) {
if (sum > 0) {
long long t = sum + a[i];
if (t < 0)
sum = t;
else {
long long b = abs(t + 1);
cnt += b;
sum = -1;
}
} else if (sum < 0) {
long long t = sum + a[i];
if (t > 0)
sum = t;
else {
long long b = abs(1 - t);
cnt += b;
sum = 1;
}
}
}
printf("%lld\n", cnt);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll mod = LLONG_MAX;
int a[100010];
int rui[100010];
int n;
int main() {
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
rui[0] = a[0];
for (int i = 1; i < n; i++) {
rui[i] += a[i] + rui[i - 1];
}
int temp = 0;
int ans = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0 && rui[i] + temp == 0) {
temp--;
ans++;
continue;
}
if (i % 2 == 1 && rui[i] + temp == 0) {
temp++;
ans++;
continue;
}
if (i % 2 == 0 && rui[i] + temp < 0) {
ans += abs(1 - (rui[i] + temp));
temp += 1 - (rui[i] + temp);
}
if (i % 2 == 1 && rui[i] + temp > 0) {
ans += abs(-(1 + rui[i] + temp));
temp += -(1 + rui[i] + temp);
}
}
if (ans < 0) ans = 1e9;
temp = 0;
int ans2 = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0 && rui[i] + temp == 0) {
temp++;
ans2++;
continue;
}
if (i % 2 == 1 && rui[i] + temp == 0) {
temp--;
ans2++;
continue;
}
if (i % 2 == 0 && rui[i] + temp > 0) {
ans2 += abs(-(1 + rui[i] + temp));
temp += -(1 + rui[i] + temp);
}
if (i % 2 == 1 && rui[i] + temp < 0) {
ans2 += abs(1 - (rui[i] + temp));
temp += 1 - (rui[i] + temp);
}
}
if (ans2 < 0) ans2 = 1e9;
cout << min(ans, 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 | java | import java.util.Scanner;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
Scanner scan = new Scanner(System.in);
int N = Integer.parseInt(scan.nextLine());
String[] str = scan.nextLine().split(" ");
int[] tmp = new int[str.length];
int sum = 0;
int cnt = 0;
for(int i = 0; i < str.length; i++){
tmp[i] = Integer.parseInt(str[i]);
}
sum = tmp[0];
for(int i = 1; i < tmp.length; i++){
if(sum >= 0){
if(sum + tmp[i] >= 0){
cnt = cnt + Math.abs(sum + tmp[i]) + 1;
sum = -1;
}else{
sum += tmp[i];
}
}else if(sum < 0){
if(sum + tmp[i] <= 0){
cnt = cnt + Math.abs(sum + tmp[i]) + 1;
sum = 1;
}else{
sum += tmp[i];
}
}
}
System.out.println(cnt);
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n=int(input())
l=list(map(int,input().split()))
c,f=0,l[0]/abs(l[0])
s=l[0]
for i in range(1,n,1):
t=s+l[i]
if s*t>0:
s=-1*f
c+=abs(t-s+1)
else:
f=-1*f
s=s+l[i]
print(int(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 | n=int(input())
a=list(map(int,input().split()))
cnt1,cnt2,sm1,sm2=0,0,0,0
for i in a:#+
sm1+=i
if a.index(i)%2 ==0:
if sm1<1:
cnt1+=1-sm1
sm1=1
else:
if sm1>-1:
cnt1+=1+sm1
sm1=-1
for i in a:#-
sm2=i
if a.index(i)%2 ==0:
if sm2>-1:
cnt2+=1+sm2
sm2=-1
else:
if sm2<1:
cnt2+=1-sm2
sm2=1
print(min(cnt1,cnt2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int f(const vector<int>& data, int n, int fst) {
vector<int> a = data;
int s = 0, ret = 0, k;
for (int i = 0; i < n; i++) {
k = (s + a[i]) * fst;
if (k <= 0 && i & 1) {
ret += (1 - k);
a[i] += (1 - k) * fst;
}
if (k >= 0 && !(i & 1)) {
ret += (k + 1);
a[i] -= (k + 1) * fst;
}
s += a[i];
}
return ret;
}
int main() {
long long n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
cout << min(f(a, n, 1), f(a, n, -1)) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void) {
vector<int> v;
long long int res = 0;
int sign = 0;
int n, t;
long long int sum = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> t;
v.push_back(t);
}
if (v[0] > 0) {
sign = 0;
} else {
sign = 1;
}
sum += v[0];
for (int i = 1; i < v.size(); i++) {
sum += v[i];
if (sign == 0) {
if (sum > 0) {
res += (sum + 1);
sum -= (sum + 1);
} else if (sum == 0) {
res += 1;
sum -= 1;
}
} else {
if (sum < 0) {
res += ((-1 * sum) + 1);
sum += ((-1 * sum) + 1);
} else if (sum == 0) {
res += 1;
sum += 1;
}
}
sign = 1 - sign;
}
cout << res << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n=int(input())
a=list(map(int,input().split()))
import sys
sum=0
cnt=0
# 奇数+
for i in range(n):
z=sum+a[i]
if i%2==0:
if z<0:
sum=z
else:
sum=-1
cnt+=(z+1)
else:
if z>0:
sum=z
else:
sum=1
cnt+=(1-z)
cnt_sbst=cnt
# 奇数-
for i in range(n):
z=sum+a[i]
if i%2==1:
if z<0:
sum=z
else:
sum=-1
cnt+=(z+1)
else:
if z>0:
sum=z
else:
sum=1
cnt+=(1-z)
cnt_plus=cnt
ans=min(cnt_plus,cnt_sbst)
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
a =list(map(int,input().split()))
s =a[0]
c = 0
if s ==0:
s = 1
c = 1
for i in range(1,N):
if s*(s+a[i])>=0 and s>0:
c = c+abs(s+a[i])+1
a[i] = a[i]-(abs(s+a[i])+1)
elif s*(s+a[i])>=0 and s<0:
c = c+abs(s+a[i])+1
a[i] = a[i]+abs(s+a[i])+1
s = s+a[i]
print(c) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int n;
cin >> n;
ll sum = 0;
cin >> sum;
ll ans = 0;
for (int i = 0; i < n - 1; ++i) {
int a;
cin >> a;
sum += a;
if (sum == 0) {
cout << "Debug" << endl;
ll need = sum - a > 0 ? -1 : 1;
sum += need;
ans += abs(need);
} else if (((sum - a > 0) == (sum > 0)) or ((sum - a < 0) == (sum < 0))) {
ll need = sum > 0 ? -(sum + 1) : -(sum - 1);
sum += need;
ans += abs(need);
}
}
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 | N = gets.to_i
a = gets.split.map(&:to_i)
first = a.shift
count = 0
if first < 0
first = first * (-1)
a.map! do |i|
i * (-1)
end
end
if first == 0
first = 1
count += 1
end
b = []
sum = first
a.each do |ai|
b << ai
sum += ai
if b.size.odd?
if sum > -1
difference = sum - (-1)
count += difference.abs
b[-1] -= difference
end
else
if sum < 1
difference = sum - (+1)
count += difference.abs
b[-1] -= difference
end
sum = sum - ai + b[-1]
end
#p "diff = #{difference}"
#p "b = #{b}"
end
p 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<iostream>
using namespace std;
long int abs(long int a){
if(a<0)return -a;
else return a;
}
bool isDifAbs(long int a,long int b){
if(a*b<0)return true;
return false;
}
int main(){
int n;
long int sum,tmp,ttmp,ans=0;
cin >> n;
cin >> sum;
for(int i=1;i<n;i++){
cin >> tmp;
if(!isDifAbs(sum,sum+tmp)){
ttmp=tmp;
if(sum<0)tmp=abs(tmp)+1;
else if(sum>0)tmp=-(abs(tmp)+1);
ans+=abs(tmp-ttmp);
}
else if(sum+tmp==0){
if(sum<0)tmp++;
if(sum>0)tmp--;
ans++;
}
sum+=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 | # https://qiita.com/_-_-_-_-_/items/34f933adc7be875e61d0
# abcde s=input() s='abcde'
# abcde s=list(input()) s=['a', 'b', 'c', 'd', 'e']
# 5(1つだけ) a=int(input()) a=5
# 1 2 | x,y = map(int,input().split())| x=1,y=2
# 1 2 3 4 5 ... n li = input().split() li=['1','2','3',...,'n']
# 1 2 3 4 5 ... n li = list(map(int,input().split())) li=[1,2,3,4,5,...,n]
# FFFTFTTFF li = input().split('T') li=['FFF', 'F', '', 'FF']
# INPUT
# 3
# hoge
# foo
# bar
# ANSWER
# n=int(input())
# string_list=[input() for i in range(n)]
import collections
def inpl(): return list(map(int, input().split()))
#### START
n = int(input())
a = list(map(int,input().split()))
ans = 1e16
for s in [1, -1]:
res, csum = 0, 0
for a_i in a:
csum += a_i
if csum * s < 0:
res += abs(csum-s)
csum = s
s *= -1
ans = min(ans, res)
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;
signed main() {
long long n;
cin >> n;
vector<long long>(a)((n));
for (long long i = 0; i < (n); ++i) {
cin >> a[i];
}
long long x = 0, b = 0, t = 0;
for (long long i = 0; i < (n); ++i) {
t += a[i];
if (i % 2 == 0) {
if (t >= 0) {
x += t + 1;
t = -1;
}
} else {
if (t <= 0) {
x += abs(t + 1);
t = 1;
}
}
}
t = 0;
for (long long i = 0; i < (n); ++i) {
t += a[i];
if (i % 2 == 0) {
if (t <= 0) {
b += t + 1;
t = -1;
}
} else {
if (t >= 0) {
b += abs(t + 1);
t = 1;
}
}
}
long long ans = min(x, b);
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 | python3 | N = int(input())
A = [int(_) for _ in input().split()]
def counter(dp):
count = 0
for i in range(1, N):
is_positive = 2 * (dp > 0) - 1
dp += A[i]
if dp * is_positive >= 0:
count += abs(dp)+1
dp = -is_positive
return count
print(min(1+abs(A[0])+counter(-1), 1+abs(A[0])+counter(1), counter(A[0])))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
A = list(map(int, input().split()))
a = A[0]
ans = 0
for i in range(1,n):
b = A[i]
if a> 0:
if a + b >= 0:
ans += abs(-1-a-b)
a = -1
else:
a += b
elif a < 0:
if a+b <= 0:
ans += abs(1-a-b)
a = 1
else:
a += b
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;
signed main() {
long long n;
cin >> n;
long long a[n + 1];
a[0] = 0;
long long cnta = 0;
long long cntb = 0;
long long sum = 0;
for (long long i = 0; i < n; i++) {
cin >> a[i + 1];
a[i + 1] += a[i];
}
for (long long i = 1; i < n + 1; i++) {
if (i % 2 == 0) {
if (0 <= (a[i] + sum)) {
cnta += (a[i] + sum + 1);
sum -= (a[i] + sum + 1);
}
} else {
if ((a[i] + sum) <= 0) {
cnta += (1 - (a[i] + sum));
sum += (1 - (a[i] - sum));
}
}
}
sum = 0;
for (long long i = 1; i < n + 1; i++) {
if (i % 2 == 1) {
if (0 <= (a[i] + sum)) {
cntb += (a[i] + sum + 1);
sum -= (a[i] + sum + 1);
}
} else {
if ((a[i] + sum) <= 0) {
cntb += (1 - (a[i] + sum));
sum += (1 - (a[i] - sum));
}
}
}
cout << min(cnta, cntb) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using P = pair<int, int>;
using PL = pair<ll, ll>;
using vp = vector<P>;
const int INF = 1 << 30, MOD = 1000000007;
const ll LINF = 1ll << 60;
struct ostdmy {
template <class T>
ostdmy& operator<<(const T& t) {
return *this;
}
};
ostdmy cer_;
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 (a > b) {
a = b;
return 1;
}
return 0;
}
template <class T>
ostream& operator<<(ostream& o, const vector<T>& v) {
for (const T& i : v) o << i << ' ';
return o;
}
template <class T>
istream& operator>>(istream& i, vector<T>& v) {
for (T& j : v) i >> j;
return i;
}
template <class T, class U>
ostream& operator<<(ostream& o, const pair<T, U>& p) {
return o << p.first << ' ' << p.second;
}
template <class T, class U>
istream& operator>>(istream& i, pair<T, U>& p) {
return i >> p.first >> p.second;
}
template <class T>
ostream& operator<<(ostream& o, const set<T>& v) {
for (const T& i : v) o << i << ' ';
return o;
}
int sign(int a) {
if (a < 0) return -1;
if (a > 0) return 1;
return 0;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(0);
int n;
cin >> n;
vl a(n);
cin >> a;
ll ans = 0;
ll bef = a[0];
if (bef == 0) {
bef = -sign(a[1]);
ans++;
}
for (int i = 1, i_l = (n); i < i_l; ++i) {
if (bef + a[i] == 0 || sign(bef) == sign(bef + a[i])) {
ans += abs(a[i] + bef + sign(bef));
bef = -sign(bef);
} else {
bef += a[i];
}
}
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 | UNKNOWN | gets
seq = gets.split.map(&:to_i)
def foo(seq)
cnt = 0
sum = seq.shift
seq.each{|a|
if sum < 0
if sum + a > 0
sum += a
else
cnt += 1 - (sum + a)
sum = 1
end
else
if sum + a < 0
sum += a
else
cnt += 1 + (sum + a)
sum = -1
end
end
p [a, sum, cnt]
}
return cnt
end
zero_cnt = 0
while seq.shift == 0
zero_cnt += 1
end
p (zero_cnt * 2) - 1 + foo(seq)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int,input().split()))
ans = 0
for i in range(n):
if i == 0:
if a[i] == 0:
f = "+"
a[i] = 1
elif a[0] > 0:
f = "+"
elif a[0] < 0:
f = "-"
else:
o = sum(a[:i])
if f == "+":
if a[i] + o > 0:
c = -1 - o
ans += abs(c - a[i])
a[i] = c
f = "-"
else:
if a[i] + o == 0:
a[i] -= 1
ans += 1
f = "-"
elif f == "-":
if a[i] + o < 0:
c = 1 - o
ans += abs(c - a[i])
a[i] = c
f = "+"
else:
if a[i] + o == 0:
a[i] += 1
ans += 1
f = "+"
#print(a)
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<long long> L(N);
for (int i = 0; i < N; i++) {
cin >> L.at(i);
}
long long v = 0, res = 0, le = 0;
bool change_flag = true;
for (int i = 0; i < N; i++) {
if (i == 0) {
v = L.at(i);
} else {
if (v > 0 && v + L.at(i) >= 0) {
while (true) {
if (v + L.at(i) < 0) {
v += L.at(i);
le = 0;
break;
} else {
le = -1 - v - L.at(i);
L.at(i) -= abs(le);
res += abs(le);
}
}
} else if (v < 0 && v + L.at(i) <= 0) {
while (true) {
if (v + L.at(i) > 0) {
v += L.at(i);
le = 0;
break;
} else {
le = 1 + v + L.at(i);
L.at(i) += abs(le);
res += abs(le);
}
}
} else {
v += L.at(i);
}
}
}
cout << res << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
int top = a[0], cnt = (a[0] == 0 ? 1 : 0);
bool sign = (top >= 0 ? true : false);
for (int i = 1; i < n; i++) {
if (sign && top + a[i] < 0) {
top += a[i];
sign = false;
} else if (!sign && top + a[i] > 0) {
top += a[i];
sign = true;
} else if (top + a[i] == 0) {
cnt++;
if (sign) {
top = -1;
sign = false;
} else {
top = 1;
sign = false;
}
} else {
if (sign) {
cnt += (top + a[i]) + 1;
top = -1;
sign = false;
} else {
cnt += 1 - (top + a[i]);
top = 1;
sign = true;
}
}
}
int t = cnt;
top = a[0], cnt = a[0] + 1;
sign = (top <= 0 ? true : false);
for (int i = 1; i < n; i++) {
if (sign && top + a[i] < 0) {
top += a[i];
sign = false;
} else if (!sign && top + a[i] > 0) {
top += a[i];
sign = true;
} else if (top + a[i] == 0) {
cnt++;
if (sign) {
top = -1;
sign = false;
} else {
top = 1;
sign = false;
}
} else {
if (sign) {
cnt += (top + a[i]) + 1;
top = -1;
sign = false;
} else {
cnt += 1 - (top + a[i]);
top = 1;
sign = true;
}
}
}
cnt = min(cnt, t);
cout << cnt << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | fun main() {
val n = readLine()!!.toInt()
val a = readLine()!!.split(" ").map { it.toLong() }
var answer = 0L
var total = a[0]
for (i in 1 until n) {
val tmp = total
total = total + a[i]
if (total == 0L) {
if (tmp > 0) {
answer += 1
total = -1
} else if (tmp < 0) {
answer += 1
total = 1
}
}
if (tmp > 0 && total > 0) {
answer += (total + 1)
total = -1
} else if (tmp < 0 && total < 0) {
answer += (-total + 1)
total = 1
}
}
println("answer $answer")
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 |
def culc(a, plus, ans):
sum = a[0]
for i in range(1, len(a)):
plus = not(plus)
sum += a[i]
if plus:
if sum <= 0:
ans += abs(sum) + 1
sum = 1
else:
if sum >= 0:
ans += abs(sum) + 1
sum = -1
return ans
n = int(input())
a = list(map(int, input().split()))
plus = None
ans = 0
if a[0] == 0:
ans += 1
a[0] = 1
plus = True
elif a[0] > 0:
plus = True
else:
plus = False
ans1 = culc(a, plus, ans)
a[0] = a[0] // abs(a[0]) * -1
ans2 = culc(a, not(plus), abs(a[0]) + 1 - ans)
print(min(ans1, ans2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n=int(input())
a=list(map(int, input().split()))
sum_now=a[0]
sum_before=-a[0]
count_1=0
count_2=0
for i in range(n):
while sum_now*sum_before>=0:
if sum_before==0:
sum_now=-a[1]/abs(a[1])
count_1+=1
else:
count_1+=abs(int(sum_now))+1
sum_now=-sum_before/abs(sum_before)
if i!=n-1:
sum_before=sum_now
sum_now=sum_now+a[i+1]
sum_now=a[0]
sum_before=-a[0]
if sum_before==0:
sum_now=a[1]/abs(a[1])
count_2+=1
else:
count_2+=abs(int(sum_now))+1
sum_now=-sum_now/abs(sum_now)
for i in range(n):
while sum_now*sum_before>=0:
if sum_before==0:
sum_now=a[1]/abs(a[1])
count_2+=1
else:
count_2+=abs(int(sum_now))+1
sum_now=-sum_before/abs(sum_before)
if i!=n-1:
sum_before=sum_now
sum_now=sum_now+a[i+1]
print(min(count_1, count_2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | # -*- coding: utf-8 -*-
n = int(input())
an = list(map(int, input().split()))
sum = an[0]
ans = 0
for i in range(1,n):
if sum * (sum + an[i]) < 0:
sum += an[i]
else:
if sum > 0:
ans += abs(sum + an[i] + 1)
sum = -1
else:
ans += abs(sum + an[i] - 1)
sum = 1
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | 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);
bool flag;
bool flag2 = false;
for (int i = 0; i < N; i++) {
if (A.at(i) > 0) {
flag = true;
break;
} else if (A.at(i) < 0) {
flag = false;
break;
}
}
int ans = 0;
int total = A.at(0);
for (int i = 0; i < N - 1; i++) {
int count = 0;
if (flag) {
if (total + A.at(i + 1) >= 0) {
count = -1 - total - A.at(i + 1);
ans += abs(count);
A.at(i + 1) = A.at(i + 1) + count;
}
total += A.at(i + 1);
flag = false;
} else if (!flag) {
if (total + A.at(i + 1) <= 0) {
count = 1 - total - A.at(i + 1);
ans += abs(count);
A.at(i + 1) = A.at(i + 1) + count;
}
total += A.at(i + 1);
flag = true;
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | 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_
print(mov, mov_)
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>
using namespace std;
int main() {
unsigned long long n;
cin >> n;
vector<long long> a(n);
for (unsigned long long i = 0; i < n; ++i) cin >> a[i];
size_t op = 0;
long long sum = a[0];
for (unsigned long long i = 1; i < n; ++i) {
if (sum > 0) {
sum += a[i];
while (sum >= 0) {
++op;
--sum;
}
} else {
sum += a[i];
while (sum <= 0) {
++op;
++sum;
}
}
}
cout << op << endl;
return EXIT_SUCCESS;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | #! /Library/Frameworks/Python.framework/Versions/3.7/bin/python3
# a[0] == 0 のケース、要検証
n = int(input())
a = list(map(int, input().split()))
count_p = 0 # 操作回数(正)
count_m = 0 # 操作回数(負)
s = 0 # 現時点の和
def counter(a, s):
if s * (s + a) < 0:
return 0
else:
if s < 0:
return 1 - (s + a)
else:
return -1 - (s + a)
# a[0]を+にする場合
if a[0] <= 0:
count_p = 1 - a[0]
s = 1
else:
s = a[0]
for i in range(n-1):
tmp = counter(a[i + 1], s)
s = s + a[i+1]+tmp
count_p = count_p + abs(tmp)
# a[0]を-にする場合
if a[0] >= 0:
count_m = 1 - a[0]
s = -1
else:
s = a[0]
for i in range(n-1):
tmp = counter(a[i + 1], s)
s = s + a[i+1]+tmp
count_m = count_m + abs(tmp)
print(str(min(count_p, count_m)))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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 ans = 0;
long long now;
cin >> now;
if (now == 0) {
now++;
ans++;
}
for (long long(i) = (0); (i) < (n - 1); ++i) {
long long tmp;
cin >> tmp;
if (now > 0) {
now += tmp;
if (now >= 0) {
ans += now + 1LL;
now = -1;
}
} else {
now += tmp;
if (now <= 0) {
ans -= (now - 1LL);
now = 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() {
long long n, i, j, sum, res, sumprev;
cin >> n;
sum = res = 0;
long long arr[n];
for (i = 0; i < n; i++) cin >> arr[i];
sum = res = 0;
sumprev = 0;
for (i = 0; i < n; i++) {
sum += arr[i];
if ((sum == 0) && (sumprev > 0)) {
arr[i]--;
sum--;
res++;
} else if ((sum == 0) && (sumprev < 0)) {
arr[i]++;
sum++;
res++;
} else if ((sumprev > 0) && (sum > 0)) {
long long d = sum - 0;
arr[i] = arr[i] - d - 1;
sum = sum - d - 1;
res = res + d + 1;
} else if ((sumprev < 0) && (sum < 0)) {
long long d = 0 - sum;
arr[i]++;
sum = sum + d + 1;
res = res + d + 1;
}
sumprev = sum;
}
cout << res;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long n;
cin >> n;
int i;
long a[n], su, cnt, cnt2;
su = 0;
cnt = 0;
for (i = 0; i < n; i++) {
cin >> a[i];
}
for (i = 0; i < n; i++) {
su += a[i];
if (a[0] >= 0) {
if (i % 2 == 0) {
if (su <= 0) {
cnt += 1 - su;
su = 1;
}
} else {
if (su >= 0) {
cnt += su + 1;
su = -1;
}
}
} else {
if (i % 2 == 0) {
if (su >= 0) {
cnt += su + 1;
su = -1;
}
} else {
if (su <= 0) {
cnt += 1 - su;
su = -1;
}
}
}
}
cout << cnt << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL << 60;
struct edge {
long long to, cost;
};
vector<vector<edge> > edges;
vector<long long> dist;
bool bellman_ford(int n, int s) {
dist = vector<long long>(n, INF);
dist[s] = 0;
for (int i = 0; i < n; i++) {
for (int v = 0; v < n; v++) {
for (int k = 0; k < edges[v].size(); k++) {
edge e = edges[v][k];
if (dist[e.to] > dist[v] + e.cost) {
dist[e.to] = dist[v] + e.cost;
if (i == n - 1 && e.to == n - 1) {
return false;
}
}
}
}
}
return false;
}
int main(int argc, const char* argv[]) {
long long N, M, a, b, c;
cin >> N >> M;
edges = vector<vector<edge> >(N, vector<edge>());
for (int i = 0; i < M; i++) {
cin >> a >> b >> c;
a--, b--;
edges[a].push_back((edge){b, -c});
}
if (bellman_ford(N, 0)) {
cout << "inf" << endl;
} else {
cout << -dist[N - 1] << endl;
}
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
now_s = 0
ans = 0
for i in range(n):
pre_s = now_s
now_s = pre_s + a[i]
if pre_s < 0:
if now_s <= 0:
if abs(pre_s) < abs(now_s):
ans += abs(pre_s) + 1
now_s = a[i] + 1
else:
ans += abs(now_s) + 1
now_s = 1
elif pre_s > 0:
if now_s >= 0:
if abs(pre_s) < abs(now_s):
ans += abs(pre_s) + 1
now_s = a[i] - 1
else:
ans += abs(now_s) + 1
now_s = -1
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (auto&& x : a) cin >> x;
long long ans1 = 0, ans2 = 0, sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 0 && sum >= 0) {
ans1 = sum + 1;
sum = -1;
} else if (i % 2 == 1 && sum <= 0) {
ans1 += -sum + 1;
sum = 1;
}
}
sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 1 && sum >= 0) {
ans2 = sum + 1;
sum = -1;
} else if (i % 2 == 0 && sum <= 0) {
ans2 += -sum + 1;
sum = 1;
}
}
cout << min(ans1, ans2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
A = list(map(int,input().split()))
ans = 0
x = 0
S = []
S.append(A[0])
for i in range(1,n):
x = A[i] + S[i - 1]
if S[i - 1] > 0:
if x >= 0:
x = - x - 1
ans += abs(x)
S.append(-1)
else:
S.append(x)
elif S[i - 1] < 0:
if x <= 0:
x = -x + 1
ans += abs(x)
S.append(1)
else:
S.append(x)
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int N;
cin >> N;
ll d = 0;
vector<ll> a(N);
for (int i = 0; i < N; i++) cin >> a.at(i);
ll sum = a.at(0);
for (int i = 1; i < N; i++) {
if (sum * (sum + a.at(i)) >= 0) {
if (sum > 0) {
d += abs(a.at(i) - (-1 - sum));
a.at(i) = -1 - sum;
} else {
d += abs(a.at(i) - (1 - sum));
a.at(i) = 1 - sum;
}
}
sum += a.at(i);
}
cout << d << 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, ansa = 0, ansb = 0, sum = 0;
cin >> n;
bool plus = true;
for (int i = 0; i < (n); i++) {
int a;
cin >> a;
while (plus && sum + a <= 0) {
a++;
ansa++;
}
while (!plus && sum + a >= 0) {
a--;
ansa++;
}
sum += a;
plus = !plus;
}
plus = false;
sum = 0;
for (int i = 0; i < (n); i++) {
int a;
cin >> a;
while (plus && sum + a >= 0) {
a++;
ansb++;
}
while (!plus && sum + a <= 0) {
a--;
ansb++;
}
sum += a;
plus = !plus;
}
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 | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int count = 0;
int a1;
cin >> a1;
int sum = a1;
if (a1 >= 0) {
for (int i = 0; i < n - 1; i++) {
int a;
cin >> a;
sum += a;
if (i % 2 == 0) {
while (sum >= 0) {
sum--;
count++;
}
} else {
while (sum <= 0) {
sum++;
count++;
}
}
}
} else {
for (int i = 0; i < n - 1; i++) {
int a;
cin >> a;
sum += a;
if (i % 2 == 1) {
while (sum >= 0) {
sum--;
count++;
}
} else {
while (sum <= 0) {
sum++;
count++;
}
}
}
}
cout << count << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | # 必要になったら結果が1か-1になるまで加えたり引いたりする方針
# 実装がちょっと無駄に考える 符号変わったりするとか先頭が0の場合とか
n = int(input())
arr = list(map(int, input().split()))
tmp = arr[0]
def f(arr, tmp):
count = 0
for i in range(1, n):
if tmp + arr[i] < 0 < tmp:
# 加えたら符号が+から-になる場合
tmp += arr[i]
elif tmp < 0 < tmp + arr[i]:
# 加えたら符号が-から+になる場合
tmp += arr[i]
elif 0 <= tmp + arr[i] <= tmp or 0 <= tmp <= tmp + arr[i]:
# 加えても+から-にはならず+のまま
count += abs(-1 - tmp - arr[i])
tmp = -1
elif tmp + arr[i] <= tmp <= 0 or tmp <= tmp + arr[i] <= 0:
# 加えても-から+にはならず-のまま
count += abs(1 - tmp - arr[i])
tmp = 1
else:
# runtime errorの出し方がわからんのでTLEを狙う(おい
while True:
if 1 == 0:
break
return count
if tmp == 0:
if arr[1] != 0:
print(min(f(arr, 1), f(arr, -1)) + 1)
else:
# もうこんなんうまくやる方法なんて思いつかんわ
b = []
ni = -2
o = 0
for i, a in enumerate(arr):
if a == 0:
if i == 0:
b.append(1)
o += 1
elif i == 1:
b.append(ni)
o += 2
else:
ni *= -1
b.append(ni)
o += 2
else:
break
b_r = list(map(lambda x: x * -1, b))
arr_a = b + arr[len(b) :]
arr_b = b_r + arr[len(b_r) :]
print(min(f(arr_a, 1), f(arr_b, -1)) + o)
else:
print(f(arr, tmp))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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;
namespace AtCoder
{
class Program
{
static void Main(string[] args)
{
//[summary]C - Green Bin
int n = int.Parse(Console.ReadLine());
var a = ReadLine();
//必要な操作の回数
long count = 0;
if (a[0] == 0)
{
if (a[1] >= 0)
{
a[0] = -1;
}
else
{
a[0] = 1;
}
count++;
}
long sum = a[0];
long next = 0;
for (int i = 1; i < n; i++)
{
next = sum + a[i];
if ((sum > 0 && next < 0) | (sum < 0 && next > 0))
{
//何もしない
}
else if (sum > 0)
{
count += 1 + Math.Abs(next);
next = -1;
}
else
{
count += 1 + Math.Abs(next);
next = 1;
}
sum = next;
}
Console.WriteLine(count);
}
static List<int> ReadLine()
{
var line = Console.ReadLine();
var array = line.Split(' ');
return array.Select(x => int.Parse(x)).ToList();
}
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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 num_operate(long n, long sum, long* a) {
long j;
for (long i = 1; i < n; i++) {
if (sum * (sum + a[i]) < 0)
sum += a[i];
else {
j += abs(sum + a[i]) + 1;
if (sum < 0)
sum = 1;
else
sum = -1;
}
}
return j;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
long n;
cin >> n;
vector<long> a(n);
for (long i = 0; i < n; i++) cin >> a[i];
long sum = a[0];
long cnt1 = num_operate(n, 1, &a.front()) + 1;
long cnt2 = num_operate(n, -1, &a.front()) + 1;
cout << min(cnt1, cnt2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
long a[110000], s[110000];
cin >> n;
for (int(i) = (0); (i) < (n); (i)++) cin >> a[i];
long t = 0;
for (int(i) = (0); (i) < (n); (i)++) {
t += a[i];
s[i] = t;
}
long cnt = 0;
int diff_acm = 0;
int diff;
if (s[0] == 0) {
s[0] = 1;
diff_acm = 1;
}
for (int(i) = (1); (i) < (n); (i)++) {
s[i] += diff_acm;
if (s[i - 1] < 0 && s[i] <= 0) {
diff = 1 - s[i];
s[i] = 1;
cnt += diff;
diff_acm += diff;
} else if (s[i - 1] > 0 && s[i] >= 0) {
diff = s[i] + 1;
s[i] = -1;
cnt += diff;
diff_acm -= diff;
}
}
cout << cnt << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using namespace std;
long long a[100006];
int main() {
long long n;
cin >> n;
for (int(i) = 0; (i) < (n); (i)++) cin >> a[i];
long long count = 0;
long long wa = 0;
for (int(i) = 0; (i) < (n); (i)++) {
if (wa > 0 && wa + a[i] >= 0) {
count += wa + a[i] + 1;
wa = -1;
continue;
}
if (wa < 0 && wa + a[i] <= 0) {
count += -(wa + a[i]) + 1;
wa = 1;
continue;
}
wa += a[i];
}
cout << count << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
A = list(map(int, input().split()))
sm=A[0]
cnt=0
if A[0]>=0:
for i in range(1,N):
sm+=A[i]
if i%2==1:
if sm>=0:
cnt+=sm+1
sm=-1
else:
if sm<=0:
cnt+=sm*-1+1
sm=1
elif A[0]<0:
for i in range(1,N):
sm+=A[i]
if i%2==1:
if sm<=0:
cnt+=sm*-1+1
sm=1
else:
if sm>=0:
cnt+=sm+1
sm=-1
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() {
long long n, i, j, ans = 0, sum = 0, flag;
cin >> n;
vector<long long> a(n);
for (i = 0; i < n; i++) {
cin >> a[i];
}
sum += a[0];
if (sum == 0) {
ans++;
for (i = 0; i < n; i++) {
if (a[i] != 0) {
if (i % 2 == 0) {
sum = 1;
} else {
sum = -1;
}
break;
}
}
}
for (i = 1; i < n; i++) {
if (sum > 0) {
flag = 1;
} else {
flag = 0;
}
if (flag == 1) {
sum += a[i];
if (sum >= 0) {
ans += (sum + 1);
sum = -1;
}
} else {
sum += a[i];
if (sum <= 0) {
ans += 1 - sum;
sum = 1;
}
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
int A[100010];
cin >> n;
for (int i = 0; i < n; i++) {
cin >> A[i];
}
int sum = 0;
int counter = 0;
for (int i = 0; i < n; i++) {
sum += A[i];
if (i % 2 == 0) {
if (sum <= 0) {
int diff = 1 - sum;
sum += diff;
counter += diff;
}
} else {
if (sum >= 0) {
int diff = sum + 1;
sum -= diff;
counter += diff;
}
}
}
int counterNeg = 0;
sum = 0;
for (int i = 0; i < n; i++) {
sum += A[i];
if (i % 2 == 0) {
if (sum >= 0) {
int diff = sum + 1;
sum -= diff;
counterNeg += diff;
}
} else {
if (sum <= 0) {
int diff = 1 - sum;
sum += diff;
counterNeg += diff;
}
}
}
int ans = counter > counterNeg ? counterNeg : counter;
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() {
long long n, x = 0, a[100001], ans = 0;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
if (a[0] == 0) {
ans += 1;
for (int i = 1; a[i] == 0; ++i) {
x = i + 1;
ans += 2;
}
if (abs(a[x]) == 1) {
ans++;
if (a[x] > 0)
a[x]++;
else
a[x]--;
}
}
long long sum1 = a[x], sum2 = a[x];
for (int i = x + 1; i < n; i++) {
sum2 += a[i];
if (sum2 >= 0 && sum1 > 0) {
ans += abs(sum2) + 1;
sum2 = sum2 - abs(sum2) - 1;
}
if (sum2 <= 0 && sum1 < 0) {
ans += abs(sum2) + 1;
sum2 = sum2 + abs(sum2) + 1;
}
sum1 = sum2;
}
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>
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;
vector<int> total(n, 0);
vector<int> res(total);
total[0] = a[0], res[0] = a[0];
for (int i = 0, i_len = (n); i < i_len; ++i) {
if (i != 0) total[i] += a[i] + total[i - 1];
if (i % 2 != 0) {
if (total[i] == 0 or total[i] > 0) {
even += abs(total[i] + 1);
total[i] = -1;
}
} else {
if (total[i] == 0 or total[i] < 0) {
even += abs(total[i] - 1);
total[i] = 1;
}
}
}
for (int i = 0, i_len = (n); i < i_len; ++i) {
if (i != 0) res[i] += a[i] + res[i - 1];
if (i % 2 == 0) {
if (res[i] == 0 or res[i] > 0) {
odd += abs(res[i] + 1);
res[i] = -1;
}
} else {
if (res[i] == 0 or res[i] < 0) {
odd += abs(res[i] - 1);
res[i] = 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() {
long long int n;
cin >> n;
long long int a[n];
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
long long int count = 0;
if (a[0] == 0) {
for (int i = 0; i < n; ++i) {
if (a[i] > 0) {
a[0] = -1;
++count;
break;
} else if (a[i] < 0) {
a[0] = 1;
++count;
break;
}
}
}
long long int cal = a[0];
if (a[0] == 0) {
count = 1 + 2 * (n - 1);
} else {
for (int i = 1; i < n; ++i) {
if (cal + a[i] == 0) {
if (cal < 0) {
++count;
++a[i];
cal = 1;
} else {
++count;
--a[i];
cal = -1;
}
} else if (cal < 0 && cal + a[i] < 0) {
count += -(cal + a[i] - 1);
a[i] += -(cal + a[i] - 1);
} else if (cal > 0 && cal + a[i] > 0) {
count += (cal + a[i] + 1);
a[i] -= (cal + a[i] + 1);
}
cal += a[i];
}
}
cout << count << "\n";
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n;
int a[100010];
long long sum[100010];
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int cnt1 = 0;
sum[0] = a[0];
while (sum[0] <= 0) {
sum[0]++;
cnt1++;
}
for (int i = 1; i < n; i++) {
sum[i] = sum[i - 1] + a[i];
if (i % 2 == 0) {
while (sum[i] <= 0) {
sum[i]++;
cnt1++;
}
} else {
while (sum[i] >= 0) {
sum[i]--;
cnt1++;
}
}
}
int cnt2 = 0;
sum[0] = a[0];
while (sum[0] >= 0) {
sum[0]--;
cnt2++;
}
for (int i = 1; i < n; i++) {
sum[i] = sum[i - 1] + a[i];
if (i % 2 == 1) {
while (sum[i] <= 0) {
sum[i]++;
cnt2++;
}
} else {
while (sum[i] >= 0) {
sum[i]--;
cnt2++;
}
}
}
int cnt = min(cnt1, cnt2);
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 | from functools import lru_cache
n = int(input())
s = list(map(int, input().split()))
@lru_cache(maxsize=None)
def cost(z):
res = abs(z - s[0])
sum = 0
for j, y in enumerate(s):
tmp = sum + y
if j == 0:
sum = tmp
continue
if sum * tmp >= 0:
c = -1 if tmp > 0 else 1
x = c - tmp
res += abs(x)
sum = c
else:
sum = tmp
return res
ans1 = cost(s[0])
ans2 = cost(1)
ans3 = cost(-1)
print(min(ans1, ans2, ans3))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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 ch_sign(int n) {
if (n == 0) return 0;
return (n > 0) - (n < 0);
}
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; ++i) cin >> a[i];
int sign1 = (a[0] > 0) - (a[0] < 0), sign2 = (-1) * sign1;
int s1 = 0, s2 = 0;
int ans1 = 0, ans2 = 0;
for (int i = 0; i < n; ++i) {
s1 += a[i];
s2 += a[i];
sign1 *= -1;
sign2 *= -1;
if (ch_sign(s1) != sign1) {
ans1 += abs(s1 - sign1);
s1 = sign1;
}
if (ch_sign(s2) != sign2) {
ans2 += abs(s2 - sign2);
s2 = sign2;
}
}
cout << ((ans1 < ans2) ? 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>
int changeCount(long long int *, int, int);
int main(void) {
int n;
if (scanf("%d\n", &n) < 1) {
exit(1);
}
long long int *a;
a = (long long int *)malloc(n * sizeof(long long int));
int i;
for (i = 0; i < n - 1; i++) {
if (scanf("%lld ", &a[i]) < 1) {
exit(1);
}
}
if (scanf("%lld", &a[n - 1]) < 1) {
exit(1);
}
int evenchangeCount = changeCount(a, n, 0);
int oddchangeCount = changeCount(a, n, 1);
if (evenchangeCount <= oddchangeCount) {
printf("%d", evenchangeCount);
} else {
printf("%d", oddchangeCount);
}
return 0;
}
int changeCount(long long int *a, int n, int oddPositive) {
int temp_1 = 0;
int temp_2 = 0;
int changeCount = 0;
int i;
for (i = 0; i < n; i++) {
temp_2 = temp_1 + a[i];
if (i % 2 == 1) {
if (oddPositive == 1) {
if (temp_2 <= 0) {
changeCount += -temp_2 + 1;
temp_2 = 1;
}
} else {
if (temp_2 >= 0) {
changeCount += temp_2 + 1;
temp_2 = -1;
}
}
} else {
if (oddPositive == 0) {
if (temp_2 <= 0) {
changeCount += -temp_2 + 1;
temp_2 = 1;
}
} else {
if (temp_2 >= 0) {
changeCount += temp_2 + 1;
temp_2 = -1;
}
}
}
temp_1 = temp_2;
}
return changeCount;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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 | var read = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
var obj;
var inLine = [];
read.on('line', function(input){inLine.push(input);});
read.on('close', function(){
obj = init(inLine);
myerr("-----start-----");
var start = new Date();
Main();
var end = new Date() - start;
myerr("----- end -----");
myerr("time : " + (end) + "ms");
});
function nextInt(){return myconv(next(),1);} function nextStrArray(){return myconv(next(),2);}
function nextIntArray(){return myconv(next(),4);} function nextCharArray(){return myconv(next(),6);}
function next(){return obj.next();} function hasNext(){return obj.hasNext();}
function init(input){
var returnObj = {
list : input, index : 0, max : input.length,
hasNext : function(){return (this.index < this.max);},
next : function(){if(!this.hasNext()){throw "ArrayIndexOutOfBoundsException これ以上ないよ";}else{var returnInput = this.list[this.index];this.index++;return returnInput;}}
};
return returnObj;
}
function myout(s){console.log(s);}
function myerr(s){console.error("debug:" + require("util").inspect(s,false,null));}
//[no]要素の扱い。数値型
//不明値、異常時:引数そのまま返す 1:数値へ変換
//2:半角SPで分割 4:半角SPで分割し、数値配列へ
//6:1文字で分割 7:1文字で分割し、数値配列へ
//8:半角SPで結合 9:改行で結合 0:文字なしで結合
function myconv(i,no){try{switch(no){case 1:return parseInt(i);case 2:return i.split(" ");case 4:return i.split(" ").map(Number);case 6:return i.split("");case 7:return i.split("").map(Number);case 8:return i.join(" ");case 9:return i.join("\n");case 0:return i.join("");default:return i;}}catch(e){return i;}}
function Main(){
var N = nextInt();
var list = nextIntArray();
var output = 0;
var sum = new Array(N);
if(list[0] == 0){
if(list[1] > 0){
sum[0] = -1;
}else{
sum[0] = 1;
}
output++;
}else{
sum[0] = list[0];
}
for(var i = 1; i < N; i++){
sum[i] = sum[i - 1] + list[i];
if((sum[i - 1] < 0 && sum[i] > 0) || (sum[i - 1] > 0 && sum[i] < 0)){
}else{
if((sum[i - 1] > 0)){
output += sum[i] + 1;
sum[i] = -1;
}else{
output += Math.abs(sum[i]) + 1;
sum[i] = 1;
}
}
}
myout(output)
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int INF = 1001001000;
const int mINF = -1001001000;
const long long LINF = 1010010010010010000;
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<long long> a(n);
for (int i = 0; i < (n); ++i) {
cin >> a[i];
}
long long ans = INF;
int flip = 0;
for (int si = 0; si < (2); ++si) {
int f = flip;
long long cnt = 0;
long long sum = 0;
for (int i = 0; i < (n); ++i) {
if (!f && sum + a[i] > -1) {
long long need = -1 - sum;
cnt += a[i] - need;
sum = -1;
} else if (f && sum + a[i] < 1) {
long long need = 1 - sum;
cnt += need - a[i];
sum = 1;
} else {
sum = sum + a[i];
}
f ^= 1;
}
ans = min(ans, cnt);
flip ^= 1;
}
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.