Search is not available for this dataset
name
stringlengths 2
88
| description
stringlengths 31
8.62k
| public_tests
dict | private_tests
dict | solution_type
stringclasses 2
values | programming_language
stringclasses 5
values | solution
stringlengths 1
983k
|
---|---|---|---|---|---|---|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
int A[100100];
int ANS[2];
for (int i = 0; i < N; ++i) {
cin >> A[i];
}
int mode;
for (int index = 0; index < 2; ++index) {
mode = index;
ANS[index] = 0;
int total = 0;
for (int i = 0; i < N; ++i) {
mode ^= 1;
int _total = total + A[i];
if (mode == 0 && _total <= 0 || mode == 1 && _total >= 0) {
_total = mode == 0 ? 1 : -1;
ANS[index] += abs(_total - (total + A[i]));
}
total = _total;
}
}
cout << min(ANS[0], ANS[1]) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long INF = 100000000000000LL;
long long a[100001];
long long r[2] = {};
int main() {
int n;
cin >> n;
int t1 = 0;
int t2 = 0;
int s1 = 0;
int s2 = 0;
int a1;
for (int i = 0; i < n; ++i) {
cin >> a1;
t1 += a1;
t2 += a1;
if (i % 2 == 0) {
if (t1 <= 0) {
s1 += -t1 + 1;
t1 = 1;
}
if (t2 >= 0) {
s2 += t2 + 1;
t2 = -1;
}
} else {
if (t1 >= 0) {
s1 += t1 + 1;
t1 = -1;
}
if (t2 <= 0) {
s2 += -t2 + 1;
t2 = 1;
}
}
}
cout << min(s1, s2) << 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 table[100005];
void table_print(int n) {
for (int i = 0; i < n; i++) cout << table[i] << ' ';
cout << endl;
}
int main() {
int n, ans;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> table[i];
}
ans = 0;
if (table[0] >= 0) {
for (int i = 0; i < n; i++) {
if (i > 0) table[i] += table[i - 1];
if (i % 2 == 0 && table[i] <= 0) {
ans += 1 - table[i];
table[i] = 1;
}
if (i % 2 == 1 && table[i] >= 0) {
ans += table[i] + 1;
table[i] = -1;
}
}
} else {
for (int i = 0; i < n; i++) {
if (i > 0) table[i] += table[i - 1];
if (i % 2 == 0 && table[i] >= 0) {
ans += table[i] + 1;
table[i] = -1;
}
if (i % 2 == 1 && table[i] <= 0) {
ans += 1 - table[i];
table[i] = 1;
}
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
A = list(map(int, input().split()))
ans = 0
acum = A[0]
for i in range(1, N):
if (acum >= 0 and acum + A[i] >= 0) or (acum < 0 and acum + A[i] < 0) or (acum + A[i] == 0):
# same signs
ans += abs(acum + A[i]) + 1
acum = 1 if acum < 0 else -1
# print("a", acum)
else:
# different signs
acum += A[i]
# print("b", acum)
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;
template <class T>
inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T>
inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
void print(vector<long long> &v) {
for (int i = 0; i < v.size(); i++) {
if (i) cout << " ";
cout << v[i];
}
cout << endl;
}
int main() {
long long n, v, cnt = 0;
cin >> n;
vector<long long> a(n, 0), sum(n, 0);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sum[0] = a[0];
for (int i = 1; i < n; i++) {
sum[i] = a[i] + sum[i - 1];
if (sum[i - 1] > 0) {
if (sum[i] >= 0) {
cnt += sum[i] + 1;
sum[i] -= sum[i] + 1;
}
} else {
if (sum[i] <= 0) {
cnt += abs(sum[i]) + 1;
sum[i] += abs(sum[i]) + 1;
}
}
}
cout << cnt << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void) {
int n;
cin >> n;
int a[n];
for (int i = 0; i < (int)(n); i++) {
cin >> a[i];
}
int ans = 1000000000;
for (int p = 0; p <= 1; p++) {
int tmpans = 0;
int sum = 0;
for (int i = 0; i < (int)(n); i++) {
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 | cpp | #include <bits/stdc++.h>
using namespace std;
int iy[] = {0, 0, 1, -1};
int ix[] = {1, -1, 0, 0};
int n, sum[10001];
long long int a[10001], ans;
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sum[0] = a[0];
for (int i = 1; i < n; i++) {
sum[i] = sum[i - 1] + a[i];
if (sum[i - 1] > 0 && sum[i] > 0) {
ans += sum[i] + 1;
sum[i] = -1;
for (int j = i + 1; j < n; j++) {
a[i] -= sum[i] + 1;
}
} else if (sum[i - 1] < 0 && sum[i] < 0) {
ans += -sum[i] + 1;
sum[i] = 1;
for (int j = i + 1; j < n; j++) {
a[i] += -sum[i] + 1;
}
} else if (sum[i] == 0) {
if (sum[i - 1] > 0) {
ans++;
sum[i] = -1;
for (int j = i + 1; j < n; j++) {
a[i]--;
}
} else {
ans++;
sum[i] = 1;
for (int j = i + 1; j < n; j++) {
a[i]++;
}
}
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> List(n);
for (int i = 0; i < n; i++) {
cin >> List.at(i);
}
int cntA, cntB, SignA, SignB;
cntA = cntB = 0;
;
for (int i = 0; i < n; i++) {
if (i == 0) {
if (List.at(i) > 0) {
SignA = List.at(i);
SignB = -1;
cntB += abs(List.at(i)) + 1;
} else {
SignB = List.at(i);
SignA = 1;
cntA += abs(List.at(i)) + 1;
}
continue;
}
if (SignA > 0) {
if (SignA + List.at(i) >= 0) {
cntA += abs(SignA + List.at(i)) + 1;
SignA = -1;
} else {
SignA += List.at(i);
}
if (SignB + List.at(i) <= 0) {
cntB += abs(SignB + List.at(i)) + 1;
SignB = 1;
} else {
SignB += List.at(i);
}
} else {
if (SignA + List.at(i) <= 0) {
cntA += abs(SignA + List.at(i)) + 1;
SignA = 1;
} else {
SignA += List.at(i);
}
if (SignB + List.at(i) >= 0) {
cntB += abs(SignB + List.at(i)) + 1;
SignB = -1;
} else {
SignB += List.at(i);
}
continue;
}
}
cout << min(cntA, cntB) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N, count = 0;
cin >> N;
vector<int> A(N);
for (int i = 0; i < N; i++) cin >> A[i];
int su = A[0];
for (int i = 1; i < N; i++) {
while (((su > 0) == (su + A[i] > 0)) || su + A[i] == 0) {
if (su + A[i] == 0) {
if (su > 0)
A[i]--;
else
A[i]++;
} else if (su + A[i] > 0) {
A[i]--;
} else {
A[i]++;
}
count++;
}
su += 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 | python3 | n = int(input())
a = list(map(int,input().split()))
P =[]
if a[0]>0:
P.append(1)
else:
P.append(-1)
for i in range(n-1):
P.append(-P[i])
S = []
S.append(a[0])
cnt = 0
for i in range(1,n):
if (S[i-1] + a[i])*P[i]<=0:
cnt += abs(P[i]-S[i-1]-a[i])
a[i] = P[i]-S[i-1]
S.append(S[i-1]+a[i])
print(cnt)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
vector<long long> a(n);
for (long long i = 0; i < n; i++) cin >> a[i];
vector<long long> c(a.begin(), a.end());
long long ans = LLONG_MAX;
long long csum = 0;
long long num = 0;
bool is_positive = true;
for (long long i = 0; i < n; i++) {
csum += c[i];
if (is_positive) {
if (csum < 0) {
num += abs(1 - csum);
csum = 1;
}
} else {
if (csum > 0) {
num += abs(csum + 1);
csum = -1;
}
}
is_positive = !is_positive;
}
ans = min(ans, num);
csum = 0;
num = 0;
is_positive = false;
for (long long i = 0; i < n; i++) {
csum += c[i];
if (is_positive) {
if (csum <= 0) {
num += abs(1 - csum);
csum = 1;
}
} else {
if (csum >= 0) {
num += abs(csum + 1);
csum = -1;
}
}
is_positive = !is_positive;
}
ans = min(ans, num);
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;
int a[n];
long long b[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
b[0] = (long long)a[0];
for (int i = 1; i < n; i++) {
b[i] = b[i - 1] + a[i];
}
long long cnt1 = 0;
long long cnt2 = 0;
for (int i = 0; i < n; i += 2) {
if (b[i] <= 0) {
cnt1 += 1 - b[i];
}
}
for (int i = 1; i < n; i += 2) {
if (0 <= b[i]) {
cnt1 += 1 + b[i];
}
}
for (int i = 0; i < n; i += 2) {
if (0 <= b[i]) {
cnt2 += 1 + b[i];
}
}
for (int i = 1; i < n; i += 2) {
if (b[i] <= 0) {
cnt2 += 1 - b[i];
}
}
long long ans = min(cnt1, cnt2);
cout << ans;
cout << 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> 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 == 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);
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);
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java |
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int ans=0;
int ans2=0;
int[] f = new int[n];
for(int i=0;i<n;i++) f[i] = sc.nextInt();
int sum1 = f[0];
int sum2 = f[0];
for(int i=0;i<n-1;i++) {
sum2 += f[i+1];
if(sum1*sum2 > 0) {
ans += Math.abs(sum2)+1;
if(sum2 >0) sum2 = -1;
else sum2 = 1;
}else if(sum2 == 0) {
ans += 1;
if(sum1 > 0) sum2 = -1;
else sum2 = 1;
}
sum1 = sum2;
}
sum1 = f[0];
sum2 = f[0];
for(int i=0;i<n-1;i++) {
sum2 += f[i+1];
if(sum1*sum2 > 0) {
ans2 += Math.abs(sum2)+1;
if(sum2 >0) sum2 = -1;
else sum2 = 1;
}else if(sum2 == 0) {
ans2 += 1;
if(sum1 < 0) sum2 = -1;
else sum2 = 1;
}
sum1 = sum2;
}
System.out.println(Math.min(ans,ans2));
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split( )))
#前から貪欲でよいか
#a[0]を正か負に定めて貪欲
#a[0]が正と仮定してよい
if a[0]<0:
for i in range(n):
a[i]*=-1
#場合分け用
a2 = [a[i] for i in range(n)]
ans1 = 0
if not a[0]:
ans1 += 1
a[0] = 1
sm = a[0]
for i in range(1,n):
sm2 = sm + a[i]
#print(sm,sm2)
if sm2*sm>0:
if sm<0:#sm+a[i]=1
ans1 += abs((1-sm)-a[i])
a[i]=1-sm
sm = 1
else:#sm+a[i] = -1
ans1 += abs(-1-sm-a[i])
a[i]=-1-sm
sm = -1
else:
sm=sm2
ans2 = abs(a2[0]+1)
a2[0]=-1
sm = -1
for i in range(1,n):
sm2 = sm + a2[i]
if sm2*sm>0:
if sm<0:#sm+a[i]=1
ans2 += abs((1-sm)-a[i])
a2[i] = 1-sm
sm = 1
else:#sm+a[i] = -1
ans2 += abs(-1-sm-a[i])
a2[i]=-1-sm
sm = -1
else:
sm =sm2
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 | UNKNOWN | 5
3 -6 4 -5 7 |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 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) {
if (vector[0] > 0)
sum1 = vector[0];
else {
sum1 = vector[0];
while (sum1 <= 0) {
sum1++;
answer1++;
}
}
} else if (sum1 < 0) {
sum1 += vector[i];
if (sum1 < 0) {
while (sum1 <= 0) {
sum1++;
answer1++;
}
}
} else {
sum1 += vector[i];
if (sum1 > 0) {
while (sum1 >= 0) {
sum1--;
answer1++;
}
}
}
}
for (int i = 0; i < n; i++) {
if (i == 0) {
if (vector[0] < 0)
sum2 = vector[0];
else {
sum2 = vector[0];
while (sum2 >= 0) {
sum2--;
answer2++;
}
}
} else if (sum2 < 0) {
sum2 += vector[i];
if (sum2 < 0) {
while (sum2 <= 0) {
sum2++;
answer2++;
}
}
} else {
sum2 += vector[i];
if (sum2 > 0) {
while (sum2 >= 0) {
sum2--;
answer2++;
}
}
}
}
cout << min(answer1, answer2) << 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=input()
a=list(map(int,input().split()))
ans=10**18
for s in -a[1]+1,a[0],-a[1]-1:
c=abs(a[0]-s)
for i in a[1:]:
if s<0:
b=-s+1
c+=max(0,b-i)
s+=max(i,b)
else:
b=-s-1
c+=max(0,i-b)
s+=min(i,b)
ans=min(ans,c)
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>
const int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
using namespace std;
int main() {
int n;
cin >> n;
long long a[n];
long long sums[n];
cin >> a[0];
sums[0] = a[0];
for (int i = 1; i < n; ++i) {
cin >> a[i];
sums[i] = sums[i - 1] + a[i];
}
long long cnt = 0;
long long v = 0;
long long diff;
if (sums[0] == 0) {
if (sums[1] >= 0) {
diff = -1 - sums[0];
sums[0] += diff;
v += diff;
cnt += abs(diff);
} else {
diff = 1 - sums[0];
sums[0] += diff;
v += diff;
cnt += abs(diff);
}
}
if (sums[0] * sums[1] > 0) {
if (sums[0] >= 0 && sums[0] < sums[1]) {
diff = -1 - sums[0];
sums[0] += diff;
v += diff;
cnt += abs(diff);
} else if (sums[0] < 0 && sums[0] > sums[1]) {
diff = 1 - sums[0];
sums[0] += diff;
v += diff;
cnt += abs(diff);
}
}
for (int i = 1; i < n; i++) {
sums[i] += v;
if (sums[i - 1] * sums[i] >= 0) {
if (sums[i - 1] < 0) {
diff = 1 - sums[i];
} else {
diff = -1 - sums[i];
}
sums[i] += diff;
v += diff;
cnt += abs(diff);
}
}
cout << cnt << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
A = [int(x) for x in input().split()]
def solve(A, N):
S = [0]*N
answer = 0
total = 0
for i in range(N):
total += A[i]
S[i] = total
if total == 0:
d = 1 if S[i-1] < 0 else -1
A[i] += d
answer += 1
total += d
else:
# S[i-1] < 0 and S[i] > 0
# S[i-1] > 0 and S[i] < 0
# S[i-1] > 0 and S[i] > 0
if S[i-1] < 0 and S[i] < 0:
answer += -S[i]+1
total += -S[i]+1
elif S[i-1] > 0 and S[i] > 0:
answer += S[i]+1
total += -(S[i]+1)
S[i] = total
#print(i, answer, "S[i-1]=%d"%S[i-1], "S[i]=%d"%S[i], sep="\t")
return answer
if A[0] == 0:
A[0] = -1
m1 = solve(A, N) + 1
A[0] = +1
m2 = solve(A, N) + 1
print(min(m1, m2))
else:
print(solve(A, N))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
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 = 0;
int sign = 1;
int count = 0;
int ans = INT_MAX;
for (int j = 0; j < (2); ++j) {
if (j == 0)
sign = 1;
else
sign = -1;
sum = 0;
count = 0;
for (int i = 0; i < (n); ++i) {
sum += a[i];
if ((sign > 0) && (sum <= 0)) {
count += (1 - sum);
sum = 1;
} else if ((sign < 0) && (sum >= 0)) {
count += abs(-1 - sum);
sum = -1;
}
sign = sign == 1 ? -1 : 1;
}
if (ans > count) ans = count;
}
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()))
s = a[0]
count = 0
total_delta = 0
if a[0] == 0:
delta = -1 * (a[1] // abs(a[1]))
total_delta += delta
count += 1
for i in range(1, n):
sign = (s + total_delta) // abs(s + total_delta)
if (s + a[i] + total_delta) * sign > 0 :
delta = (sign * -1) - (s + a[i] + total_delta)
total_delta += delta
count += abs(delta)
elif (s + a[i] + total_delta) == 0:
total_delta += sign * -1
count += 1
s += a[i]
print(count)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll LINF = 1LL << 60;
const int INF = 1e9 + 7;
vector<vector<ll>> g(100010);
vector<ll> dist(100010);
int main() {
ll n;
cin >> n;
ll sum = 0;
ll score = 0;
for (ll i = 0; i < n; ++i) {
ll a;
cin >> a;
if (i == 0) {
sum += a;
continue;
}
if ((sum < 0 && sum + a <= 0) || (sum > 0 && sum + a >= 0)) {
score += 1 + abs(sum + a);
sum = -1 * (sum / sum);
} else {
sum += a;
}
}
cout << score << 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())
ai=input().split()
ai=[int(x) for x in ai]
Sn=[0]
op=0
for i in range(n-1):
Si=Sn[i]+ai[i]
Sip=Si+ai[i+1]
if(Si>0 and Sip>0):
if(Sip>=Si):
op+=Si+1
Si=-Si-1
elif(Sip<Si):
op+=Sip+1
ai[i + 1]+=-Sip-1
if(Si < 0 and Sip < 0):
if(-Sip>=-Si):
op += -Si + 1
Si = -Si + 1
elif(-Sip<-Si):
op += -Sip + 1
ai[i + 1]+=-Sip+1
if(Si==0):
Si+=1
op+=1
Sn.append(Si)
Si=Sn[n-1]+ai[n-1]
if(Si==0):
Si+=1
op+=1
print(op)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
int main() {
long N;
std::cin >> N;
long long a[N];
for (long i = 0; i < N; i++) std::cin >> a[i];
long long p = 0, n = 0;
long long sum = 0;
if (a[0] < 0) {
p += -a[0] + 1;
sum = 1;
} else {
sum += a[0];
}
for (long i = 1; i < N; i++) {
if (i % 2 == 0) {
if (sum + a[i] <= 0) {
p += -(sum + a[i]) + 1;
sum = 1;
} else
sum += a[i];
} else {
if (sum + a[i] >= 0) {
p += (sum + a[i]) + 1;
sum = -1;
} else {
sum += a[i];
}
}
std::cerr << "sum: " << sum << std::endl;
std::cerr << "p: " << p << std::endl;
}
sum = 0;
if (a[0] > 0) {
n += a[0] + 1;
sum = -1;
} else {
sum += a[0];
}
for (long i = 1; i < N; i++) {
if (i % 2 == 0) {
if (sum + a[i] >= 0) {
n += (sum + a[i]) + 1;
sum = -1;
} else
sum += a[i];
} else {
if (sum + a[i] <= 0) {
n += -(sum + a[i]) + 1;
sum = 1;
} else {
sum += a[i];
}
}
std::cerr << "sum: " << sum << std::endl;
std::cerr << "n: " << n << std::endl;
}
std::cout << std::min(p, n) << std::endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
A = [int(_) for _ in input().split()]
def counter(dp):
count = 0
for i in range(1, N):
is_positive = 2 * (dp > 0) - 1
dp += A[i]
if dp * is_positive >= 0:
count += abs(dp)+1
dp = -is_positive
return count
print(min(counter(A[0]), abs(A[0])+1+counter(A[0] <= 0)))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll ts = 1000000007;
ll sum, sum2, ans, i;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
ll n;
cin >> n;
vector<ll> a(n);
for (ll i = 0; i < n; i++) cin >> a[i];
bool can = false;
ll ans = 0, sum = a[0], nextSum = a[0];
if (a[0] == 0) {
ans = 1;
a[0] = 1;
}
for (int i = 1; i < n; i++) {
nextSum += a[i];
if (sum < 0 && nextSum < 0 || sum > 0 && nextSum > 0 || nextSum == 0) {
ll N;
if (nextSum >= 0) N = nextSum + 1;
if (nextSum < 0) N = nextSum - 1;
ans += abs(N);
if (a[0] >= 0 && i % 2 == 1 || a[0] <= 0 && i % 2 == 0)
nextSum = -1;
else
nextSum = 1;
sum = nextSum;
} else {
sum = nextSum;
}
}
cout << ans << "\n";
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
A = list(map(int, input().split()))
def sol(S):
ret = 0
for a in A[1:]:
b = a
if S * (S + b) > 0:
b = (abs(S) + 1) * (1 if S < 0 else -1)
if S + b == 0:
b = b - 1 if b < 0 else b + 1
ret += abs(b - a)
S += b
return ret
ans = min(
sol(A[0]),
(sol(0) + abs(A[0])) if A[0] < 0 else (sol(-1) + abs(A[0]) + 1)
)
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
bool ispositive(long long num) {
if (num > 0)
return true;
else if (num < 0)
return false;
}
int main() {
int n;
cin >> n;
long long sum = 0, k, ans = 0;
cin >> k;
sum = k;
bool f = (ispositive(k)) ? true : false;
for (int i = 0; i < n - 1; i++) {
cin >> k;
sum += k;
if (sum == 0 && f) {
sum--;
ans++;
} else if (sum == 0 && !f) {
sum++;
ans++;
} else if (f & ispositive(sum)) {
ans += (sum + 1);
sum = -1;
} else if (!f & !ispositive(sum)) {
ans += abs(sum) + 1;
sum = 1;
}
f = !f;
}
cout << ans << "\n";
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> a(n);
vector<long long> sum1(n), sum2(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
if (i == 0) {
sum1[i] = a[i];
} else {
sum1[i] = sum1[i - 1] + a[i];
}
sum2[i] = sum1[i];
}
long long count1 = 0, count2 = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
if (sum1[i] <= 0) {
long long add = 1 - sum1[i];
count1 += add;
for (int j = i + 1; j < n; j++) {
sum1[j] += add;
}
}
} else {
if (sum1[i] >= 0) {
long long add = sum1[i] + 1;
count1 += add;
for (int j = i + 1; j < n; j++) {
sum1[j] -= add;
}
}
}
}
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
if (sum2[i] >= 0) {
long long add = sum2[i] + 1;
count2 += add;
for (int j = i + 1; j < n; j++) {
sum2[j] -= add;
}
}
} else {
if (sum2[i] <= 0) {
long long add = 1 - sum2[i];
count2 += add;
for (int j = i + 1; j < n; j++) {
sum2[j] += add;
}
}
}
}
long long ans = min(count1, count2);
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;
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
int sum = 0;
for (long long i = 0; i < n; i++) {
int x;
cin >> x;
sum += x;
a[i] = sum;
}
int f = a[0] / abs(a[0]);
long long int ans = 0;
long long int fix = 0;
for (long long i = 0; i < n; i++) {
if (f == 1) {
if (a[i] + fix <= 0) {
ans += 1 - (fix + a[i]);
fix += 1 - (fix + a[i]);
}
f = -1;
} else {
if (a[i] + fix >= 0) {
ans += (fix + a[i]) + 1;
fix -= ((fix + a[i]) + 1);
}
f = 1;
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long func(long long a[], int n) {
long long cnt = 0;
long long s = 0;
for (int i = 1; i < n; i++) {
s += a[i - 1];
long long t = 0, u;
if (s > 0) {
u = (-1) * s - 1;
if (u < a[i]) {
t = a[i] - u;
a[i] = u;
}
} else {
u = (-1) * s + 1;
if (u > a[i]) {
t = u - a[i];
a[i] = u;
}
}
cnt += t;
}
return cnt;
}
int main() {
int n;
cin >> n;
long long a[n];
for (int i = 0; i < (n); i++) cin >> a[i];
long long cnt1 = func(a, n);
int d;
if (a[0] > 0) {
d = a[0] + 1;
a[0] = -1;
} else {
d = (-1) * a[0] + 1;
a[0] = 1;
}
long long cnt2 = d + func(a, n);
cout << min(cnt1, cnt2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
template <class T>
bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class T>
bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
using namespace std;
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, ll>;
const int inf = 1e9;
const ll inf_l = 1e18;
const int MAX = 2 * 1e5;
const int mod = 1e9 + 7;
int main() {
int n;
cin >> n;
vint a(n);
for (int i = 0; i < (int)(n); i++) cin >> a[i];
ll accum = a[0];
ll ans = 0;
for (int i = 1; i <= n - 1; i++) {
ll tmp = accum + a[i];
ll value = a[i];
if (accum * tmp >= 0) {
if (accum > 0) a[i] = -accum - 1;
if (accum < 0) a[i] = -accum + 1;
}
ans += abs(value - a[i]);
accum += 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 | java | import java.util.*;
// ABC 6-C
// http://abc006.contest.atcoder.jp/tasks/abc006_3
public class Main {
public static void main (String[] args) throws java.lang.Exception {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = in.nextInt();
}
long answer = 0;
if (nums[0] == 0) {
answer = solve(nums, 0, 0);
} else {
answer = solve(nums, nums[0], 1);
}
System.out.println(answer);
//
// long sum = 0;
// long answer = 0;
//
// for (int i = 0; i < n; i++) {
// int a = in.nextInt();
//
// if (sum < 0 && sum + a < 0) {
// answer += 1 + Math.abs(sum + a);
// sum = 1;
// } else if (sum > 0 && sum + a > 0) {
// answer += 1 + sum + a;
// sum = -1;
// } else if (sum + a == 0) {
// answer++;
// if (sum < 0) {
// sum = 1;
// } else {
// sum = -1;
// }
// } else {
// sum += a;
// }
// }
// System.out.println(answer);
}
public static long solve(int[] nums, long sum, int index) {
if (index == nums.length) {
return 0;
}
if (sum < 0 && sum + nums[index] < 0) {
return 1 + Math.abs(sum + nums[index]) + solve(nums, 1, index + 1);
} else if (sum > 0 && sum + nums[index] > 0) {
return 1 + sum + nums[index] + solve(nums, -1, index + 1);
} else if (sum == 0) {
return 1 + Math.min(solve(nums, 1, index + 1), solve(nums, -1, index + 1));
} else if (sum + nums[index] == 0) {
if (sum > 0) {
return 1 + solve(nums, -1, index + 1);
} else {
return 1 + solve(nums, 1, index + 1);
}
} else {
return solve(nums, sum + nums[index], index + 1);
}
//
// else if (sum < 0 && sum + nums[index] > 0) {
// return solve(nums, sum + nums[index], index + 1);
// } else if (sum > 0 && sum + nums[index] < 0) {
// return solve(nums, sum + nums[index], index + 1);
// } else {
// // sum == 0 or sum + nums[index] == 0
// if (sum == 0) {
// return 1 + Math.min(solve(nums, 1, index + 1), solve(nums, -1, index + 1));
// }
// // sum + nums[index] == 0
// else {
// if (sum < 0) {
// return Math.abs(sum) + 1 + solve(nums, 1, index + 1);
// } else {
// return Math.abs(sum) + 1 + solve(nums, -1, index + 1);
// }
// }
// }
// else if (sum + nums[index] == 0) {
// if (sum < 0) {
// return Math.abs(sum) + 1 + solve(nums, 1, index + 1);
// } else if (sum > 0) {
// return Math.abs(sum) + 1 + solve(nums, -1, index + 1);
// } else {
// return 1 + Math.min(solve(nums, 1, index + 1), solve(nums, -1, index + 1));
// }
// } else if (sum == 0) {
// return 1 + Math.min(solve(nums, 1, index + 1), solve(nums, -1, index + 1));
// } else {
// return solve(nums, sum + nums[index], index + 1);
// }
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long INF = 300000000;
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;
int su = 0;
for (int j = 0; j < n; ++j) {
su += a[j];
if (i == 0) {
if (j % 2 == 0 && su <= 0) {
count += abs(-su + 1);
su = 1;
} else if (j % 2 == 1 && su >= 0) {
count += abs(-su - 1);
su = 1;
}
}
if (i == 1) {
if (j % 2 == 0 && su >= 0) {
count += abs(-su + 1);
su = 1;
} else if (j % 2 == 1 && su <= 0) {
count += abs(-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;
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
int sum = 0, res = 0, prevsum = 0;
for (int i = 0; i < n; ++i) {
sum += a[i];
if (i == 0) {
if (n == 1) {
if (sum != 0)
cout << 0;
else
cout << 1;
return 0;
} else if (sum != 0) {
prevsum = sum;
continue;
}
if (a[i + 1] > 0) {
res += abs(a[i + 1]) + 1;
prevsum = -(abs(a[i + 1]) + 1);
sum = -(abs(a[i + 1]) + 1);
} else if (a[i + 1] < 0) {
res += abs(a[i + 1]) + 1;
prevsum = (abs(a[i + 1]) + 1);
sum = (abs(a[i + 1]) + 1);
} else {
++res;
}
}
if (sum > 0 && prevsum < 0 || sum < 0 && prevsum > 0) {
prevsum += a[i];
continue;
} else {
if (sum == 0) {
if (prevsum < 0) {
++res;
++sum;
prevsum = sum;
continue;
} else if (prevsum > 0) {
--res;
--sum;
prevsum = sum;
continue;
}
} else if (sum > 0) {
res += abs(a[i]) + 1 + abs(prevsum);
sum = -1;
prevsum = -1;
continue;
} else {
res += abs(prevsum) - abs(a[i]) + 1;
sum = 1;
prevsum = 1;
}
}
}
cout << res;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
int main(void) {
long long n, now = 0, ans = 0;
bool flag;
scanf("%ld", &n);
long long a[n];
scanf("%ld", &a[0]);
now = a[0];
if (now < 0) {
flag = false;
} else {
flag = true;
}
for (long long i = 1; i < n; i++) {
scanf("%ld", &a[i]);
now += a[i];
printf("%ld\n", now);
if (flag) {
if (now >= 0) {
ans += (-1 - now) * (-1);
now = -1;
}
flag = false;
} else {
if (now <= 0) {
ans += 1 - now;
now = 1;
}
flag = true;
}
}
printf("%ld", 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 | java | import java.util.*;
// ABC 6-C
// http://abc006.contest.atcoder.jp/tasks/abc006_3
public class Main {
public static void main (String[] args) throws java.lang.Exception {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = in.nextInt();
}
long answer = 0;
if (nums[0] == 0) {
answer = solve(nums, 0, 0);
} else {
answer = solve(nums, nums[0], 1);
}
System.out.println(answer);
//
// long sum = 0;
// long answer = 0;
//
// for (int i = 0; i < n; i++) {
// int a = in.nextInt();
//
// if (sum < 0 && sum + a < 0) {
// answer += 1 + Math.abs(sum + a);
// sum = 1;
// } else if (sum > 0 && sum + a > 0) {
// answer += 1 + sum + a;
// sum = -1;
// } else if (sum + a == 0) {
// answer++;
// if (sum < 0) {
// sum = 1;
// } else {
// sum = -1;
// }
// } else {
// sum += a;
// }
// }
// System.out.println(answer);
}
public static long solve(int[] nums, long sum, int index) {
if (index == nums.length) {
return 0;
}
if (sum < 0 && sum + nums[index] < 0) {
return 1 + Math.abs(sum + nums[index]) + solve(nums, 1, index + 1);
} else if (sum > 0 && sum + nums[index] > 0) {
return 1 + sum + nums[index] + solve(nums, -1, index + 1);
} else if (sum + nums[index] == 0) {
if (sum < 0) {
return Math.abs(sum) + 1 + solve(nums, 1, index + 1);
} else if (sum > 0) {
return Math.abs(sum) + 1 + solve(nums, -1, index + 1);
} else {
return 1 + Math.min(solve(nums, 1, index + 1), solve(nums, -1, index + 1));
}
} else if (sum == 0) {
return 1 + Math.min(solve(nums, 1, index + 1), solve(nums, -1, index + 1));
} else {
return solve(nums, sum + nums[index], index + 1);
}
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n;
long long func(vector<long long int>& s, vector<int>& hugo, int k) {
long long int ret = 0;
for (int i = 1; i < n; i++) {
if (s[i] == k) {
if (hugo[i - 1] == 0) {
hugo[i] = 1;
ret++;
k--;
} else {
hugo[i] = 0;
ret++;
k++;
}
} else if (s[i] > k) {
if (hugo[i - 1] == 0) {
hugo[i] = 1;
ret += s[i] - k + 1;
k += s[i] - k + 1;
} else {
hugo[i] = 0;
}
} else {
if (hugo[i - 1] == 0) {
hugo[i] = 1;
} else {
hugo[i] = 0;
ret += k - s[i] + 1;
k -= k - s[i] + 1;
}
}
}
return ret;
}
void solve() {
cin >> n;
vector<long long int> v(n), sum(n), sum2(n);
for (int i = 0; i < n; i++) {
cin >> v[i];
if (i == 0)
sum[i] = v[i];
else
sum[i] = sum[i - 1] + v[i];
}
vector<int> hugo(n);
sum2 = sum;
long long int ans = 0;
if (sum[0] == 0) {
vector<int> hugo2(n);
hugo[0] = 0;
ans = min(func(sum, hugo, -1), func(sum2, hugo2, 1));
} else if (sum[0] > 0) {
hugo[0] = 0;
ans = func(sum, hugo, 0);
} else {
hugo[0] = 1;
ans = func(sum, hugo, 0);
}
cout << ans << endl;
return;
}
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 | python3 | n = int(input())
a = [int(x) for x in input().split()]
temp = 0
count1 = 0
count2 = 0
if a[0] == 0:
a[0] = 1
count1 = 1
sum = a[0]
for i in range(1, n):
if abs(a[i]) <= abs(sum) or a[i] * sum >= 0:
if sum > 0:
temp = -1 * abs(sum) - 1
count1 += abs(temp - a[i])
else:
temp = abs(sum) + 1
count1 += abs(temp - a[i])
a[i] = temp
sum += a[i]
count2 = abs(a[0]) + 1
if a[0] > 0:
a[0] = -1
else:
a[0] = 1
sum = a[0]
for i in range(1, n):
if abs(a[i]) <= abs(sum) or a[i] * sum >= 0:
count2 += abs(sum - a[i]) + 1
if sum > 0:
temp = -1 * abs(sum) - 1
count2 += abs(temp - a[i])
else:
temp = abs(sum) + 1
count2 += abs(temp - a[i])
a[i] = temp
sum += a[i]
print(min(count1, count2))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int N, A[100000];
int main() {
cin >> N;
for (int i = 0; i < N; i++) {
cin >> A[i];
}
long long prev = A[0], sum = A[0], cnt = 0;
for (int i = 1; i < N; i++) {
sum += A[i];
if (sum == 0) {
cnt++;
if (prev > 0) {
sum = -1;
} else {
sum = 1;
}
} else if (sum > 0 && prev > 0) {
cnt += sum + 1;
sum = -1;
} else if (sum < 0 && prev < 0) {
cnt += abs(sum) + 1;
sum = 1;
}
prev = sum;
}
cout << cnt << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
long long cur1 = 0, cur2 = 0;
int a1 = 0, a2 = 0;
for (int i = 0; i < n; ++i) {
cur1 += a[i];
cur2 += a[i];
if (i % 2 == 0) {
if (cur1 >= 0) {
a1 += cur1 + 1;
cur1 = -1;
}
if (cur2 <= 0) {
a2 += -cur2 + 1;
cur2 = 1;
}
} else {
if (cur1 <= 0) {
a1 += -cur1 + 1;
cur1 = 1;
}
if (cur2 >= 0) {
a2 += cur2 + 1;
cur2 = -1;
}
}
}
cout << min(a1, a2);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < (int)(n); i++) {
cin >> a.at(i);
}
int nzp = 0;
for (int i = 0; i < (int)(n); i++) {
if (a.at(i) != 0) {
nzp = i;
break;
}
if (i == n - 1) {
cout << a.size() * 2 - 1 << endl;
return 0;
}
}
if (a.at(nzp) < 0) {
for (int i = (nzp); i < (int)(n); i++) {
a.at(i) *= -1;
}
}
if (nzp == n - 1) {
cout << 2 * nzp - 1 << endl;
return 0;
}
int cnt = 0;
int sum = 0;
if (a.at(nzp) + a.at(nzp + 1) > 0) {
if (a.at(nzp) <= a.at(nzp + 1)) {
cnt = max(2 * nzp - 1, 0) + a.at(nzp) + 2;
sum = -1;
}
} else {
cnt = max(2 * nzp - 1, 0);
if (a.at(nzp) == 1 && nzp > 0) {
cnt++;
a.at(nzp)++;
}
if (nzp > 0) {
sum = a.at(nzp) - 1;
} else {
sum = a.at(nzp);
}
}
for (int i = (nzp + 1); i < (int)(n); i++) {
if (sum > 0) {
if (sum + a.at(i) >= 0) {
cnt += sum + a.at(i) + 1;
sum = -1;
} else {
sum += a.at(i);
}
} else {
if (sum + a.at(i) <= 0) {
cnt += -1 * (sum + a.at(i)) + 1;
sum = 1;
} else {
sum += a.at(i);
}
}
}
cout << cnt << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n;
int sum = 0;
int ans1 = 0;
int ans2 = 0;
int i, j;
int t = 1;
int main() {
cin >> n;
int a[100010];
for (i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
if (sum * t <= 0) {
ans1 += abs(sum - t);
sum = t;
}
t *= -1;
}
t = -1;
sum = 0;
for (i = 0; i < n; i++) {
sum += a[i];
if (sum * t <= 0) {
ans2 += abs(sum - t);
sum = t;
}
t *= -1;
}
printf("%d\n", min(ans1, ans2));
cin >> i;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = input().split()
a = [int(m) for m in a]
q = []
k = 0
kco = 0
#+-
for i in range(n):
if i == 0:
if a[i] > 0:
q.append(a[i])
else:
q.append(1)
kco += 1 - a[i]
k += q[i]
else:
k += a[i]
if i % 2 != 0:
if k < 0:
q.append(a[i])
else:
q.append(a[i]-k-1)
kco += k + 1
k += q[i] - a[i]
if i % 2 == 0:
if k > 0:
q.append(a[i])
else:
q.append(a[i]-k+1)
kco += -k + 1
k += q[i] - a[i]
xco = kco
q = []
k = 0
kco = 0
#-+
for i in range(n):
if i == 0:
if a[i] < 0:
q.append(a[i])
else:
q.append(-1)
kco += 1 - a[i]
k += q[i]
else:
k += a[i]
if i % 2 == 0:
if k < 0:
q.append(a[i])
else:
q.append(a[i]-k-1)
kco += k + 1
k += q[i] - a[i]
if i % 2 != 0:
if k > 0:
q.append(a[i])
else:
q.append(a[i]-k+1)
kco += -k + 1
k += q[i] - a[i]
yco = kco
print(min(xco, yco)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void) {
int64_t n;
int64_t i;
int64_t sum;
bool default_flag;
bool plus_flag, minus_flag;
int64_t ope_count;
cin >> n;
vector<int64_t> a(n);
for (i = 0; i < n; i++) {
cin >> a.at(i);
}
sum = 0;
ope_count = 0;
default_flag = true;
plus_flag = false;
minus_flag = false;
for (i = 0; i < n; i++) {
sum += a.at(i);
if (default_flag == true) {
default_flag = false;
if (a.at(i) > a.at(i + 1)) {
while (sum >= 0) {
ope_count++;
sum--;
}
minus_flag = true;
} else if (a.at(i) <= a.at(i + 1)) {
while (sum <= 0) {
ope_count++;
sum++;
}
plus_flag = true;
}
} else if (plus_flag == true) {
while (sum >= 0) {
ope_count++;
sum--;
}
plus_flag = false;
minus_flag = true;
} else if (minus_flag == true) {
while (sum <= 0) {
ope_count++;
sum++;
}
plus_flag = true;
minus_flag = false;
}
}
cout << ope_count << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
vector<long long> a;
for (long long i = 0; i < n; i++) {
long long ai;
cin >> ai;
a.push_back(ai);
}
long long count = 0;
if (a.at(0) == 0) {
a.at(0) = 1;
count = 1;
}
long long sum = a.at(0);
for (long long i = 0; i < n - 1; i++) {
long long next_sum = sum + a.at(i + 1);
if (sum > 0 && next_sum >= 0) {
long long diff = 1 + next_sum;
count += diff;
a.at(i + 1) -= diff;
next_sum -= diff;
} else if (sum < 0 && next_sum <= 0) {
long long diff = 1 - next_sum;
count += diff;
a.at(i + 1) += diff;
next_sum += diff;
}
sum = next_sum;
}
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;
int64_t sum_from1tok(vector<int> vec, int k) {
int64_t sum = 0;
for (int i = 0; i < (int)(k); i++) {
sum += vec.at(i);
}
return sum;
}
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < (int)(n); i++) {
int num;
cin >> num;
a.at(i) = num;
}
int afirst = a.at(0);
int seicount = 0;
int64_t seisum = 0;
if (a.at(0) > 0) {
seisum = a.at(0);
for (int i = 1; i < n; i++) {
seisum += a.at(i);
if (i % 2 != 0) {
if (seisum >= 0) {
seicount += abs(seisum) + 1;
seisum = -1;
}
} else {
if (seisum <= 0) {
seicount += abs(seisum) + 1;
seisum = 1;
}
}
}
} else if (a.at(0) < 0) {
seicount = abs(a.at(0)) + 1;
a.at(0) = 1;
seisum = 1;
for (int i = 1; i < n; i++) {
seisum += a.at(i);
if (i % 2 != 0) {
if (seisum >= 0) {
seicount += abs(seisum) + 1;
seisum = -1;
}
} else {
if (seisum <= 0) {
seicount += abs(seisum) + 1;
seisum = 1;
}
}
}
} else {
seisum = 1;
a.at(0) = 1;
seicount = 1;
for (int i = 1; i < n; i++) {
seisum += a.at(i);
if (i % 2 != 0) {
if (seisum >= 0) {
seicount += abs(seisum) + 1;
seisum = -1;
}
} else {
if (seisum <= 0) {
seicount += abs(seisum) + 1;
seisum = 1;
}
}
}
}
a.at(0) = afirst;
int fucount = 0;
int64_t fusum = 0;
if (a.at(0) > 0) {
fucount = abs(a.at(0)) + 1;
a.at(0) = -1;
fusum = -1;
for (int i = 1; i < n; i++) {
fusum += a.at(i);
if (i % 2 != 0) {
if (fusum <= 0) {
fucount += abs(fusum) + 1;
fusum = 1;
}
} else {
if (fusum >= 0) {
fucount += abs(fusum) + 1;
fusum = -1;
}
}
}
} else if (a.at(0) < 0) {
for (int i = 1; i < n; i++) {
fusum += a.at(i);
if (i % 2 != 0) {
if (fusum <= 0) {
fucount += abs(fusum) + 1;
fusum = 1;
}
} else {
if (fusum >= 0) {
fucount += abs(fusum) + 1;
fusum = -1;
}
}
}
} else {
fusum = -1;
a.at(0) = -1;
fucount = 1;
for (int i = 1; i < n; i++) {
fusum += a.at(i);
if (i % 2 != 0) {
if (fusum <= 0) {
fucount += abs(fusum) + 1;
fusum = 1;
}
} else {
if (fusum >= 0) {
fucount += abs(fusum) + 1;
fusum = -1;
}
}
}
}
cout << min(seicount, fucount) << 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;
int main() {
ll acc;
ll N;
vector<ll> A;
cin >> N;
for (ll i = 0; i < N; i++) {
ll tmp;
cin >> tmp;
A.push_back(tmp);
}
ll ans_pos = 0;
if (A[0] <= 0) {
acc = 1;
ans_pos += abs(A[0]) + 1;
} else {
acc = A[0];
}
for (ll i = 1; i < N; i++) {
ll next = acc + A[i];
if (acc * next >= 0) {
ans_pos += abs(next) + 1;
next = -1 * (acc / abs(acc));
}
acc = next;
}
ll ans_neg = 0;
acc = A[0];
if (acc >= 0) {
acc = 1;
ans_neg += abs(A[0]) + 1;
} else {
acc = A[0];
}
for (ll i = 1; i < N; i++) {
ll next = acc + A[i];
if (acc * next >= 0) {
ans_neg += abs(next) + 1;
next = -1 * (acc / abs(acc));
}
acc = next;
}
ll ans = min(ans_pos, ans_neg);
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int Max = 1e5 + 5;
int lst[Max];
int main() {
long long n;
cin >> n;
long long res = 0, sum = 0;
for (int i = 1; i <= n; i++) scanf("%d", &lst[i]);
if (lst[2] >= 0 && lst[1] == 0) {
sum = -1;
res++;
} else if (lst[1] == 0 && lst[2] < 0) {
sum = 1;
res++;
} else {
sum = lst[1];
}
if (sum > 0) {
for (int i = 2; i <= n; i++) {
sum += lst[i];
if (i % 2 == 1 && sum <= 0) {
res += (1 - sum);
sum = 1;
} else if (i % 2 == 0 && sum >= 0) {
res += (sum + 1);
sum = -1;
}
}
} else {
for (int i = 2; i <= n; i++) {
sum += lst[i];
if (i % 2 == 0 && sum <= 0) {
res += (1 - sum);
sum = 1;
} else if (i % 2 == 1 && sum >= 0) {
res += (sum + 1);
sum = -1;
}
}
}
cout << res;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | # c_WA2.py よりはWAが減った
n = int(input())
a = [int(a) for a in input().split()]
times = 0
sum_prefix = a[0]
for i in range(1, n):
if sum_prefix < 0:
if sum_prefix + a[i] > 0:
sum_prefix += a[i]
else:
times += 1 - (sum_prefix + a[i])
sum_prefix = 1
elif sum_prefix > 0:
if sum_prefix + a[i] < 0:
sum_prefix += a[i]
else:
times += abs(-1 - (sum_prefix + a[i]))
sum_prefix = -1
times2 = 0
if a[0] > 0:
times2 += abs(-1 - a[0])
sum_prefix = -1
else:
times2 += 1 - a[0]
sum_prefix = 1
for i in range(1, n):
if sum_prefix < 0:
if sum_prefix + a[i] > 0:
sum_prefix = sum_prefix + a[i]
else:
times2 += 1 - (sum_prefix + a[i])
sum_prefix = 1
elif sum_prefix > 0:
if sum_prefix + a[i] < 0:
sum_prefix = sum_prefix + a[i]
else:
times2 += abs(-1 - (sum_prefix + a[i]))
sum_prefix = -1
print(min(times, times2))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 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];
long long s1 = 0, s2 = 0, r1 = 0, r2 = 0, ans;
for (int i = 0; i < n; i++) {
s1 += a[i];
if ((i & 1) && s1 <= 0) {
r1 = r1 + 1 - s1;
s1 = 1;
} else if (!(i & 1) && s1 >= 0) {
r1 = r1 + s1 + 1;
s1 = -1;
}
s2 += a[i];
if (!(i & 1) && s1 <= 0) {
r2 = r2 + 1 - s2;
s2 = 1;
} else if ((i & 1) && s1 >= 0) {
r2 = r2 + s2 + 1;
s2 = -1;
}
}
ans = min(r1, r2);
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.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
List<Integer> alist = new ArrayList<>();
for (int i = 0; i < n; i++) {
alist.add(sc.nextInt());
}
int cntOdd = 0;
int cntEvn = 0;
int sum = 0;
for (int i = 0; i < alist.size(); i++) {
sum += alist.get(i);
//iが偶数のとき正
if(i%2 == 0) {
if(sum > 0) {
continue;
} else {
while(sum <= 0) {
int calc = 1;
sum += calc;
cntEvn++;
}
}
} else {
if(sum < 0) {
continue;
} else {
while(sum >= 0) {
int calc = -1;
sum += calc;
cntEvn++;
}
}
}
}
sum =0;
for (int i = 0; i < alist.size(); i++) {
sum += alist.get(i);
//iが偶数のとき負
if (i%2 == 0) {
if(sum < 0) {
continue;
} else {
while(sum >= 0) {
int calc = -1;
sum += calc;
cntOdd++;
}
}
} else {
if(sum > 0) {
continue;
} else {
while(sum <= 0) {
int calc = 1;
sum += calc;
cntOdd++;
}
}
}
}
if(cntOdd <= cntEvn) {
System.out.println(cntOdd);
} else {
System.out.println(cntEvn);
}
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 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>
bool chmax(T& a, const T& b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
bool chmin(T& a, const T& b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
template <typename T1, typename T2>
pair<T1, T2> operator+(const pair<T1, T2>& l, const pair<T1, T2>& r) {
return make_pair(l.first + r.first, l.second + r.second);
}
template <typename T1, typename T2>
pair<T1, T2> operator-(const pair<T1, T2>& l, const pair<T1, T2>& r) {
return make_pair(l.first - r.first, l.second - r.second);
}
const long long int MOD = 1e9 + 7, INF = 1e18;
long long int N, arr[100000], sums[100000];
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
cin >> N;
for (long long int i = (0), i_end_ = (N); i < i_end_; i++) {
cin >> arr[i];
}
bool flag;
long long int sum = 0;
long long int ans = 0;
sums[0] = arr[0];
for (long long int i = (0), i_end_ = (N - 1); i < i_end_; i++) {
sums[i + 1] = arr[i + 1] + sums[i];
}
if (sums[0] > 0)
flag = true;
else
flag = false;
for (long long int i = (0), i_end_ = (N - 1); i < i_end_; i++) {
sums[i + 1] += sum;
if (flag ^ ((i % 2) == 1)) {
if (sums[i + 1] >= 0) {
sum -= (sums[i + 1] + 1);
ans += abs(sums[i + 1] + 1);
sums[i + 1] -= (sums[i + 1] + 1);
}
} else {
if (sums[i + 1] <= 0) {
sum -= (sums[i + 1] - 1);
ans += abs(sums[i + 1] - 1);
sums[i + 1] -= (sums[i + 1] - 1);
}
}
}
long long int tmp = ans;
sum = 0;
ans = 0;
sums[0] = arr[0];
for (long long int i = (0), i_end_ = (N - 1); i < i_end_; i++) {
sums[i + 1] = arr[i + 1] + sums[i];
}
if (sums[0] >= 0)
flag = true;
else
flag = false;
for (long long int i = (0), i_end_ = (N - 1); i < i_end_; i++) {
sums[i + 1] += sum;
if (flag ^ ((i % 2) == 1)) {
if (sums[i + 1] >= 0) {
sum -= (sums[i + 1] + 1);
ans += abs(sums[i + 1] + 1);
sums[i + 1] -= (sums[i + 1] + 1);
}
} else {
if (sums[i + 1] <= 0) {
sum -= (sums[i + 1] - 1);
ans += abs(sums[i + 1] - 1);
sums[i + 1] -= (sums[i + 1] - 1);
}
}
}
cout << min(tmp, 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 | #include <bits/stdc++.h>
long long calc(long long *A, long long N, long long start) {
long long s = A[0];
long long count = 0;
if (s == 0) {
count++;
s = start;
}
for (long long i = 1; i < N; i++) {
s += A[i];
if (i % 2 == (start == 1 ? 0 : 1)) {
if (s <= 0) {
count += 1 - s;
s = 1;
}
} else {
if (s >= 0) {
count += s + 1;
s = -1;
}
}
}
return count;
}
int main() {
long long N;
scanf("%lld", &N);
long long *A = (long long *)malloc(N * sizeof(long long));
long long n;
long long i = 0;
while (scanf("%lld", &n) != EOF) {
A[i] = n;
i++;
}
long long can1 = calc(A, N, 1);
long long can2 = calc(A, N, -1);
printf("%lld", ((can1) < (can2) ? (can1) : (can2)));
free(A);
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<signed long long> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
signed long long ans = 0;
if (a[0] == 0) {
for (int i = 0; i < n; ++i) {
if (a[i] != 0) {
if ((a[i] > 0 && i % 2 == 0) || (a[i] < 0 && i % 2 == 1)) {
++ans;
a[0] = 1;
break;
} else {
++ans;
a[0] = -1;
break;
}
} else {
if (i == n - 1) {
++ans;
a[0] = 1;
}
}
}
}
if (a[0] > 0) {
signed long long sum = a[0];
for (int i = 1; i < n; ++i) {
if (i % 2 == 1) {
if (sum + a[i] < 0) {
sum += a[i];
} else {
ans += sum + a[i] + 1;
sum = -1;
}
} else {
if (sum + a[i] > 0) {
sum += a[i];
} else {
ans += abs(sum + a[i] - 1);
sum = 1;
}
}
}
} else {
signed long long sum = a[0];
for (int i = 1; i < n; ++i) {
if (i % 2 == 1) {
if (sum + a[i] > 0) {
sum += a[i];
} else {
ans += abs(sum + a[i] - 1);
sum = 1;
}
} else {
if (sum + a[i] < 0) {
sum += a[i];
} else {
ans += sum + a[i] + 1;
sum = -1;
}
}
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> vector;
long long temp;
for (int i = 0; i < n; i++) {
cin >> temp;
vector.push_back(temp);
}
long long answer = 0;
long long sum = 0;
for (int i = 0; i < n; i++) {
if (sum == 0)
sum += vector[i];
else if (sum < 0) {
if (sum + vector[i] > 0) {
sum += vector[i];
} else {
answer += abs((-1) * sum + 1 - vector[i]);
sum = 1;
}
} else {
if (sum + vector[i] < 0) {
sum += vector[i];
} else {
answer += abs((-1) * sum - 1 - vector[i]);
sum = -1;
}
}
}
cout << answer << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int sum(int i, vector<int> &a) {
if (i == 0) return a[0];
return a[i] + sum(i - 1, a);
}
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int c = 0;
for (int i = 0; i < n - 1; i++) {
if (sum(i, a) >= 0) {
if (sum(i + 1, a) >= 0) {
c += abs(-1 - sum(i, a) - a[i + 1]);
a[i + 1] = -1 - sum(i, a);
}
}
if (sum(i, a) < 0) {
if (sum(i + 1, a) <= 0) {
c += abs(1 - sum(i, a) - a[i + 1]);
a[i + 1] = 1 - sum(i, a);
}
}
}
cout << c << 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 itertools
from collections import Counter
from collections import defaultdict
import bisect
from heapq import heappush, heappop
def main():
n = int(input())
a = list(map(int, input().split()))
cumulative = 0
ans = 0
for v in a:
if cumulative == 0: # first time
cumulative += v
else:
if cumulative > 0:
if cumulative + v >= 0:
ans += abs(cumulative + v) + 1
cumulative += v - (abs(cumulative + v) + 1)
else:
cumulative += v
else:
if cumulative + v <= 0:
ans += abs(cumulative + v) + 1
cumulative += v + (abs(cumulative + v) + 1)
else:
cumulative += v
print(ans)
if __name__ == '__main__':
main()
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAX_N = 1e5 + 10;
int A[MAX_N];
int N;
ll cnt_0(bool flg) {
ll res = 1, sm = 0;
if (flg)
sm += 1;
else
sm -= 1;
for (int i = 1; i < N; ++i) {
ll tmp = sm + (ll)A[i];
if (sm > 0) {
if (tmp >= 0) {
res += tmp + 1;
sm = -1;
} else
sm = tmp;
} else if (sm < 0) {
if (tmp <= 0) {
res += -tmp + 1;
sm = 1;
} else
sm = tmp;
}
}
return res;
}
ll cnt() {
bool flg = A[0] > 0;
ll res = 0, sm = A[0];
for (int i = 1; i < N; ++i) {
ll tmp = sm + (ll)A[i];
if (sm > 0) {
if (tmp >= 0) {
res += tmp + 1;
sm = -1;
} else
sm = tmp;
} else if (sm < 0) {
if (tmp <= 0) {
res += -tmp + 1;
sm = 1;
} else
sm = tmp;
}
}
return res;
}
int main() {
scanf("%d", &N);
for (int i = 0; i < N; ++i) scanf("%d", &A[i]);
ll res;
if (A[0] == 0)
res = min(cnt_0(true), cnt_0(false));
else
res = cnt();
printf("%lld\n", res);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < (int)(n); i++) cin >> a[i];
vector<long long> cusum(n);
cusum[0] = a[0];
for (int i = 1; i < n; i++) {
cusum[i] = cusum[i - 1] + a[i];
}
int tc = 2;
long long ans = 1e18;
while (tc--) {
long long sum = 0;
long long tmp = 0;
for (int i = 0; i < n; i++) {
long long x = cusum[i] + sum;
if (x >= 0) {
if ((tc && i % 2 == 0) || (tc == 0 && i % 2 == 1)) {
continue;
} else {
tmp += x + 1;
sum -= (x + 1);
}
} else {
if ((tc && i % 2 == 0) || (tc == 0 && i % 2 == 1)) {
tmp += ((-1) * x + 1);
sum += ((-1) * x + 1);
} else {
continue;
}
}
}
if (cusum[n - 1] + sum == 0) tmp++;
ans = min(ans, tmp);
}
cout << ans << '\n';
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
template <class T>
inline T chmax(T& a, const T b) {
return a = (a < b) ? b : a;
}
template <class T>
inline T chmin(T& a, const T b) {
return a = (a > b) ? b : a;
}
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const ll MOD = 1000000007;
const ll INF = 1e18;
const double PI = acos(-1);
using namespace std;
ll N;
ll solve(vector<ll> A) {
ll ret = 0;
ll sum = 0;
for (ll i = 0; i < ll(N - 1); ++i) A[i + 1] += A[i];
for (ll i = (1); i < (N); ++i) {
A[i] += sum;
if (A[i] * A[i - 1] >= 0) {
if (A[i] == 0) {
if (A[i - 1] < 0) {
sum++;
ret++;
A[i] += 1;
} else {
sum--;
ret++;
A[i] -= 1;
}
} else if (A[i] > 0) {
sum -= A[i] + 1;
ret += A[i] + 1;
A[i] = -1;
} else {
sum += -A[i] + 1;
ret += -A[i] + 1;
A[i] = 1;
}
}
}
return ret;
}
signed main() {
cin >> N;
vector<ll> A(N);
for (ll i = 0; i < ll(N); ++i) cin >> A[i];
ll ans = 0;
if (A[0] != 0)
ans = solve(A);
else {
A[0] = 1;
ans = solve(A) + 1;
A[0] = -1;
chmin(ans, solve(A) + 1);
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
constexpr long long inf = 1e9 + 7;
long long solve(vector<long long> As, bool flag) {
long long ans = 0;
for (auto A : As) {
if (flag && A <= 0)
ans += 1 - A;
else if (!flag && A >= 0)
ans += 1 + A;
flag = !flag;
}
return ans;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
long long N;
cin >> N;
vector<long long> A(N);
for (long long n = 0; n < N; n++) cin >> A[n];
cout << min(solve(A, false), solve(A, true)) << 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 = 1e9, MOD = 1e9 + 7;
const double EPS = 1e-9, PI = 3.141592653589793;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
long long n, a[100001], seq, ans = 0, tmp;
cin >> n;
for (long long i = 0; i < n; i++) cin >> a[i];
seq = a[0];
for (long long i = 1; i < n; i++) {
tmp = seq;
if (seq > 0) {
seq += a[i];
if (seq >= 0) {
seq = -1;
ans += abs(-1 - tmp - a[i]);
}
} else {
tmp = seq;
seq += a[i];
if (seq <= 0) {
seq = 1;
ans += (1 - tmp - a[i]);
}
}
}
cout << ans << "\n";
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n, a[100000];
long long cnt1, cnt2, sum;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) {
if (i % 2 == 0 && sum + a[i] <= 0) {
cnt1 += (-1) * sum + 1 - a[i];
sum = 1;
} else if (i % 2 != 0 && sum + a[i] >= 0) {
cnt1 += a[i] + sum + 1;
sum = -1;
} else
sum += a[i];
}
sum = 0;
for (int i = 0; i < n; i++) {
if (i % 2 != 0 && sum + a[i] <= 0) {
cnt2 += (-1) * sum + 1 - a[i];
sum = 1;
} else if (i % 2 != 0 && sum + a[i] >= 0) {
cnt2 += a[i] + sum + 1;
sum = -1;
} else
sum += a[i];
}
cout << min(cnt1, cnt2);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n=int(input())
a=list(map(int,input().split()))
sum=a[0]
if(sum==0):
opp=1
sum=1
for i in a[1:]:
if(sum*(sum+i)>=0):
opp+=abs(sum+i)+1
if(sum<0):sum=1
else:sum=-1
else:sum+=i
opm=1
sum=-1
for i in a[1:]:
if(sum*(sum+i)>=0):
opm+=abs(sum+i)+1
if(sum<0):sum=1
else:sum=-1
else:sum+=i
op=min(opm,opp)
else:
op=0
for i in a[1:]:
if(sum*(sum+i)>=0):
op+=abs(sum+i)+1
if(sum<0):sum=1
else:sum=-1
else:sum+=i
print(op) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
long long sum;
cin >> sum;
if (sum == 0) {
long long delta1 = 1;
sum = 1;
int ar[N];
for (int i = 1; i < N; ++i) {
cin >> ar[i];
int temp = ar[i];
if (sum > 0 && sum + temp > 0) {
sum = -1;
delta1 += sum + temp + 1;
} else if (sum < 0 && sum + temp < 0) {
sum = 1;
delta1 += 1 - (sum + temp);
} else {
sum += temp;
}
}
long long delta2 = 1;
sum = -1;
for (int i = 1; i < N; ++i) {
int temp = ar[i];
if (sum > 0 && sum + temp > 0) {
sum = -1;
delta2 += sum + temp + 1;
} else if (sum < 0 && sum + temp < 0) {
sum = 1;
delta2 += 1 - (sum + temp);
} else {
sum += temp;
}
}
if (delta1 < delta2)
cout << delta1;
else
cout << delta2;
} else {
long long delta = 0;
for (int i = 1; i < N; ++i) {
int temp;
cin >> temp;
if (sum > 0 && sum + temp >= 0) {
delta += sum + temp + 1;
sum = -1;
} else if (sum < 0 && sum + temp <= 0) {
delta += 1 - (sum + temp);
sum = 1;
} else {
sum += temp;
}
}
cout << delta;
}
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long N = lread();
auto A = aryread();
long ans = long.max;
{
auto B = A.dup;
long sum = B[0];
long cost;
foreach (i; 1 .. N)
{
if (0 < sum)
{
if (sum + B[i] < 0)
{
sum += B[i];
continue;
}
long x = -1 - B[i] - sum;
cost += abs(x);
B[i] += x;
sum += B[i];
}
else
{
if (0 < sum + B[i])
{
sum += B[i];
continue;
}
long x = 1 - B[i] - sum;
cost += abs(x);
B[i] += x;
sum += B[i];
}
}
// writeln(cost);
// writeln(B);
ans = ans.min(cost);
}
{
auto B = A.dup;
long cost;
if (0 < B[0])
{
cost += 1 + B[0];
B[0] -= 1 + B[0];
}
else
{
cost += abs(1 - B[0]);
B[0] += 1 - B[0];
}
long sum = B[0];
foreach (i; 1 .. N)
{
if (0 < sum)
{
if (sum + B[i] < 0)
{
sum += B[i];
continue;
}
long x = -1 - B[i] - sum;
cost += abs(x);
B[i] += x;
sum += B[i];
}
else
{
if (0 < sum + B[i])
{
sum += B[i];
continue;
}
long x = 1 - B[i] - sum;
cost += abs(x);
B[i] += x;
sum += B[i];
}
}
// writeln(cost);
// writeln(B);
ans = ans.min(cost);
}
writeln(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() {
long long int n;
cin >> n;
long long int sum, pre_sum;
long long int ans = 0;
sum = 0;
int zero_c = 0;
while (n > 0) {
long long int a;
cin >> a;
n--;
sum += a;
if (sum == 0) {
zero_c++;
} else {
break;
}
}
if (zero_c != 0) ans = 2 * zero_c - 1;
pre_sum = sum;
for (int i = 0; i < n; i++) {
long long int a;
cin >> a;
sum += a;
if (sum == 0) {
sum = (pre_sum > 0 ? -1 : 1);
ans++;
} else if ((sum > 0 && pre_sum > 0) || (sum < 0 && pre_sum < 0)) {
ans += abs(sum - (pre_sum > 0 ? -1 : 1));
sum = (pre_sum > 0 ? -1 : 1);
}
pre_sum = sum;
}
std::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;
long long int a[n], s[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
if (i == 0) {
s[0] = a[0];
} else {
s[i] = s[i - 1] + a[i];
}
}
long long int ans = 0, tmp;
for (int i = 0; i < n; i++) {
if (i == 0 && a[0] == 0) {
for (int j = 0; j < n; j++) {
s[j]++;
}
ans++;
}
if (i != 0) {
if (s[i] == 0) {
if (s[i - 1] < 0) {
for (int j = i; j < n; j++) {
s[j]++;
}
} else {
for (int j = i; j < n; j++) {
s[j]--;
}
}
ans++;
} else {
if (s[i - 1] > 0 == s[i] > 0) {
tmp = s[i];
if (s[i] < 0) {
for (int j = i; j < n; j++) {
s[j] += abs(tmp) + 1;
}
} else {
for (int j = i; j < n; j++) {
s[j] -= abs(tmp) + 1;
}
}
ans += abs(tmp) + 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];
long long cnt1 = 0, cnt2 = 0;
long long sum = a[0];
for (int i = 0; i < n; i++) {
if (i % 2 == 0 && sum < 0)
sum = 1;
else if (i % 2 == 1 && sum > 0)
sum = -1;
cnt1 += abs(sum) + 1;
}
for (int i = 0; i < n; i++) {
if (i % 2 == 0 && sum > 0)
sum = -1;
else if (i % 2 == 1 && sum < 0)
sum = 1;
cnt1 += abs(sum) + 1;
}
cout << min(cnt1, cnt2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
int main() {
long long n;
cin >> n;
long long c[2], s[2];
vector<long long> a(n);
for (int i = 0; i < (n); ++i) {
cin >> a[i];
for (int j : {0, 1}) {
s[j] += a[i];
auto p = 1 - (i + j) % 2 * 2;
if (s[j] * p <= 0) {
c[j] += abs(p - s[j]);
s[j] = p;
}
}
}
cout << min(c[0], c[1]) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | function Main(s) {
var s = s.split("\n");
var n = parseInt(s[0], 10);
var a = s[1].split(" ").map(e => parseInt(e, 10));
var acc1 = 0, cnt1 = 0, arr1 = [];
var acc2 = 0, cnt2 = 0, arr2 = [];
for (var i = 0; i < n; i++) {
acc1 += a[i];
if (i === 0) {
if (acc1 === 0) {
acc1++;
cnt1++;
}
} else {
if (arr1[i - 1] > 0) {
if (acc1 >= 0) {
cnt1 += (acc1 + 1);
acc1 -= (acc1 + 1);
}
} else {
if (acc1 <= 0) {
cnt1 += (Math.abs(acc1) + 1);
acc1 += (Math.abs(acc1) + 1);
}
}
}
arr1.push(acc1);
}
for (var i = 0; i < n; i++) {
acc2 += a[i];
if (i === 0) {
if (acc2 === 0) {
acc2--;
cnt2++;
}
} else {
if (arr2[i - 1] > 0) {
if (acc2 >= 0) {
cnt2 += (acc2 + 1);
acc2 -= (acc2 + 1);
}
} else {
if (acc2 <= 0) {
cnt2 += (Math.abs(acc2) + 1);
acc2 += (Math.abs(acc2) + 1);
}
}
}
arr2.push(acc2);
}
console.log(Math.min(cnt1, cnt2));
}
Main(require("fs").readFileSync("/dev/stdin", "utf8")); |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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.*;
import java.math.BigInteger;
// warm-up
public class Main {
static void solve() {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt(), t=n, i=0;
long[] a = new long[n];
BigInteger o = BigInteger.ZERO, s = BigInteger.ZERO;
while (t-->0) a[i++] = sc.nextLong();
for (i=0; i<n; i++) {
long k=a[i];
int p = s.compareTo(BigInteger.ZERO), q = s.add(new BigInteger(""+a[i])).compareTo(BigInteger.ZERO);
if (q==0)
a[i]=(p<0) ? BigInteger.ONE.subtract(s).longValue() : s.add(BigInteger.ONE).longValue();
else if ((p<0 && q<0)||(p>0 && q>0))
a[i]=(q<0) ? BigInteger.ONE.subtract(s).longValue() : BigInteger.ZERO.subtract(BigInteger.ONE).subtract(s).longValue();
o = o.add(new BigInteger(""+Math.abs(k-a[i])));
s = s.add(new BigInteger(""+a[i]));
}
System.out.println(o);
sc.close();
}
public static void main(String args[]) {
solve();
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <iostream>
#include <math.h>
using namespace std;
int a(long long int z) {
if (z > 0) return 1;
else if (z < 0) return -1;
else return 0;
}
int main(){
long long int n, sum = 0, in, ans = 0;
cin >> n >> sum;
for(int i = 1;i<n;i++){
cin >> in;
if(i == 1){
if(sum == 0){
sum = -1 * a(in);
ans++;
}
}
if(a(sum) * a(in) < 0 && abs(sum) < abs(in)){
sum += in;
continue;
}
else if(a(sum) * a(in) < 0){
ans += abs(sum+in) + 1;
sum = -1 * a(sum);
continue;
}
ans += abs(sum+in) + 1;
sum = 1 * a(sum);
}
}
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() {
freopen("testcase", "r", stdin);
int N, temp;
vector<int> a;
scanf("%d", &N);
int start = 0;
bool v = false;
for (int i = 0; i < N; i++) {
scanf("%d", &temp);
if (temp == 0) {
if (!v) {
start += 1;
}
} else if (!v)
v = true;
a.push_back(temp);
}
long long sum = 0, cnt = 0;
if (start != 0) {
cnt = 2 * (start - 1) + 1;
if (a[start] > 0) {
if (a[start] > 1) {
sum = a[start] - 1;
} else {
sum = 1;
cnt += 1;
}
} else {
if (a[start] < -1) {
sum = a[start] + 1;
} else {
sum = -1;
cnt += 1;
}
}
} else {
sum = a[start];
}
start++;
for (size_t i = start; i != a.size(); i++) {
if (sum + a[i] >= 0 && sum > 0) {
cnt += sum + a[i] + 1;
sum = -1;
} else if (sum + a[i] <= 0 && sum < 0) {
cnt += 1 - sum - a[i];
sum = 1;
} else {
sum += a[i];
}
}
if (sum == 0) cnt += 1;
printf("%lld\n", cnt);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long c1 = 0, s1 = 0;
long long c2 = 0, s2 = 0;
for (int i = 0; i < n; i++) {
s1 += a[i];
s2 += a[i];
if (i % 2 == 0) {
if (s1 < 0) {
c1 += 1 - s1;
s1 = 1;
}
if (s2 > 0) {
c2 += s2 + 1;
s2 = -1;
}
} else {
if (s2 < 0) {
c2 += 1 - s2;
s2 = 1;
}
if (s1 > 0) {
c1 += s1 + 1;
s1 = -1;
}
}
}
printf("%d\n", min(c1, c2));
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 29;
inline int two(int n) { return 1 << n; }
inline int test(int n, int b) { return (n >> b) & 1; }
inline void set_bit(int &n, int b) { n |= two(b); }
inline void unset_bit(int &n, int b) { n &= ~two(b); }
const long long mod = 1e9 + 7;
const int N = 1e6 + 9;
long long a[N];
vector<long long> v[N];
long long modexp(long long a, long long n) {
long long r = 1;
while (n) {
if (n & 1) r = (r * a) % mod;
a = (a * a) % mod;
n >>= 1;
}
return r;
}
bool cmp(const pair<double, long long> &a, const pair<double, int> &b) {
if (a.first == b.first) {
return a.second < b.second;
} else
return a.first > b.first;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n, k = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long sum = a[0], ans = 0;
if (sum >= 0) {
k = 1;
}
for (int i = 1; i < n; i++) {
sum += a[i];
if (k == 1) {
if (sum >= 0) {
ans += sum + 1;
sum = -1;
}
} else {
if (sum <= 0) {
ans += (-1 * sum) + 1;
sum = 1;
}
}
if (k == 0)
k = 1;
else
k = 0;
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #![allow(non_snake_case)]
#![allow(dead_code)]
#![allow(unused_macros)]
#![allow(unused_imports)]
use std::str::FromStr;
use std::io::*;
use std::collections::*;
use std::cmp::*;
struct Scanner<I: Iterator<Item = char>> {
iter: std::iter::Peekable<I>,
}
macro_rules! exit {
() => {{
exit!(0)
}};
($code:expr) => {{
if cfg!(local) {
writeln!(std::io::stderr(), "===== Terminated =====")
.expect("failed printing to stderr");
}
std::process::exit($code);
}}
}
impl<I: Iterator<Item = char>> Scanner<I> {
pub fn new(iter: I) -> Scanner<I> {
Scanner {
iter: iter.peekable(),
}
}
pub fn safe_get_token(&mut self) -> Option<String> {
let token = self.iter
.by_ref()
.skip_while(|c| c.is_whitespace())
.take_while(|c| !c.is_whitespace())
.collect::<String>();
if token.is_empty() {
None
} else {
Some(token)
}
}
pub fn token(&mut self) -> String {
self.safe_get_token().unwrap_or_else(|| exit!())
}
pub fn get<T: FromStr>(&mut self) -> T {
self.token().parse::<T>().unwrap_or_else(|_| exit!())
}
pub fn vec<T: FromStr>(&mut self, len: usize) -> Vec<T> {
(0..len).map(|_| self.get()).collect()
}
pub fn mat<T: FromStr>(&mut self, row: usize, col: usize) -> Vec<Vec<T>> {
(0..row).map(|_| self.vec(col)).collect()
}
pub fn char(&mut self) -> char {
self.iter.next().unwrap_or_else(|| exit!())
}
pub fn chars(&mut self) -> Vec<char> {
self.get::<String>().chars().collect()
}
pub fn mat_chars(&mut self, row: usize) -> Vec<Vec<char>> {
(0..row).map(|_| self.chars()).collect()
}
pub fn line(&mut self) -> String {
if self.peek().is_some() {
self.iter
.by_ref()
.take_while(|&c| !(c == '\n' || c == '\r'))
.collect::<String>()
} else {
exit!();
}
}
pub fn peek(&mut self) -> Option<&char> {
self.iter.peek()
}
}
fn main() {
let cin = stdin();
let cin = cin.lock();
let mut sc = Scanner::new(cin.bytes().map(|c| c.unwrap() as char));
let n: usize = sc.get();
let a: Vec<i64> = sc.vec(n);
let mut p = 0;
let mut ans1 = 0;
for i in 0..n {
let mut s = p + a[i];
if i == 0 && s >= 0 {
ans1 += s.abs()+1;
s += -s - 1;
} else if s == 0 {
s += if p > 0 { 1 } else { -1 };
ans1 += 1;
} else if s * p > 0 {
ans1 += s.abs()+1;
s += if s > 0 { -s - 1 } else { s + 1 };
}
p = s;
}
let mut p = 0;
let mut ans2 = 0;
for i in 0..n {
let mut s = p + a[i];
if i == 0 && s <= 0 {
ans2 += s.abs()+1;
s += s + 1;
} else if s == 0 {
s += if p > 0 { 1 } else { -1 };
ans2 += 1;
} else if s * p > 0 {
ans2 += s.abs()+1;
s += if s > 0 { -s - 1 } else { s + 1 };
}
p = s;
}
println!("{}", min(ans1, ans2));
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define MOD 1000000007
# define INF (1 < <29)
#define MODSET(d) if ((d) >= MOD) d %= MOD;
#define MODNEGSET(d) if ((d) < 0) d = ((d % MOD) + MOD) % MOD;
#define MODADDSET(d) if ((d) >= MOD) d -= MOD;
#define MODADDWHILESET(d) while ((d) >= MOD) d -= MOD;
//defines
#define FILE_IO freopen("in.txt","r",stdin); freopen("out.txt","w",stdout);
#define sc1(a,type) type a; cin>>a;
#define sc2(a,b,type) type a,b; cin>>a>>b;
#define sc3(a, b, c,type) type a,b,c; cin>>a>>b>>c;
#define sc4(a, b, c, d,type) type a ,b,c,d; cin>>a>>b>>c>>d;
#define nl cout<<"\n";
#define foreach(v, c) for(__typeof( (c).begin()) v = (c).begin(); v != (c).end(); ++v)
#define revforeach(v, c) for(__typeof( (c).rbegin()) v = (c).rbegin(); v != (c).rend(); ++v)
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);
#define re(i,b) for(int i=0;i<int(b);i++)
#define re1(i,b) for(int i=1;i<=int(b);i++)
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(),c.rend()
#define mpresent(container, element) (container.find(element) != container.end()) //for map,set..etc (returns true/false value)
#define vpresent(container, element) (find(all(container),element) != container.end()) //for vectors,strings,list,deque (returns true/false value)
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define ins insert
#define F first
#define S second
#define clr clear()
#define sz(x) ((int)x.size())
#define dt distance
#define test(t) int t; cin>>t; while(t--)
#define csb(i) __builtin_popcount(i)
#define csbll(i) __builtin_popcountll(i)
#define clz(x) __builtin_clz(x)
#define clzl(x) __builtin_clzl(x)
#define cp(x) __builtin_parity(x)
#define adv(v,num) advance(v,num)//used for lists and other structures that use iterators,when you can't access elements randomly ( iterator moves num positions)
#define mod 1000000007
#define MAX_ARR 1000000
#define v2d(rowsize,colsize,type,name) vector<vector<type>> name(rowsize,vector<type>(colsize));
#define digits_in(i) (ll)log10(i)+1 // gives no of digits in a number
#define sqr(x) (x)*(x)
//does not apply for i==0 , add an excetion contition for n==0 ( cust return count 1 for that inseted of using this function)
//typedef
typedef string str;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<str> vs;
typedef vector<char> vc;
typedef pair<int,int> pii;
typedef pair<str,int> psi;
typedef pair<int,str> pis;
typedef vector<pii> vii;
typedef map<int,int> mii;
typedef map<ll,ll> mll;
typedef map<str,int> msi;
typedef map<char,int> mci;
typedef map<int,str> mis;
typedef unordered_map<int,int> umii;
typedef unordered_map<str,int> umsi;
typedef unordered_map<int,str> umis;
typedef unordered_map<str,str> umss;
typedef unordered_map<char,int> umci;
typedef set<str> ss;
typedef set<int> si;
typedef unordered_set<str> uss;
typedef unordered_set<int> usi;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
// #ifndef ONLINE_JUDGE
// #include "debug.h"
// #else
// #define debug(args...)
// #endif
int main(){fastio
// #ifndef ONLINE_JUDGE
// FILE_IO
// #endif
vll v;
test(t){
int temp;cin>>temp;
v.pb(temp);
}
ll ct=0;
re(i,sz(v)-1){
// debug(v[i] ,v[i]+v[i+1]);
if( (v[i]<0 && v[i]+v[i+1]<0) || (v[i]>0 && v[i]+v[i+1]>0 || v[i]+v[i+1]==0) ){
if( v[i]>0 && v[i]+v[i+1]>0){
ct+=v[i]+v[i+1]+1;
}
else if(v[i]<0 && v[i]+v[i+1]<0 ){
ct+=abs(v[i]+v[i+1])+1;
}
else{
ct+=1;
}
v[i+1]= v[i]>0?-1:1;
}
else{
v[i+1]+=v[i];
}
// debug(ct);
}
cout<<ct;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
def solve(tmp):
ans = 0
for i in range(1, n):
if tmp * (tmp + a[i]) < 0:
tmp += a[i]
else:
ans += abs(tmp + a[i]) + 1
if tmp < 0:
tmp = 1
else:
tmp = - 1
return ans
ans = 0
if a[0] == 0:
ans = min(solve(1) + 1, solve(-1) + 1)
else:
ans = solve(a[0])
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.*;
public class Main {
private static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int n = sc.nextInt();
long[] a = new long[n];
for (int i = 0;i < n;i++) a[i] = sc.nextLong();
long ret = calc(a,true);
ret = Math.min(calc(a,false),ret);
System.out.println(ret);
}
private static long calc(long[] a, boolean b) {
long sum = a[0];
long ret = 0;
if (b) {
ret = Math.abs(sum)+1;
if (sum<0) {
sum = 1;
} else {
sum = -1;
}
}
long tmp = 0;
for (int i = 1;i < a.length;i++) {
long num = a[i];
tmp = sum;
sum += num;
if ((tmp<0&&sum>=0)||(tmp>=0&&sum<0)) continue;
long l = Math.abs(sum)+1;
if (sum>=0) {
sum -= l;
} else {
sum += l;
}
ret += l;
}
if (sum==0) ret++;
return 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;
int main() {
int n, s;
int count = 0;
vector<int> a;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> s;
a.emplace_back(s);
}
int sum = a[0];
for (int i = 0; i < n - 1; ++i) {
if (i > 0) {
sum = sum + a[i];
}
if (sum * (sum + a[i + 1]) >= 0 && abs(sum) >= abs(sum + a[i + 1])) {
count = count + abs(sum + a[i + 1]) + 1;
if (sum + a[i + 1] < 0) {
sum = sum + abs(sum + a[i + 1]) + 1;
} else {
if (sum > 0 && sum + a[i + 1] == 0) {
sum = sum - 1;
}
if (sum < 0 && sum + a[i + 1] == 0) {
sum = sum + 1;
} else {
sum = sum - abs(sum + a[i + 1]) - 1;
}
}
}
if (sum * (sum + a[i + 1]) >= 0 && abs(sum) < abs(sum + a[i + 1])) {
count = count + abs(sum) + 1;
if (sum < 0) {
sum = sum + abs(sum) + 1;
} else {
sum = sum - abs(sum) - 1;
}
}
}
cout << count << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <bits/stdc++.h>
int main() {
int n;
scanf("%d", &n);
long long a[n];
for (int i = 0; i < n; i++) {
scanf("%lld", &a[i]);
}
long long mans = 0, pans = 0;
long long msub[n], psub[n];
if (a[0] == 0) {
pans = 1;
mans = 1;
psub[0] = 1;
msub[0] = -1;
}
if (a[0] > 0) {
psub[0] = a[0];
msub[0] = -1;
mans = a[0] + 1;
} else {
psub[0] = 1;
msub[0] = a[0];
pans = a[0] + 1;
}
int i;
for (i = 1; i < n; i++) {
if (i % 2 == 1 && psub[i - 1] + a[i] >= 0) {
pans += psub[i - 1] + a[i] + 1;
psub[i] = -1;
} else if (i % 2 == 0 && psub[i - 1] + a[i] <= 0) {
pans += 1 - (psub[i - 1] + a[i]);
psub[i] = 1;
} else {
psub[i] = psub[i - 1] + a[i];
}
if (i % 2 == 1 && msub[i - 1] + a[i] <= 0) {
mans += 1 - (msub[i - 1] + a[i]);
msub[i] = 1;
} else if (i % 2 == 0 && msub[i - 1] + a[i] >= 0) {
mans += msub[i - 1] + a[i] + 1;
msub[i] = -1;
} else {
msub[i] = msub[i - 1] + a[i];
}
}
printf("%lld", mans < pans ? mans : pans);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T>
inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
template <class T>
inline T sqr(T x) {
return x * x;
}
int main(void) {
int n;
cin >> n;
long long a[n + 1];
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
long long S1, S2;
long long ans[2];
if (a[1] == 0) {
S1 = 1;
ans[0] = 1;
for (int i = (2); i < (n + 1); ++i) {
S2 = S1 + a[i];
if ((S1 < 0 && S2 > 0) || (S1 > 0 && S2 < 0)) {
S1 = S2;
} else {
ans[0] += abs(S2) + 1;
if (S1 < 0)
S2 = 1;
else
S2 = -1;
S1 = S2;
}
}
S1 = -1;
ans[1] = 1;
for (int i = (2); i < (n + 1); ++i) {
S2 = S1 + a[i];
if ((S1 < 0 && S2 > 0) || (S1 > 0 && S2 < 0)) {
S1 = S2;
} else {
ans[1] += abs(S2) + 1;
if (S1 < 0)
S2 = 1;
else
S2 = -1;
S1 = S2;
}
}
cout << min(ans[0], ans[1]) << endl;
} else {
S1 = a[1];
ans[0] = 0;
for (int i = (2); i < (n + 1); ++i) {
S2 = S1 + a[i];
if ((S1 < 0 && S2 > 0) || (S1 > 0 && S2 < 0)) {
S1 = S2;
} else {
ans[0] += abs(S2) + 1;
if (S1 < 0)
S2 = 1;
else
S2 = -1;
S1 = S2;
}
}
cout << ans[0] << 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.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static int[][] map;
static int[][] label;
static ArrayList<String> list;
static int M;
static int N;
static int T;
static int P;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
long sum = scanner.nextLong();
long ans = 0;
boolean sign = true;
if(sum < 0)sign = false;
for (int i = 0; i < n - 1; i++) {
sum += scanner.nextLong();
// System.out.println(sum);
// System.out.println(ans);
if(sign){
if(sum >= 0){
ans += sum + 1;
sum = -1;
}
sign = false;
}else{
if(sum <= 0){
ans -= sum - 1;
sum = 1;
}
sign = true;
}
}
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 ll = long long;
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < (int)(n); i++) cin >> a[i];
int count = 0, sum_count = a[0];
if (a[0] < 0) {
for (int i = 1; i < (int)(n); i++) {
sum_count += a[i];
if (i % 2 == 0) {
while (sum_count >= 0) {
sum_count--;
count++;
}
} else {
while (sum_count <= 0) {
sum_count++;
count++;
}
}
}
} else if (a[0] > 0) {
for (int i = 1; i < (int)(n); i++) {
sum_count += a[i];
if (i % 2 == 0) {
while (sum_count <= 0) {
sum_count++;
count++;
}
} else {
while (sum_count >= 0) {
sum_count--;
count++;
}
}
}
}
cout << count << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < (int)(n); i++) cin >> a.at(i);
ll ans = 0, sum = 0;
for (int i = 0; i < (int)(n); i++) {
int now = a.at(i);
if (i == 0) {
if (now == 0) {
if (a.at(1) > 0)
sum--;
else
sum++;
ans++;
} else {
sum += now;
}
continue;
}
if ((sum < 0 && sum + now > 0) || (sum > 0 && sum + now < 0)) {
sum += now;
} else {
ll add = abs(sum + now) + 1;
if (sum < 0)
sum = 1;
else
sum = -1;
ans += add;
}
}
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;
using ll = long long;
int main(void) {
ll n;
cin >> n;
vector<ll> a(n);
for (auto& it : a) cin >> it;
ll total = a[0];
ll ans = 0;
for (ll i = 1; i < n; i++) {
if (total > 0) {
if (total + a[i] >= 0) {
ans += abs(-total - 1 - a[i]);
a[i] = -total - 1;
}
} else {
if (total + a[i] <= 0) {
ans += abs(-total + 1 - a[i]);
a[i] = -total + 1;
}
}
total += 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()))
answer = 0
sum_a = [0]
sum_a[0] = a[0]
for i in range(1, n):
sum_a.append(sum_a[i-1] + a[i])
if sum_a[i-1] * sum_a[i] >= 0:
if sum_a[i-1] < 0:
sum_a[i] = 1
answer += abs(a[i] - (1 - sum_a[i-1]))
a[i] = 1 - sum_a[i-1]
elif sum_a[i-1] > 0:
sum_a[i] = -1
answer += abs(a[i] - (-1 - sum_a[i-1]))
a[i] = -1 - sum_a[i-1]
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;
int n;
long long f(vector<long long>& sum, vector<long long>& pm) {
int tmp;
tmp = 0;
for (int i = 0; i < (n - 1); i++) {
sum[i + 1] += pm[i];
if (sum[i] * sum[i + 1] >= 0) {
if (sum[i + 1] == 0) {
if (sum[i] < 0)
sum[i + 1] = pm[i + 1] = 1;
else
sum[i + 1] = pm[i + 1] = -1;
} else if (sum[i + 1] < 0) {
pm[i + 1] = 1 - sum[i + 1];
sum[i + 1] = 1;
} else if (sum[i + 1] > 0) {
pm[i + 1] = -1 - sum[i + 1];
sum[i + 1] = -1;
}
tmp += abs(pm[i + 1]);
}
}
return tmp;
}
signed main(void) {
cin >> n;
vector<long long> s(n), t, pm(n, 0);
long long ans, tmp;
for (int i = 0; i < (n); i++) {
int a;
cin >> a;
s[i] = a;
}
for (int i = 0; i < (n - 1); i++) s[i + 1] += s[i];
ans = 1e18;
copy(s.begin(), s.end(), back_inserter(t));
tmp = 0;
if (t[0] <= 0) {
pm[0] = 1 - t[0];
t[0] = 1;
tmp += abs(pm[0]);
}
tmp += f(t, pm);
for (int i = 0; i < (n); i++) cout << t[i] << " ";
cout << endl;
ans = min(ans, tmp);
for (int i = 0; i < (n); i++) pm[i] = 0;
tmp = 0;
if (s[0] >= 0) {
pm[0] = -1 - s[0];
s[0] = -1;
tmp += abs(pm[0]);
}
tmp += f(s, pm);
ans = min(ans, tmp);
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
using boost::multiprecision::cpp_int;
using namespace std;
#if __has_include("print.hpp")
#include "print.hpp"
#endif
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define MOD 1000000007
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
typedef long long ll;
typedef pair<ll, ll> p;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> v(n);
rep(i, n) cin >> v[i];
ll sum = v[0];
ll res = 0;
for (int i = 1; i < n; ++i) {
// cout << i << del;
if((sum + v[i]) * sum < 0){
sum += v[i];
}else{
ll t = -sum - (sum / abs(sum)) ;
ll remain = t - v[i];
// cout << t << endl;
// cout << remain << endl;
res += abs(remain);
sum += t;
v[i] += t;
}
// cout << sum << endl;
// cout << "===" << endl;
}
cout << res << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <bits/stdc++.h>
int main(void) {
int a[100000];
int i, n, check = 0;
long long int sum = 0, count = 0;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for (i = 0; i < n; i++) {
sum += a[i];
switch (check) {
case 1:
if (sum >= 0) {
count += sum + 1;
sum = -1;
}
break;
case -1:
if (sum <= 0) {
count += 1 - sum;
sum = -1;
}
break;
default:
break;
}
if (sum > 0) {
check = 1;
} else {
check = -1;
}
}
printf("%lld", count);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java |
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
public class Main implements Runnable { // クラス名はMain1
PrintWriter out = new PrintWriter(System.out);
InputReader sc = new InputReader(System.in);
public static void main(String[] args) {
Thread.setDefaultUncaughtExceptionHandler((t, e) -> System.exit(1));
new Thread(null, new Main(), "", 1024 * 1024 * 1024).start(); // 16MBスタックを確保して実行
}
public void run() {
try {
int N = sc.nextInt();
long[] A = new long[N];
for (int i = 0; i < N; i++) {
A[i] = sc.nextLong();
}
long[] wa = new long[N + 1];
for (int i = 1; i <= N; i++) {
wa[i] += wa[i - 1] + A[i - 1];
}
//out.println("A:" + Arrays.toString(A));
//out.println("W:" + Arrays.toString(wa));
boolean plus = wa[1] >= 0 ? true : false;
long cntp = 0;
long cntm = 0;
for (int i = 1; i <= N; i++) {
wa[i] += cntp - cntm;
if (!plus && wa[i] >= 0) {
long p = Math.abs(wa[i]) + 1;
cntm += p;
wa[i] -= p;
A[i - 1] -= p;
} else if (plus && wa[i] <= 0) {
long m = Math.abs(wa[i]) + 1;
cntp += m;
wa[i] += m;
A[i - 1] += m;
}
plus = !plus;
}
//out.println("A:" + Arrays.toString(A));
//out.println("W:" + Arrays.toString(wa));
out.println(cntp + cntm);
} catch (ArithmeticException ae) {
//ae.printStackTrace();
throw new RuntimeException();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
} finally {
out.flush();
out.close();
}
}
// 高速なScanner
static class InputReader {
private InputStream in;
private byte[] buffer = new byte[1024];
private int curbuf;
private int lenbuf;
public InputReader(InputStream in) {
this.in = in;
this.curbuf = this.lenbuf = 0;
}
public boolean hasNextByte() {
if (curbuf >= lenbuf) {
curbuf = 0;
try {
lenbuf = in.read(buffer);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0)
return false;
}
return true;
}
private int readByte() {
if (hasNextByte())
return buffer[curbuf++];
else
return -1;
}
private boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
private void skip() {
while (hasNextByte() && isSpaceChar(buffer[curbuf]))
curbuf++;
}
public boolean hasNext() {
skip();
return hasNextByte();
}
public String next() {
if (!hasNext())
throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while (!isSpaceChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public int nextInt() {
if (!hasNext())
throw new NoSuchElementException();
int c = readByte();
while (isSpaceChar(c))
c = readByte();
boolean minus = false;
if (c == '-') {
minus = true;
c = readByte();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res = res * 10 + c - '0';
c = readByte();
} while (!isSpaceChar(c));
return (minus) ? -res : res;
}
public long nextLong() {
if (!hasNext())
throw new NoSuchElementException();
int c = readByte();
while (isSpaceChar(c))
c = readByte();
boolean minus = false;
if (c == '-') {
minus = true;
c = readByte();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res = res * 10 + c - '0';
c = readByte();
} while (!isSpaceChar(c));
return (minus) ? -res : res;
}
public double nextDouble() {
return Double.parseDouble(next());
}
public int[] nextIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public long[] nextLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public char[][] nextCharMap(int n, int m) {
char[][] map = new char[n][m];
for (int i = 0; i < n; i++)
map[i] = next().toCharArray();
return map;
}
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 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 vll = vector<long long>;
long long mod = 1e9 + 7;
using namespace std;
using Graph = vector<vector<int>>;
Graph G;
int cnt_digit(long long N) {
int digit = 0;
while (N > 0) {
N /= 10;
digit++;
}
return digit;
}
int n;
vll a;
long long solve(bool isp) {
long long sum = 0ll;
long long ret = 0ll;
for (int i = 0; i < n; i++) {
sum += a[i];
if (isp and sum <= 0) {
sum += -sum + 1;
sum = 1ll;
}
if (not isp and sum >= 0) {
sum += sum + 1;
sum = -1ll;
}
isp ^= 1;
}
return ret;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> n;
a = vll(n);
for (auto& e : a) {
cin >> e;
}
long long res = min(solve(0), solve(1));
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>
int main() {
int n, a[100002], i, d[100002], b;
scanf("%d", &n);
for (i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
d[1] = a[1];
b = 0;
for (i = 2; i <= n; i++) {
d[i] = d[i - 1] + a[i];
if (d[i] * d[i - 1] >= 0) {
if (d[i - 1] < 0) {
b = b + 1 - d[i];
d[i] = 1;
}
if (d[i - 1] > 0) {
b = b + 1 + d[i];
d[i] = -1;
}
}
}
printf("%d\n", b);
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 chk(long a[], int N, bool t) {
long total = 0;
long ops = 0;
for (int i = 0; i < N; i++) {
total += a[i];
if (t == true && (total < 1)) {
ops += (1 - total);
total = 1;
} else if (t == false && (total > -1)) {
ops += (total + 1);
total = -1;
}
t = !t;
}
return ops;
}
int main() {
long N;
cin >> N;
long a[1000001];
for (long i = 0; i < N; i++) {
cin >> a[i];
}
printf("%d\n", min(chk(a, N, true), chk(a, N, false)));
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> a(N);
for (int i = 0; i < N; i++) {
cin >> a.at(i);
}
int ans1 = 0, ans2 = 0, sum1 = 0, sum2 = 0;
for (int i = 0; i < N; i++) {
sum1 += a.at(i);
if (i % 2 == 0 && sum1 <= 0) {
ans1 += 1 - sum1;
sum1 = 1;
} else if (i % 2 != 0 && sum1 >= 0) {
ans1 += sum1 + 1;
sum1 = -1;
}
}
for (int i = 0; i < N; i++) {
sum2 += a.at(i);
if (i % 2 == 0 && sum2 >= 0) {
ans2 += sum2 + 1;
sum2 = -1;
} else if (i % 2 != 0 && sum2 <= 0) {
ans2 += 1 - sum2;
sum2 = 1;
}
}
cout << min(ans1, ans2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
const int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
using namespace std;
int main() {
int n;
cin >> n;
long long a[n];
long long sums[n];
cin >> a[0];
sums[0] = a[0];
for (int i = 1; i < n; ++i) {
cin >> a[i];
sums[i] = sums[i - 1] + a[i];
}
long long cnt = 0;
long long diff;
for (int i = 1; i < n; i++) {
if (sums[i - 1] * sums[i] >= 0) {
if (sums[i - 1] < 0) {
diff = 1 - sums[i];
} else {
diff = -1 - sums[i];
}
for (int j = i; j < n; j++) {
sums[j] += diff;
}
cnt += abs(diff);
}
}
cout << cnt << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
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(void) {
int n;
cin >> n;
int array[100000];
for (int i = (0); i < (100000); ++i) {
int a;
cin >> array[i];
}
int ans = 0;
bool flag = false;
int sum = array[0];
if (array[0] == 0) {
if (array[1] > 0) {
ans++;
sum = -1;
flag = false;
} else if (array[1] < 0) {
ans++;
sum = 1;
flag = true;
} else {
sum = 1;
ans++;
}
} else if (array[0] > 0)
flag = true;
else
flag = false;
for (int i = (1); i < (n); ++i) {
sum += array[i];
if (flag) {
if (sum >= 0) {
ans += (sum + 1);
sum -= (sum + 1);
cerr << "ans"
<< " = " << (ans) << " (L" << 91 << ")"
<< " "
<< "<stdin>" << endl;
;
}
flag = false;
} else {
if (sum <= 0) {
ans += -1 * (sum - 1);
sum += -1 * (sum - 1);
cerr << "ans"
<< " = " << (ans) << " (L" << 98 << ")"
<< " "
<< "<stdin>" << endl;
;
}
flag = true;
}
}
cout << ans << endl;
return 0;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.