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 | python3 | n = int(input())
a = list(map(int, input().split()))
now_s = 0
ans = 0
for i in range(n-1):
now_s += a[i]
next_s = now_s + a[i+1]
if now_s == 0:
if next_s > 0:
now_s -= 1
ans += 1
else:
now_s += 1
ans += 1
if now_s*next_s >= 0:
ans += abs(next_s) + 1
a[i+1] = -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 | python3 | import numpy as np
import copy
n = int(input())
a = list(map(int,input().split()))
b=copy.deepcopy(a)
cntm=0
cntp=0
sum_a=0
sum_b=0
if a[0]<=0:
cntm+=-a[0]+1
a[0]=1
if b[0]>=0:
cntp+=b[0]+1
b[0]=-1
for i in range(n-1):
sum_a += a[i]
sum_b += b[i]
if abs(sum_a) >= abs(a[i+1]) or sum_a*a[i+1]>=0:
cntp += abs(-sum_a-np.sign(sum_a) -a[i+1])
a[i+1]=-sum_a-np.sign(sum_a)
if abs(sum_b) >= abs(b[i+1]) or sum_b*b[i+1]>=0:
cntm += abs(-sum_b-np.sign(sum_b) -b[i+1])
b[i+1]=-sum_b-np.sign(sum_b)
print(min(cntm, cntp)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n =sc.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
int[] sum = new int[n+1];
for (int i = 1; i <= n; i++) {
sum[i] = sum[i-1] + a[i-1];
}
int count1 = count(n, true, sum, 0 );
int count2 = count(n, false, sum, 0);
System.out.println(Math.min(count1, count2));
}
private static int count(int n , boolean pastPlus, int[] sum, long carry){
int count2 = 0;
for (int i = 1; i <= n; i++) {
long cur = sum[i] + carry;
if (pastPlus && cur > 0) {
// minus nisinaito
count2 += cur + 1;
carry = - cur - 1;
}
if (!pastPlus && cur < 0) {
count2 += -cur + 1;
carry = -cur + 1;
}
pastPlus = !pastPlus;
}
return count2;
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long a, first;
cin >> first;
long long sum = first, pre_sum, count = 0;
for (int i = 1; i < n; ++i) {
cin >> a;
if (first == 0) {
if (a >= 0)
first--;
else
first++;
sum = first;
count++;
}
pre_sum = sum;
sum += a;
if (sum * pre_sum >= 0) {
count += (abs(sum) + 1);
if (pre_sum < 0)
sum += (abs(sum) + 1);
else
sum -= (abs(sum) + 1);
}
}
cout << count << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N = 0;
cin >> N;
vector<long long int> A(N), a(N), b(N);
for (int i = 0; i < N; i++) cin >> A[i];
for (int i = 0; i < N; i++) a[i] = A[i];
for (int i = 0; i < N; i++) b[i] = A[i];
long long int cnt1 = 0, cnt2 = 0;
long long int M1 = 0, M2 = 0;
for (int i = 0; i < N; i++) {
if (M1 + a[i] > 1) {
if (i % 2 == 0) {
;
} else {
cnt1 = abs(1 - (M1 + a[i]));
a[i] = 1 - (M1 + a[i]);
}
} else if (M1 + a[i] < -1) {
if (i % 2 == 1) {
;
} else {
cnt1 = abs(-1 - (M1 + a[i]));
a[i] = -1 - (M1 + a[i]);
}
} else if (M1 + a[i] == 0) {
if (i % 2 == 0) {
cnt1 = abs(1 - (M1 + a[i]));
a[i] = 1 - (M1 + a[i]);
} else {
cnt1 = abs(-1 - (M1 + a[i]));
a[i] = -1 - (M1 + a[i]);
}
}
M1 += a[i];
}
for (int i = 0; i < N; i++) {
if (M2 + b[i] > 1) {
if (i % 2 == 1) {
;
} else {
cnt2 = abs(1 - (M2 + b[i]));
b[i] = 1 - (M2 + b[i]);
}
} else if (M2 + b[i] < -1) {
if (i % 2 == 0) {
;
} else {
cnt2 = abs(-1 - (M2 + b[i]));
a[i] = -1 - (M2 + b[i]);
}
} else if (M2 + b[i] == 0) {
if (i % 2 == 1) {
cnt2 = abs(1 - (M2 + b[i]));
b[i] = 1 - (M2 + b[i]);
} else {
cnt2 = abs(-1 - (M2 + b[i]));
b[i] = -1 - (M2 + b[i]);
}
}
M2 += b[i];
}
long long int ans = min(cnt1, cnt2);
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> vector;
long long temp;
for(int i=0; i<n; i++) {
cin >> temp;
vector.push_back(temp);
}
long long answer1=0;
long long answer2=0;
long long sum1=0;
long long sum2=0;
for(int i=0; i<n; i++) {
if(i == 0) {
sum1 = vector[0]; //初項
}
else if(sum1 < 0) {
if(sum1 + vector[i] > 0){ //和の符号がデフォルトで異なるとき
// answer -> そのまま
sum1 += vector[i];
}
else {
answer1 += abs((-1)*sum1+1 - vector[i]); // vector[i] -> -sum1+1 までincrimentすると和は1
sum1 = 1;
}
}
else {
if(sum1 + vector[i] < 0) {
//answer->そのまま
sum1 += vector[i];
}
else {
answer1 += abs((-1)*sum1-1 - vector[i]); // vector[i] -> -sum1-1 までincrimentすると和は-1
sum1 = -1;
}
}
}
for(int i=0; i<n; i++) {
if(i==0) {
if(vector[0] > 0) {
sum2 = -1;
answer2 += abs(-1-vector[0]);
}
else {
sum2 = 1;
answer2 += abs(1-vector[0]);
}
}
else if(sum2 < 0) {
if(sum2 + vector[i] > 0){ //和の符号がデフォルトで異なるとき
// answer-> そのまま
sum2 += vector[i];
}
else {
answer2 += abs((-1)*sum2+1 - vector[i]); // vector[i] -> -sum1+1 までincrimentすると和は1
sum2 = 1;
}
}
else {
if(sum2 + vector[i] < 0) {
//answer->そのまま
sum2 += vector[i];
}
else {
answer2 += abs((-1)*sum2-1 - vector[i]); // vector[i] -> -sum1-1 までincrimentすると和は-1
sum2 = -1;
}
}
cout << min(answer1,answer2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
s = [a[0]]
for i in range(1, n):
s.append(s[-1]+a[i])
cnt_a = 0
base = 0
for i in range(n):
if (base + s[i] > 0) != (i % 2 == 0):
cnt_a += (1 + abs(base + s[i]))
base += (1 + abs(base + s[i])) * (-1) ** i
cnt_b = 0
base = 0
for i in range(n):
if (base + s[i] < 0) != (i % 2 == 0):
cnt_b += (1 + abs(base + s[i]))
base -= (1 + abs(base + s[i])) * (-1) ** i
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 | cpp | #include <bits/stdc++.h>
using namespace std;
long long a[100000], b[100001];
int main() {
int long long n;
cin >> n;
for (long long i = 0; i < n; i++) {
cin >> a[i];
b[i] += a[i];
b[i + 1] = b[i];
}
int long long sum = 0, sum2 = 0;
if (b[0] == 0) {
long long i = 0;
while (b[i] == 0 && i < n) i++;
if (i % 2 == 0) {
b[0] = 1;
sum2 = 1;
} else {
b[0] = -1;
sum2 = -1;
}
sum++;
}
for (long long i = 1; i < n; i++) {
b[i] += sum2;
if (b[i] == 0) {
if (b[i - 1] > 0) {
sum2--;
sum++;
b[i] = -1;
} else {
sum2++;
sum++;
b[i] = 1;
}
} else {
if (b[i - 1] > 0 && b[i] > 0) {
sum2 -= (b[i] + 1);
sum += (b[i] + 1);
b[i] = -1;
} else if (b[i] < 0 && b[i - 1] < 0) {
sum2 += (0 - b[i] + 1);
sum += (0 - b[i] + 1);
b[i] = 1;
}
}
}
cout << sum << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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);
int main() {
int N;
scanf("%d", &N);
vector<int> arr(N);
for (int i = (0); i < (N); ++i) {
scanf("%d", &arr[i]);
}
int count1 = 0;
int sum = 0;
bool positive = true;
if (arr[0] < 0) {
positive = false;
}
sum += arr[0];
for (int i = 1; i < N; i++) {
if (positive) {
if (sum + arr[i] < 0) {
sum = sum + arr[i];
positive = false;
} else {
int must = -sum - 1;
int diff = abs(arr[i] - must);
count1 += diff;
sum += must;
positive = false;
}
} else {
if (sum + arr[i] > 0) {
sum = sum + arr[i];
positive = true;
} else {
int must = -sum + 1;
int diff = abs(must - arr[i]);
count1 += diff;
sum += must;
positive = true;
}
}
}
int count2 = 2;
sum = 0;
positive = false;
if (arr[0] < 0) {
positive = true;
}
sum += arr[0];
for (int i = 1; i < N; i++) {
if (positive) {
if (sum + arr[i] < 0) {
sum = sum + arr[i];
positive = false;
} else {
int must = -sum - 1;
int diff = abs(arr[i] - must);
count2 += diff;
sum += must;
positive = false;
}
} else {
if (sum + arr[i] > 0) {
sum = sum + arr[i];
positive = true;
} else {
int must = -sum + 1;
int diff = abs(must - arr[i]);
count2 += diff;
sum += must;
positive = true;
}
}
}
printf("%d", min(count1, count2));
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
s, ans, i = 0, 0, 0
while a[i] == 0 and i < n:
ans += 2
i += 1
ans = max(0, ans - 1)
if i == n:
print(ans)
exit()
if ans > 0:
if abs(a[i]) == 1:
ans += 1
s = a[i] // abs(a[i])
else:
s = a[0]
i += 1
for j in range(i, n):
if abs(a[j]) > abs(s) and (a[j] == abs(a[j])) != (s == abs(s)):
s += a[j]
else:
pre_s = s
s = -1 * s // abs(s)
ans += abs(a[j] - s + pre_s)
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
int a[10005];
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
int nCount = 0, sum = a[0];
bool isPositive;
if (a[0] == 0) {
nCount++;
sum++;
isPositive = true;
} else if (a[0] > 0) {
isPositive = true;
} else {
isPositive = false;
}
for (int i = 1; i < n; i++) {
if (isPositive) {
if ((a[i] + sum) < 0) {
sum += a[i];
} else {
nCount += abs(a[i] + sum) + 1;
sum = -1;
}
isPositive = !isPositive;
} else {
if ((a[i] + sum) > 0) {
sum += a[i];
} else {
nCount += abs(a[i] + sum) + 1;
sum = 1;
}
isPositive = !isPositive;
}
}
cout << nCount << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <bits/stdc++.h>
int main(void) {
int n;
long sum1 = 0;
long sum2 = 0;
long tmp;
long lcount = 0;
long rcount = 0;
long a[100000];
char input[1500000];
int i = 0, j = 0;
int cp = 0, tcp = 0;
char tp[12];
tp[12] = '\0';
fgets(input, 1500000, stdin);
n = atoi(input);
fgets(input, 1500000, stdin);
for (i = 0; i < n; i++) {
while (input[cp] != ' ' && input[cp] != '\n') {
tp[tcp] = input[cp];
tcp++;
cp++;
}
tp[tcp] = '\0';
tcp = 0;
cp++;
a[i] = atoi(tp);
}
tmp = a[0];
for (i = 1; i < n; i++) {
if (i % 2 == 0) {
tmp += a[i];
while (tmp > -1) {
lcount++;
tmp--;
}
} else {
tmp += a[i];
while (tmp < 1) {
lcount++;
tmp++;
}
}
}
tmp = a[0];
for (i = 1; i < n; i++) {
if (i % 2 == 1) {
tmp += a[i];
while (tmp > -1) {
rcount++;
tmp--;
}
} else {
tmp += a[i];
while (tmp < 1) {
rcount++;
tmp++;
}
}
}
printf("%ld\n", lcount > rcount ? rcount : lcount);
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;
vector<long long> a;
enum State { Plus, Minus, Zero };
State GetState(long long sum) {
State state;
if (sum > 0)
state = Plus;
else if (sum == 0)
state = Zero;
else
state = Minus;
return state;
}
unsigned long long Count(State initialState) {
unsigned long long count = 0;
vector<long long> b = a;
State state = GetState(b[0]);
if (state == Zero) {
state = initialState;
if (state == Plus)
b[0] = 1;
else if (state == Minus)
b[0] = -1;
count++;
}
long long sum = b[0];
for (int i = 1; i < n; i++) {
State nextState = GetState(sum + b[i]);
switch (nextState) {
case Plus:
if (state == Plus) {
long long bf_a = b[i];
b[i] = -1 - sum;
count += abs(b[i] - bf_a);
nextState = Minus;
}
break;
case Minus:
if (state == Minus) {
long long bf_a = b[i];
b[i] = 1 - sum;
count += abs(b[i] - bf_a);
nextState = Plus;
}
break;
case Zero:
if (state == Plus) {
long long bf_a = b[i];
b[i] = -1 - sum;
count += abs(b[i] - bf_a);
nextState = Minus;
} else if (state == Minus) {
long long bf_a = b[i];
b[i] = 1 - sum;
count += abs(b[i] - bf_a);
nextState = Plus;
}
default:
break;
}
sum += b[i];
state = nextState;
}
if (sum == 0) count++;
return count;
}
int main() {
cin >> n;
a.resize(n);
for (int i = 0; i < n; i++) cin >> a[i];
unsigned long long pCount = Count(Plus);
unsigned long long mCount = Count(Minus);
unsigned long long ans = pCount < mCount ? pCount : mCount;
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 | # -*- coding:utf-8 -*-
def solve():
N = int(input())
A = list(map(int, input().split()))
# dp[i] := i番目までの最小の操作回数
dp = [None] * N
dp[0] = 0
ans = 0
# A = [0 0 0 0 0 100 ...] みたいなときは
# A = [-1 2 -2 2 -2 100 ...] にしたい
if A[0] == 0:
hugou = 1
for i in range(1, N):
if A[i] == 0:
continue
if A[i] > 0:
hugou = 1
break
else:
hugou = -1
break
for j in range(i, 0, -1):
if hugou == -1:
A[j] = 2
else:
A[j] = -2
hugou *= -1
ans += 2
if hugou > 0:
A[0] = -1
else:
A[0] = 1
ans += 1
# ruiseki[i] := i番目までの累積和
ruiseki = [0] * N
ruiseki[0] = A[0]
for i in range(1, N):
total = A[i] + ruiseki[i-1]
if ruiseki[i-1] > 0:
# ruiseki[i]をマイナス値にする必要がある
if total < 0:
ruiseki[i] = total
else:
diff = abs(total - (-1))
ans += diff
A[i] -= diff
total -= diff
ruiseki[i] = total
else:
# ruiseki[i]をプラス値にする必要がある
if total > 0:
ruiseki[i] = total
else:
diff = abs(total - 1)
ans += diff
A[i] += diff
total += diff
ruiseki[i] = total
print(ans)
if __name__ == "__main__":
solve()
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
sa = input().split()
a = [0 for i in range(n)]
sumeven = 0
sumodd = 0
for i,sai in enumerate(sa):
a[i] = int(sai)
if i % 2 == 0:
sumeven += a[i]
else:
sumodd += a[i]
ret = 0
sm = 0
for i,ai in enumerate(a):
if sumeven < sumodd:
if i == 0:
ret += max(ai + 1, 0)
sm += min(-1, ai)
elif i % 2 == 1:
ret += max(-sm+1-ai,0)
sm += max(ai, -sm+1)
elif i % 2 == 0:
ret += max(sm+1 + ai, 0)
sm += min(ai, -(sm+1))
else:
if i == 0:
ret += max(1 - ai, 0)
sm += max(1, ai)
elif i % 2 == 0:
ret += max(-sm+1-ai,0)
sm += max(ai, -sm+1)
elif i % 2 == 1:
ret += max(sm+1 + ai, 0)
sm += min(ai, -(sm+1))
print(ret) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long INF = 1e12;
vector<long long> v;
vector<long long> sum;
int n;
long long add(long long idx, long long minus_max, long long plus_min,
long long flag) {
if (idx == 0) {
if (minus_max >= 0)
return minus_max + 1LL;
else if (plus_min <= 0)
return -plus_min + 1LL;
return 0;
} else if (flag == 0) {
long long change = max(0LL, sum[idx - 1] - (plus_min - 2));
if (idx == n - 1) {
return change + add(idx - 1, sum[idx - 1], plus_min + change, 1LL);
}
return change + add(idx - 1, max(sum[idx - 1], minus_max + change),
plus_min + change, 1LL);
} else {
long long change = max(0LL, (minus_max + 2) - sum[idx - 1]);
if (idx == n - 1) {
return change + add(idx - 1, minus_max - change, sum[idx - 1], 0LL);
}
return change + add(idx - 1, minus_max - change,
min(sum[idx - 1], plus_min - change), 0LL);
}
}
int main(void) {
cin >> n;
v.resize(n);
sum.resize(n);
for (int i = 0; i < (n); i++) cin >> v[i];
for (int i = 0; i < (n); i++) {
if (i == 0)
sum[i] = v[i];
else
sum[i] = v[i] + sum[i - 1];
}
long long ans = INF;
ans = min(ans, add(n - 1, sum[n - 1], sum[n - 1], 0LL));
ans = min(ans, add(n - 1, sum[n - 1], sum[n - 1], 1LL));
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>
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;
}
const long long INF = 1LL << 60;
long long foo(const vector<long long>& a, bool oddPositive) {
size_t n = a.size();
vector<long long> b1(n);
long long ans = 0;
b1[0] = a[0];
if (oddPositive && a[0] <= 0) {
b1[0] = 1;
ans += abs(a[0] + 1);
}
if (!oddPositive && a[0] >= 0) {
b1[0] = -1;
ans += abs(a[0] + 1);
}
for (int i = 1; i < n; i++) {
if ((b1[i - 1] + a[i]) * b1[i - 1] < 0) {
b1[i] = b1[i - 1] + a[i];
continue;
}
if (a[i] + b1[i - 1] == 0) {
ans += 1;
} else if (b1[i - 1] * a[i] > 0) {
ans += abs(a[i]) + abs(b1[i - 1]) + 1;
} else {
ans += abs(b1[i - 1]) - abs(a[i]) + 1;
}
b1[i] = b1[i - 1] < 0 ? 1 : -1;
}
return ans;
}
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; ++i) {
long long tmp = 0;
cin >> tmp;
a[i] = tmp;
}
long long ans1 = foo(a, true);
long long ans2 = foo(a, false);
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 | java | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.next());
int[] a = new int[n];
for(int i = 0;i < n;i++) {
a[i] = Integer.parseInt(sc.next());
}
sc.close();
int sum = a[0];
int cnt = 0;
if(a[0] > 0) {
for(int i = 1;i < n;i++) {
sum += a[i];
if(i%2!=0 && sum >=0) {
cnt += Math.abs(-1-sum);
sum = -1;
}else if(i%2==0&&sum <=0) {
cnt += Math.abs(1-sum);
sum = 1;
}
}
}else if(a[0] < 0) {
for(int i = 1;i < n;i++) {
sum += a[i];
if(i%2==0 && sum >=0) {
cnt += Math.abs(-1-sum);
sum = -1;
}else if(i%2!=0&&sum <=0) {
cnt += Math.abs(1-sum);
sum = 1;
}
}
}
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 | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
scanf("%lld", &n);
vector<long long> a(n);
for (int i = 0; i < (n); ++i) scanf("%lld", &a[i]);
long long ans1 = 0, ans2 = 0;
long long total = 0, flag = 1;
for (int i = 0; i < (n); ++i) {
flag *= -1;
total += a[i];
if (total * flag <= 0) {
ans1 += abs(total - flag);
total = flag;
}
}
total = 0, flag = 1;
for (int i = 0; i < (n); ++i) {
flag *= -1;
total += a[i];
if (total * flag <= 0) {
ans2 += abs(total - flag);
total = flag;
}
}
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 | n1=int(input())
l1=list(map(int,input().split()))
suml=[sum(l1[:i]) for i in range(1,n1+1)]
Num=0
for j in range(1,n1):
if j==1 and suml[j]==0:
l1[j]=l1[j]+1
Num=Num+1
suml=[sum(l1[:i]) for i in range(1,n1+1)]
else:
while (suml[j-1]*suml[j]>0) or (suml[j] ==0):
if suml[j-1]<0:
l1[j]=l1[j]+1
Num=Num+1
suml=[sum(l1[:i]) for i in range(1,n1+1)]
elif suml[j-1]>0:
l1[j]=l1[j]-1
Num=Num+1
suml=[sum(l1[:i]) for i in range(1,n1+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 | cpp | #include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using mii = map<int, int>;
using pqls = priority_queue<long long>;
using pqlg = priority_queue<long long, vector<long long>, greater<long long>>;
using mll = map<long long, long long>;
using pll = pair<long long, long long>;
using sll = set<long long>;
long long divup(long long a, long long b);
long long kaijou(long long i);
long long P(long long n, long long k);
long long C(long long n, long long k);
long long GCD(long long a, long long b);
long long LCM(long long a, long long b);
bool prime(long long N);
double distance(vector<long long> p, vector<long long> q, long long n);
void press(vector<long long> &v);
void ranking(vector<long long> &v);
void erase(vector<long long> &v, long long i);
void unique(vector<long long> &v);
void printv(vector<long long> v);
vector<ll> keta(ll x);
long long modpow(long long a, long long n, long long mod);
long long modinv(long long a, long long mod);
vector<long long> inputv(long long n);
vector<long long> yakusuu(int n);
map<long long, long long> soinsuu(long long n);
vector<vector<long long>> maze(long long i, long long j, vector<string> &s);
vector<long long> eratos(long long n);
set<long long> eraset(long long n);
long long divup(long long a, long long b) {
long long x = abs(a);
long long y = abs(b);
long long z = (x + y - 1) / y;
if ((a < 0 && b > 0) || (a > 0 && b < 0))
return -z;
else if (a == 0)
return 0;
else
return z;
}
long long kaijou(long long i) {
if (i == 0) return 1;
long long j = 1;
for (long long k = 1; k <= i; k++) {
j *= k;
}
return j;
}
long long P(long long n, long long k) {
if (n < k) return 0;
long long y = 1;
for (long long i = 0; i < k; i++) {
y *= (n - i);
}
return y;
}
long long C(long long n, long long k) {
if (n < k) return 0;
return P(n, k) / kaijou(k);
}
long long GCD(long long a, long long b) {
if (a < b) swap(a, b);
long long d = a % b;
if (d == 0) {
return b;
}
return GCD(b, d);
}
long long LCM(long long a, long long b) { return (a / GCD(a, b)) * b; }
bool prime(long long N) {
if (N == 1) {
return false;
}
if (N < 0) return false;
long long p = sqrt(N);
for (long long i = 2; i <= p; i++) {
if (N % i == 0) {
return false;
}
}
return true;
}
double distance(vector<long long> p, vector<long long> q, long long n) {
double x = 0;
for (long long i = 0; i < n; i++) {
x += pow((p.at(i) - q.at(i)), 2);
}
return sqrt(x);
}
void press(vector<long long> &v) {
long long n = v.size();
vector<long long> w(n);
map<long long, long long> m;
for (auto &p : v) {
m[p] = 0;
}
long long i = 0;
for (auto &p : m) {
p.second = i;
i++;
}
for (long long i = 0; i < n; i++) {
w.at(i) = m[v.at(i)];
}
v = w;
return;
}
void ranking(vector<long long> &v) {
long long n = v.size();
map<long long, long long> m;
long long i;
for (i = 0; i < n; i++) {
m[v.at(i)] = i;
}
vector<long long> w(n);
i = 0;
for (auto &p : m) {
v.at(i) = p.second;
i++;
}
return;
}
void erase(vector<long long> &v, long long i) {
long long n = v.size();
if (i > n - 1) return;
for (long long j = i; j < n - 1; j++) {
v.at(j) = v.at(j + 1);
}
v.pop_back();
return;
}
void unique(vector<long long> &v) {
long long n = v.size();
set<long long> s;
long long i = 0;
while (i < n) {
if (s.count(v.at(i))) {
erase(v, i);
n--;
} else {
s.insert(v.at(i));
i++;
}
}
return;
}
void printv(vector<long long> v) {
cout << "{ ";
for (auto &p : v) {
cout << p << ",";
}
cout << "}" << endl;
}
vector<ll> keta(ll x) {
if (x == 0) return {0};
ll n = log10(x) + 1;
vll w(n, 0);
for (ll i = 0; i < n; i++) {
ll p;
p = x % 10;
x = x / 10;
w[n - 1 - i] = p;
}
return w;
}
long long modpow(long long a, long long n, long long mod) {
long long res = 1;
while (n > 0) {
if (n & 1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
long long modinv(long long a, long long mod) { return modpow(a, mod - 2, mod); }
vector<long long> inputv(long long n) {
vector<long long> v(n);
for (long long i = 0; i < n; i++) {
cin >> v[i];
}
return v;
}
vector<long long> yakusuu(long long n) {
vector<long long> ret;
for (long long i = 1; i <= sqrt(n); ++i) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n) {
ret.push_back(n / i);
}
}
}
sort(ret.begin(), ret.end());
return ret;
}
map<long long, long long> soinsuu(long long n) {
map<long long, long long> m;
long long p = sqrt(n);
while (n % 2 == 0) {
n /= 2;
if (m.count(2)) {
m[2]++;
} else {
m[2] = 1;
}
}
for (long long i = 3; i * i <= n; i += 2) {
while (n % i == 0) {
n /= i;
if (m.count(i)) {
m[i]++;
} else {
m[i] = 1;
}
}
}
if (n != 1) m[n] = 1;
return m;
}
vector<vector<long long>> maze(ll i, ll j, vector<string> &s) {
ll h = s.size();
ll w = s[0].size();
queue<vector<long long>> q;
vector<vector<long long>> dis(h, vll(w, -1));
q.push({i, j});
dis[i][j] = 0;
while (!q.empty()) {
auto v = q.front();
q.pop();
if (v[0] > 0 && s[v[0] - 1][v[1]] == '.' && dis[v[0] - 1][v[1]] == -1) {
dis[v[0] - 1][v[1]] = dis[v[0]][v[1]] + 1;
q.push({v[0] - 1, v[1]});
}
if (v[1] > 0 && s[v[0]][v[1] - 1] == '.' && dis[v[0]][v[1] - 1] == -1) {
dis[v[0]][v[1] - 1] = dis[v[0]][v[1]] + 1;
q.push({v[0], v[1] - 1});
}
if (v[0] < h - 1 && s[v[0] + 1][v[1]] == '.' && dis[v[0] + 1][v[1]] == -1) {
dis[v[0] + 1][v[1]] = dis[v[0]][v[1]] + 1;
q.push({v[0] + 1, v[1]});
}
if (v[1] < w - 1 && s[v[0]][v[1] + 1] == '.' && dis[v[0]][v[1] + 1] == -1) {
dis[v[0]][v[1] + 1] = dis[v[0]][v[1]] + 1;
q.push({v[0], v[1] + 1});
}
}
return dis;
}
long long modC(long long n, long long k, long long mod) {
if (n < k) return 0;
long long p = 1, q = 1;
for (long long i = 0; i < k; i++) {
p = p * (n - i) % mod;
q = q * (i + 1) % mod;
}
return p * modinv(q, mod) % mod;
}
long long POW(long long a, long long n) {
long long res = 1;
while (n > 0) {
if (n & 1) res = res * a;
a = a * a;
n >>= 1;
}
return res;
}
vector<long long> eratos(long long n) {
if (n < 2) return {};
vll v(n - 1);
for (long long i = 0; i < n - 1; i++) {
v[i] = i + 2;
}
ll i = 0;
while (i < n - 1) {
ll p = v[i];
for (ll j = i + 1; j < n - 1; j++) {
if (v[j] % p == 0) {
v.erase(v.begin() + j);
n--;
}
}
i++;
}
v.resize(n - 1);
return v;
}
set<long long> eraset(long long n) {
set<long long> s;
vll v = eratos(n);
for (auto &t : v) {
s.insert(t);
}
return s;
}
vll line(ll x1, ll y1, ll x2, ll y2) {
vector<ll> v(3);
v[0] = y1 - y2;
v[1] = x2 - x1;
v[2] = -x1 * (y1 - y2) + y1 * (x1 - x2);
return v;
}
double dis(vll v, ll x, ll y) {
double s = sqrt(v[0] * v[0] + v[1] * v[1]);
return (double)abs(v[0] * x + v[1] * y + v[2]) / s;
}
ll const mod = 1e9 + 7;
int main() {
ll n;
cin >> n;
auto a = inputv(n);
auto b = a;
ll l = 0;
ll res = 0;
for (long long i = 0; i < n; i++) {
if (l == 0) {
if (a[0] == 0) {
for (long long j = 0; j < n; j++) {
if (a[j] != 0) {
a[0] = a[j] / abs(a[j]);
if (j % 1) a[0] *= (-1);
res++;
break;
}
}
}
if (!a[0]) {
a[0] = 1;
res++;
}
l += a[0];
} else if (l < 0) {
if (a[i] < -l + 1) {
res += -l + 1 - a[i];
a[i] = -l + 1;
l = 1;
} else {
l += a[i];
}
} else if (l > 0) {
if (a[i] > -l - 1) {
res += abs(a[i] - (-l - 1));
a[i] = -l - 1;
l = -1;
} else {
l += a[i];
}
}
}
a = b;
ll L = 0;
ll res2 = 0;
for (long long i = 0; i < n; i++) {
if (L == 0) {
if (a[0] == 0) {
for (long long j = 0; j < n; j++) {
if (a[j] != 0) {
a[0] = a[j] / abs(a[j]);
if (j % 1) a[0] *= (-1);
break;
}
}
}
if (!a[0]) {
a[0] = 1;
}
L += a[0];
L *= (-1);
L = L / abs(L);
res += (b[0] - L);
} else if (L < 0) {
if (a[i] < -L + 1) {
res2 += -L + 1 - a[i];
a[i] = -L + 1;
L = 1;
} else {
L += a[i];
}
} else if (L > 0) {
if (a[i] > -L - 1) {
res2 += abs(a[i] - (-L - 1));
a[i] = -L - 1;
L = -1;
} else {
L += a[i];
}
}
}
cout << min(res, res2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import numpy as np
n=int(input())
a=list(map(int,input().split()))
r=[0]
for i in range(n):
r.append(r[i]+a[i])
r.pop(0)
sum1,sum2=0,0
sousa1,sousa2=0,0
for i in range(n):
if np.sign(r[i]+sousa1) != 1-2*(i%2):
sum1+=abs(1-2*(i%2)-r[i]-sousa1)
sousa1+=1-2*(i%2)-r[i]
for i in range(n):
if np.sign(r[i]+sousa2) != 1-2*((i+1)%2):
sum2+=abs(1-2*((i+1)%2)-r[i]-sousa2)
sousa2+=1-2*((i+1)%2)-r[i]
print(min(sum1,sum2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++) {
a[i]=sc.nextInt();
}
//偶数を正とする
int counter=0;
int sum=0;
for(int i=0;i<n;i++) {
sum+=a[i];
if(i%2==0 && sum<=0) {
//-aだったら+1の操作をa回で0になり
//正にするならそこからさらに+1
counter+=Math.abs(sum)+1;
sum=1;
}else if(i%2==1 && sum>=0){
//+aだったらa回の-1と1回の-1でマイナス
counter+=sum+1;
sum=-1;
}
}
int counter1=0;
int sum1=0;
//奇数を正
for(int i=0;i<n;i++) {
sum1+=a[i];
if(i%2==0 && sum1>=0) {
//+aだったらa回の-1と1回の-1でマイナス
counter1+=sum1+1;
sum1=-1;
}else if(i%2==1 && sum1<=0){
//-aだったら+1の操作をa回で0になり
//正にするならそこからさらに+1
counter1+=Math.abs(sum1)+1;
sum1=1;
}
}
System.out.println(Math.min(counter,counter1));
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int mpow(int base, int exp);
void ipgraph(int m);
void dfs(int u, int par);
const int mod = 1000000007;
const int N = 3e5, M = N;
vector<int> g[N];
int a[N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int i, n, k, j;
cin >> n;
for (i = 0; i < n; i++) cin >> a[i];
long long ans = 0;
if (a[0] == 0) ans = 1, a[0] = 1;
if (a[1] > 0 and ans == 1) a[0] = -1;
long long pre = a[0], to;
for (i = 1; 1 < n ? i < n : i > n; 1 < n ? i += 1 : i -= 1) {
to = pre + a[i];
long long need = abs(pre) + 1;
if (pre > 0) need = -need;
if (to == 0 or (pre > 0 and to > 0) or (pre < 0 and to < 0)) {
pre += need;
ans += abs(need - a[i]);
} else
pre = to;
}
cout << ans << endl;
return 0;
}
int mpow(int base, int exp) {
base %= mod;
int result = 1;
while (exp > 0) {
if (exp & 1) result = ((long long)result * base) % mod;
base = ((long long)base * base) % mod;
exp >>= 1;
}
return result;
}
void ipgraph(int m) {
int i, u, v;
for (i = 0; i < m; i++) {
cin >> u >> v;
u++, v++;
g[u - 1].push_back(v - 1);
g[v - 1].push_back(u - 1);
}
}
void dfs(int u, int par) {
for (int v : g[u]) {
if (v == par) continue;
dfs(v, u);
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
import numpy as np
ans = 0
sum0 = a[0]
sum1 = a[0]
for i in range(1, n):
sum1 += a[i]
if np.sign(sum0) != np.sign(sum1) and sum1 != 0: #合計の符号が逆となっており、0でない
sum0 = sum1
pass
elif sum1 == 0: #合計が0になった場合は、符号が逆になるよう1か-1を足す
sum1 -= 1 * np.sign(sum0)
ans += 1
sum0 = sum1
elif np.sign(sum0) == np.sign(sum1): #符号が同じ場合は、+1か-1になるまで足す
if np.sign(sum1) == 1:
ans += (sum1 + 1)
sum1 = -1
else:
ans += abs(sum1 - 1)
sum1 = 1
sum0 = sum1
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] a = new int[n];
boolean flag = true;
int sum = 0, ans = 0;
int i = 1;
for (int j = 0; j < a.length; j++) {
a[j] = in.nextInt();
}
if (a[0] != 0) {
sum += a[0];
flag = a[0] > 0;
} else {
++ans;
for (; i < a.length; i++) {
if (a[i] != 0) {
flag = a[i] > 0;
if (flag) {
sum = a[i] - 1;
} else {
sum = a[i] + 1;
}
break;
}
ans += 2;
}
}
for (; i < a.length; i++) {
if (flag && sum + a[i] >= 0) {
ans += sum + a[i] + 1;
sum = -1;
flag = false;
} else if (!flag && sum + a[i] <= 0) {
ans += 1 - (sum + a[i]);
sum = 1;
flag = true;
} else {
sum += a[i];
flag = sum > 0;
}
}
System.out.println(ans);
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
vector<int64_t> s(n);
for (int i = 0; i < n; i++) cin >> a[i];
s[0] = (int64_t)max(a[0], 1);
int retval_p = (a[0] <= 0) ? abs(a[0]) + 1 : 0;
for (int i = 1; i < n; i++) {
s[i] = (int64_t)a[i] + s[i - 1];
if (i % 2 == 1 && s[i] >= 0) {
retval_p += abs(s[i]) + 1;
s[i] = -1;
} else if (i % 2 == 0 && s[i] <= 0) {
retval_p += abs(s[i]) + 1;
s[i] = 1;
}
}
s[0] = (int64_t)min(a[0], -1);
int retval_m = (a[0] >= 0) ? abs(a[0]) + 1 : 0;
for (int i = 1; i < n; i++) {
s[i] = (int64_t)a[i] + s[i - 1];
if (i % 2 == 1 && s[i] <= 0) {
retval_m += abs(s[i]) + 1;
s[i] = 1;
} else if (i % 2 == 0 && s[i] >= 0) {
retval_m += abs(s[i]) + 1;
s[i] = -1;
}
}
cout << min(retval_p, retval_m) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | # coding: utf-8
import math
if __name__ == "__main__":
n = int(input())
nums = list(map(int, input().split()))
acm = nums[0]
counts = 0
for num in nums[1:]:
if acm * (acm+num)>0:
counts += abs(acm+num)+1
acm = -acm//abs(acm)
print('counts:', counts)
print('acm:', acm)
elif acm * (acm+num)<0:
acm = acm+num
print('counts:', counts)
print('acm:', acm)
else:
if acm == 0:
counts += 1
acm = 1
print('counts:', counts)
print('acm:', acm)
else:
counts += 1
acm = -acm//abs(acm)
print('counts:', counts)
print('acm:', acm)
print(counts) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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 collections import defaultdict
def solve(pre_is_positive, total, ans, a_list):
for a in a_list:
total += a
if pre_is_positive:
if total == 0:
ans += 1
total = -1
elif total > 0:
ans += abs(total) + 1
total = -1
elif not pre_is_positive:
if total == 0:
ans += 1
total = 1
elif total < 0:
ans += abs(total) + 1
total = 1
pre_is_positive = total > 0
return ans
def main():
n = int(input())
a_list = list(map(int, input().split()))
total = a_list[0]
if total > 0:
ans = solve(True, total, 0, a_list[1:])
ans = min(ans, solve(False, -1, abs(total) + 1, a_list[1:]))
else:
ans = solve(False, total, 0, a_list[1:])
ans = min(ans, solve(True, 1, abs(total) + 1, a_list[1:]))
print(ans)
if __name__ == '__main__':
main()
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
int main() {
int n;
std::cin >> n;
int counter = 0;
long seq = 0;
scanf("%d ", &seq);
long first_sum = seq;
bool prevsign = first_sum >= 0 ? true : false;
for (int i = 1; i < n; i++) {
std::cout << first_sum << std::endl;
scanf("%d", &seq);
if (i < n - 1) scanf(" ");
if (!(prevsign ^ (first_sum + seq > 0 ? true : false)) ||
!(first_sum + seq)) {
long nseq = (!prevsign ? 1 : -1) - first_sum;
counter += (int)abs(nseq - seq);
first_sum += nseq;
} else
first_sum += seq;
prevsign = !prevsign;
}
std::cout << counter;
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 MX = 1e5 + 5, INF = 5 << 28, MOD = 1e9 + 7;
long long N;
vector<long long> A;
void input() {
cin >> N;
A.resize(N);
for (long long i = (long long)(0); i <= (long long)(N - 1); ++i) {
cin >> A[i];
}
}
void solve() {
long long ans = INF;
long long fugo = 1;
for (long long fg = (long long)(0); fg <= (long long)(1); ++fg) {
if (fg == 0) {
fugo = 1;
} else
fugo = 0;
long long prev = 0;
long long s = 0;
long long ans1 = 0;
for (long long i = (long long)(0); i <= (long long)(N - 1); ++i) {
s += A[i];
if (fugo) {
if (s > 0) {
ans1 += 0;
} else if (s == 0) {
ans1 += 1;
s += 1;
} else {
ans1 += abs(s) + 1;
s += abs(s) + 1;
}
} else {
if (s > 0) {
ans1 += (abs(s) + 1);
s -= abs(s) + 1;
} else if (s == 0) {
ans1 += 1;
s -= 1;
} else {
ans1 += 0;
}
}
prev = s;
fugo ^= 1;
}
ans = min(ans1, ans);
}
cout << ans << endl;
}
signed main() {
input();
solve();
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = [int(i) for i in input().split()]
# a[0]の符号を正にするか負にするかの2択で
# 後は一意に決まる
_sum = 0
count = 0
out = 0
# a[0]の符号を正にする場合
if a[0] <= 0:
count += a[0] - (-1)
_sum = 1
else:
_sum = a[0]
# print("count:{} sum:{}".format(count, _sum))
for i in range(1, len(a)):
if i % 2 == 1:
# a[i]までの和を負にする
# a[i] >= 0なら「-1」にする
if _sum + a[i] >= 0:
count += _sum + a[i] - (-1)
_sum = -1
else:
_sum += a[i]
else:
# a[i]までの和を正にする
# a[i] <= 0なら「1」にする
if _sum + a[i] <= 0:
count += 1 - (_sum + a[i])
_sum = 1
else:
_sum += a[i]
# print("count:{} sum:{}".format(count, _sum))
# print()
out = count
_sum = 0
count = 0
# a[0]の符号を負にする場合
if a[0] >= 0:
count += 1 - a[0]
_sum = -1
else:
_sum = a[0]
# print("count:{} sum:{}".format(count, _sum))
for i in range(1, len(a)):
if i % 2 == 0:
# a[i]までの和を負にする
# a[i] >= 0なら「-1」にする
if _sum + a[i] >= 0:
count += _sum + a[i] - (-1)
_sum = -1
else:
_sum += a[i]
else:
# a[i]までの和を正にする
# a[i] <= 0なら「1」にする
if _sum + a[i] <= 0:
count += 1 - (_sum + a[i])
_sum = 1
else:
_sum += a[i]
# print("count:{} sum:{}".format(count, _sum))
print(min(out, count))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | n = gets.to_i
as = gets.split.map(&:to_i)
base_as = as.dup
if as[0] == 0
if as.count == 0
puts 1
exit
end
if as[1] < 0
as[0] = 1
else
as[0] = -1
end
ans = 1
end
sum = as[0]
(1..n-1).each do |i|
a = as[i]
if sum * (sum+a) > 0
as[i] = -sum
if sum > 0
as[i] -= 1
else
as[i] += 1
end
elsif sum + a == 0
if sum > 0
as[i] -= 1
else
as[i] += 1
end
end
sum += as[i]
end
ans = 0
(0..n-1).each do |i|
ans += (base_as[i] - as[i]).abs
end
puts ans |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
typedef vector<vector<int> > vii;
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
long long n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < (int)n; i++) cin >> a[i];
long long sum = a[0], op_cnt = 0, op_cnt1 = 0;
for (int i = (int)1; i < (int)n; i++) {
if (sum < 0 && sum + a[i] <= 0) {
op_cnt += (0 - sum - a[i]) + 1;
sum = 1;
} else if (sum >= 0 && sum + a[i] >= 0) {
op_cnt += sum + a[i] + 1;
sum = -1;
} else
sum += a[i];
}
sum = (a[0] > 0 ? -1 : 1);
op_cnt1 += abs(a[0] - sum);
for (int i = (int)1; i < (int)n; i++) {
if (sum < 0 && sum + a[i] <= 0) {
op_cnt1 += (0 - sum - a[i]) + 1;
sum = 1;
} else if (sum >= 0 && sum + a[i] >= 0) {
op_cnt1 += sum + a[i] + 1;
sum = -1;
} else
sum += a[i];
}
cout << min(op_cnt, op_cnt1) << 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>
long long const MOD = 1e9 + 7;
long long const INF = 1e18;
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> v(n);
for (int(i) = 0; (i) < (n); (i)++) {
long long a;
cin >> a;
v[i] = v[max(0, i - 1)] + a;
}
long long ans = INF;
for (int(I) = 0; (I) < (2); (I)++) {
long long cnt = 0, change = 0;
vector<long long> u(v);
for (int(i) = 0; (i) < (n); (i)++) {
u[i] += change;
if (i % 2 == I) {
if (u[i] <= 0) {
cnt += -u[i] + 1;
change = -u[i] + 1;
}
} else {
if (u[i] >= 0) {
cnt += u[i] + 1;
change = -u[i] - 1;
}
}
}
ans = min(ans, cnt);
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> a(N);
for (int i = 0; i < N; i++) cin >> a[i];
int initial_sign[2] = {-1, 1};
vector<int> res(2);
for (int i = 0; i < 2; i++) {
int cumsum = 0;
int current_sign = initial_sign[i];
for (int j = 0; j < N; j++) {
cumsum += a[j];
if (cumsum * current_sign <= 0) {
res[i] += abs(cumsum - current_sign);
cumsum = current_sign;
}
current_sign = -1 * current_sign;
}
}
cout << min(res[0], res[1]);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, sum = 0, in, ans = 0;
cin >> n >> sum;
for (int i = 1; i < n; i++) {
cin >> in;
if (sum * in < 0 && abs(sum) < abs(in)) {
sum += in;
continue;
} else if (sum * in < 0) {
ans += abs(sum) - abs(in) + 1;
if (sum > 0)
sum = -1;
else
sum = 1;
continue;
}
ans += abs(sum) + abs(in) + 1;
if (sum < 0) {
sum = 1;
} else {
sum = -1;
}
}
if (sum == 0) ans++;
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long ans = 0;
long long sum = 0;
long long tmp = 0;
for (int j = 0; j < 2; j++) {
for (int i = 0; i < n; i++) {
if (j == 0 && i == 0) {
sum = a[i];
continue;
} else if (j == 1 && i == 0) {
tmp = ans;
sum = a[i] > 0 ? -1 : 1;
tmp += a[i] > 0 ? a[i] + 1 : -(a[i] - 1);
continue;
}
if (sum > 0) {
if (sum + a[i] >= 0) {
ans += sum + a[i] + 1;
sum += a[i] - (sum + a[i] + 1);
continue;
}
} else {
if (sum + a[i] <= 0) {
ans -= sum + a[i] - 1;
sum += a[i] - (sum + a[i] - 1);
continue;
}
}
sum += a[i];
}
}
cout << (ans < tmp ? ans : tmp) - 2 << 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;
__attribute__((constructor)) void initial() {
cin.tie(0);
ios::sync_with_stdio(false);
}
int main() {
long long N;
cin >> N;
vector<long long> a;
for (int i = 0; i < (N); i++) {
long long ai;
cin >> ai;
a.push_back(ai);
}
long long changeCount = 0;
long long sum = 0;
bool nextSumPositive = a[0] > 0;
for (int i = 0; i < (N); i++) {
sum += a[i];
if (nextSumPositive) {
if (sum <= 0) {
long long change = -sum + 1;
changeCount += abs(change);
sum += change;
}
} else {
if (sum >= 0) {
long long change = -sum - 1;
changeCount += abs(change);
sum += change;
}
}
nextSumPositive = !nextSumPositive;
}
cout << changeCount << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int DX[] = {1, 1, 0, -1, -1, -1, 0, 1};
int DY[] = {0, -1, -1, -1, 0, 1, 1, 1};
int n;
ll hoge(ll a[]) {
ll ans = 0;
ll temp = 0;
for (int(i) = 0; (i) < (n); (i)++) {
if (temp > 0 && temp + a[i] > 0) {
ans += abs(-1 - temp - a[i]);
temp = -1;
} else if (temp < 0 && temp + a[i] < 0) {
ans += abs(1 - temp - a[i]);
temp = 1;
} else if (temp + a[i] == 0) {
if (temp > 0) {
temp = -1;
} else {
temp = 1;
}
ans += 1;
} else {
temp += a[i];
}
}
return ans;
}
void solve() {
cin >> n;
ll a[n];
for (int(i) = 0; (i) < (n); (i)++) cin >> a[i];
ll temp = 0;
if (a[0] == 0) {
temp = 1;
a[0] = 1;
}
ll ans1 = hoge(a) + temp;
temp = 0;
if (a[0] > 0) {
temp += (a[0] * (-1) - 1);
a[0] = -1;
} else if (a[0] < 0) {
temp = (a[0] * (-1) + 1);
a[0] = 1;
} else {
temp = 1;
a[0] = -1;
}
ll ans2 = hoge(a) + temp;
cout << min(ans1, ans2) << endl;
}
int main() {
solve();
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
bool DifSign(vector<ll> a, int i) {
if ((a[i] > 0 && a[i + 1] < 0) || (a[i] < 0 && a[i + 1] > 0)) {
return true;
} else {
return false;
}
}
int main() {
int n;
cin >> n;
vector<ll> a(n);
for (int i = 0; i < (n); i++) cin >> a[i];
ll s = a[0];
for (int i = 1; i < n; i++) {
a[i] += s;
s = a[i];
}
vector<ll> b = a;
int cnt, ans1 = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0 && a[i] <= 0) {
cnt = 0;
while (a[i] <= 0) {
a[i]++;
cnt++;
ans1++;
}
for (int j = i + 1; j < n; j++) {
a[j] += cnt;
}
}
if (i % 2 == 1 && a[i] >= 0) {
cnt = 0;
while (a[i] >= 0) {
a[i]--;
cnt++;
ans1++;
}
for (int j = i + 1; j < n; j++) {
a[j] -= cnt;
}
}
}
int ans2 = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 1 && b[i] <= 0) {
cnt = 0;
while (b[i] <= 0) {
b[i]++;
cnt++;
ans2++;
}
for (int j = i + 1; j < n; j++) {
a[j] += cnt;
}
}
if (i % 2 == 0 && b[i] >= 0) {
cnt = 0;
while (b[i] >= 0) {
b[i]--;
cnt++;
ans2++;
}
for (int j = i + 1; j < n; j++) {
b[j] -= cnt;
}
}
}
cout << min(ans1, ans2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text.RegularExpressions;
using System.Diagnostics;
//var input = Console.ReadLine().Split().Select(int.Parse).ToArray();
namespace AtCoderSolve
{
class Solve
{
const int mod = 1000000007;
static void Main(string[] args)
{
//var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
//Console.SetOut(sw);
int N = int.Parse(Console.ReadLine());
var a = Console.ReadLine().Split().Select(long.Parse).ToArray();
long ans = 0;
long[] sum = new long[N];
for(var i = 0; i < N; i++)
{
if (i == 0) { sum[i] = a[i]; }
else
{
sum[i] = sum[i - 1] + a[i];
if (sum[i] * sum[i - 1] >= 0)
{
ans += 1 + Math.Abs(sum[i]);
if (sum[i-1]>0) { sum[i] = -1; }
else { sum[i] = 1; }
}
}
//Console.WriteLine($"{sum[i]} {ans}");
}
Console.WriteLine(ans);
//Console.Out.Flush();
}
}
public class Calculation
{
}
public class Graph
{
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -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 = (1 << 30);
const int mod = 1000000007;
using ll = long long;
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (auto &k : a) cin >> k;
ll sum = a[0];
int sign = (sum > 0) ? 1 : -1;
ll ans = 0;
for (int i = 1; i < n; ++i) {
sign *= -1;
ll tempsum = sum + a[i];
int sumsign = (tempsum > 0) ? 1 : -1;
if (sumsign != sign) {
ans += abs(abs(sum - sign) * sign - a[i]);
sum = sign;
} else {
sum += a[i];
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(int(i) for i in input().split())
b = []
for i in range(0,len(a)):
b.append(a[i])
def solve(cnt,A,N):
for i in xrange(1, N):
if sum(A[0:i])>0:
if sum(A[0:i+1])>=0:
r = A[i]
A[i]=-sum(A[0:i])-1
cnt+=abs(r-A[i])
else:
if sum(A[0:i+1])<=0:
r = A[i]
A[i]=-sum(A[0:i])+1
cnt+=abs(r-A[i])
return cnt
cnt1=0
if b[0]<=0:
ini=b[0]
b[0]=1
cnt1=abs(1-ini)
ans1=solve(cnt1,b,n)
cnt2=0
if a[0]>=0:
ini=a[0]
a[0]=-1
cnt2=abs(-1-ini)
ans2=solve(cnt2,a,n)
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<int64_t> vec(N);
for (int i = 0; i < N; i++) {
cin >> vec.at(i);
}
int64_t ans = 0;
int64_t x = vec.at(0);
for (int i = 1; i < N; i++) {
while ((vec.at(i) + x) * x >= 0) {
if (x > 0) {
ans += abs(vec.at(i) + 1 + x);
vec.at(i) -= (vec.at(i) + 1 + x);
}
if (x < 0) {
ans += abs(1 - x - vec.at(i));
vec.at(i) += (1 - x - vec.at(i));
}
}
x = vec.at(i) + x;
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < (n); ++i) cin >> a[i];
int sum = a[0];
int res = 0;
for (int i = 1; i < n; i++) {
if (sum > 0) {
sum += a[i];
if (sum >= 0) {
while (sum >= 0) {
sum--;
res++;
}
}
} else {
sum += a[i];
if (sum <= 0) {
while (sum <= 0) {
sum++;
res++;
}
}
}
}
cout << res << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
typedef long long ll;
int main(){
int n;
cin >> n;
vector<int> a(n);
rep(i,n) cin >> a[i];
ll prevArraySum = a[0];
ll currentArraySum = a[0];
ll res = 0;
if(a[0] == 0){
res = 1;
prevArraySum = 1;
currentArraySum = 1;
FOR(i,1,n){
if(prevArraySum > 0){
currentArraySum = prevArraySum + a[i];
if(currentArraySum >= 0){
res += abs(-1 - currentArraySum);
prevArraySum = -1;
}else{
prevArraySum = currentArraySum;
}
}else{
currentArraySum = prevArraySum + a[i];
if(currentArraySum <= 0){
res += abs(1 - currentArraySum);
prevArraySum = 1;
}else{
prevArraySum = currentArraySum;
}
}
}
int res1 = res;
res = 1;
FOR(i,1,n){
if(prevArraySum > 0){
currentArraySum = prevArraySum + a[i];
if(currentArraySum >= 0){
res += abs(-1 - currentArraySum);
prevArraySum = -1;
}
}else{
currentArraySum = prevArraySum + a[i];
if(currentArraySum <= 0){
res += abs(1 - currentArraySum);
prevArraySum = 1;
}else{
prevArraySum = currentArraySum;
}
}
}
res = min(res,res1);
}else{
FOR(i,1,n){
if(prevArraySum > 0){
currentArraySum = prevArraySum + a[i];
if(currentArraySum >= 0){
res += abs(-1 - currentArraySum);
prevArraySum = -1;
}else{
prevArraySum = currentArraySum;
}
}else{
currentArraySum = prevArraySum + a[i];
if(currentArraySum <= 0){
res += abs(1 - currentArraySum);
prevArraySum = 1;
}else{
prevArraySum = currentArraySum;
}
}
}
}
cout << res << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | // AtCoder-Template.cpp : このファイルには 'main' 関数が含まれています。プログラム実行の開始と終了がそこで行われます。
//
#include <iostream>
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
using namespace std;
using ll = long long;
#define fst first
#define snd second
#define FOR(i,N) for(auto i=0; i<N; ++i)
#define FORREV(i,N,_cnt) for(auto i=N-1,cnt=_cnt; cnt > 0; --i, --cnt)
#define ALL(x) x.begin(), x.end()
/* clang-format off */
template <class T, size_t D> struct _vec { using type = vector<typename _vec<T, D - 1>::type>; };
template <class T> struct _vec<T, 0> { using type = T; };
template <class T, size_t D> using vec = typename _vec<T, D>::type;
template <class T> vector<T> make_v(size_t size, const T& init) { return vector<T>(size, init); }
template <class... Ts> auto make_v(size_t size, Ts... rest) { return vector<decltype(make_v(rest...))>(size, make_v(rest...)); }
template <class T> inline void chmin(T& a, const T& b) { if (b < a) a = b; }
template <class T> inline void chmax(T& a, const T& b) { if (b > a) a = b; }
/* clang-format on */
int main() {
#ifdef DEBUG
ifstream ifs("in.txt");
cin.rdbuf(ifs.rdbuf());
#endif
ll N; cin >> N; vector<ll> A(N); FOR(i, N) cin >> A[i];
// RUISEKI
vector<ll> Rui(N); Rui[0] = A[0];
ll ans = 0;
if (Rui[0] < 0) {
ans += -Rui[0] + 1;
Rui[0] = 1;
}
FOR(i, N - 1) {
Rui[i + 1] += Rui[i] + A[i + 1];
if (i + 1 >= 1 and Rui[i+1] * Rui[i] >= 0) {
if (Rui[i + 1] * Rui[i] == 0 and Rui[i] > 0) {
ans += 1; Rui[i + 1] = -1;
}
else if (Rui[i + 1] * Rui[i] == 0 and Rui[i] < 0) {
ans += 1; Rui[i + 1] = 1;
}
else { // 両方とも同じ符号になった場合
if (Rui[i + 1] > 0) {
ans += Rui[i + 1] + 1;
Rui[i + 1] = -1;
}
else {
ans += - Rui[i + 1] + 1;
Rui[i + 1] = 1;
}
}
}
}
///////////////////////////////////////////////
///////// ///////////////
///////////////////////////////////////////////
Rui.clear(); Rui.resize(N); Rui[0] = A[0];
ll tmp = 0;
if (Rui[0] > 0) {
tmp += Rui[0] + 1;
Rui[0] = -1;
}
FOR(i, N - 1) {
Rui[i + 1] += Rui[i] + A[i + 1];
if (i + 1 >= 1 and Rui[i + 1] * Rui[i] >= 0) {
if (Rui[i + 1] * Rui[i] == 0 and Rui[i] > 0) {
tmp += 1; Rui[i + 1] = -1;
}
else if (Rui[i + 1] * Rui[i] == 0 and Rui[i] < 0) {
tmp += 1; Rui[i + 1] = 1;
}
else { // 両方とも同じ符号になった場合
if (Rui[i + 1] > 0) {
tmp += Rui[i + 1] + 1;
Rui[i + 1] = -1;
}
else {
tmp += -Rui[i + 1] + 1;
Rui[i + 1] = 1;
}
}
}
}
chmin(ans, tmp);
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int,input().split()))
x = a[0]
ans = float('inf')
for _ in range(2):
tmp = 0
if _:
if a[0] > 0:
x = a[0]
else:
x = 1 - a[0]
tmp = 1 - a[0]
else:
if a[0] < 0:
x = a[0]
else:
x = -1 - a[0]
tmp = 1 + a[0]
for i in range(1, n):
y = x + a[i]
if x*y >= 0:
if x > 0:
tmp += y+1
y -= y+1
elif x < 0:
tmp -= y-1
y -= y-1
x = y
ans = min(ans, tmp)
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>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse4")
using namespace std;
inline long long in() {
long long x;
scanf("%lld", &x);
return x;
}
int32_t main() {
long long n = in();
vector<long long> a(n), pre;
for (auto &i : a) i = in();
partial_sum(a.begin(), a.end(), back_inserter(pre));
long long ans = 1e9, cur, mv;
for (long long sign = 0; sign < 2; sign++) {
cur = 0, mv = 0;
for (long long i = 0; i < n; i++) {
long long x = pre[i] + cur;
if (i % 2 == sign and x >= 0) {
cur -= (x + 1);
mv += (x + 1);
} else if (i % 2 != sign and x <= 0) {
cur += (1 - x);
mv += (1 - x);
}
}
ans = min(ans, mv);
}
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 <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <utility>
#include <numeric>
#include <cmath>
using namespace std;
//変数デバッグ
#define DEB(variable) cout << #variable << '=' << variable << endl
//for簡易表記(引数ミス防止)
#define FOR(LoopVariable,numberOFbegin,numberOFend) for (int LoopVariable = (numberOFbegin); (LoopVariable) < (numberOFend); (LoopVariable)++)
#define REP(LoopVariable,numberOFend) for(int LoopVariable = 0;(LoopVariable)<(numberOFend);LoopVariable++)
int n;
vector<long long int> v;
int solve(int first_pon){
long long int pon = first_pon, sum = 0, result = 0;
REP(i,n){
sum += v[i];
if(pon>0&&sum>=0){
result += sum + 1;
sum = -1;
}else if(pon<0&&sum<=0){
result += (-sum) + 1;
sum = 1;
}
if(sum>0){
pon = 1;
}else{
pon = -1;
}
}
return result;
}
int main(){
cin >> n ;
v = vector<long long int>(n);
REP(i,n){
cin >> v[i];
}
cout << long long min(solve(1), solve(-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()))
sun = 0
ans1 = 0
for i in range(n):
if i == 0:
SUN = 1
else:
SUN = sun
now = a[i]
if SUN * (sun+now) >= 0:
ans1 += abs(sun+now) + 1
if sun > 0:
sun = -1
else:
sun = 1
else:
sun += now
sun = 0
ans2 = 0
for i in range(n):
now = a[i]
if i == 0:
SUN = -1
else:
SUN = sun
if SUN * (sun+now) >= 0:
ans2 += abs(sun+now) + 1
if sun > 0:
sun = -1
else:
sun = 1
else:
sun += now
#print(ans1,ans2)
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> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
long long sum_ = a[0];
long long count = 0;
if (a[0] > 0) {
for (int i = 1; i < n; ++i) {
if (i % 2 == 1) {
if (sum_ + a[i] >= 0) {
int r = -(sum_ + a[i]) - 1;
count += abs(r);
a[i] += r;
}
} else {
if (sum_ + a[i] <= 0) {
int r = -(sum_ + a[i]) + 1;
count += abs(r);
a[i] += r;
}
}
sum_ += a[i];
}
} else {
for (int i = 1; i < n; ++i) {
if (i % 2 == 1) {
if (sum_ + a[i] <= 0) {
int r = -(sum_ + a[i]) + 1;
count += abs(r);
a[i] += r;
}
} else {
if (sum_ + a[i] >= 0) {
int r = -(sum_ + a[i]) - 1;
count += abs(r);
a[i] += r;
}
}
sum_ += a[i];
}
}
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 main(void) {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int sum = 0;
int count1 = 0;
bool flag = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (flag && sum >= 0) {
count1 += sum + 1;
sum = -1;
} else if (!flag && sum <= 0) {
count1 += abs(sum) + 1;
sum = 1;
}
flag ^= 1;
}
flag = 0;
sum = 0;
int count2 = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (!flag && sum >= 0) {
count2 += sum + 1;
sum = -1;
} else if (flag && sum <= 0) {
count2 += abs(sum) + 1;
sum = 1;
}
flag ^= 1;
}
cout << min(count1, count2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long INF = 10000000000;
const long long MOD = 1000000007;
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
int main() {
int n;
cin >> n;
long long a[100100];
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
long long ans = INF;
for (int i = 0; i < 2; ++i) {
long long count = 0;
long long su = 0;
for (int j = 0; j < n; ++j) {
su += a[j];
if (i == 0) {
if (j % 2 == 0 && su <= 0) {
count += -su + 1;
su = 1;
} else if (j % 2 == 1 && su >= 0) {
count += su + 1;
su = -1;
}
} else {
if (j % 2 == 0 && su >= 0) {
count += su + 1;
su = -1;
} else if (j % 2 == 1 && su <= 0) {
count += -su + 1;
su = 1;
}
}
}
ans = min(ans, count);
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
template <class T>
using VEC = std::vector<T>;
template <class T>
using MAT = std::vector<std::vector<T>>;
void DUMP() { cerr << endl; }
template <class Head, class... Tail>
void DUMP(Head &&head, Tail &&...tail) {
cerr << head << ", ";
DUMP(std::move(tail)...);
}
template <typename T>
ostream &operator<<(ostream &os, vector<T> &vec) {
os << "{";
for (auto v : vec) os << v << ",";
os << "}";
return os;
}
int sign(LL n) { return n == 0 ? 0 : n / abs(n); }
int main() {
int n;
cin >> n;
VEC<LL> a(n);
for (int i = (0); i < (int)(n); ++i) {
cin >> a[i];
}
auto make = [&](LL tgt) -> int {
VEC<LL> cur(n, 0);
cur[0] = a[0];
int cnt = 0;
if (sign(cur[0]) == sign(tgt)) {
cnt = 0;
} else {
cnt += abs(a[0] - tgt);
}
for (int i = (1); i < (int)(n); ++i) {
tgt = -tgt;
cur[i] = cur[i - 1] + a[i];
if (sign(cur[i]) != sign(tgt)) {
cnt += abs(cur[i] - tgt);
cur[i] = tgt;
}
}
return cnt;
};
cout << min(make(1), make(-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 <iostream>
using namespace std;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
long long main(void){
long long n;
cin >> n;
long long a[n];
rep(i,n){
cin >> a[i];
}
long long ans = 1000000000;
for(long long p=0;p<=1;p++){
long long tmpans = 0;
long long sum = 0;
rep(i,n){
if(i % 2 == p){
if(a[i] + sum <= 0){
tmpans += 1 - (a[i] + sum);
sum = 1;
}
else{
sum = a[i] + sum;
}
}
else{
if(a[i] + sum >= 0){
tmpans += 1 + a[i] + sum;
sum = -1;
}
else{
sum = a[i] + sum;
}
}
}
ans = min(ans,tmpans);
}
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 | import sys
from collections import deque
import copy
import math
def get_read_func(fileobject):
if fileobject == None :
return raw_input
else:
return fileobject.readline
def calc(A, N):
s = 0L
count = 0
pre_sign = 0
for i in range(N):
s += A[i]
if s == 0:
if pre_sign == 1:
s -= 1L
elif pre_sign == -1:
s += 1L
count += 1
if pre_sign == s/abs(s):
if s < 0L:
ope = 1L - s
else:
ope = -1L - s
s += ope
count += abs(ope)
pre_sign = s/abs(s)
return count
def main():
if len(sys.argv) > 1:
f = open(sys.argv[1])
else:
f = None
read_func = get_read_func(f);
input_raw = read_func().strip().split()
[N] = [int(input_raw[0])]
input_raw = read_func().strip().split()
A = [int(input_raw[i]) for i in range(N)]
s = 0
count = 0
pre_sign = 0
if A[0] != 0:
count = calc(A, N)
else:
A[0] = -1
count_minus =calc(A, N) + 1
A[0] = 1
count_plus =calc(A, N) + 1
count = min(count_minus, count_plus)
print count
if __name__ == '__main__':
main()
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect
from heapq import heappush, heappop
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
n = INT()
A = LIST()
def solve(lis):
lis = lis.copy()
tmp_sum = lis[0]
lis = lis[1:]
cnt = 0
for x in lis:
if (tmp_sum + x) * tmp_sum < 0: # 異符号
tmp_sum += x
else:
if tmp_sum > 0:
cnt += abs(-tmp_sum-1-x)
tmp_sum = -1
else:
cnt += abs(-tmp_sum+1-x)
tmp_sum = 1
return cnt
ans = 0
if A[0] > 0:
ans = solve(A)
A[0] = -1
ans = min(ans, solve(A)+abs(A[0]+1))
elif A[0] < 0:
ans = solve(A)
A[0] = 1
ans = min(ans, solve(A)+abs(A[0]-1))
else:
A[0] = 1
ans = solve(A)+1
A[0] = -1
ans = min(ans, solve(A)+1)
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = [int(i) for i in input().split()]
num,answer = a[0],0
for i in range(1,n):
if num*(num+a[i]) >= 0:
x = (-num)//abs(num)
answer += abs((x-num)-a[i])
num = x
else:
num += a[i]
print(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 | cpp | #include <bits/stdc++.h>
using namespace std;
unsigned long long inf = (1LL << 62);
long long mod = 1000000007;
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long max(long long a, long long b) {
if (a < b) {
return b;
} else
return a;
}
long long min(long long a, long long b) {
if (a < b) return a;
return b;
}
pair<long long, long long> dx[4] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
vector<pair<long long, long long> > list;
void floyd(int N, long long** d) {
for (int k = 0; k < (int)(N + 1); k++) {
for (int i = 0; i < (int)(N + 1); i++) {
if (d[i + 1][k + 1] == inf) continue;
for (int j = 0; j < (int)(N + 1); j++) {
if (d[j + 1][k + 1] == inf) continue;
d[i + 1][j + 1] =
min(d[i + 1][j + 1], d[i + 1][k + 1] + d[j + 1][k + 1]);
}
}
}
}
unsigned long long modpow(unsigned long long a, unsigned long long b) {
long long retval = 0;
if (b == 0) {
return 1;
} else if (b % 2 == 0) {
retval = modpow(a, b / 2) % mod;
return (retval * retval) % mod;
} else {
return ((a % mod) * modpow(a, b - 1)) % mod;
}
}
long long pow(long long a, long long b) {
long long ret;
if (b == 0)
return 1;
else if (b % 2 == 0) {
ret = pow(a, b / 2);
return ret * ret;
} else if (b % 2 == 1) {
return a * pow(a, b - 1);
}
}
int bit(long long a, long long n) {
if (a & pow(2, n - 1) != 0) {
return 1;
} else {
return 0;
}
}
unsigned long long modkaijou(unsigned long long a) {
if (a == 1 || a == 0) {
return 1;
}
long long retval = 1;
for (unsigned long long i = a; i >= 1; i--) {
retval = (retval * (i % mod)) % mod;
}
return retval % mod;
}
unsigned long long modcomb(unsigned long long a, unsigned long long b) {
if (a == 0 || b == 0) return 1;
return (((modkaijou(a) * modpow(modkaijou(a - b), mod - 2)) % mod) *
modpow(modkaijou(b), mod - 2)) %
mod;
}
class DisjointSet {
public:
vector<int> p, rank, num;
DisjointSet() {}
DisjointSet(int size) {
rank.resize(size, 0);
p.resize(size, 0);
num.resize(size, 0);
for (int i = 0; i < (int)(size); i++) {
p[i] = i;
rank[i] = 0;
num[i] = 1;
}
}
bool same(int x, int y) { return findSet(x) == findSet(y); }
void unite(int x, int y) {
if (!same(x, y)) link(findSet(x), findSet(y));
}
void link(int x, int y) {
if (rank[x] > rank[y]) {
p[y] = x;
num[x] += num[y];
} else {
p[x] = y;
num[y] += num[x];
if (rank[x] == rank[y]) {
rank[y]++;
}
}
}
long long NumberOfElements(int x) { return num[findSet(x)]; }
int findSet(int x) {
if (x != p[x]) {
p[x] = findSet(p[x]);
num[x] = 1;
}
return p[x];
}
};
bool compare_b(pair<long long, long long> a, pair<long long, long long> b) {
if (a.first < b.first) {
return true;
} else if (a.first == b.first) {
return a.second < b.second;
} else {
return false;
}
}
int ketasuu(int a) {
int ret = 1;
int val = a;
while (val / 10 != 0) {
ret += 1;
val /= 10;
}
return ret;
}
typedef struct trio {
long long a, b, y;
} ti;
string to_binary(long long a) {
long long val = a;
string retval = "";
retval = to_string(val % 2);
while (val / 2 != 0) {
val /= 2;
retval = (to_string(val % 2)) + retval;
}
return retval;
}
int ctoi(char a) { return a - '0'; }
string S, T;
int a[100005];
int sum[100005];
int main() {
int N, M;
long long g = 0;
long long p1, p2;
cin >> N;
for (int i = 0; i < (int)(N); i++) {
cin >> a[i + 1];
sum[i + 1] = sum[i] + a[i + 1];
}
int offset = 0;
int ans = 0;
for (int i = 1; i <= N - 1; i++) {
if ((sum[i + 1] + offset) * (sum[i] + offset) >= 0) {
if (sum[i + 1] + offset <= 0) {
ans += (1 - sum[i + 1] - offset);
offset += (1 - sum[i + 1] - offset);
} else {
ans += (sum[i + 1] + offset + 1);
offset += -(sum[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 | python3 | from itertools import accumulate
N=int(input())
AX=list(accumulate(list(map(int, input().split()))))
ans=0
for i in range(1,N):
if (AX[i]>=0 and AX[i-1]>=0):
tmp=-1-AX[i]
for j in range(i,N):
AX[j]+=tmp
ans+=abs(tmp)
if (AX[i]<=0 and AX[i-1]<=0):
tmp=1-AX[i]
for j in range(i,N):
AX[j]+=tmp
ans+=abs(tmp)
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 = list(map(int,input().split()))
count_list = []
previous = ''
flag_list = ['odd','even']
for f in flag_list:
count = 0
summary = 0
for i,a in enumerate(a_list):
if i == 0:
if f == 'odd':
previous = '+'
if a <= 0:
a_result = 1
count += 1-a
else:
a_result = a
else:
previous = '-'
if a >= 0:
a_result = 1
count += 1-a
else:
a_result = a
summary += a_result
else:
if previous == '+':
if summary + a < 0:
a_result = a
else:
a_result = (-1)*abs(-1-summary)
count += abs(a-a_result)
previous = '-'
else:
if summary + a > 0:
a_result = a
else:
a_result = abs(1-summary)
count += abs(a-a_result)
previous = '+'
summary += a_result
count_list.append(count)
print(min(count_list)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <bits/stdc++.h>
int main() {
int i, n, a[100001];
scanf("%d", &n);
for (i = 1; i <= n; i++) scanf("%d", &(a[i]));
int j, parity;
long long ans[2], sum[2];
if (a[1] > 0) {
sum[0] = a[1];
sum[1] = -1;
ans[0] = 0;
ans[1] = a[1] + 1;
} else {
sum[0] = 1;
sum[1] = a[1];
ans[0] = 1 - a[1];
ans[1] = 0;
}
for (i = 2; i <= n; i++) {
for (j = 0; j < 2; j++) {
parity = (i + j) % 2 * 2 - 1;
if ((sum[j] + a[i]) * parity <= 0) {
ans[j] += 1 - (sum[j] + a[i]) * parity;
sum[j] = parity;
} else
sum[j] += a[i];
}
}
printf("%lld\n", (ans[0] < ans[1]) ? ans[0] : ans[1]);
fflush(stdout);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import math
n = int(input())
ai = tuple(map(int, input().split()))
kisuu = 0.0
guusuu = 0.0
for i, a in enumerate(ai):
if i % 2:
kisuu += a
else:
guusuu += a
kisuu /= math.floor(n / 2)
guusuu /= n - math.floor(n / 2)
if kisuu > guusuu:
kisuu = True
guusuu = False
else:
kisuu = False
guusuu = True
sum = 0
ans = 0
for i, a in enumerate(ai):
sum += a
if i % 2:
if kisuu:
if sum <= 0:
ans += 1 - sum
sum = 1
else:
if sum >= 0:
ans += 1 + sum
sum = -1
else:
if guusuu:
if sum <= 0:
ans += 1 - sum
sum = 1
else:
if sum >= 0:
ans += 1 + sum
sum = -1
print(sum)
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 Graph = vector<vector<long long>>;
const long long mod = 1000000007;
long long digitsum(long long n, long long b) {
if (b < 2) return -1;
if (n < b) return n;
return digitsum(n / b, b) + n % b;
}
long long mpow(long long a, long long x);
long long m_inv(long long n);
vector<long long> split(long long n, long long a);
string xal_number(long long n, long long x);
long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; }
long long lcm(long long x, long long y) { return x * y / gcd(x, y); }
class Factorial {
private:
vector<long long> fac;
public:
Factorial(long long N) {
fac.push_back(1);
for (long long i = (0); i < (N); ++i) fac.push_back(fac[i] * (i + 1) % mod);
}
long long fact(long long a) { return fac[a]; }
long long ifac(long long a) { return m_inv(fac[a]); }
long long cmb(long long n, long long r);
};
struct UnionFind {
vector<long long> par;
UnionFind(long long n = 1) { init(n); }
void init(long long n = 1) {
par.resize(n);
for (long long i = (0); i < (n); ++i) par[i] = -1;
}
long long root(long long x) {
if (par[x] < 0)
return x;
else
return par[x] = root(par[x]);
}
long long size(long long x) { return -par[root(x)]; }
bool issame(long long x, long long y) { return root(x) == root(y); }
bool connect(long long x, long long y);
};
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 ans = 0;
vector<long long> S(n + 1);
S[0] = 0;
for (long long i = (0); i < (n); ++i) {
S[i + 1] = S[i] + a[i];
if (S[i + 1] * S[i] > 0) {
ans += abs(S[i + 1]) + 1;
S[i + 1] = (S[i] > 0) ? -1 : 1;
}
if (S[i + 1] == 0) {
ans += 1;
S[i + 1] += (S[i] > 0) ? -1 : 1;
}
}
long long ans2 = 0;
S[n] = 0;
for (long long i = n; i > 0; --i) {
S[i - 1] = S[i] + a[i];
if (S[i - 1] * S[i] > 0) {
ans2 += abs(S[i - 1]) + 1;
S[i - 1] = (S[i] > 0) ? -1 : 1;
}
if (S[i - 1] == 0) {
ans2 += 1;
S[i - 1] += (S[i] > 0) ? -1 : 1;
}
}
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 | UNKNOWN | import scala.io.StdIn
import scala.annotation.tailrec
object ABC059C extends App {
val n = StdIn.readInt
val a = StdIn.readLine.split(" ").map(_.toLong)
// 1項目が 0 の時は 1 か -1 に変える
val init1 = if(a.head <= 0) 1 - a.head else 0
val ans1 = a.tail./:(a.head + init1, init1.abs)((acc,i) => {
val (bsum, bcnt) = acc
val sum = bsum + i
val cnt = if(bsum < 0 && sum < 0) 1 - sum
else if(bsum > 0 && sum > 0) -1 - sum
else if(sum == 0) if(bsum < 0) 1 else -1
else 0
(sum+cnt, bcnt+cnt.abs)
})._2
val init2 = if(a.head < 0) 0 else -1 - a.head
val ans2 = a.tail./:(a.head + init2, init2.abs)((acc,i) => {
val (bsum, bcnt) = acc
val sum = bsum + i
val cnt = if(bsum < 0 && sum < 0) 1 - sum
else if(bsum > 0 && sum > 0) -1 - sum
else if(sum == 0) if(bsum < 0) 1 else -1
else 0
(sum+cnt, bcnt+cnt.abs)
})._2
println(math.min(ans1, ans2))
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n, num[100003];
long long sum[100003], ans, ans1, ans2;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> num[i];
}
sum[1] = num[1];
if (num[1] == 0) {
sum[1] = -1;
ans1 = 1;
for (int i = 2; i <= n; i++) {
if (sum[i - 1] > 0) {
if (num[i] + sum[i - 1] < 0) {
sum[i] = sum[i - 1] + num[i];
continue;
} else {
sum[i] = -1;
ans1 += abs(sum[i] - (num[i] + sum[i - 1]));
}
} else {
if (num[i] + sum[i - 1] > 0) {
sum[i] = num[i] + sum[i - 1];
continue;
} else {
sum[i] = 1;
ans1 += abs(sum[i] - (num[i] + sum[i - 1]));
}
}
}
sum[1] = -1;
ans2 = 1;
for (int i = 2; i <= n; i++) {
if (sum[i - 1] > 0) {
if (num[i] + sum[i - 1] < 0) {
sum[i] = sum[i - 1] + num[i];
continue;
} else {
sum[i] = -1;
ans2 += abs(sum[i] - (num[i] + sum[i - 1]));
}
} else {
if (num[i] + sum[i - 1] > 0) {
sum[i] = num[i] + sum[i - 1];
continue;
} else {
sum[i] = 1;
ans2 += abs(sum[i] - (num[i] + sum[i - 1]));
}
}
}
cout << min(ans1, ans2) << endl;
return 0;
}
for (int i = 2; i <= n; i++) {
if (sum[i - 1] > 0) {
if (num[i] + sum[i - 1] < 0) {
sum[i] = sum[i - 1] + num[i];
continue;
} else {
sum[i] = -1;
ans += abs(sum[i] - (num[i] + sum[i - 1]));
}
} else {
if (num[i] + sum[i - 1] > 0) {
sum[i] = num[i] + sum[i - 1];
continue;
} else {
sum[i] = 1;
ans += abs(sum[i] - (num[i] + sum[i - 1]));
}
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.Scanner;
import java.util.Arrays;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] a = new int [n];
for(int i = 0;i < n;i++){
a[i] = sc.nextInt();
}
int[] sum1 = new int[n];
int[] sum2 = new int[n];
sum1[0] = a[0];
sum2[0] = a[0];
int count = 0;
count = Math.min(solve1(sum1,a,count),solve2(sum2,a,count));
System.out.println(count);
}
public static int solve1(int[] sum,int[] a,int count){
if(sum[0] <= 0){
count += 1 - sum[0];
sum[0] = 1;
}
for(int i = 0;i < sum.length-1;i++){
sum[i+1] = sum[i] + a[i+1];
if((i+1) % 2 == 1){
if(sum[i+1] >= 0){
count += 1 + sum[i+1];
sum[i+1] = -1;
}
}
if((i+1) % 2 == 0){
if(sum[i+1] <= 0){
count += 1 - sum[i+1];
sum[i+1] = 1;
}
}
}
return count;
}
public static int solve2(int[] sum,int[] a,int count){
if(sum[0] >= 0){
count += 1 + sum[0];
sum[0] = -1;
}
for(int i = 0;i < sum.length-1;i++){
sum[i+1] = sum[i] + a[i+1];
if((i+1) % 2 == 1){
if(sum[i+1] <= 0){
count += 1 - sum[i+1];
sum[i+1] = 1;
}
}
if((i+1) % 2 == 0){
if(sum[i+1] >= 0){
count += 1 + sum[i+1];
sum[i+1] = -1;
}
}
}
return count;
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | macro_rules! input {
(source = $s:expr, $($r:tt)*) => {
let mut iter = $s.split_whitespace();
input_inner!{iter, $($r)*}
};
($($r:tt)*) => {
let s = {
use std::io::Read;
let mut s = String::new();
std::io::stdin().read_to_string(&mut s).unwrap();
s
};
let mut iter = s.split_whitespace();
input_inner!{iter, $($r)*}
};
}
macro_rules! input_inner {
($iter:expr) => {};
($iter:expr, ) => {};
($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
let $var = read_value!($iter, $t);
input_inner!{$iter $($r)*}
};
}
macro_rules! read_value {
($iter:expr, ( $($t:tt),* )) => {
( $(read_value!($iter, $t)),* )
};
($iter:expr, [ $t:tt ; $len:expr ]) => {
(0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
};
($iter:expr, chars) => {
read_value!($iter, String).chars().collect::<Vec<char>>()
};
($iter:expr, usize1) => {
read_value!($iter, usize) - 1
};
($iter:expr, $t:ty) => {
$iter.next().unwrap().parse::<$t>().expect("Parse error")
};
}
fn solve(as_: Vec<i64>) -> i64 {
let mut sum = 0;
let mut count = 0;
if as_[0] == 0 {
for i in 1..as_.len() as i64{
if i.is_negative() {
if i % 2 == 0 {
sum = -1;
count += 1;
break;
} else {
sum = 1;
count += 1;
break;
}
}
if i.is_positive() {
if i % 2 == 0 {
sum = 1;
count += 1;
break;
} else {
sum = -1;
count += 1;
break;
}
}
}
} else {
sum = as_[0];
}
if sum == 0 {
sum += 1;
count += 1;
}
for i in 1..as_.len() {
let cur = as_[i];
if sum > 0 {
if cur + sum < 0 {
sum += cur;
} else {
let exp = -1 - sum;
count += cur - exp;
sum = -1;
}
} else if sum < 0 {
if cur + sum > 0 {
sum += cur;
} else {
let exp = 1 - sum;
count += exp - cur;
sum = 1;
}
}
}
count
}
fn main() {
input!{
n: usize,
as_: [i64; n],
}
println!("{}", solve(as_));
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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 | f x [] = 0
f x (a:as)
| x<0&&(-x)<a || x>0&&(-x)>a = f (x+a) as
| otherwise = abs (x+a)+1 + f (signum (-x)) as
main = do
getLine
(a:as) <- map read . words <$> getLine
print $ if a == 0
then f 1 as `min` f (-1) as + 1
else f a as `min` f (-a) as |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <bits/stdc++.h>
int r(int a) {
if (a % 2 == 0)
return -1;
else
return 1;
}
int main() {
int n, i;
long long a[10000], ans = 0, ans2 = 0, tmp = 0, tmp2 = 0;
scanf("%d", &n);
for (i = 0; i < n; i++) scanf("%lld", &a[i]);
for (i = 0; i < n; i++) {
tmp += a[i];
tmp2 += a[i];
if (tmp * r(i) <= 0) {
ans += tmp * r(i + 1) + 1;
tmp = r(i);
}
if (tmp2 * r(1 + i) <= 0) {
ans2 += tmp2 * r(i) + 1;
tmp2 = r(i + 1);
}
}
printf("%lld\n", ans2 < ans ? ans2 : ans);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> v(n);
for (int i = 0; i < n; i++) {
cin >> v[i];
}
long long sum;
long long ans = 10000000;
long long ans_temp;
sum = 0;
ans_temp = 0;
for (int i = 0; i < n; i++) {
sum += v[i];
if (i % 2 == 0) {
if (sum <= 0) {
ans_temp += 1 - sum;
sum = 1;
}
} else {
if (sum >= 0) {
ans_temp += 1 + sum;
sum = -1;
}
}
}
ans = min(ans, ans_temp);
sum = 0;
ans_temp = 0;
for (int i = 0; i < n; i++) {
sum += v[i];
if (i % 2 == 1) {
if (sum <= 0) {
ans_temp += 1 - sum;
sum = 1;
}
} else {
if (sum >= 0) {
ans_temp += 1 + sum;
sum = -1;
}
}
}
ans = min(ans, ans_temp);
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
as=gets.split.map(&:to_i)
ans=0
sum=as[0]
(1..n-1).each do |i|
if sum>0
if sum+as[i]>=0
ans+=(-1-(sum+as[i])).abs
sum=-1
else
sum+=as[i]
end
else
if sum+as[i]<=0
ans+=(1-(sum+as[i])).abs
sum=1
else
sum+=as[i]
end
end
end
puts ans
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int,input().split()))
def find(a):
f = "+" if a[0] < 0 else "-"
s = a[0]
c = 0
#print((s,f,c))
for i in range(1,n):
if f == "+" and s + a[i] > 0:
f = "-"
s = s + a[i]
# print((s,f,c))
elif f == "+":
c = c + abs(s + a[i])+1
f = "-"
s = 1
# print((s,f,c))
elif f == "-" and s + a[i] < 0:
f = "+"
s = s + a[i]
# print((s,f,c))
else:
c = c + abs(s+a[i])+1
f = "+"
s = -1
return c
c = find(a)
print(c)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | def main():
n = int(input())
A = list(map(int, input().split()))
res = 0
sums = []
for i in range(n):
if i == 0:
sums.append(A[i])
if A[i] == 0:
index = -1
for j in range(n):
if A[j] != 0:
index = A.index(A[j])
break
if index == -1:
sums[i] = 1
res += 1
elif (index % 2 and A[index] > 0) or (index % 2 == 0 and A[index] < 0):
sums[i] = -1
res += 1
else:
sums[i] = 1
res += 1
else:
sums.append(sums[i-1] + A[i])
if sums[i] == 0:
if sums[i-1] > 0:
A[i] -= 1
sums[i] = -1
res += 1
else:
A[i] += 1
sums[i] = 1
res += 1
elif (sums[i-1] > 0) and (sums[i] > 0):
res += 1 + sums[i]
sums[i] = -1
elif (sums[i-1] < 0) and (sums[i] < 0):
res += 1 - (sums[i])
sums[i] = 1
print(res)
if __name__ == '__main__':
main() |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
int count = 0;
vector<int> a(100000);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
if (a[0] == 0) {
if (a[1] > 0) {
a[0]--;
count++;
} else {
a[0]++;
count++;
}
}
vector<int> sum(100000);
sum[0] = a[0];
for (int i = 1; i < n; i++) {
sum[i] = sum[i - 1] + a[i];
if (sum[i] * sum[i - 1] >= 0) {
if (sum[i - 1] < 0) {
while (sum[i] * sum[i - 1] >= 0) {
sum[i]++;
count++;
}
} else {
while (sum[i] * sum[i - 1] >= 0) {
sum[i]--;
count++;
}
}
}
}
cout << count;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | def main():
n = int(input())
A = [int(a) for a in input().split()]
min_count = float('inf')
tot = 0
if A[0] != 0:
tot = A[0]
count = 0
for a in A[1:]:
if tot > 0 and tot + a >= 0:
count += tot+a+1
tot = -1
elif tot < 0 and tot + a <= 0:
count += -(tot+a)+1
tot = 1
else:
tot += a
min_count = min(min_count, count)
else:
count = 1
for i in [1, -1]:
tot = i
for a in A[1:]:
if tot > 0 and tot + a >= 0:
count += tot+a+1
tot = -1
elif tot < 0 and tot + a <= 0:
count += -(tot+a)+1
tot = 1
else:
tot += a
min_count = min(min_count, count)
return min_count
if __name__ == '__main__':
print(main()) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a[] = new int[n];
for(int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
int sum = a[0];
if(sum == 0) {
boolean flag = true;
int cnt = 1;
while(flag) {
if(a[cnt] > 0) {
if(cnt % 2 == 0) {
sum = 1;
} else {
sum = -1;
}
flag = false;
} else if(a[cnt] < 0) {
if(cnt % 2 == 0) {
sum = -1;
} else {
sum = 1;
}
flag = false;
}
cnt++;
}
}
int ans = 0;
for(int i = 1; i < n; i++) {
if(sum > 0) {
if(sum + a[i] >= 0) {
ans += sum + a[i] + 1;
sum = -1;
} else {
sum = sum + a[i];
}
} else {
if(sum + a[i] <= 0) {
ans += Math.abs(a[i] + sum) + 1;
sum = 1;
} else {
sum = sum + a[i];
}
}
}
System.out.println(ans);
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
A = list(map(int, input().split()))
sum = A[0]
ans = 0
if sum == 0:
ans += 1
sum = 1
for i in range(1, len(A)):
if sum > 0:
if sum + A[i] >= 0:
ans += abs(sum + A[i]) + 1
sum = -1
else:
sum += A[i]
elif sum < 0:
if sum + A[i] <= 0:
ans += abs(sum + A[i]) + 1
sum = 1
else:
sum += A[i]
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long seq(vector<long long> &a, long long sum, int beg, long long count) {
long long prev;
while (beg < a.size()) {
prev = sum;
sum += a[beg];
if ((sum ^ prev) >= 0LL || sum == 0LL) {
if (prev > 0) {
count += sum + 1;
sum = -1;
} else {
count += 1 - sum;
sum = 1;
}
}
beg++;
}
return count;
}
int main(int argc, char *argv[]) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
long long res;
if (a[0] == 0)
res = min(seq(a, 1, 1, 1), seq(a, -1, 1, 1));
else
res = seq(a, a[0], 1, 0);
std::cout << res << std::endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const double PI = 3.1415926535897932384626433832795;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
bool isDiffer(long long a, long long b) {
if (b == 0) return false;
if (((a > 0) && (b < 0)) || ((a < 0) && (b > 0)))
return true;
else
return false;
}
int main() {
ios::sync_with_stdio(false);
long long n;
cin >> n;
vector<long long> v;
for (int i = 0; i < n; i++) {
long long t;
cin >> t;
v.push_back(t);
}
long long ans = 0;
if (v[0] == 0) {
v[0] = -1;
ans += 1;
}
long long os = v[0];
for (int i = 1; i < n; i++) {
if (!isDiffer(os, v[i] + os)) {
long long ob = (os >= 0) ? -1 : 1;
ans += llabs(ob - os - v[i]);
v[i] = ob - os;
}
os += v[i];
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.next());
int[] a1 = new int[n];
int[] a2 = new int[n];
for (int i = 0; i < n; i++) {
int temp = Integer.parseInt(sc.next());
a1[i] = temp;
a2[i] = temp;
}
int ans1 = 0;
int ans2 = 0;
long temp = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0 && temp + a1[i] >= 0) {
ans1 += temp + a1[i] + 1;
a1[i] -= temp + a1[i] + 1;
}
if (i % 2 != 0 && temp + a1[i] <= 0) {
ans1 += Math.abs(temp + a1[i] - 1);
a1[i] += Math.abs(temp + a1[i] - 1);
}
temp += a1[i];
}
if (Arrays.stream(a1).mapToLong(x -> x).sum() == 0) {
ans1++;
}
temp = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0 && temp + a2[i] <= 0) {
ans2 += Math.abs(temp + a2[i] - 1);
a2[i] += Math.abs(temp + a2[i] - 1);
}
if (i % 2 != 0 && temp + a2[i] >= 0) {
ans2 += temp + a2[i] + 1;
a2[i] -= temp + a2[i] + 1;
}
temp += a2[i];
}
if (Arrays.stream(a2).mapToLong(x -> x).sum() == 0) {
ans2++;
}
System.out.println(Math.min(ans1, ans2));
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int,input().split()))
sums = 0
count = 0
for i in range(n):
sums = sums + a[i]
if i % 2 ==0 :
if sums > 0:
None
else:
count = count + 1 -sums
sums = 1
else:
if sums < 0:
None
else:
count = count + 1 + sums
sums = -1
count2 = 0
sums2 = 0
for i in range(n):
sums2 = sums2 + a[i]
if i % 2 ==1 :
if sums > 0:
None
else:
count2 = count2 + 1 -sums2
sums2 = 1
else:
if sums2 < 0:
None
else:
count2 = count2 + 1 + sums2
sums2 = -1
print(min(count,count2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | def f(a)
g = lambda{|car, cdr, total=0|
sum = car
r = [car]
cdr.each{|curr|
if sum >= 0
r << new_curr = [curr, -sum-1].min
sum += new_curr
total += curr - new_curr
else
r << new_curr = [curr, -sum+1].max
sum += new_curr
total += new_curr - curr
end
}
# p r
total += 1 if sum == 0
total
}
x = g.(a[0], a[1..-1])
y = g.(a[0] > 0 ? -1 : 1, a[1..-1], a[0].abs+1)
[x, y].min
end
N = gets.to_i
A = gets.split.take(N).map(&:to_i)
p f(A)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ABC59C
{
class Program
{
static void Main(string[] args)
{
int n = int.Parse(Console.ReadLine());
string[] str = Console.ReadLine().Split(' ');
int[] a = new int[n];
for(int i = 0; i < n; i++)
{
a[i] = int.Parse(str[i]);
}
int x = 0;
int y = 0;
int f = 0;
int sum = 0;
for(int i = 0; i < n; i++)
{
sum += a[i];
if (f == 1 && sum >= 0)
{
x += (sum + 1);
f = 0;
sum = -1;
}else if (f == 0 && sum <= 0)
{
x += (1 - sum);
f = 1;
sum = 1;
}else
{
f = 1 - f;
}
}
sum = 0;
f = 1;
for (int i = 0; i < n; i++)
{
sum += a[i];
if (f == 1 && sum >= 0)
{
y += (sum + 1);
f = 0;
sum = -1;
}
else if (f == 0 && sum <= 0)
{
y += (1 - sum);
f = 1;
sum = 1;
}
else
{
f = 1 - f;
}
}
x = Math.Min(x, y);
Console.WriteLine(x);
}
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | 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
if a[0] != 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)
else:
posL = L[:]
negL = L[:]
pos_delay, neg_delay = 1, -1
pos_all_over, neg_all_over = 1, 1
sign = 1
for i in range(1, n):
posL[i] += pos_delay
if posL[i] <= 0 and sign == -1:
pos_delay += 1 - posL[i]
pos_all_over += 1 - posL[i]
posL[i] = 1
elif posL[i] >= 0 and sign == 1:
pos_delay -= posL[i] + 1
pos_all_over += posL[i] + 1
posL[i] = -1
sign *= -1
sign = -1
for i in range(1, n):
negL[i] += neg_delay
if negL[i] <= 0 and sign == -1:
neg_delay += 1 - negL[i]
neg_all_over += 1 - negL[i]
negL[i] = 1
elif negL[i] >= 0 and sign == 1:
neg_delay -= negL[i] + 1
neg_all_over += negL[i] + 1
negL[i] = -1
sign *= -1
print(pos_all_over, min(pos_all_over, neg_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 | python3 | n = int(input())
i = input()
i = i.split()
for item in range(len(i)):
i[item] = int(i[item])
signM = None
tot = 0
count = 0
if i[0] > 0:
signN = 'neg'
elif i[0] < 0:
signN = 'pos'
else:
if i[1] > 0:
tot = -1
count += 1
signN = 'pos'
elif i[1] < 0:
tot = 1
count += 1
signN = 'neg'
for x in range(len(i)):
tot += i[x]
#print('tot beg', tot)
if tot > 0:
signM = 'pos'
elif tot < 0:
signM = 'neg'
elif tot == 0:
signM = 'z'
if signN != signM and signM != 'z':
signN = signM
elif signN == 'pos' and signM == 'pos':
count += (tot + 1)
tot -= (tot +1)
signM = 'neg'
elif signN == 'neg' and signM == 'neg':
count += (abs(tot) + 1)
tot = 1
signM = 'pos'
if tot == 0:
count += 1
if signN == 'neg':
tot += 1
signM = 'pos'
elif signN == 'pos':
tot -= 1
signM = 'neg'
signN = signM
#print('tot end', tot)
#print('count', count)
print(count) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
int ju = 0;
for (int i = 0; i < (int)(n); i++) {
cin >> a[i];
if (i % 2 == 0 && a[i] >= 0) ju++;
if (i % 2 == 1 && a[i] < 0) ju++;
}
long long int sum = 0, ans = 0;
if (ju > n / 2) {
for (int i = 0; i < (int)(n); i++) {
sum += a[i];
if (sum <= 0 && i % 2 == 0) {
ans += abs(1 - sum);
sum = 1;
} else if (sum >= 0 && i % 2 == 1) {
ans += abs(-1 - sum);
sum = -1;
}
}
} else {
for (int i = 0; i < (int)(n); i++) {
sum += a[i];
if (sum >= 0 && i % 2 == 0) {
ans += abs(-1 - sum);
sum = -1;
} else if (sum <= 0 && i % 2 == 1) {
ans += abs(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;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int count = 0;
int ans = 0;
for (int i = 0; i < n; i++) {
count += a[i];
if (i % 2 == 0) {
if (count <= 0) {
ans += abs(count) + 1;
count = 1;
}
} else {
if (count >= 0) {
ans += count + 1;
count = -1;
}
}
}
int count2 = 0;
int ans2 = 0;
for (int i = 0; i < n; i++) {
count2 += a[i];
if (i % 2 == 1) {
if (count2 <= 0) {
ans2 += abs(count2) + 1;
count2 = 1;
}
} else {
if (count2 >= 0) {
ans2 += count2 + 1;
count2 = -1;
}
}
}
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.*;
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();
long sum = sc.nextLong();
long cnt = 0;
for(int i=1; i<n; i++) {
long v= sc.nextLong();
if(sum * v >= 0) {
cnt += v + Math.abs(sum-v) + 1;
if(sum<0) {
v = 1;
}else {
v = -1;
}
}else {
int s = 1;
if(v<0) {
s = -1;
}
while(Math.abs(sum)>=Math.abs(v)) {
cnt++;
v += s;
}
}
sum += v;
}
System.out.println(cnt);
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() {
vector<int> a, b;
int n, ansa = 0, ansb = 0;
cin >> n;
for (int i = 0; i < n; ++i) {
int tmp;
cin >> tmp;
a.push_back(tmp);
b.push_back(tmp);
}
if (a[0] < 0) {
for (int i = 1; i < n; ++i) {
a[i] += a[i - 1];
if (i % 2 == 1) {
while (a[i] < 1) {
++a[i];
++ansa;
}
} else {
while (a[i] > -1) {
--a[i];
++ansa;
}
}
}
ansb = 1 - b[0];
b[0] = 1;
for (int i = 1; i < n; ++i) {
b[i] += b[i - 1];
if (i % 2 == 0) {
while (b[i] < 1) {
++b[i];
++ansb;
}
} else {
while (b[i] > -1) {
--b[i];
++ansb;
}
}
}
} else {
for (int i = 1; i < n; ++i) {
a[i] += a[i - 1];
if (i % 2 == 0) {
while (a[i] < 1) {
++a[i];
++ansa;
}
} else {
while (a[i] > -1) {
--a[i];
++ansa;
}
}
}
ansb = b[0] + 1;
b[0] = -1;
for (int i = 1; i < n; ++i) {
b[i] += b[i - 1];
if (i % 2 == 1) {
while (b[i] < 1) {
++b[i];
++ansb;
}
} else {
while (b[i] > -1) {
--b[i];
++ansb;
}
}
}
}
cout << min(ansa, ansb) << 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 llint = long long;
using VI = std::vector<int>;
using PII = std::pair<int, int>;
using SI = std::set<int>;
using MII = std::map<int, int>;
using TIII = std::tuple<int, int, int>;
using MCI = std::map<char, int>;
using VPII = std::vector<PII>;
using Complex = std::complex<double>;
using namespace std;
int N;
int a[100010];
int solve() {
int ans1 = 0;
int sum = 0;
for (int(i) = (int)(0); (i) < (int)(N); ++(i)) {
sum += a[i];
if (i % 2 == 0 && sum <= 0) ans1 += (1 - sum), sum = 1;
if (i % 2 == 1 && sum >= 0) ans1 += (sum + 1), sum = -1;
}
int ans2 = 0;
sum = 0;
for (int(i) = (int)(0); (i) < (int)(N); ++(i)) {
sum += a[i];
if (i % 2 == 1 && sum <= 0) ans2 += (1 - sum), sum = 1;
if (i % 2 == 0 && sum >= 0) ans2 += (sum + 1), sum = -1;
}
return min(ans1, ans2);
}
signed main() {
cin >> N;
for (int(i) = (int)(0); (i) < (int)(N); ++(i)) cin >> a[i];
cout << solve() << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long n, ai;
vector<long long> a;
long long calc(vector<long long> &a, bool even) {
long long c = 0, sum = 0;
bool lp = even, cp;
for (int(i) = 0; (i) < (n); (i)++) {
sum += a[i];
cp = 0 < sum;
if (lp == cp) {
c += abs(sum) + 1;
sum = lp ? -1 : 1;
}
lp = !lp;
}
return c;
}
int main() {
cin >> n;
for (int(i) = 0; (i) < (n); (i)++) {
cin >> ai;
a.push_back(ai);
}
cout << min(calc(a, true), calc(a, false)) << '\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 dfs(vector<set<int>> graph, vector<int> seen, int x) {
int n = int(seen.size()) - 1;
seen.at(x) = 1;
int count = 0;
for (int i = 1; i <= n; i++) {
count += seen.at(i);
}
if (count == n) {
return 1;
}
bool flag = false;
for (int nextx : graph.at(x)) {
if (seen.at(nextx) == 0) {
flag = true;
break;
}
}
if (flag == false and count != n) {
return 0;
}
int tmp = 0;
for (int nextx : graph.at(x)) {
if (seen.at(nextx) == 0) {
tmp += dfs(graph, seen, nextx);
}
}
return tmp;
}
int main() {
int n;
cin >> n;
vector<int> data(n);
for (int i = 0; i < n; i++) {
int tmp;
cin >> tmp;
data.at(i) = tmp;
}
int answereven = 0;
int sum1 = 0;
for (int i = 0; i < n; i++) {
sum1 += data.at(i);
if (i % 2 == 0 and sum1 <= 0) {
answereven += 1 - sum1;
sum1 = 1;
} else if (i % 2 == 1 and sum1 >= 0) {
answereven += sum1 + 1;
sum1 = -1;
}
}
int answerodd = 0;
sum1 = 0;
for (int i = 0; i < n; i++) {
sum1 += data.at(i);
if (i % 2 == 0 and sum1 >= 0) {
answerodd += sum1 + 1;
sum1 = -1;
} else if (i % 2 == 1 and sum1 <= 0) {
answerodd += 1 - sum1;
sum1 = 1;
}
}
cout << min(answereven, answerodd) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<long long> a(N);
for (int i = 0; i < N; i++) {
cin >> a[i];
}
long long ans = 0;
while (ans < N && a[ans] == 0) ans++;
long long sum;
if (ans == 0)
sum = a[0];
else if (ans < N && a[ans + 1] > 0)
sum = -1;
else
sum = 1;
for (int i = ans + 1; i < N; i++) {
if (sum * (sum + a[i]) >= 0) {
if (sum + a[i] >= 0) {
ans += abs(sum + a[i] + 1);
a[i] = -sum - 1;
} else {
ans += abs(sum + a[i] - 1);
a[i] = -sum + 1;
}
}
sum += a[i];
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
cnt = 0
if a[0] < 0:
flag = -1
else:
flag = 1
for i in range(1,n):
if flag == -1:
if sum(a[0:i]) + a[i] <= 0:
cnt += -(sum(a[0:i]) + a[i]) + 1
a[i] = -sum(a[0:i]) + 1
flag = 1
elif flag == 1:
if sum(a[0:i]) + a[i] >= 0:
cnt += sum(a[0:i]) + a[i] + 1
a[i] = -sum(a[0:i]) - 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 | #include <bits/stdc++.h>
int main(void) {
int num_integer;
if (scanf("%d", &num_integer) != 1) {
puts("num_integer input error.");
return 1;
}
int integer, sum = 0, num_operation = 0;
for (int i = 0; i < num_integer; i++) {
if (scanf("%d", &integer) != 1) {
puts("integer input error.");
}
if (sum > 0 && (sum + integer) >= 0) {
num_operation += sum + integer + 1;
sum = -1;
} else if (sum < 0 && (sum + integer) <= 0) {
num_operation += -(sum + integer) + 1;
sum = 1;
} else {
sum += integer;
}
}
printf("%d", num_operation);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
int ans = (a[0] > 0 ? 0 : 1 - a[0]);
int now = (a[0] > 0 ? a[0] : 1);
for (int i = 1; i < n; i++) {
if (now < 0 and now + a[i] <= 0) {
ans += (1 - (now + a[i]));
now = 1;
} else if (now > 0 and now + a[i] > 0) {
ans += (now + a[i] - (-1));
now = -1;
} else
now = now + a[i];
}
int ans2 = (a[0] < 0 ? 0 : a[0] - (-1));
now = (a[0] < 0 ? a[0] : -1);
for (int i = 1; i < n; i++) {
if (now < 0 and now + a[i] <= 0) {
ans2 += (1 - (now + a[i]));
now = 1;
} else if (now > 0 and now + a[i] > 0) {
ans2 += (now + a[i] - (-1));
now = -1;
} else
now = now + a[i];
}
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 | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[100000];
for (int i = 0; i < (int)(n); i++) cin >> a[i];
bool zeroFlag = false;
long long costA = 0, costB = 0;
int sum = 0, prev = 0;
for (int i = 0; i < (int)(n); i++) {
if (a[0] == 0) {
a[0] = 1;
costA++;
zeroFlag = true;
}
prev = sum;
sum += a[i];
if (i == 0) continue;
if ((prev * sum) < 0) continue;
if (prev < 0) {
costA += abs(1 - sum);
sum = 1;
} else {
costA += abs(-1 - sum);
sum = -1;
}
}
if (zeroFlag) {
a[0] = -1;
costB++;
} else {
a[0] = -1 * a[0] / abs(a[0]);
}
sum = 0;
prev = 0;
for (int i = 0; i < (int)(n); i++) {
prev = sum;
sum += a[i];
if (i == 0) continue;
if ((prev * sum) < 0) continue;
if (prev < 0) {
costB += abs(1 - sum);
sum = 1;
} else {
costB += abs(-1 - sum);
sum = -1;
}
}
cout << min(costA, costB) << endl;
return 0;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.