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 count(int sign0, vector<int> a, int n) {
int count = 0;
int total = 0;
for (int i = 0; i < n; i++) {
total += a.at(i);
if (i % 2 == 1) {
while (total * sign0 >= 0) {
total -= sign0;
count++;
}
} else {
while (total * sign0 <= 0) {
total += sign0;
count++;
}
}
}
return count;
}
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a.at(i);
int plus = count(1, a, n);
int minus = count(-1, a, n);
cout << min(plus, minus) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
int main(void) {
double num[10 * 10 * 10 * 10 * 10];
int i, n, ssign;
double sum = 0;
double count = 0;
double fsum, fnum;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%lf", &num[i]);
}
if (num[0] == 0) {
num[0]++;
count++;
}
for (i = 1; i < n; i++) {
sum += num[i - 1];
fsum = fabs(sum);
fnum = fabs(num[i]);
while (1) {
if (fsum > fnum) {
if (sum < 0) {
num[i]++;
count++;
} else if (sum > 0) {
num[i]--;
count++;
}
} else if (fsum == fnum) {
if (sum < 0) {
num[i]++;
count++;
} else {
num[i]--;
count++;
}
} else if (fsum < fnum && sum > 0 && num[i] > 0) {
num[i]--;
count++;
} else if (fsum < fnum && sum < 0 && num[i] < 0) {
num[i]++;
count++;
} else
break;
}
}
printf("%.0f\n", count);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ABC59C
{
class Program
{
static void Main(string[] args)
{
int n = int.Parse(Console.ReadLine());
string[] str = Console.ReadLine().Split(' ');
int[] a = new int[n];
for(int i = 0; i < n; i++)
{
a[i] = int.Parse(str[i]);
}
int x = 0;
int f = 0;
int sum = 0;
if (a[0] < 0) f = 1;
if (a[0] == 0)
{
a[0]++;
x++;
}
for(int i = 0; i < n; i++)
{
sum += a[i];
if (f == 1 && sum >= 0)
{
x += (sum + 1);
f = 0;
sum = -1;
}else if (f == 0 && sum <= 0)
{
x += (1 - sum);
f = 1;
sum = 1;
}else
{
f = 1 - f;
}
}
Console.WriteLine(x);
}
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int,input().split()))
a_1 = a
ans = 0
ans_2 = 0
o = 0
for i in range(n):
if i == 0:
if a[i] == 0:
f = "+"
a[i] = 1
elif a[0] > 0:
f = "+"
elif a[0] < 0:
f = "-"
else:
o += a[i-1]
if f == "+":
if a[i] + o > 0:
c = -1 - o
ans += abs(c - a[i])
a[i] = c
f = "-"
else:
if a[i] + o == 0:
a[i] -= 1
ans += 1
f = "-"
elif f == "-":
if a[i] + o < 0:
c = 1 - o
ans += abs(c - a[i])
a[i] = c
f = "+"
else:
if a[i] + o == 0:
a[i] += 1
ans += 1
f = "+"
a = a_1
for i in range(n):
if i == 0:
if a[i] == 0:
f = "+"
a[i] = 1
elif a[0] > 0:
f = "-"
elif a[0] < 0:
f = "+"
else:
o += a[i-1]
if f == "+":
if a[i] + o > 0:
c = -1 - o
ans_2 += abs(c - a[i])
a[i] = c
f = "-"
else:
if a[i] + o == 0:
a[i] -= 1
ans += 1
f = "-"
elif f == "-":
if a[i] + o < 0:
c = 1 - o
ans_2 += abs(c - a[i])
a[i] = c
f = "+"
else:
if a[i] + o == 0:
a[i] += 1
ans += 1
f = "+"
print(a)
print(min(ans,ans_2))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int d[n];
for (int i = 0; i < n; i++) {
cin >> d[i];
}
int count = 0;
int sum = d[0];
int f = 0;
if (d[0] > 0) {
f = -1;
}
if (d[0] < 0) {
f = 1;
}
for (int i = 1; i < n; i++) {
sum += d[i];
if (sum == 0) {
if (f == 1) {
count++;
f = -1;
sum = 1;
continue;
}
if (f == -1) {
count++;
f = 1;
sum = -1;
continue;
}
}
if (sum > 0) {
if (f == 1) {
f = -1;
continue;
}
if (f == -1) {
count += sum + 1;
sum = -1;
f = 1;
continue;
}
}
if (sum < 0) {
if (f == -1) {
f = 1;
continue;
}
if (f == 1) {
count += 1 - sum;
sum = 1;
f = -1;
continue;
}
}
}
int ccount = 0;
int ssum;
int ff = 0;
if (d[0] > 0) {
ff = 1;
ccount = 1 + d[0];
ssum = -1;
}
if (d[0] < 0) {
ff = -1;
ccount = 1 - d[0];
ssum = 1;
}
for (int i = 1; i < n; i++) {
ssum += d[i];
if (ssum == 0) {
if (ff == 1) {
ccount++;
ff = -1;
ssum = 1;
continue;
}
if (ff == -1) {
ccount++;
ff = 1;
ssum = -1;
continue;
}
}
if (ssum > 0) {
if (ff == 1) {
ff = -1;
continue;
}
if (ff == -1) {
ccount += ssum + 1;
ssum = -1;
ff = 1;
continue;
}
}
if (ssum < 0) {
if (ff == -1) {
ff = 1;
continue;
}
if (ff == 1) {
ccount += 1 - ssum;
ssum = 1;
ff = -1;
continue;
}
}
}
int s = min(count, ccount);
cout << s << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const long long int linf = 1LL << 50;
int main(int argc, char const* argv[]) {
int n;
cin >> n;
vector<long long int> a;
for (int i = 0; i < n; i++) {
long long int x;
cin >> x;
a.push_back(x);
}
long long int res = 0;
long long int sum = a[0];
for (int i = 1; i < n; i++) {
long long int tmp = sum + a[i];
if ((sum > 0 && tmp < 0) || (sum < 0 && tmp > 0)) {
sum = tmp;
} else {
long long int target;
if (sum > 0)
target = -1;
else
target = 1;
res += abs(target - tmp);
sum = target;
}
}
cout << res << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long int a[n], sum = 0, cnt = 0;
for (int i = 0; i < n; i++) cin >> a[i];
sum = a[0];
if (a[0] == 0) {
cnt++;
if (a[1] > 0)
sum++;
else
sum--;
}
for (int i = 1; i < n; i++) {
if ((sum >= 0 && sum + a[i] >= 0) || (sum <= 0 && sum + a[i] <= 0)) {
if (sum < 0) {
long long int k = sum + a[i];
cnt += abs(k - 1);
sum = 1;
} else {
long long int k = sum + a[i];
cnt += abs(k + 1);
sum = -1;
}
} else
sum += a[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 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) {
signed long long tmp = 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;
}
break;
}
}
}
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> a(n);
for (int i = 0; i < n; i++) {
cin >> a.at(i);
}
long long sum = a.at(0);
long long op_1 = 0;
bool flag = sum > 0 ? 1 : 0;
for (int j = 1; j < n; j++) {
if (flag) {
sum += a.at(j);
if (sum >= 0) {
op_1 += (sum + 1);
sum = -1;
}
flag = 0;
} else {
sum += a.at(j);
if (sum <= 0) {
op_1 += (-1 * sum + 1);
sum = 1;
}
flag = 1;
}
}
sum = a.at(0);
long long op_2 = 0;
if (sum > 0) {
sum = -1;
op_2 += (sum + 1);
flag = 0;
} else {
sum = 1;
op_2 += (sum * -1 + 1);
flag = 1;
}
for (int j = 1; j < n; j++) {
if (flag) {
sum += a.at(j);
if (sum >= 0) {
op_2 += sum + 1;
sum = -1;
}
flag = 0;
} else {
sum += a.at(j);
if (sum <= 0) {
op_2 += -1 * sum + 1;
sum = 1;
}
flag = 1;
}
}
cout << (op_1 > op_2 ? op_2 : op_1) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
long long a[MAXN];
int main(void) {
int n;
long long ans = 0, sum = 0;
bool flag = true;
scanf("%d", &n);
scanf("%lld", &ans);
if (ans < 0)
flag = false;
else if (ans == 0) {
sum += 1;
ans = 1;
}
for (int i = 1; i < n; i++) {
long long x;
scanf("%lld", &x);
a[i] = x;
ans += x;
if (flag) {
if (ans >= 0) {
sum += ans + 1;
ans = -1;
}
flag = false;
} else {
if (ans <= 0) {
sum += abs(ans) + 1;
ans = 1;
}
flag = true;
}
}
long long num = 0;
if (ans < 0) {
ans = 1;
num += abs(num) + 1;
flag = true;
} else {
ans = -1;
num += num + 1;
flag = false;
}
for (int i = 1; i < n; i++) {
long long x = a[i];
ans += x;
if (flag) {
if (ans >= 0) {
num += ans + 1;
ans = -1;
}
flag = false;
} else {
if (ans <= 0) {
num += abs(ans) + 1;
ans = 1;
}
flag = true;
}
}
sum = min(sum, num);
printf("%lld\n", sum);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
using System.Collections.Generic;
using System.Linq;
namespace AtCoder
{
class Program
{
static int n;
static void Main(string[] args)
{
//[summary]C - Sequence
n = int.Parse(Console.ReadLine());
var a = ReadLine();
long count = 0;
if (a[0] == 0)
{
var a1 = new List<long>(a);
a1[0] = 1;
long count1 = CountOperations(a, a1);
var a2 = new List<long>(a);
a2[0] = -1;
long count2 = CountOperations(a, a2);
if (count1 < count2)
{
count = count1;
}
else
{
count = count2;
}
}
else
{
var a3 = new List<long>(a);
count = CountOperations(a, a3);
}
Console.WriteLine(count);
}
static long CountOperations(List<long> a, List<long> newA)
{
newA = GetNewNumbers(newA);
long count = 0;
for(int i = 0; i < n; i++)
{
count += Math.Abs(a[i] - newA[i]);
}
return count;
}
static List<long> GetNewNumbers(List<long> a)
{
long sum = a[0];
long next = 0;
for (int i = 1; i < n; i++)
{
next = sum + a[i];
if ((sum > 0 && next < 0) | (sum < 0 && next > 0))
{
//何もしない
}
else if (sum > 0)
{
a[i] = -1 - sum;
next = -1;
}
else
{
a[i] = 1 - sum;
next = 1;
}
sum = next;
}
return a;
}
static List<long> ReadLine()
{
var line = Console.ReadLine();
var array = line.Split(' ');
return array.Select(x => long.Parse(x)).ToList();
}
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | # -*- coding: utf-8 -*-
n = int(input())
an = list(map(int, input().split()))
sum = an[0]
ans = 0
for i in range(1,n):
if sum * (sum + an[i]) < 0 or (i==1 and sum>0):
sum += an[i]
else:
if sum > 0:
ans += abs(sum + an[i] + 1)
sum = -1
else:
ans += abs(sum + an[i] - 1)
sum = 1
ans1 = ans
ans = 0
for i in range(1, n):
if sum * (sum + an[i]) < 0 or (i == 1 and sum < 0):
sum += an[i]
else:
if sum > 0:
ans += abs(sum + an[i] + 1)
sum = -1
else:
ans += abs(sum + an[i] - 1)
sum = 1
print(min([ans1,ans]))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import numpy as np
from copy import deepcopy
n = int(input())
a = list(map(int, input().split()))
c = [0] * n
for i in range(n):
c[i] = c[i - 1] + a[i]
c = np.array(c)
ans1, ans2 = 0, 0
tmp = deepcopy(c)
for i in range(n):
t = tmp[i]
if i % 2 == 0 and tmp[i] >= 0:
tmp -= t + 1
ans1 += t + 1
elif i % 2 == 1 and tmp[i] <= 0:
tmp += -t + 1
ans1 += -t + 1
tmp = deepcopy(c)
for i in range(n):
t = c[i]
if i % 2 == 1 and tmp[i] >= 0:
tmp -= t + 1
ans2 += t + 1
elif i % 2 == 0 and tmp[i] <= 0:
tmp += -t + 1
ans2 += -t + 1
print(min(ans1, ans2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using mii = map<int, int>;
using pqls = priority_queue<long long>;
using pqlg = priority_queue<long long, vector<long long>, greater<long long>>;
using mll = map<long long, long long>;
using pll = pair<long long, long long>;
using sll = set<long long>;
long long divup(long long a, long long b);
long long kaijou(long long i);
long long P(long long n, long long k);
long long C(long long n, long long k);
long long GCD(long long a, long long b);
long long LCM(long long a, long long b);
bool prime(long long N);
double distance(vector<long long> p, vector<long long> q, long long n);
void press(vector<long long> &v);
void ranking(vector<long long> &v);
void erase(vector<long long> &v, long long i);
void unique(vector<long long> &v);
void printv(vector<long long> v);
vector<ll> keta(ll x);
long long modpow(long long a, long long n, long long mod);
long long modinv(long long a, long long mod);
vector<long long> inputv(long long n);
vector<long long> yakusuu(int n);
map<long long, long long> soinsuu(long long n);
vector<vector<long long>> maze(long long i, long long j, vector<string> &s);
vector<long long> eratos(long long n);
set<long long> eraset(long long n);
long long divup(long long a, long long b) {
long long x = abs(a);
long long y = abs(b);
long long z = (x + y - 1) / y;
if ((a < 0 && b > 0) || (a > 0 && b < 0))
return -z;
else if (a == 0)
return 0;
else
return z;
}
long long kaijou(long long i) {
if (i == 0) return 1;
long long j = 1;
for (long long k = 1; k <= i; k++) {
j *= k;
}
return j;
}
long long P(long long n, long long k) {
if (n < k) return 0;
long long y = 1;
for (long long i = 0; i < k; i++) {
y *= (n - i);
}
return y;
}
long long C(long long n, long long k) {
if (n < k) return 0;
return P(n, k) / kaijou(k);
}
long long GCD(long long a, long long b) {
if (a < b) swap(a, b);
long long d = a % b;
if (d == 0) {
return b;
}
return GCD(b, d);
}
long long LCM(long long a, long long b) { return (a / GCD(a, b)) * b; }
bool prime(long long N) {
if (N == 1) {
return false;
}
if (N < 0) return false;
long long p = sqrt(N);
for (long long i = 2; i <= p; i++) {
if (N % i == 0) {
return false;
}
}
return true;
}
double distance(vector<long long> p, vector<long long> q, long long n) {
double x = 0;
for (long long i = 0; i < n; i++) {
x += pow((p.at(i) - q.at(i)), 2);
}
return sqrt(x);
}
void press(vector<long long> &v) {
long long n = v.size();
vector<long long> w(n);
map<long long, long long> m;
for (auto &p : v) {
m[p] = 0;
}
long long i = 0;
for (auto &p : m) {
p.second = i;
i++;
}
for (long long i = 0; i < n; i++) {
w.at(i) = m[v.at(i)];
}
v = w;
return;
}
void ranking(vector<long long> &v) {
long long n = v.size();
map<long long, long long> m;
long long i;
for (i = 0; i < n; i++) {
m[v.at(i)] = i;
}
vector<long long> w(n);
i = 0;
for (auto &p : m) {
v.at(i) = p.second;
i++;
}
return;
}
void erase(vector<long long> &v, long long i) {
long long n = v.size();
if (i > n - 1) return;
for (long long j = i; j < n - 1; j++) {
v.at(j) = v.at(j + 1);
}
v.pop_back();
return;
}
void unique(vector<long long> &v) {
long long n = v.size();
set<long long> s;
long long i = 0;
while (i < n) {
if (s.count(v.at(i))) {
erase(v, i);
n--;
} else {
s.insert(v.at(i));
i++;
}
}
return;
}
void printv(vector<long long> v) {
cout << "{ ";
for (auto &p : v) {
cout << p << ",";
}
cout << "}" << endl;
}
vector<ll> keta(ll x) {
if (x == 0) return {0};
ll n = log10(x) + 1;
vll w(n, 0);
for (ll i = 0; i < n; i++) {
ll p;
p = x % 10;
x = x / 10;
w[n - 1 - i] = p;
}
return w;
}
long long modpow(long long a, long long n, long long mod) {
long long res = 1;
while (n > 0) {
if (n & 1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
long long modinv(long long a, long long mod) { return modpow(a, mod - 2, mod); }
vector<long long> inputv(long long n) {
vector<long long> v(n);
for (long long i = 0; i < n; i++) {
cin >> v[i];
}
return v;
}
vector<long long> yakusuu(long long n) {
vector<long long> ret;
for (long long i = 1; i <= sqrt(n); ++i) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n) {
ret.push_back(n / i);
}
}
}
sort(ret.begin(), ret.end());
return ret;
}
map<long long, long long> soinsuu(long long n) {
map<long long, long long> m;
long long p = sqrt(n);
while (n % 2 == 0) {
n /= 2;
if (m.count(2)) {
m[2]++;
} else {
m[2] = 1;
}
}
for (long long i = 3; i * i <= n; i += 2) {
while (n % i == 0) {
n /= i;
if (m.count(i)) {
m[i]++;
} else {
m[i] = 1;
}
}
}
if (n != 1) m[n] = 1;
return m;
}
vector<vector<long long>> maze(ll i, ll j, vector<string> &s) {
ll h = s.size();
ll w = s[0].size();
queue<vector<long long>> q;
vector<vector<long long>> dis(h, vll(w, -1));
q.push({i, j});
dis[i][j] = 0;
while (!q.empty()) {
auto v = q.front();
q.pop();
if (v[0] > 0 && s[v[0] - 1][v[1]] == '.' && dis[v[0] - 1][v[1]] == -1) {
dis[v[0] - 1][v[1]] = dis[v[0]][v[1]] + 1;
q.push({v[0] - 1, v[1]});
}
if (v[1] > 0 && s[v[0]][v[1] - 1] == '.' && dis[v[0]][v[1] - 1] == -1) {
dis[v[0]][v[1] - 1] = dis[v[0]][v[1]] + 1;
q.push({v[0], v[1] - 1});
}
if (v[0] < h - 1 && s[v[0] + 1][v[1]] == '.' && dis[v[0] + 1][v[1]] == -1) {
dis[v[0] + 1][v[1]] = dis[v[0]][v[1]] + 1;
q.push({v[0] + 1, v[1]});
}
if (v[1] < w - 1 && s[v[0]][v[1] + 1] == '.' && dis[v[0]][v[1] + 1] == -1) {
dis[v[0]][v[1] + 1] = dis[v[0]][v[1]] + 1;
q.push({v[0], v[1] + 1});
}
}
return dis;
}
long long modC(long long n, long long k, long long mod) {
if (n < k) return 0;
long long p = 1, q = 1;
for (long long i = 0; i < k; i++) {
p = p * (n - i) % mod;
q = q * (i + 1) % mod;
}
return p * modinv(q, mod) % mod;
}
long long POW(long long a, long long n) {
long long res = 1;
while (n > 0) {
if (n & 1) res = res * a;
a = a * a;
n >>= 1;
}
return res;
}
vector<long long> eratos(long long n) {
if (n < 2) return {};
vll v(n - 1);
for (long long i = 0; i < n - 1; i++) {
v[i] = i + 2;
}
ll i = 0;
while (i < n - 1) {
ll p = v[i];
for (ll j = i + 1; j < n - 1; j++) {
if (v[j] % p == 0) {
v.erase(v.begin() + j);
n--;
}
}
i++;
}
v.resize(n - 1);
return v;
}
set<long long> eraset(long long n) {
set<long long> s;
vll v = eratos(n);
for (auto &t : v) {
s.insert(t);
}
return s;
}
vll line(ll x1, ll y1, ll x2, ll y2) {
vector<ll> v(3);
v[0] = y1 - y2;
v[1] = x2 - x1;
v[2] = -x1 * (y1 - y2) + y1 * (x1 - x2);
return v;
}
double dis(vll v, ll x, ll y) {
double s = sqrt(v[0] * v[0] + v[1] * v[1]);
return (double)abs(v[0] * x + v[1] * y + v[2]) / s;
}
ll const mod = 1e9 + 7;
int main() {
ll n;
cin >> n;
auto a = inputv(n);
auto b = a;
ll l = 0;
ll res = 0;
for (long long i = 0; i < n; i++) {
if (l == 0) {
if (a[0] == 0) {
for (long long j = 0; j < n; j++) {
if (a[j] != 0) {
a[0] = a[j] / abs(a[j]);
if (j % 1) a[0] *= (-1);
res++;
break;
}
}
}
if (!a[0]) {
a[0] = 1;
res++;
}
l += a[0];
} else if (l < 0) {
if (a[i] < -l + 1) {
res += -l + 1 - a[i];
a[i] = -l + 1;
l = 1;
} else {
l += a[i];
}
} else if (l > 0) {
if (a[i] > -l - 1) {
res += abs(a[i] - (-l - 1));
a[i] = -l - 1;
l = -1;
} else {
l += a[i];
}
}
}
a = b;
ll L = 0;
ll res2 = 0;
for (long long i = 0; i < n; i++) {
if (L == 0) {
if (a[0] == 0) {
for (long long j = 0; j < n; j++) {
if (a[j] != 0) {
a[0] = a[j] / abs(a[j]);
if (!j % 1) a[0] *= (-1);
break;
}
}
}
if (!a[0]) {
a[0] = 1;
}
L += a[0];
L *= (-1);
L = L / abs(L);
res2 += (b[0] - L);
} else if (L < 0) {
if (a[i] < -L + 1) {
res2 += -L + 1 - a[i];
a[i] = -L + 1;
L = 1;
} else {
L += a[i];
}
} else if (L > 0) {
if (a[i] > -L - 1) {
res2 += abs(a[i] - (-L - 1));
a[i] = -L - 1;
L = -1;
} else {
L += a[i];
}
}
}
cout << min(res, res2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
long long 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 cng = 0;
long long ans1 = 0, ans2 = 0;
if (a[0] == 0)
{
if (0 < a[1])
{
cng--;
}
else
{
cng++;
}
ans++;
}
for (int i = 1; i < n; i++)
{
if (((s[i] + cng < 0) && (0 < s[i - 1] + cng)) || ((s[i] + cng > 0) && (0 > s[i - 1] + cng)))
{
//ok
continue;
}
assert(s[i - 1] + cng != 0);
if (s[i] + cng >= 0) //減らさないといけない
{
ans += s[i] + cng + 1;
cng -= s[i] + cng + 1;
}
else //増やさないといけない
{
ans += -(s[i] + cng) + 1;
cng += -(s[i] + cng) + 1;
}
}
cout << ans << endl;
return 0;
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
int odd = 0, even = 0, sum, sign;
sum = 0;
sign = 1;
for (int i = 0; i < n; i++) {
sum += a[i];
if (sum == 0 || (sum < 0 && sign == 1) || (sum > 0 && sign == -1)) {
odd += abs(sum) + 1;
sum = sign;
}
sign *= -1;
}
sum = 0;
sign = -1;
for (int i = 0; i < n; i++) {
sum += a[i];
if (sum == 0 || (sum < 0 && sign == 1) || (sum > 0 && sign == -1)) {
even += abs(sum) + 1;
sum = sign;
}
sign *= -1;
}
cout << min(odd, even) << 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[i];
int odd_count = 0, odd_sum_count;
for (int i = 0; i < (int)(n); i++) {
odd_sum_count += a[i];
if (i % 2 == 0) {
while (odd_sum_count >= 0) {
odd_sum_count--;
odd_count++;
}
} else {
while (odd_sum_count <= 0) {
odd_sum_count++;
odd_count++;
}
}
}
int even_count = 0, even_sum_count;
for (int i = 0; i < (int)(n); i++) {
even_sum_count += a[i];
if (i % 2 == 0) {
while (even_sum_count <= 0) {
even_sum_count++;
even_count++;
}
} else {
while (even_sum_count >= 0) {
even_sum_count--;
even_count++;
}
}
}
cout << min(even_count, odd_count) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> a(N);
for (int i = 0; i < N; i++) cin >> a.at(i);
int count = 0;
if (a.at(0) == 0) {
a.at(0) = 1;
count++;
}
int sum = a.at(0);
for (int i = 1; i < N; i++) {
if (sum > 0 && sum + a.at(i) >= 0) {
count += abs(-1 - sum - a.at(i));
a.at(i) = -1 - sum;
} else if (sum < 0 && sum + a.at(i) <= 0) {
count += abs(1 - sum - a.at(i));
a.at(i) = 1 - sum;
}
sum += a.at(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 | UNKNOWN | n = gets.to_i
arr = gets.chomp.split(" ").map(&:to_i)
$count = 0
def check(i,arr)
if i > arr.size - 1
arr[1] += 1
$count += 1
return
end
if arr[i] > 0
arr[1] -= 1
elsif arr[i] < 0
arr[1] += 1
else
check(i+1,arr)
end
end
num = arr[0] + arr[1]
if num == 0
check(2,arr)
end
num = arr[0] + arr[1]
(2...arr.size).each do |i|
diff = num + arr[i]
# puts %(num : #{num})
# puts %(diff : #{diff})
if num > 0
if diff > 0
arr[i] -= diff.abs+1
$count += diff.abs+1
end
else
if diff < 0
arr[i] += diff.abs+1
$count += diff.abs+1
end
end
if diff == 0
if num > 0
arr[i] -= 1
else
arr[i] += 1
end
$count += 1
end
num += arr[i]
end
#p arr
puts $count |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int 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 (a(sum) * a(in) < 0 && abs(sum) < abs(in)) {
sum += in;
continue;
} else if (a(sum) * a(in) < 0) {
ans += abs(sum) - abs(in) + 1;
if (sum > 0)
sum = -1;
else
sum = 1;
continue;
}
ans += abs(sum) + abs(in) + 1;
if (a(sum) < 0) {
sum = 1;
} else {
sum = -1;
}
}
if (sum == 0) ans++;
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import numpy as np
n=int(input())
a=list(map(int,input().split()))
r=[0]
for i in range(n):
r.append(r[i]+a[i])
r.pop(0)
q=[r[i] for i in range(n)]
pm=[1-2*(i%2) for i in range(n)]
mp=[1-2*((i+1)%2) for i in range(n)]
sum1,sum2=0,0
sousa1,sousa2=0,0
for i in range(n):
if np.sign(r[i]+sousa1) != pm[i]:
sum1+=abs(pm[i]-r[i]-sousa1)
sousa1=pm[i]-r[i]
for i in range(n):
if np.sign(q[i]+sousa1) != mp[i]:
sum2+=abs(mp[i]-q[i]-sousa2)
sousa2=mp[i]-q[i]
print(min(sum1,sum2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | N = gets.to_i
a = gets.split.map(&:to_i)
first = a.shift
count = 0
if first < 0
first = first * (-1)
a.map! do |i|
i * (-1)
end
end
if first == 0
first = 1
count += 1
end
b = []
a.each do |ai|
b << ai
if b.size.odd?
if first + b.inject(:+) > -1
difference = first + b.inject(:+) - (-1)
count += difference.abs
b[-1] -= difference
end
else
if first + b.inject(:+) < 1
difference = first + b.inject(:+) - (+1)
count += difference.abs
b[-1] -= difference
end
end
#p "diff = #{difference}"
#p "b = #{b}"
end
p count
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> a(N);
for (int i = 0; i < N; i++) cin >> a.at(i);
bool fla = false;
for (int i = 0; i < N; i++) {
if (a.at(i) != 0) {
if ((a.at(i) > 0) && (i % 2 == 0))
fla = true;
else if (i % 2 == 1)
fla = true;
break;
}
}
int t = 0, res = 0;
for (int i = 0; i < N; i++) {
int b = a.at(i);
if (fla) {
if (t + b <= 0) {
while (t + b <= 0) {
b++;
res++;
}
}
} else {
if (t + b >= 0) {
while ((t + b >= 0)) {
b--;
res++;
}
}
}
t += b;
fla = !fla;
}
cout << res << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | def main():
n = int(input())
A = [int(a) for a in input().split()]
min_count = float('inf')
if A[0] != 0:
tot = A[0]
count = 0
for a in A[1:]:
if tot > 0 and tot + a >= 0:
count += tot+a+1
tot = -1
elif tot < 0 and tot + a <= 0:
count += -(tot+a)+1
tot = 1
else:
tot += a
min_count = min(min_count, count)
else:
for i in [1, -1]:
count = 1
tot = i
for a in A[1:]:
if tot > 0 and tot + a >= 0:
count += tot+a+1
tot = -1
elif tot < 0 and tot + a <= 0:
count += -(tot+a)+1
tot = 1
else:
tot += a
min_count = min(min_count, count)
return min_count
if __name__ == '__main__':
print(main()) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = [int(ai) for ai in input().split()]
count = 0
a_sum = a[0]
for ai in a[1:]:
tmp_sum = a_sum + ai
if tmp_sum < 0 and a_sum < 0:
c = +1 - tmp_sum
a_sum = 1
elif tmp_sum > 0 and a_sum > 0:
c = -1 - tmp_sum
a_sum = -1
elif tmp_sum == 0 and a_sum < 0:
c = 1
a_sum = 1
elif tmp_sum == 0 and a_sum > 0:
c = 1
a_sum = -1
else:
c = 0
count += abs(c)
a_sum = tmp_sum + c
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;
int main() {
ll acc, ans;
ll N;
vector<ll> A;
cin >> N;
for (ll i = 0; i < N; i++) {
ll tmp;
cin >> tmp;
A.push_back(tmp);
}
acc = A[0];
ans = 0;
for (ll i = 1; i < N; i++) {
ll next = acc + A[i];
if (acc * next >= 0) {
ans += abs(next) + 1;
next = -1 * (acc / abs(acc));
}
acc = next;
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
const int mod = 1e9 + 7;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
long long s_i = a[0];
long long s_i_1;
int d = 0;
int c = 0;
for (int i = 1; i < n; i++) {
if (s_i == 0) {
s_i += 1;
c++;
}
s_i_1 = s_i + a[i];
if ((s_i_1 > 0 && s_i > 0) || (s_i < 0 && s_i_1 < 0)) {
d = abs(s_i_1 - s_i);
if (s_i > 0) {
if (s_i_1 != 0) {
s_i_1 -= d + 1;
c += d + 1;
} else {
s_i_1 -= 1;
d += 1;
}
} else {
if (s_i_1 != 0) {
s_i_1 += d + 1;
c += d + 1;
} else {
s_i_1 += 1;
c += 1;
}
}
}
s_i = s_i_1;
}
if (s_i == 0) {
c += 1;
}
cout << c << 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 | #!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')
#A
def A():
a = input().split()
a = list(map(lambda x: x.capitalize(), a))
a,b,c = a
print(a[0]+b[0]+c[0])
return
#B
def B():
a = II()
b = II()
if a > b:
print("GREATER")
if a < b:
print("LESS")
if a == b:
print("EQUAL")
return
#C
def C():
n = II()
a = LI()
if a[-1] == 0:
suma = 1
else:
suma = a[0]
b = 0
for i in a[1:]:
if (suma + i) * suma < 0:
suma += i
continue
b += abs(suma + i) + 1
suma = -1 * (suma > 0) or 1
ans = b
if a[-1] == 0:
suma = -1
else:
suma = -a[0]
b = 0
for i in a[1:]:
if (suma + i) * suma < 0:
suma += i
continue
suma = -1 * (suma > 0) or 1
b += abs(suma + i) + 1
print(min(ans,b))
return
#D
def D():
s = S()
for i in range(len(s) - 1):
if s[i] == s[i+1]:
print(i + 1, i + 2)
return
for i in range(len(s) - 2):
if s[i] == s[i + 2]:
print(i + 1, i + 3)
return
print(-1, -1)
return
#Solve
if __name__ == '__main__':
C()
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int,input().split()))
if a[0] > 0:
f = 1
else:
f = -1
m = a[0]
cnt = 0
for i in range(1, n):
f *= -1
m += a[i]
if f == 1:
if m > 0:
continue
else:
cnt += f-m
m += f-m
else:
if m < 0:
continue
else:
cnt += m-f
m += f-m
print(cnt)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 |
n = int(input())
al = list(map(int, input().split()))
ans1 = al[0]
curr_sum = 0
next_sign = 1
for a in al[1:]:
next_sum = curr_sum + a
if next_sum >= 0 and next_sign == -1:
ans1 += next_sum+1
next_sum = -1
elif next_sum <= 0 and next_sign == 1:
ans1 += (1-next_sum)
next_sum = 1
curr_sum = next_sum
next_sign *= -1
ans2 = al[0]
curr_sum = 0
next_sign = -1
for a in al[1:]:
next_sum = curr_sum + a
if next_sum >= 0 and next_sign == -1:
ans2 += next_sum+1
next_sum = -1
elif next_sum <= 0 and next_sign == 1:
ans2 += (1-next_sum)
next_sum = 1
curr_sum = next_sum
next_sign *= -1
print(min(ans1,ans2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = (int)(0); i < (int)(n); i++) cin >> a[i];
int ans = 0;
int sum = a[0];
for (int i = (int)(0); i < (int)(n - 1); i++) {
if (sum < 0) {
if (a[i + 1] < abs(sum)) {
ans += abs(sum) - a[i + 1] + 1;
a[i + 1] += abs(sum) - a[i + 1] + 1;
}
sum += a[i + 1];
} else if (sum > 0) {
if (a[i + 1] > -sum) {
ans += a[i + 1] + sum + 1;
a[i + 1] -= a[i + 1] + sum - 1;
}
sum += a[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 | cpp | #include <bits/stdc++.h>
using namespace std;
int n;
long long solve(vector<long long>& pref, long long x) {
vector<long long> mod(n + 1);
mod[1] += x;
long long res = 0;
for (int i = 2; i <= n; i++) {
mod[i] += mod[i - 1];
long long now = pref[i] + mod[i];
long long prev = pref[i - 1] + mod[i - 1];
if (now == 0) {
if (prev > 0) {
res += 1;
mod[i] -= 1;
} else {
res += 1;
mod[i] += 1;
}
} else {
if (prev > 0 && now > 0) {
res += now + 1;
mod[i] -= now + 1;
}
if (prev < 0 && now < 0) {
res += 1 - now;
mod[i] += 1 - now;
}
}
}
return res + x;
}
int main() {
cin >> n;
vector<long long> a(n), pref(n + 1);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
pref[i + 1] = pref[i] + a[i];
}
long long ans = 1e18;
if (a[0] == 0) {
ans = min(ans, solve(pref, 1));
ans = min(ans, solve(pref, -1));
} else {
ans = min(ans, solve(pref, 0));
if (a[0] > 0)
ans = min(ans, solve(pref, -1 - a[0]));
else
ans = min(ans, solve(pref, 1 - a[0]));
}
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;
long long sum1, sum2, ans1, ans2, x;
int n;
int main() {
sum1 = sum2 = ans1 = ans2;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%lld", &x);
sum1 += x;
sum2 += x;
if (i & 1) {
if (sum1 >= 0) {
ans1 += sum1 + 1;
sum1 = -1;
}
if (sum2 <= 0) {
ans2 += 1 - sum2;
sum2 = 1;
}
} else {
if (sum1 <= 0) {
ans1 += 1 - sum1;
sum1 = 1;
}
if (sum2 >= 0) {
ans2 += sum2 + 1;
sum2 = -1;
}
}
}
printf("%d\n", min(ans1, ans2));
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int INTMAX = 2147483647;
const int64_t LLMAX = 9223372036854775807;
const int MOD = 1000000007;
template <class T>
inline bool chmax(T& a, const T& b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
inline bool chmin(T& a, const T& b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
inline void swap(int64_t& a, int64_t& b) {
a ^= b;
b ^= a;
a ^= b;
}
inline void swap(int& a, int& b) {
a ^= b;
b ^= a;
a ^= b;
}
int main() {
int n;
int64_t ans = 0, tmp, s;
vector<int64_t> a;
cin >> n;
a.resize(n);
for (int i{0}; i < (int)(n); i++) cin >> a[i];
s = a[0];
tmp = 0;
if (s < 0) {
tmp += (1 - s);
s = 1;
}
for (int i{1}; i < (int)(n); i++) {
if (!(s + a[i]) || (s ^ (s + a[i])) >= 0) {
if (s > 0) {
tmp += 1LL + s + a[i];
s = -1;
} else {
tmp += 1LL - (s + a[i]);
s = 1;
}
} else
s += a[i];
}
ans = tmp;
s = a[0];
tmp = 0;
if (s > 0) {
tmp += 1LL + s;
s = -1;
}
for (int i{1}; i < (int)(n); i++) {
if (!(s + a[i]) || (s ^ (s + a[i])) >= 0) {
if (s > 0) {
tmp += 1LL + s + a[i];
s = -1;
} else {
tmp += 1LL - (s + a[i]);
s = 1;
}
} else
s += a[i];
}
chmin(ans, tmp);
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<ll> v(n, 0ll);
for (int i = (int)(0); i < (int)(n); i++) cin >> v[i];
vector<ll> memo(n + 1, 0ll);
memo[1] = v[0];
ll cnt = 0;
for (int i = 2; i <= n; i++) {
memo[i] = memo[i - 1] + v[i - 1];
if (not(memo[i - 1] * memo[i] < 0)) {
if (memo[i] < 0) {
int plus = memo[i] * (-1) + 1;
memo[i] += plus;
cnt += plus;
} else if (memo[i] > 0) {
int plus = memo[i] * -1 - 1;
memo[i] += plus;
cnt -= plus;
} else {
if (memo[i - 1] < 0) {
memo[i]++;
cnt++;
} else {
memo[i]--;
cnt++;
}
}
}
}
cout << cnt << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> a(N);
for (int i = 0; i < N; i++) {
cin >> a[i];
}
int ans = 0;
while (ans < N && a[ans] == 0) ans++;
int sum;
if (ans == 0)
sum = a[0];
else if (ans < N && a[ans + 1] > 0)
sum = -1;
else
sum = 1;
for (int i = ans + 1; i < N; i++) {
if (sum * (sum + a[i]) >= 0) {
if (sum + a[i] >= 0) {
ans += abs(sum + a[i] + 1);
a[i] = -sum - 1;
} else {
ans += abs(sum + a[i] - 1);
a[i] = -sum + 1;
}
}
sum += a[i];
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int sign(long long n) { return ((n > 0) - (n < 0)); }
int main() {
int n;
scanf("%d", &n);
long long sum = 0;
long long ans = 0;
for (int i = 0; i < n; i++) {
int ra;
scanf("%d", &ra);
long long a = ra;
if (sum == 0) {
if (a == 0) {
a++;
ans++;
}
} else if (sum + a == 0) {
a - sign(sum);
ans++;
} else if (sign(sum + a) + sign(sum) != 0) {
long long tmp = a;
if (sum + a > 0) {
a = a - (sum + a) - 1;
} else if (sum + a < 0) {
a = a - (sum + a) + 1;
} else {
a -= sign(sum);
}
ans += abs(tmp - a);
}
sum += a;
}
printf("%llu\n", ans);
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
long long sum1 = 0, sum2 = 0;
long long ans1 = 0, ans2 = 0;
for (int i = 0; i < N; ++i) {
long long t;
cin >> t;
if (i == 0) {
if (t == 0) {
sum1 = 1;
ans1 = 1;
sum2 = -1;
ans2 = -1;
} else {
sum1 = t;
sum2 = t > 0 ? -1 : 1;
ans2 += t + 1;
}
} else {
if (sum1 < 0 && sum1 + t <= 0) {
ans1 += 1 - sum1 - t;
sum1 = 1;
} else if (sum1 > 0 && sum1 + t >= 0) {
ans1 += abs(-1 - sum1 - t);
sum1 = -1;
} else {
sum1 += t;
}
if (sum2 < 0 && sum2 + t <= 0) {
ans2 += 1 - sum2 - t;
sum2 = 1;
} else if (sum2 > 0 && sum2 + t >= 0) {
ans2 += abs(-1 - sum2 - t);
sum2 = -1;
} else {
sum2 += t;
}
}
}
cout << min(ans1, ans2) << "\n";
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | # n + 1 長の array を返す
def cumulative_sum(array)
tmp = [0]
previous = 0
array.each { |num| tmp << previous += num }
tmp
end
n = gets.to_i
nums = gets.split.map(&:to_i)
cumulative_sums = cumulative_sum(nums)
cumulative_sums.delete_at(0)
previous_plus = cumulative_sums.delete_at(0) > 0 ? true : false
ans = 0
added = 0
cumulative_sums.each_with_index do |sum, i|
if previous_plus && sum + added >= 0
ans += (sum + added).abs + 1
added -= (sum + added).abs + 1
elsif !previous_plus && sum + added <= 0
ans += (sum + added).abs + 1
added += (sum + added).abs + 1
end
previous_plus = !previous_plus
end
puts ans
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java |
import java.util.*;
import java.io.*;
import java.math.BigInteger;
public class Main {
private static final int mod =(int)1e9+7;
public static void main(String[] args) throws Exception {
Scanner sc=new Scanner(System.in);
PrintWriter out=new PrintWriter(System.out);
int n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++) {
a[i]=sc.nextInt();
}
long sum=a[0];
long operations=0;
if(sum==0) {
sum++;
operations++;
}
for(int i=1;i<n;i++) {
if(sum>0) {
if(sum+a[i]<0) {
sum+=a[i];
}else {
if(sum+a[i]==0) {
sum+=a[i]-1;
operations++;
}else {
long req=-1-1*sum;
sum=-1;
operations+=(-1*req+a[i]);
}
}
}else {
if(sum+a[i]>0) {
sum+=a[i];
}else {
if(sum+a[i]==0) {
sum+=a[i]+1;
operations++;
}else {
long req=1+-1*sum;
sum=1;
operations+=(req-a[i]);
}
}
}
}
System.out.println(operations);
}
static boolean vis[]=new boolean[10001];
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
// Function to find gcd of array of
// numbers
static int f(int arr[], int n)
{
int result = n;
int max=-1;
int ans=0;
for (int element: arr){
if(vis[element]==false)
result = gcd(n, element);
if(result>max) {
max=result;
ans=element;
}
}
return ans;
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | import Control.Applicative
import Data.List
main = do
_ <- getLine
as <- (map read) . words <$> getLine
print $ sum $ zipWith (\a b -> abs (a-b)) as (f 0 as)
where
f _ [] = []
f sum (x:xs)
| sum < 0 = if sum + x > 0 then x : f (sum+x) xs else (1-sum) : f 1 xs
| sum > 0 = if sum + x < 0 then x : f (sum+x) xs else (-1-sum) : f (-1) xs
| sum == 0 = x : f x xs
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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 j = 0; j < N; j++) {
cin >> a[j];
}
int c1 = 0, c2 = 0;
int sum = 0;
for (int i = 0; i < N; i++) {
sum += a[i];
if (i % 2 == 1 && sum >= 0) {
c1 += sum + 1;
sum = -1;
}
if (i % 2 == 0 && sum <= 0) {
c1 += -sum + 1;
sum = 1;
}
}
sum = 0;
for (int i = 0; i < N; i++) {
sum += a[i];
if (i % 2 == 1 && sum <= 0) {
c2 += -sum + 1;
sum = 1;
}
if (i % 2 == 0 && sum >= 0) {
c2 += sum + 1;
sum = -1;
}
}
cout << min(c1, c2) << 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>
#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
//#define int long long
using bll = boost::multiprecision::cpp_int;
using ll = long long;
//constexpr int INF = 1e9;//INT_MAX=(1<<31)-1=2147483647
constexpr ll INF = (ll)1e18;//(1LL<<63)-1=9223372036854775807
constexpr ll MOD = (ll)1e9 + 7;
constexpr double EPS = 1e-9;
constexpr int dx[4]={1,0,-1,0};
constexpr int dy[4]={0,1,0,-1};
#define p(var) std::cout<<var<<std::endl
#define PI (acos(-1))
#define rep(i, n) for(ll i=0, i##_length=(n); i< i##_length; ++i)
#define repeq(i, n) for(ll i=1, i##_length=(n); i<=i##_length; ++i)
#define all(v) (v).begin(), (v).end()
#define uniq(v) (v).erase(unique((v).begin(), (v).end()), (v).end());
template<typename T> inline void pv(vector<T> v) { for(ll i=0, N=v.size(); i<N; i++) cout<< v[i] << (i==N-1 ? '\n' : ' '); }
template<typename T> inline T gcd(T a, T b) { return b ? gcd(b,a%b) : a; }
template<typename T> inline T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<typename T1, typename T2> inline T1 power(T1 x, T2 n){ return n ? power(x*x%MOD,n/2)*(n%2?x:1)%MOD : 1; }
template<typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template<typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }
template<typename T> class dvector : public std::vector<T> {
public:
dvector() : std::vector<T>() {}
explicit dvector(size_t n, const T& value = T()) : std::vector<T>(n,value) {}
dvector(const std::vector<T>& v) : std::vector<T>(v) {}
T& operator[](size_t n){ return this->at(n); }
};
template<typename T1, typename T2> ostream& operator<<(ostream& s, pair<T1, T2>& p) {return s << "(" << p.first << ", " << p.second << ")";}
template<typename T> ostream& operator<<(ostream& s, dvector<T>& v) {
for (int i = 0, len = v.size(); i < len; ++i){ s << v[i]; if (i < len - 1) s << "\t"; } return s; }
template<typename T> ostream& operator<<(ostream& s, dvector< dvector<T> >& vv) {
for (int i = 0, len = vv.size(); i < len; ++i){ s << vv[i] << endl; } return s; }
template<typename T1, typename T2> ostream& operator<<(ostream& s, map<T1, T2>& m) {
s << "{" << endl; for (auto itr = m.begin(); itr != m.end(); ++itr){ s << "\t" << (*itr).first << " : " << (*itr).second << endl; } s << "}" << endl; return s; }
template<typename T> ostream& operator<<(ostream& s, set<T>& se) {
s << "{ "; for (auto itr = se.begin(); itr != se.end(); ++itr){ s << (*itr) << "\t"; } s << "}" << endl; return s; }
template<typename T> ostream& operator<<(ostream& s, multiset<T>& se) {
s << "{ "; for (auto itr = se.begin(); itr != se.end(); ++itr){ s << (*itr) << "\t"; } s << "}" << endl; return s; }
#ifdef LOCAL_DEV
#define debug(var) std::cout<<#var" = "<<var<<std::endl
#else
#define debug(var)
#endif
#ifdef LOCAL_TEST
#define vector dvector
#endif
/*-----8<-----8<-----*/
signed main() {
ll N;
cin>>N;
vector<ll> a(N,0);
rep(i,N)cin>>a[i];
vector<ll> rui(N+1,0);
rep(i,N)rui[i+1]=rui[i]+a[i];
ll c,t=a[0]>0 ? 1 : -1;
if([&]{
rep(i,N-1){
if(t==1){
if(rui[i+2]>0)return false;
}else{
if(rui[i+2]<0)return false;
}
t*=-1;
}
return true;
}()){
p(0);return 0;
}
//+
t=0;
ll ansb=0;
if(rui[1]>0){
}else{
t+=-rui[1]+1;
ansb+=abs(-rui[1]+1);
}
c=-1;
for(ll i=1;i<N;i++){
ll tt=rui[i+1]+t;
if(c==1){
if(tt>0){
}else{
t+=-tt+1;
ansb+=abs(-tt+1);
}
}else{
if(tt>0){
t+=-tt-1;
ansb+=abs(-tt-1);
}else{
}
}
c*=-1;
}
//-
t=0;
ll ansc=0;
if(rui[1]>0){
t+=-rui[1]-1;
ansc+=abs(-rui[1]-1);
}else{
}
c=1;
for(ll i=1;i<N;i++){
ll tt=rui[i+1]+t;
if(c==1){
if(tt>0){
}else{
t+=-tt+1;
ansc+=abs(-tt+1);
}
}else{
if(tt>0){
t+=-tt-1;
ansc+=abs(-tt-1);
}else{
}
}
c*=-1;
}
p(min(ansb,ansc));
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <iostream>
#include <cmath>
using namespace std;
int sign(int a){
if (a > 0)
return 1;
else
if (a < 0)
return -1;
else
return 0;
}
int main(void){
int n;
cin >> n;
int sum = 0, s = 1;
int a;
int num = 0;
for (int i = 0; i < n; i++){
cin >> a;
sum += a;
if (i != 0 && (sign(s) == sign(sum) || sum = 0)){
num += abs(sum) + 1;
sum = sign(s) * -1;
s *= -1;
}
else {
s = sign(sum);
}
}
cout << num << endl;
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import numpy as np
import copy
N=int(input())
l=list(map(int, input().split())) #リスト入力
cp = copy.copy(l)
#even_c=0
#odd_c=0
c=0
if l[0]!=0:
#l[0]=-1
#even_c=even_c+1
for k in range(1,N):
#print(sum(l[:k]),sum(l[:k+1]))
s=sum(l[:k])
if s*sum(l[:k+1])>0:
#print(k)
c=c+abs(-np.sign(s)-s-l[k])
l[k]=-np.sign(s)-s
#print(l)
if sum(l)==0:
print(c+1)
#print(1+sum([abs(l[n]-cp[n]) for n in range(N)]))
else:
print(c)
#print(sum([abs(l[n]-cp[n]) for n in range(N)]))
else:
#1
sei_l=copy.copy(l)
sei_l[0]=1
for k in range(1,N):
#print(sum(l[:k]),sum(l[:k+1]))
if sum(sei_l[:k])*sum(sei_l[:k+1])>0:
#print(k)
sei_l[k]=-np.sign(sum(sei_l[:k]))-sum(sei_l[:k])
#print(l)
if sum(sei_l)==0:
c1=1+sum([abs(sei_l[n]-cp[n]) for n in range(N)])
else:
c1=sum([abs(sei_l[n]-cp[n]) for n in range(N)])
#-1
fu_l=copy.copy(l)
sei_l[0]=-1
for k in range(1,N):
#print(sum(l[:k]),sum(l[:k+1]))
if sum(fu_l[:k])*sum(fu_l[:k+1])>0:
#print(k)
fu_l[k]=-np.sign(sum(fu_l[:k]))-sum(fu_l[:k])
#print(l)
if sum(fu_l)==0:
c2=1+sum([abs(fu_l[n]-cp[n]) for n in range(N)])
else:
c2=sum([abs(fu_l[n]-cp[n]) for n in range(N)])
print(max(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;
using P = pair<long long, long long>;
using Graph = vector<vector<long long>>;
using ll = long long;
ll gcd(ll A, ll B) {
if (B == 0) return A;
return gcd(B, A % B);
}
ll lcm(ll A, ll B) { return A * B / gcd(A, B); }
bool root(string s) {
long long n = s.length();
if (n == 2) {
if (s[0] == s[1]) {
return true;
} else
return false;
}
if (n == 3) {
if (s[0] == s[2]) {
return true;
} else
return false;
}
return false;
}
bool IsPrime(long long num) {
if (num < 2)
return false;
else if (num == 2)
return true;
else if (num % 2 == 0)
return false;
double sqrtNum = sqrt(num);
for (long long i = 3; i <= sqrtNum; i += 2) {
if (num % i == 0) {
return false;
}
}
return true;
}
vector<long long> seen;
vector<ll> h;
void dfs(const Graph &G, long long v) {
seen[v] = 0;
P good = make_pair(v, h[v]);
for (auto next_v : G[v]) {
if (seen[next_v] != -1) continue;
if (h[next_v] > good.second) {
good = make_pair(next_v, h[next_v]);
}
dfs(G, next_v);
}
if (!(h[v] == good.second && v == good.first)) {
seen[good.first] = 1;
}
}
long long n;
void dfs(string s, char mx) {
if (s.length() == n) {
cout << s << endl;
} else {
for (char c = 'a'; c <= mx; c++) {
dfs(s + c, ((c == mx) ? (char)(mx + 1) : mx));
}
}
}
signed main() {
long long n;
cin >> n;
long long a[n];
for (long long i = 0; i < n; i++) cin >> a[i];
long long ans = 0;
long long sum[n];
if (a[0] == 0) {
ans++;
if (a[1] > 0) {
sum[0] = -1;
} else {
sum[0] = 1;
}
}
sum[0] = a[0];
bool pred = sum[0] > 0;
{
for (long long i = 1; i < n; i++) {
if (sum[i - 1] + a[i] == 0) {
ans++;
if (pred) {
sum[i] = -1;
pred = false;
} else {
sum[i] = 1;
pred = true;
}
} else {
if (pred != (sum[i - 1] + a[i] > 0)) {
sum[i] = sum[i - 1] + a[i];
pred = !pred;
} else {
if (sum[i - 1] + a[i] > 0) {
sum[i] = -1;
} else
sum[i] = 1;
ans += abs(sum[i - 1] + a[i]) + 1;
pred = !pred;
}
}
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
int main(void) {
int n;
std::cin >> n;
std::vector<long long int> a(n);
for (int i = 0; i < n; i++) {
std::cin >> a[i];
}
long long int count = 0;
if (a[0] == 0) {
count++;
a[0] += 1;
}
long long int sum = a[0];
for (int i = 1; i < n; i++) {
if (sum > 0) {
sum += a[i];
if (sum >= 0) {
count += sum + 1;
sum = -1;
}
} else {
sum += a[i];
if (sum <= 0) {
count += -sum + 1;
sum = 1;
}
}
}
std::cout << count << std::endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, a, cnt = 0, sum;
cin >> n >> sum;
for (int i = 1; i < n; ++i) {
cin >> a;
if (sum > 0) {
sum += a;
if (sum >= 0) {
cnt += sum + 1;
sum = -1;
}
} else {
sum += a;
if (sum <= 0) {
cnt += 1 - sum;
sum = 1;
}
}
}
cout << cnt << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
struct speedup {
speedup() {
cin.tie();
ios::sync_with_stdio(false);
cout << fixed << setprecision(18);
}
} speedup;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < (int)n; i++) cin >> a[i];
vector<long long> wa(n);
long long ans = 0;
for (int i = 0; i < (int)n; i++) {
if (i == 0) {
if (a[i] == 0) {
if (a[i] > 0) {
ans++;
wa[i] = -1;
} else {
ans++;
wa[i] = 1;
}
} else {
wa[i] = a[i];
}
} else {
wa[i] = wa[i - 1] + a[i];
if (wa[i - 1] > 0 && wa[i] > 0) {
ans += wa[i] + 1;
wa[i] = -1;
} else if (wa[i - 1] < 0 && wa[i] < 0) {
ans += abs(wa[i]) + 1;
wa[i] = 1;
} else if (wa[i] == 0) {
if (wa[i - 1] < 0) {
ans++;
wa[i] = 1;
} else {
ans++;
wa[i] = -1;
}
}
}
}
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;
const int INF = 999999999;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int sum1 = 0;
int count1 = 0;
int sum2 = 0;
int count2 = 0;
for (int i = 0; i < n; i++) {
sum1 += a[i];
if (i % 2 == 0) {
if (sum1 <= 0) {
count1 += abs(sum1) + 1;
sum1 = 1;
}
} else {
if (sum1 >= 0) {
count1 += abs(sum1) + 1;
sum1 = -1;
}
}
}
for (int i = 0; i < n; i++) {
sum2 += a[i];
if (i % 2 == 0) {
if (sum2 >= 0) {
count2 += abs(sum2) + 1;
sum2 = -1;
}
} else {
if (sum2 <= 0) {
count2 += abs(sum2) + 1;
sum2 = 1;
}
}
}
cout << min(count1, count2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | # -*- coding: utf-8 -*-
n = input()
a = [int(x) for x in input().split()]
ans = 1e16
for s in (1, -1):
res, cumsum = 0, 0
for j in range(n):
cumsum += a[j]
if cumsum * s <= 0:
res += abs(cumsum-s)
cumsum = s
s *= -1
ans = min(ans, res)
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N, a[100000];
cin >> N;
for (int i = 0; i < N; ++i) cin >> a[i];
int counter = 0;
long long b[100000];
if (a[0] == 0) {
++a[0];
++counter;
b[0] = a[0];
for (int i = 1; i < N; ++i) {
b[i] = b[i - 1] + a[i];
if (i % 2 == 0) {
while (b[i - 1] * b[i] >= 0) {
++b[i];
++counter;
}
} else {
while (b[i - 1] * b[i] >= 0) {
--b[i];
++counter;
}
}
}
} else if (a[0] > 0) {
b[0] = a[0];
for (int i = 1; i < N; ++i) {
b[i] = b[i - 1] + a[i];
if (i % 2 == 0) {
while (b[i - 1] * b[i] >= 0) {
++b[i];
++counter;
}
} else {
while (b[i - 1] * b[i] >= 0) {
--b[i];
++counter;
}
}
}
} else {
b[0] = a[0];
for (int i = 1; i < N; ++i) {
b[i] = b[i - 1] + a[i];
if (i % 2 == 0) {
while (b[i - 1] * b[i] >= 0) {
--b[i];
++counter;
}
} else {
while (b[i - 1] * b[i] >= 0) {
++b[i];
++counter;
}
}
}
}
cout << counter << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import sys
def input():
return sys.stdin.readline().strip()
sys.setrecursionlimit(20000000)
def main():
N = int(input())
A = list(map(int, input().split()))
S = A[0]
cnt = 0
if A[0] == 0:
for a in (1, -1):
cnt = 1
S = a
for i in range(1, N):
s = S + A[i]
if A[i] == 0:
if S > 0:
cnt += S + 1
S = -1
else:
cnt += abs(S) + 1
S = 1
else:
if s == 0:
if S < 0:
cnt += 1
S = 1
else:
cnt += 1
S = -1
else:
if S * s > 0:
if S < 0:
cnt += abs(s) + 1
S = 1
else:
cnt += s + 1
S = -1
else:
S = s
else:
for i in range(1, N):
s = S + A[i]
if A[i] == 0:
if S > 0:
cnt += S + 1
S = -1
else:
cnt += abs(S) + 1
S = 1
else:
if s == 0:
if S < 0:
cnt += 1
S = 1
else:
cnt += 1
S = -1
else:
if S * s > 0:
if S < 0:
cnt += abs(s) + 1
S = 1
else:
cnt += s + 1
S = -1
else:
S = s
print(cnt)
if __name__ == "__main__":
main()
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void) {
int n;
cin >> n;
long long int a[n];
for (int i = 0; i < (n); i++) {
cin >> a[i];
}
long long int count = 0;
long long int sum = 0;
bool p = false;
bool plus = true;
for (int i = 0; i < (n); i++) {
sum += a[i];
if (!p && a[i] == 0) {
count++;
} else if (!p && a[i] > 0) {
plus = true;
p = true;
if (count > 0) sum = a[i] - 1;
} else if (!p && a[i] < 0) {
plus = false;
p = true;
if (count > 0) sum = a[i] + 1;
} else if (!plus && sum >= 0) {
count += sum + 1;
sum = -1;
} else if (plus && sum <= 0) {
count += 1 - sum;
sum = 1;
}
plus = !plus;
}
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>
const int MaxN = 1e5;
using namespace std;
int a[MaxN + 5], sum;
int pos = 0, n;
long long ans = 0;
bool ok = 0;
void Init() {
pos = 0;
ok = 0;
sum = ans = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
if (a[i] != 0 && !pos) pos = i;
}
if (a[1] == 0) {
if (a[pos] < 0) {
if (pos & 1)
a[1] = -1, ans++;
else
a[1] = 1, ans++;
} else if (a[pos] > 0) {
if (pos & 1)
a[1] = 1, ans++;
else
a[1] = -1, ans++;
}
}
if (a[1] > 0) ok = 1;
sum = a[1];
}
void Solve() {
for (int i = 2; i <= n; i++) {
if (ok) {
sum += a[i];
if (sum >= 0) ans += sum + 1, sum = -1;
ok = 0;
} else if (ok == 0) {
sum += a[i];
if (sum <= 0) ans += abs(sum) + 1, sum = 1;
ok = 1;
}
}
printf("%lld\n", ans);
}
int main() {
while (~scanf("%d", &n)) {
Init();
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 <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include <set>
#include <vector>
#include <queue>
using namespace std;
int main(){
int n;
cin >> n;
int64_t sum = 0;
vector<int64_t> arr;
for (int i = 0; i < n; ++i) {
int64_t ai;
cin >> ai;
sum += ai;
arr.push_back(sum);
}
int64_t b1 = 0;
int64_t s1 = 0;
int64_t b2 = 0;
int64_t s2 = 0;
for (int i = 0; i < n; ++i) {
if (i % 2 == 0) {
b1 += max(0LL, 1 - arr[i]-s1);
s1 += max(0LL, 1 - arr[i]-s1);
b2 += max(0LL, arr[i] +s2+ 1);
s2 -= max(0LL, arr[i] +s2+ 1);
}
else {
b1 += max(0LL, arr[i] + s1 + 1);
s1 -= max(0LL, arr[i] + s1 + 1);
b2 += max(0LL, 1 - arr[i] - s2);
s2 += max(0LL, 1 - arr[i] - s2);
}
}
cout << min(b1, b2) << 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 | import sequtils, strutils, algorithm, math, future, sets, tables, hashes, intsets
let read = iterator : string {.closure.} = (while true : (for s in stdin.readLine.split : yield s))
var
n = read().parseint
a = newseqwith(n, read().parseint)
pre: int
ans1 = 0
ans2 = 0
var
sum = a[0]
if sum == 0:
sum = 1
ans1 = 1
for i in 1 ..< n:
if sum * (sum + a[i]) >= 0:
if sum < 0:
ans1 += abs(1 - a[i] - sum)
sum = 1
else:
ans1 += abs(-1 - a[i] - sum)
sum = -1
else:
sum += a[i]
if a[0] >= 0:
sum = -1
ans2 += abs(-1 - a[0])
else:
sum = 1
ans2 += abs(1 - a[0])
for i in 1 ..< n:
if sum * a[i] >= 0:
if sum < 0:
ans2 += abs(1 - a[i] - sum)
sum = 1
else:
ans2 += abs(-1 - a[i] - sum)
sum = -1
else:
sum += a[i]
echo min(ans1, ans2)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int signs[2] = {-1, 1}, cnt[2] = {0, 0};
for (int i = 0; i < 2; i++) {
long long sum = 0;
int sign = signs[i];
for (int j = 0; j < n; j++) {
sum += a[j];
if (sum == 0) {
sum += sign;
cnt[i]++;
} else if (sum * sign < 0) {
sum = sum + sum * (-1) + sign;
cnt[i] = cnt[i] + abs(sum) + 1;
}
sign *= -1;
}
}
cout << (cnt[0] < cnt[1] ? cnt[0] : cnt[1]) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
int main() {
int N;
cin >> N;
long long ans1 = 0, ans2 = 0;
long long sum1[N], sum2[N];
for (int i = 0; i < N; i++) {
long long t;
cin >> t;
if (i == 0) {
if (t > 0) {
sum1[i] = t;
sum2[i] = -1;
ans2 += t + 1;
} else if (t < 0) {
sum1[i] = t;
sum2[i] = 1;
ans2 += -t + 1;
} else {
sum1[i] = 1;
sum2[i] = -1;
}
continue;
}
sum1[i] = sum1[i - 1] + t;
if (sum1[i - 1] > 0) {
if (sum1[i] >= 0) {
ans1 += sum1[i] + 1;
sum1[i] = -1;
}
} else if (sum1[i - 1] < 0) {
if (sum1[i] <= 0) {
ans1 += -sum1[i] + 1;
sum1[i] = 1;
}
}
sum2[i] = sum2[i - 1] + t;
if (sum2[i - 1] > 0) {
if (sum2[i] >= 0) {
ans2 += sum2[i] + 1;
sum2[i] = -1;
}
} else if (sum2[i - 1] < 0) {
if (sum2[i] <= 0) {
ans2 += -sum2[i] + 1;
sum2[i] = 1;
}
}
}
cout << min(ans1, ans2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = [int(_) for _ in input().split()]
sum_i = a[0]
cnt = 0
for i in range(1, n):
if sum_i > 0:
if sum_i + a[i] < 0:
sum_i += a[i]
else:
cnt += abs(a[i]+sum_i+1)
sum_i = -1
elif sum_i < 0:
if sum_i + a[i] > 0:
sum_i += a[i]
else:
cnt += abs(a[i]+sum_i-1)
sum_i = 1
print(cnt) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int count(int sign0, vector<int> a, int n) {
int count = 0;
int total = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 1) {
if ((total + a.at(i)) * sign0 < 0)
total += a.at(i);
else {
count += abs(total + a.at(i)) + 1;
total = -1 * sign0;
}
}
if (i % 2 == 0) {
if ((total + a.at(i)) * sign0 > 0)
total += a.at(i);
else {
count += abs(total + a.at(i)) + 1;
total = sign0;
}
}
}
return count;
}
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a.at(i);
int plus = count(1, a, n);
int minus = count(-1, a, n);
cout << min(plus, minus) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | import qualified Data.Vector.Unboxed as VU
import qualified Data.ByteString.Char8 as B
import Data.Char
solve :: VU.Vector Int -> Int -> Int
solve vec n = otherwise = minimum $ map fst [f, g]
where
t = VU.take 2 vec
d = VU.drop 2 vec
f = VU.foldl' step (fst $ init t) d
g = VU.foldl' step (snd $ init t) d
init :: VU.Vector Int -> ((Int, Int), (Int, Int))
init vec
| a + b == 0 = ((1, 1), (1, negate 1))
| a + b > 0 = ((0, a + b), (1 + a + b, negate 1))
| otherwise = ((0, a + b), (abs (1 - (a + b)), 1))
where
a = VU.head vec
b = VU.last vec
step :: (Int, Int) -> Int -> (Int, Int)
step (res, acc) x
| acc + x == 0 = (res + 1, negate (signum acc))
| (signum acc) /= signum (acc + x) = (res, acc + x)
| otherwise =
let
aim = negate $ signum acc
y = aim - (acc + x)
in
(res + abs y, aim)
main = do
n <- readLn :: IO Int
as <- VU.unfoldrN n (B.readInt . B.dropWhile isSpace) <$> B.getLine
print $ solve as n |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9 + 7;
long long n, g, ans, t;
int main() {
cin >> n;
cin >> t;
if (t == 0) {
int a[100001];
g = 1;
ans += 1;
for (int i = 2; i <= n; i++) {
cin >> a[i];
if (g > 0) {
g += a[i];
if (g > -1) {
ans += g + 1;
g = -1;
}
} else {
g += t;
if (g < 1) {
ans += 1 - g;
g = 1;
}
}
}
long long ans2 = 1;
g = -1;
for (int i = 2; i <= n; i++) {
if (g > 0) {
g += a[i];
if (g > -1) {
ans2 += g + 1;
g = -1;
}
} else {
g += t;
if (g < 1) {
ans2 += 1 - g;
g = 1;
}
}
}
cout << min(ans, ans2);
return 0;
}
g = t;
while (--n) {
cin >> t;
if (g > 0) {
g += t;
if (g > -1) {
ans += g + 1;
g = -1;
}
} else {
g += t;
if (g < 1) {
ans += 1 - g;
g = 1;
}
}
}
cout << ans;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
bool isPositive(int i) { return i >= 0; }
bool isNegative(int i) { return i < 0; }
int operation(vector<int> a) {
int opeCount = 0;
int preSum = 0;
int sum = 0;
int i = 0;
while (i < a.size()) {
if (i == 0) {
if (a[i] == 0) {
if (isPositive(a[i + 1])) {
a[i] = a[i] - 1;
opeCount++;
} else {
a[i] = a[i] + 1;
opeCount++;
}
}
preSum = a[i];
i++;
} else {
sum = preSum + a[i];
if (sum == 0) {
if (isPositive(preSum)) {
a[i] = a[i] - 1;
opeCount++;
} else {
a[i] = a[i] + 1;
opeCount++;
}
} else {
if (isPositive(sum) && isPositive(preSum)) {
a[i] = a[i] - 1;
opeCount++;
} else if (isNegative(sum) && isNegative(preSum)) {
a[i] = a[i] + 1;
opeCount++;
} else {
preSum = sum;
i++;
}
}
}
}
return opeCount;
}
int main() {
int n, ai;
cin >> n;
vector<int> a;
for (int i = 0; i < n; ++i) {
cin >> ai;
a.push_back(ai);
}
int result = operation(a);
cout << result << 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 ans = 0, c, n, count = 0, b = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> c;
if (i == 0) {
count = c;
if (c < 0) b = 1;
} else {
count += c;
if (b == 0) {
if (count >= 0) {
ans += count + 1;
count = -1;
}
b = 1;
} else {
while (count <= 0) {
ans += -1 * count + 1;
count = 1;
}
b = 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 | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<long long> A(N);
long long count = 0;
cin >> A[0];
for (int i = 1; i < N; ++i) {
cin >> A[i];
A[i] += A[i - 1];
if (A[i - 1] < 0) {
if (A[i] < 0) {
count += abs(A[i]) + 1;
A[i] = 1;
} else if (A[i] == 0) {
++A[i];
++count;
}
} else if (0 < A[i - 1]) {
if (0 < A[i]) {
count += abs(A[i]) + 1;
A[i] = -1;
} else if (A[i] == 0) {
--A[i];
++count;
}
}
}
cout << count << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n=int(input())
a=list(map(int,input().split()))
b=[]
for i in range(n):
b.append(a[i])
ct1=0
if a[0]<=0:
a[0]=1
ct1+=1-a[0]
for i in range(1,n):
if i%2==1:
if sum(a[0:i+1])>=0:
ct1+=sum(a[0:i+1])+1
a[i]=-sum(a[0:i])-1
else:
if sum(a[0:i+1])<=0:
ct1+=1-sum(a[0:i+1])
a[i]=-sum(a[0:i])+1
ct2=0
if b[0]>=0:
b[0]=-1
ct2+=b[0]-1
for i in range(1,n):
if i%2==0:
if sum(b[0:i+1])>=0:
ct2+=sum(b[0:i+1])+1
b[i]=-sum(b[0:i])-1
else:
if sum(b[0:i+1])<=0:
ct2+=1-sum(b[0:i+1])
b[i]=-sum(b[0:i])+1
print(min(ct1,ct2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | def solve(digits)
if digits[0].zero?
digits1 = digits.dup
digits1[0] = 1
digits2 = digits.dup
digits2[0] = -1
return [digits1, digits2].map{|dgt| solve(dgt)}.min + 1
end
sum = digits[0]
cnt = 0
(1...digits.size).each do |i|
sum1 = sum
sum2 = sum1 + digits[i]
if sum1 * sum2 >= 0
target = sum1 > 0 ? -1 : 1
diff = target - sum2
cnt += diff.abs
sum += diff
end
sum += digits[i]
end
cnt
end
n = gets.to_i
digits = gets.split.map(&:to_i)
puts solve(digits)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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.util.*;
import java.util.function.IntFunction;
import java.util.function.Supplier;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner();
int n=scanner.nextInt();
int[] a=new int[n+1];
for(int i=1;i<=n;i++){
a[i]=scanner.nextInt();
}
Arrays.parallelPrefix(a,(c,b)->c+b);
//put(Arrays.toString(a));
long ans=0;
long ruiseki=0;
for(int i=1;i<=n;i++){
//put(format("i=%d",i));
//put(format("ruiseki=%d",ruiseki));
long val=a[i]+ruiseki;
long val_=a[i-1]+ruiseki;
//put(format("val=%d",val));
//put(format("val_=%d",val_));
if(val==0){
int bit=a[i-1]/Math.abs(a[i-1]);
ruiseki+=bit*1;
ans+=Math.abs(bit);
}else if(val>0&&val_>0){
ruiseki-=(val+1);
ans+=Math.abs(val+1);
}else if(val<0&&val_<0){
ruiseki+=Math.abs(val)+1;
ans+=Math.abs(val)+1;
}
//put(ans);
//put();
}
put(ans);
}
public static void print(Object object){
System.out.print(object);
}
public static void put(Object object) {
System.out.println(object);
}
public static void put(){
System.out.println();
}
public static String format(String string, Object... args) {
return String.format(string, args);
}
}
final class Scanner {
private final InputStream in = System.in;
private final byte[] buffer = new byte[1024];
private int ptr = 0;
private int buflen = 0;
private boolean hasNextByte() {
if (ptr < buflen) {
return true;
} else {
ptr = 0;
try {
buflen = in.read(buffer);
} catch (IOException e) {
e.printStackTrace();
}
if (buflen <= 0) {
return false;
}
}
return true;
}
private int readByte() {
if (hasNextByte())
return buffer[ptr++];
else
return -1;
}
private boolean isPrintableChar(int c) {
return 33 <= c && c <= 126;
}
public boolean hasNext() {
while (hasNextByte() && !isPrintableChar(buffer[ptr]))
ptr++;
return hasNextByte();
}
public String next() {
if (!hasNext())
throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while (isPrintableChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public long nextLong() {
if (!hasNext())
throw new NoSuchElementException();
long n = 0;
boolean minus = false;
int b = readByte();
if (b == '-') {
minus = true;
b = readByte();
}
if (b < '0' || '9' < b) {
throw new NumberFormatException();
}
while (true) {
if ('0' <= b && b <= '9') {
n *= 10;
n += b - '0';
} else if (b == -1 || !isPrintableChar(b)) {
return minus ? -n : n;
} else {
throw new NumberFormatException();
}
b = readByte();
}
}
public int nextInt() {
long nl = nextLong();
if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE)
throw new NumberFormatException();
return (int) nl;
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
final class Pair {
final public int x, y;
Pair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int hashCode() {
return x+y;
}
@Override
public boolean equals(Object obj) {
boolean result=super.equals(obj);
if(obj.getClass()!=this.getClass()){
return false;
}
Pair pair=(Pair)obj;
if(this.x==pair.x&&this.y==pair.y) return true;
return false;
}
@Override
public String toString() {
return String.format("(%d,%d)", x, y);
}
}
final class Tuple<T,V>{
//immutabl1でないことに注意(T,Vがmutableの場合変更可能)
final public T t;
final public V v;
Tuple(T t,V v){
this.t=t;
this.v=v;
}
@Override
public int hashCode() {
return (t.hashCode()+v.hashCode());
}
@Override
public boolean equals(Object obj) {
if(obj.getClass()!=this.getClass()){
return false;
}
Tuple<T,V> tuple=(Tuple)obj;
return tuple.t.equals(this.t)&&tuple.v.equals(this.v);
}
@Override
public String toString() {
return String.format("<Tuple>=<%s,%s>",t,v);
}
}
final class LowerBoundComparator<T extends Comparable<? super T>>
implements Comparator<T>
{
public int compare(T x, T y)
{
return (x.compareTo(y) >= 0) ? 1 : -1;
}
}
final class UpperBoundComparator<T extends Comparable<? super T>>
implements Comparator<T>
{
public int compare(T x, T y)
{
return (x.compareTo(y) > 0) ? 1 : -1;
}
}
final class Util {
static long gcd(long a,long b){
if(a%b==0)return b;
return gcd(b,a%b);
}
static long lcm(long a,long b){
long gcd=gcd(a,b);
long result=b/gcd;
return a*result;
}
static int kaijoMod(int n,int mod){
if(n<1) return -1;
long result=1;
for(int i=n;i>1;i--){
result*=i;
result%=mod;
}
return (int)result;
}
static <T extends Comparable> Map<T,Integer> count(List<T> list){
//副作用
Collections.sort(list);
Map<T,Integer> result=new HashMap<>();
int l=0,r=0;
while(l<list.size()){
while(r<list.size()-1&&list.get(r).equals(r+1)){
r++;
}
result.put(list.get(r),r-l+1);
r++;
l=r;
}
return result;
}
static Map<Integer,Integer> count(int[] array){
//副作用
Arrays.sort(array);
Map<Integer,Integer> result=new HashMap<>();
int l=0,r=0;
while(l<array.length){
while(r<array.length-1&&array[r]==array[r+1]){
r++;
}
result.put(array[l],r-l+1);
r++;
l=r;
}
return result;
}
static String toStringBWS(Iterable iterable){
Iterator ite=iterable.iterator();
return toStringBWS(ite);
}
static String toStringBWS(Iterator ite){
StringBuilder sb=new StringBuilder();
sb.append(ite.next());
while(ite.hasNext()){
sb.append(" ");
sb.append(ite.next());
}
return sb.toString();
}
static String toStringBWS(int[] array){
StringBuilder sb=new StringBuilder();
for(int i=0;i<array.length-1;i++){
sb.append(array[i]);
sb.append(" ");
}
sb.append(array[array.length-1]);
return sb.toString();
}
static String toStringBWS(long[] array){
StringBuilder sb=new StringBuilder();
for(int i=0;i<array.length-1;i++){
sb.append(array[i]);
sb.append(" ");
}
sb.append(array[array.length-1]);
return sb.toString();
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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 solve(vector<int> A) {
int res = 0;
int sum = A[0];
for (int i = 1; i < A.size(); i++) {
if (sum > 0) {
sum += A[i];
while (sum >= 0) {
res++;
sum--;
}
} else if (sum < 0) {
sum += A[i];
while (sum <= 0) {
res++;
sum++;
}
}
}
return res;
}
int main() {
int N;
cin >> N;
vector<int> A(N);
for (int i = 0; i < N; i++) {
cin >> A[i];
}
int res;
if (A[0] != 0) {
res = solve(A);
cout << res << endl;
} else {
int res_first_plus = 1, res_first_minus = 1;
A[0] = 1;
res_first_plus += solve(A);
A[0] = -1;
res_first_minus += solve(A);
res = min(res_first_plus, res_first_minus);
cout << res << endl;
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long a[n], b[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
b[i] = a[i];
}
long long sum = a[0];
if (sum == 0) {
sum = 1;
}
long long cnt1 = 0;
for (int i = 1; i < n; i++) {
long long sumNext = sum + a[i];
if (sumNext * sum >= 0) {
if (sumNext <= 0) {
cnt1 += 1 - sumNext;
sumNext = 1;
} else {
cnt1 += 1 + sumNext;
sumNext = -1;
}
}
sum = sumNext;
}
sum = a[0];
if (sum == 0) {
sum = -1;
}
long long cnt2 = 0;
for (int i = 1; i < n; i++) {
long long sumNext = sum + b[i];
if (sumNext * sum >= 0) {
if (sumNext <= 0) {
cnt2 += 1 - sumNext;
sumNext = 1;
} else {
cnt2 += 1 + sumNext;
sumNext = -1;
}
}
sum = sumNext;
}
cout << min(cnt1, cnt2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import numpy as np
n = int(input())
a = list(map(int,input().split()))
cnt=0
sum_a=0
if a[0]==0:
cnt+=1
a[0]=1
for i in range(n-1):
sum_a += a[i]
if abs(sum_a) >= abs(a[i+1]) or sum_a*a[i+1]>=0:
cnt += abs(-sum_a-np.sign(sum_a) -a[i+1])
a[i+1]=-sum_a-np.sign(sum_a)
print(cnt) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | from functools import lru_cache
n = int(input())
s = list(map(int, input().split()))
@lru_cache(maxsize=None)
def cost_e():
res = 0
sum = 0
for j, y in enumerate(s, 1):
tmp = sum + y
if j & 1 and tmp >= 0:
sum = -1
res += abs(tmp) + 1
elif not j & 1 and tmp <= 0:
sum = 1
res += abs(tmp) + 1
else:
sum = tmp
return res
@lru_cache(maxsize=None)
def cost_o():
res = 0
sum = 0
for j, y in enumerate(s, 1):
tmp = sum + y
if j & 1 and tmp <= 0:
sum = 1
res += abs(tmp) + 1
elif not j & 1 and tmp >= 0:
sum = -1
res += abs(tmp) + 1
else:
sum = tmp
return res
print(cost_e(), cost_o())
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < (int)(n); i++) cin >> a[i];
int s = a[0];
long long ans = 0;
if (s == 0) ans++;
for (int i = (1); i < (int)(n); i++) {
if (s > 0 && s + a[i] >= 0) {
ans += s + a[i] + 1;
s = -1;
} else if (s < 0 && s + a[i] <= 0) {
ans += -(s + a[i]) + 1;
s = 1;
} else
s += a[i];
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<string>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll INFF=0x3f3f3f3f3f3f3f3f;
int a[100010];
int n;
ll solve()
{
ll ans=0;
ll t=a[0];
for(int i=1;i<n;i++)
{
if(t<0)
{
t+=a[i];
if(t<=0)
{
ans+=1-t;
t=1;
}
continue;
}
t+=a[i];
if(t>=0)
{
ans+=t+1;
t=-1;
}
}
return ans;
}
int main()
{
scanf("%d",&n);
ll sum=0;
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
ll sum=0,t=a[0];
if(t==0)
{
a[0]=1;
ll sum1=solve();
a[0]=-1;
ll sum2=solve();
sum=min(sum1,sum2)+1;
}
else
{
ll sum0=solve();
a[0]=1;
ll sum1=solve()+abs(1-t);
a[0]=-1;
ll sum2=solve()+abs(-1-t);
sum=min(sum0,min(sum1,sum2));
}
printf("%lld\n",sum);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
As = list(map(int,input().split()))
sum = 0
count = 0
for i in range(0,n - 1):
sum += As[i]
if sum > 0:
if sum + As[i + 1] > 0:
count += max(sum + As[i + 1] + 1,0)
As[i + 1] -= sum + As[i + 1] + 1
elif sum == 0:
count += 1
if As[i + 1] >=0:
sum -= 1
else:
sum += 1
else:
if sum + As[i + 1] < 0:
count += max(abs(sum + As[i + 1] - 1),0)
As[i + 1] += abs(sum + As[i + 1] - 1)
sum += As[-1]
if sum == 0:
count += 1
print(count)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 100010;
int n;
long long a[MAXN];
long long countNum(long long initialMove) {
long long cnt = abs(initialMove);
long long cur = a[0] + initialMove;
for (int i = 0; i < n; i++) {
long long next = cur + a[i + 1];
if (cur > 0 && next >= 0) {
cnt += next + 1;
next = -1;
}
if (cur < 0 && next <= 0) {
cnt += -next + 1;
next = 1;
}
cur = next;
}
return cnt;
}
void solve() {
long long minCnt =
min(countNum(0), min(countNum(-a[0] + 1), countNum(-a[0] - 1)));
cout << minCnt << endl;
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
solve();
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;
int d[n];
for(int i=0;i<n;i++) cin >> d[i];
int count=0;
int sum=d[0];
int f =0;
if(d[0]>0){
f=-1;
}
if(d[0]<0){
f=1;
}
for(int i=1;i<n;i++){
sum+=d[i];
if(sum>0){
if(f==1){
f=-1;
continue;
}
if(f==-1){
count+=sum+1;
sum=-1;
f=1;
continue;
}
}
if(sum<0){
if(f==-1){
f=1;
continue;
}
if(f==1){
count+=1-sum;
sum=1;
f=-1;
continue;
}
}
cout << count << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < (n); ++i) cin >> a[i];
long long total = a[0];
long long total2 = a[0];
long long ans = 0;
for (int i = (1); i < (n); ++i) {
total += a[i];
if (total * total2 >= 0) {
if (total2 > 0) {
ans += total + 1;
total = -1;
} else {
ans += -total + 1;
total = 1;
}
}
total2 = total;
}
total = a[0];
total2 = a[0];
long long ans2 = 0;
if (total2 > 0) {
ans2 += total2 + 1;
total2 = -1;
} else {
ans2 += -total2 + 1;
total2 = 1;
}
for (int i = (1); i < (n); ++i) {
total += a[i];
if (total * total2 >= 0) {
if (total2 > 0) {
ans2 += total + 1;
total = -1;
} else {
ans2 += -total + 1;
total = 1;
}
}
total2 = total;
}
cout << min(ans, ans2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a.at(i);
}
int sum = a.at(0), ans = 0;
if (sum == 0) {
int nonzero = -1, sign = 0;
for (int i = 1; i < n; i++) {
if (a.at(i) != 0) {
nonzero = i;
sign = (a.at(i) > 0) - (a.at(i) < 0);
break;
}
}
if (nonzero == -1) {
sum++;
} else if (nonzero % 2 == 0) {
if (sign == 1) {
sum--;
} else {
sum++;
}
} else {
if (sign == 1) {
sum++;
} else {
sum--;
}
}
}
for (int i = 1; i < n; i++) {
if (sum > 0) {
while (a.at(i) + sum >= 0) {
a.at(i)--;
ans++;
}
} else if (sum < 0) {
while (a.at(i) + sum <= 0) {
a.at(i)++;
ans++;
}
}
sum += a.at(i);
}
cout << ans;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | def 解():
iN = int(input())
aA = [int(_) for _ in input().split()]
iL = len(aA)
iStart = 0
if sum(aA[0::2]) < sum(aA[1::2]):
iStart = 1
iC = 0
aD = [0]*iL
if 0 % 2 == iStart :
if aA[0] <= 0:
iC += -1 * aA[0] + 1
aA[0] = 1
else:
if 0 <= aA[0] :
iC += aA[0] + 1
aA[0] = -1
aD[0] = aA[0]
for i in range(1,iL):
aD[i] = aD[i-1]+aA[i]
if i % 2 == iStart :
if aD[i] <= 0 :
iC += -1*aD[i] +1
aD[i] = 1
else:
if 0 <= aD[i] :
iC += aD[i] +1
aD[i] = -1
print(iC)
解()
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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 functools
n = int(input())
a = list(map(int, input().split()))
@functools.lru_cache()
def memo(n):
r = a[0]
cnt = 0
for i in range(1,n):
if r>0:
if r+a[i]<0:
r+=a[i]
else:
cnt += a[i] +r +1
r = -1
elif r<0:
if r+a[i]>0:
r+=a[i]
else:
cnt += -a[i] - r+1
r = 1
return cnt
print(memo(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;
const long long MOD = (1e+9) + 7;
const long long INF = 2e+9 + 10;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
int res = 0;
if (a[0] == 0) {
a[0]++;
res++;
}
int sum = a[0];
bool sign = sum > 0;
for (int i = 1; i < n; i++) {
sum += a[i];
if (sign && sum >= 0) {
while (sum >= 0) {
sum--;
res++;
}
} else if (!sign && sum <= 0) {
while (sum <= 0) {
sum++;
res++;
}
}
sign = sum > 0;
}
cout << res << "\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 main() {
int n;
cin >> n;
long long a[n];
long long wa[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
wa[i] = 0;
}
long long ans = 0;
wa[0] = a[0];
if (wa[0] < 0) {
for (int i = 1; i < n; i++) {
if (i % 2 == 1) {
if (wa[i - 1] + a[i] > 0) {
wa[i] = wa[i - 1] + a[i];
continue;
} else if (wa[i - 1] + a[i] == 0) {
wa[i] = 1;
ans++;
} else {
ans += 1 - (wa[i - 1] + a[i]);
wa[i] = 1;
}
} else {
if (wa[i - 1] + a[i] < 0) {
wa[i] = wa[i - 1] + a[i];
continue;
} else if (wa[i - 1] + a[i] == 0) {
wa[i] = -1;
ans++;
} else {
ans += 1 + (wa[i - 1] + a[i]);
wa[i] = -1;
}
}
}
} else {
if (wa[0] == 0) {
ans++;
wa[0]++;
}
for (int i = 1; i < n; i++) {
if (i % 2 == 0) {
if (wa[i - 1] + a[i] > 0) {
wa[i] = wa[i - 1] + a[i];
continue;
} else if (wa[i - 1] + a[i] == 0) {
wa[i] = 1;
ans++;
} else {
ans += 1 - (wa[i - 1] + a[i]);
wa[i] = 1;
}
} else {
if (wa[i - 1] + a[i] < 0) {
wa[i] = wa[i - 1] + a[i];
continue;
} else if (wa[i - 1] + a[i] == 0) {
wa[i] = -1;
ans++;
} else {
ans += 1 + (wa[i - 1] + a[i]);
wa[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 | UNKNOWN | after [] n = 1
after as n
| head as == 0 = after (tail as) (n + 1)
| head as < 0 = if odd n then 1 else (-1)
| head as > 0 = if odd n then (-1) else 1
solve [] v acc = acc
solve as v acc
| v == 0 =
if after as 0 < 0
then solve as 1 (1 + acc)
else solve as (-1) (1 + acc)
| v < 0 =
let w = v + (head as) in
if w <= 0
then solve (tail as) 1 (1 - w + acc)
else solve (tail as) w acc
| v > 0 =
let w = v + (head as) in
if w >= 0
then solve (tail as) (-1) (1 + w + acc)
else solve (tail as) w acc
main = do
n <- read <$> getLine :: IO Int
l <- getLine
let as = fmap read (words l) :: [Int] in
putStrLn (show (solve (tail as) (head as) 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()))
totals = [0] * N
totals[0] = A[0]
con = 0
if totals[0] == 0:
for i in range(1,N):
if A[i] != 0:
f = A[i]
if f > 0:
totals[0] = -1
else:
totals[0] = 1
break
else:
totals[0] = 1
con += 1
for i in range(1,N):
totals[i] = totals[i - 1] + A[i]
if totals[i - 1] * totals[i] >= 0:
if totals[i - 1] < 0:
con += abs(1 - totals[i])
totals[i] += abs(1 - totals[i])
elif totals[i - 1] > 0:
con += abs(-1 - totals[i])
totals[i] -= abs(-1 - totals[i])
print(totals[:i + 1])
print(con)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
int sum1 = 0, cost1 = 0;
for (int i = 0; i < n; i++) {
sum1 += a[i];
if (i % 2 == 0 && sum1 < 0) sum1 += abs(sum1) + 1, cost1 += abs(sum1) + 1;
if (i % 2 == 1 && sum1 > 0) sum1 -= abs(sum1) - 1, cost1 += abs(sum1) + 1;
}
int sum2 = 0, cost2 = 0;
for (int i = 0; i < n; i++) {
sum2 += a[i];
if (i % 2 == 0 && sum1 > 0) sum2 -= abs(sum2) - 1, cost2 += abs(sum2) + 1;
if (i % 2 == 1 && sum1 < 0) sum2 += abs(sum2) + 1, cost1 += abs(sum2) + 1;
}
cout << min(cost1, cost2) << 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()))
sum = a[0]
ans = 0
if sum > 0:
flg = 1
else:
flg = -1
for i in range(1, n):
sum += a[i]
if flg == 1:
if sum < 0:
pass
flg = -1
else:
ans += sum + 1
sum = -1
flg = -1
else:
if sum > 0:
pass
flg = 1
else:
ans += (sum) * (-1) + 1
sum = 1
flg = 1
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long INF = 1e+9 + 7;
long long n, m, l;
string s, t;
long long d[100000], dp[100][100];
int main() {
cin >> n;
for (long long i = (0); i < (n); i++) cin >> d[i];
int sum1 = 0, sum2 = 0;
int ans1 = 0, ans2 = 0;
for (long long i = (0); i < (n); i++) {
sum1 += d[i];
if (i % 2 == 0) {
if (sum1 <= 0) {
ans1 += -sum1 + 1;
sum1 = 1;
}
} else {
if (sum1 >= 0) {
ans1 += sum1 + 1;
sum1 = -1;
}
}
}
for (long long i = (0); i < (n); i++) {
sum2 += d[i];
if (i % 2 == 0) {
if (sum2 >= 0) {
ans2 += sum2 + 1;
sum2 = -1;
}
} else {
if (sum2 <= 0) {
ans2 += -sum2 + 1;
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>
using namespace std;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
int inf = 1e9 + 1000;
long long infi = 1e18 + 100;
long long n;
long long a[100005];
int main() {
cin >> n;
for (int i = 0; i <= (int)(n - 1); i++) cin >> a[i];
a[n] = 0;
long long sum;
long long ans = 0;
for (int i = 0; i <= (int)(n - 1); i++) {
long long p = sum;
sum += a[i];
if (p < 0 && sum <= 0) {
ans += (1 - sum);
sum = 1;
} else if (p > 0 && sum >= 0) {
ans += (sum + 1);
sum = -1;
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int a[100050];
int main() {
int n;
scanf("%d", &n);
int cnt1 = 0, cnt2 = 0;
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
int lazy1 = 0, lazy2 = 0;
for (int i = 0; i < n; i++) {
int sum = 0;
for (int j = 0; j <= i; j++) {
sum += a[j];
}
sum += lazy1;
if (i % 2 == 0 && sum <= 0) {
lazy1 += 1 - sum;
cnt1 += 1 - sum;
}
if (i % 2 == 1 && sum > 0) {
lazy1 -= 1 + sum;
cnt1 += sum + 1;
}
}
for (int i = 0; i < n; i++) {
int sum = 0;
for (int j = 0; j <= i; j++) {
sum += a[j];
}
sum += lazy2;
if (i % 2 == 1 && sum <= 0) {
lazy2 += 1 - sum;
cnt2 += 1 - sum;
}
if (i % 2 == 0 && sum > 0) {
lazy2 -= 1 + sum;
cnt2 += sum + 1;
}
}
printf("%d\n", 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]
count = 0
for i in range(1, n) :
temp = sum
sum += a[i]
if temp > 0 and sum > 0 :
while sum >= 0 :
count += 1
sum -= 1
elif temp < 0 and sum < 0 :
while sum <= 0 :
count += 1
sum += 1
if sum == 0 :
count += 1
print(count)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | require 'prime'
include Math
def max(a,b); a > b ? a : b end
def min(a,b); a < b ? a : b end
def swap(a,b); a, b = b, a end
def gif; gets.to_i end
def gff; gets.to_f end
def gsf; gets.chomp end
def gi; gets.split.map(&:to_i) end
def gf; gets.split.map(&:to_f) end
def gs; gets.chomp.split.map(&:to_s) end
def gc; gets.chomp.split('') end
def pr(num); num.prime_division end
def digit(num); num.to_s.length end
def array(s,ini=nil); Array.new(s){ini} end
def darray(s1,s2,ini=nil); Array.new(s1){Array.new(s2){ini}} end
def rep(num); num.times{|i|yield(i)} end
def repl(st,en,n=1); st.step(en,n){|i|yield(i)} end
def f(sum,a,count)
repl 1,a.size-1 do |i|
sum << a[i]+sum[i-1]
if sum[i-1] > 0
if sum[i] >= 0
count += sum[i]+1
sum[i] = -1
end
elsif sum[i-1] < 0
if sum[i] <= 0
count += 1-sum[i]
sum[i] = 1
end
end
end
return count
end
n = gif
a = gi
sum1 = []
sum2 = []
sum3 = []
ans1 = nil
ans2 = nil
ans3 = nil
if a[0] != 0
sum1 << a[0]
ans1 = f sum1,a,0
else
sum2 << 1
ans2 = f sum2,a,1
sum3 << -1
ans3 = f sum3,a,1
end
if ans1
puts ans1
else
puts min ans2,ans3
end |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void) {
int n;
cin >> n;
vector<long long int> a(n);
for (int i = 0; i < (n); i++) {
cin >> a[i];
}
long long int oddcount = 0, evencount = 0;
long long int oddsum = 0, evensum = 0;
bool odd = true, even = false;
for (int i = 0; i < (n); i++) {
oddsum += a[i];
evensum += a[i];
if (odd && oddsum <= 0) {
oddcount += 1 - oddsum;
oddsum = 1;
}
if (even && oddsum >= 0) {
oddcount += 1 + oddsum;
oddsum = -1;
}
if (even && evensum <= 0) {
evencount += 1 - evensum;
evensum = 1;
}
if (odd && evensum >= 0) {
evencount += 1 + evensum;
evensum = -1;
}
odd = !odd;
even = !even;
}
cout << fmin(oddcount, evencount) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; i++) {
cin >> a.at(i);
}
long long cnt = 0;
long long tot = a.at(0);
long long k = 1;
if (a.at(0) == 0) {
k = -1;
for (long long i = 1; i < n; i++) {
if (a.at(i) != 0) {
k = i;
break;
}
}
if (k == -1) {
cout << (long long)(1 + 2 * (n - 1)) << endl;
return 0;
}
if (a.at(k) > 0) {
tot = -1;
cnt = 1 + 2 * (k - 1);
} else {
tot = 1;
cnt = 1 + 2 * (k - 1);
}
}
for (long long i = k; i < n; i++) {
long long after;
if (tot < 0) {
after = max(1 - tot, a.at(i));
cnt += abs(after - a.at(i));
}
if (tot > 0) {
after = min(-1 - tot, a.at(i));
cnt += abs(after - a.at(i));
}
if (tot == 0) int a = 1 / 0;
tot += after;
}
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() {
int n, a = 0, b = 0, ansa = 0, ansb = 0;
cin >> n;
for (int i = 0; i < n; ++i) {
int k = 0;
cin >> k;
a += k;
b += k;
if (i % 2 == 0) {
if (a >= 0) {
ansa += abs(a) + 1;
a = -1;
}
if (b <= 0) {
ansb += abs(b) + 1;
b = 1;
}
} else {
if (a <= 0) {
ansa += abs(a) + 1;
a = 1;
}
if (b >= 0) {
ansb += abs(b) + 1;
b = -1;
}
}
}
cout << min(ansa, ansb) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int DX[] = {1, 1, 0, -1, -1, -1, 0, 1};
int DY[] = {0, -1, -1, -1, 0, 1, 1, 1};
int n;
ll hoge(ll a[]) {
ll ans = 0;
int temp = 0;
for (int(i) = 0; (i) < (n); (i)++) {
if (temp > 0 && temp + a[i] > 0) {
ans += abs(-1 - temp - a[i]);
temp = -1;
} else if (temp < 0 && temp + a[i] < 0) {
ans += abs(1 - temp - a[i]);
temp = 1;
} else if (temp + a[i] == 0) {
if (temp > 0) {
temp = -1;
} else {
temp = 1;
}
ans += 1;
} else {
temp += a[i];
}
}
return ans;
}
void solve() {
cin >> n;
ll a[n];
for (int(i) = 0; (i) < (n); (i)++) cin >> a[i];
ll ans1 = hoge(a);
int temp = 0;
if (a[0] > 0) {
temp += (a[0] * (-1) - 1);
a[0] = -1;
} else if (a[0] < 0) {
temp = (a[0] * (-1) + 1);
a[0] = 1;
} else {
temp = 1;
a[0] = -1;
}
ll ans2 = hoge(a) + temp;
cout << min(ans1, ans2) << endl;
}
int main() {
solve();
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n=int(input())
a=list(map(int, input().split()))
sum_now=a[0]
sum_before=-a[0]
count_1=0
count_2=0
for i in range(n):
while sum_now*sum_before>=0:
if sum_before==0:
sum_now=-a[1]/abs(a[1])
count_1+=1
else:
count_1+=abs(int(sum_now))+1
sum_now=-sum_before/abs(sum_before)
if i!=n-1:
sum_before=sum_now
sum_now=sum_now+a[i+1]
sum_now=a[0]
sum_before=-a[0]
if sum_before==0:
sum_now=a[1]/abs(a[1])
sum_before=-sum_now
count_2+=1
else:
count_2+=abs(int(sum_now))+1
sum_now=-sum_now/abs(sum_now)
sum_before=-sum_now
for i in range(n):
while sum_now*sum_before>=0:
if sum_before==0:
sum_now=a[1]/abs(a[1])
count_2+=1
else:
count_2+=abs(int(sum_now))+1
sum_now=-sum_before/abs(sum_before)
if i!=n-1:
sum_before=sum_now
sum_now=sum_now+a[i+1]
print(min(count_1, count_2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <stdint.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
using default_counter_t = int64_t;
// macro
#define let auto const&
#define overload4(a, b, c, d, name, ...) name
#define rep1(n) \
for (default_counter_t i = 0, end_i = default_counter_t(n); i < end_i; ++i)
#define rep2(i, n) \
for (default_counter_t i = 0, end_##i = default_counter_t(n); i < end_##i; \
++i)
#define rep3(i, a, b) \
for (default_counter_t i = default_counter_t(a), \
end_##i = default_counter_t(b); \
i < end_##i; ++i)
#define rep4(i, a, b, c) \
for (default_counter_t i = default_counter_t(a), \
end_##i = default_counter_t(b); \
i < end_##i; i += default_counter_t(c))
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep1(n) \
for (default_counter_t i = default_counter_t(n) - 1; i >= 0; --i)
#define rrep2(i, n) \
for (default_counter_t i = default_counter_t(n) - 1; i >= 0; --i)
#define rrep3(i, a, b) \
for (default_counter_t i = default_counter_t(b) - 1, \
begin_##i = default_counter_t(a); \
i >= begin_##i; --i)
#define rrep4(i, a, b, c) \
for (default_counter_t \
i = (default_counter_t(b) - default_counter_t(a) - 1) / \
default_counter_t(c) * default_counter_t(c) + \
default_counter_t(a), \
begin_##i = default_counter_t(a); \
i >= begin_##i; i -= default_counter_t(c))
#define rrep(...) \
overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__)
#define ALL(f, c, ...) \
(([&](decltype((c)) cccc) { \
return (f)(std::begin(cccc), std::end(cccc), ##__VA_ARGS__); \
})(c))
// function
template <class C>
constexpr C& Sort(C& a) {
std::sort(std::begin(a), std::end(a));
return a;
}
template <class C>
constexpr auto& Min(C const& a) {
return *std::min_element(std::begin(a), std::end(a));
}
template <class C>
constexpr auto& Max(C const& a) {
return *std::max_element(std::begin(a), std::end(a));
}
template <class C>
constexpr auto Total(C const& c) {
return std::accumulate(std::begin(c), std::end(c), C(0));
}
template <typename T>
auto CumSum(std::vector<T> const& v) {
std::vector<T> a(v.size() + 1, T(0));
for (std::size_t i = 0; i < a.size() - size_t(1); ++i) a[i + 1] = a[i] + v[i];
return a;
}
template <typename T>
constexpr bool ChMax(T& a, T const& b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T>
constexpr bool ChMin(T& a, T const& b) {
if (b < a) {
a = b;
return true;
}
return false;
}
void In(void) { return; }
template <typename First, typename... Rest>
void In(First& first, Rest&... rest) {
cin >> first;
In(rest...);
return;
}
template <class T, typename I>
void VectorIn(vector<T>& v, const I n) {
v.resize(size_t(n));
rep(i, v.size()) cin >> v[i];
}
void Out(void) {
cout << "\n";
return;
}
template <typename First, typename... Rest>
void Out(First first, Rest... rest) {
cout << first << " ";
Out(rest...);
return;
}
constexpr auto yes(const bool c) { return c ? "yes" : "no"; }
constexpr auto Yes(const bool c) { return c ? "Yes" : "No"; }
constexpr auto YES(const bool c) { return c ? "YES" : "NO"; }
#ifdef USE_STACK_TRACE_LOGGER
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Weverything"
#include <glog/logging.h>
#pragma clang diagnostic pop
#endif //__clang__
#endif // USE_STACK_TRACE_LOGGER
signed main(int argc, char* argv[]) {
(void)argc;
#ifdef USE_STACK_TRACE_LOGGER
google::InitGoogleLogging(argv[0]);
google::InstallFailureSignalHandler();
#else
(void)argv;
#endif // USE_STACK_TRACE_LOGGER
int64_t n;
In(n);
vector<int64_t> a(n);
rep(i, n) In(a[i]);
bool sn = true;
int64_t sum = 0;
int64_t cost1 = 0;
rep(i, n) {
if (sn) {
int64_t k = max(int64_t(0), 1 - sum - a[i]);
cost1 += k;
sum += a[i] + k;
} else {
int64_t k = max(int64_t(0), sum + a[i] + 1);
cost1 += k;
sum += a[i] - k;
}
sn = (!sn);
}
sn = false;
sum = 0;
int64_t cost2 = 0;
rep(i, n) {
if (sn) {
int64_t k = max(int64_t(0), 1 - sum - a[i]);
cost2 += k;
sum += a[i] + k;
} else {
int64_t k = max(int64_t(0), sum + a[i] + 1);
cost2 += k;
sum += a[i] - #include <stdint.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
using default_counter_t = int64_t;
// macro
#define let auto const&
#define overload4(a, b, c, d, name, ...) name
#define rep1(n) \
for (default_counter_t i = 0, end_i = default_counter_t(n); i < end_i; ++i)
#define rep2(i, n) \
for (default_counter_t i = 0, end_##i = default_counter_t(n); i < end_##i; \
++i)
#define rep3(i, a, b) \
for (default_counter_t i = default_counter_t(a), \
end_##i = default_counter_t(b); \
i < end_##i; ++i)
#define rep4(i, a, b, c) \
for (default_counter_t i = default_counter_t(a), \
end_##i = default_counter_t(b); \
i < end_##i; i += default_counter_t(c))
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep1(n) \
for (default_counter_t i = default_counter_t(n) - 1; i >= 0; --i)
#define rrep2(i, n) \
for (default_counter_t i = default_counter_t(n) - 1; i >= 0; --i)
#define rrep3(i, a, b) \
for (default_counter_t i = default_counter_t(b) - 1, \
begin_##i = default_counter_t(a); \
i >= begin_##i; --i)
#define rrep4(i, a, b, c) \
for (default_counter_t \
i = (default_counter_t(b) - default_counter_t(a) - 1) / \
default_counter_t(c) * default_counter_t(c) + \
default_counter_t(a), \
begin_##i = default_counter_t(a); \
i >= begin_##i; i -= default_counter_t(c))
#define rrep(...) \
overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__)
#define ALL(f, c, ...) \
(([&](decltype((c)) cccc) { \
return (f)(std::begin(cccc), std::end(cccc), ##__VA_ARGS__); \
})(c))
// function
template <class C>
constexpr C& Sort(C& a) {
std::sort(std::begin(a), std::end(a));
return a;
}
template <class C>
constexpr auto& Min(C const& a) {
return *std::min_element(std::begin(a), std::end(a));
}
template <class C>
constexpr auto& Max(C const& a) {
return *std::max_element(std::begin(a), std::end(a));
}
template <class C>
constexpr auto Total(C const& c) {
return std::accumulate(std::begin(c), std::end(c), C(0));
}
template <typename T>
auto CumSum(std::vector<T> const& v) {
std::vector<T> a(v.size() + 1, T(0));
for (std::size_t i = 0; i < a.size() - size_t(1); ++i) a[i + 1] = a[i] + v[i];
return a;
}
template <typename T>
constexpr bool ChMax(T& a, T const& b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T>
constexpr bool ChMin(T& a, T const& b) {
if (b < a) {
a = b;
return true;
}
return false;
}
void In(void) { return; }
template <typename First, typename... Rest>
void In(First& first, Rest&... rest) {
cin >> first;
In(rest...);
return;
}
template <class T, typename I>
void VectorIn(vector<T>& v, const I n) {
v.resize(size_t(n));
rep(i, v.size()) cin >> v[i];
}
void Out(void) {
cout << "\n";
return;
}
template <typename First, typename... Rest>
void Out(First first, Rest... rest) {
cout << first << " ";
Out(rest...);
return;
}
constexpr auto yes(const bool c) { return c ? "yes" : "no"; }
constexpr auto Yes(const bool c) { return c ? "Yes" : "No"; }
constexpr auto YES(const bool c) { return c ? "YES" : "NO"; }
#ifdef USE_STACK_TRACE_LOGGER
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Weverything"
#include <glog/logging.h>
#pragma clang diagnostic pop
#endif //__clang__
#endif // USE_STACK_TRACE_LOGGER
signed main(int argc, char* argv[]) {
(void)argc;
#ifdef USE_STACK_TRACE_LOGGER
google::InitGoogleLogging(argv[0]);
google::InstallFailureSignalHandler();
#else
(void)argv;
#endif // USE_STACK_TRACE_LOGGER
int64_t n;
In(n);
vector<int64_t> a(n);
rep(i, n) In(a[i]);
bool sn = true;
int64_t sum = 0;
int64_t cost1 = 0;
rep(i, n) {
if (sn) {
int64_t k = max(int64_t(0), 1 - sum - a[i]);
cost1 += k;
sum += a[i] + k;
} else {
int64_t k = max(int64_t(0), sum + a[i] + 1);
cost1 += k;
sum += a[i] - k;
}
sn = (!sn);
}
sn = false;
sum = 0;
int64_t cost2 = 0;
rep(i, n) {
if (sn) {
int64_t k = max(int64_t(0), 1 - sum - a[i]);
cost2 += k;
sum += a[i] + k;
} else {
int64_t k = max(int64_t(0), sum + a[i] + 1);
cost2 += k;
sum += a[i] - k;
#include <stdint.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
using default_counter_t = int64_t;
// macro
#define let auto const&
#define overload4(a, b, c, d, name, ...) name
#define rep1(n) \
for (default_counter_t i = 0, end_i = default_counter_t(n); i < end_i; ++i)
#define rep2(i, n) \
for (default_counter_t i = 0, end_##i = default_counter_t(n); i < end_##i; \
++i)
#define rep3(i, a, b) \
for (default_counter_t i = default_counter_t(a), \
end_##i = default_counter_t(b); \
i < end_##i; ++i)
#define rep4(i, a, b, c) \
for (default_counter_t i = default_counter_t(a), \
end_##i = default_counter_t(b); \
i < end_##i; i += default_counter_t(c))
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep1(n) \
for (default_counter_t i = default_counter_t(n) - 1; i >= 0; --i)
#define rrep2(i, n) \
for (default_counter_t i = default_counter_t(n) - 1; i >= 0; --i)
#define rrep3(i, a, b) \
for (default_counter_t i = default_counter_t(b) - 1, \
begin_##i = default_counter_t(a); \
i >= begin_##i; --i)
#define rrep4(i, a, b, c) \
for (default_counter_t \
i = (default_counter_t(b) - default_counter_t(a) - 1) / \
default_counter_t(c) * default_counter_t(c) + \
default_counter_t(a), \
begin_##i = default_counter_t(a); \
i >= begin_##i; i -= default_counter_t(c))
#define rrep(...) \
overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__)
#define ALL(f, c, ...) \
(([&](decltype((c)) cccc) { \
return (f)(std::begin(cccc), std::end(cccc), ##__VA_ARGS__); \
})(c))
// function
template <class C>
constexpr C& Sort(C& a) {
std::sort(std::begin(a), std::end(a));
return a;
}
template <class C>
constexpr auto& Min(C const& a) {
return *std::min_element(std::begin(a), std::end(a));
}
template <class C>
constexpr auto& Max(C const& a) {
return *std::max_element(std::begin(a), std::end(a));
}
template <class C>
constexpr auto Total(C const& c) {
return std::accumulate(std::begin(c), std::end(c), C(0));
}
template <typename T>
auto CumSum(std::vector<T> const& v) {
std::vector<T> a(v.size() + 1, T(0));
for (std::size_t i = 0; i < a.size() - size_t(1); ++i) a[i + 1] = a[i] + v[i];
return a;
}
template <typename T>
constexpr bool ChMax(T& a, T const& b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T>
constexpr bool ChMin(T& a, T const& b) {
if (b < a) {
a = b;
return true;
}
return false;
}
void In(void) { return; }
template <typename First, typename... Rest>
void In(First& first, Rest&... rest) {
cin >> first;
In(rest...);
return;
}
template <class T, typename I>
void VectorIn(vector<T>& v, const I n) {
v.resize(size_t(n));
rep(i, v.size()) cin >> v[i];
}
void Out(void) {
cout << "\n";
return;
}
template <typename First, typename... Rest>
void Out(First first, Rest... rest) {
cout << first << " ";
Out(rest...);
return;
}
constexpr auto yes(const bool c) { return c ? "yes" : "no"; }
constexpr auto Yes(const bool c) { return c ? "Yes" : "No"; }
constexpr auto YES(const bool c) { return c ? "YES" : "NO"; }
#ifdef USE_STACK_TRACE_LOGGER
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Weverything"
#include <glog/logging.h>
#pragma clang diagnostic pop
#endif //__clang__
#endif // USE_STACK_TRACE_LOGGER
signed main(int argc, char* argv[]) {
(void)argc;
#ifdef USE_STACK_TRACE_LOGGER
google::InitGoogleLogging(argv[0]);
google::InstallFailureSignalHandler();
#else
(void)argv;
#endif // USE_STACK_TRACE_LOGGER
int64_t n;
In(n);
vector<int64_t> a(n);
rep(i, n) In(a[i]);
bool sn = true;
int64_t sum = 0;
int64_t cost1 = 0;
rep(i, n) {
if (sn) {
int64_t k = max(int64_t(0), 1 - sum - a[i]);
cost1 += k;
sum += a[i] + k;
} else {
int64_t k = max(int64_t(0), sum + a[i] + 1);
cost1 += k;
sum += a[i] - k;
}
sn = (!sn);
}
sn = false;
sum = 0;
int64_t cost2 = 0;
rep(i, n) {
if (sn) {
int64_t k = max(int64_t(0), 1 - sum - a[i]);
cost2 += k;
sum += a[i] + k;
} else {
int64_t k = max(int64_t(0), sum + a[i] + 1);
cost2 += k;
sum += a[i] - k;
}
sn = (!sn);
}
cout << min(cost1, cost2) << endl;
return EXIT_SUCCESS;
}#include <stdint.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
using default_counter_t = int64_t;
// macro
#define let auto const&
#define overload4(a, b, c, d, name, ...) name
#define rep1(n) \
for (default_counter_t i = 0, end_i = default_counter_t(n); i < end_i; ++i)
#define rep2(i, n) \
for (default_counter_t i = 0, end_##i = default_counter_t(n); i < end_##i; \
++i)
#define rep3(i, a, b) \
for (default_counter_t i = default_counter_t(a), \
end_##i = default_counter_t(b); \
i < end_##i; ++i)
#define rep4(i, a, b, c) \
for (default_counter_t i = default_counter_t(a), \
end_##i = default_counter_t(b); \
i < end_##i; i += default_counter_t(c))
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep1(n) \
for (default_counter_t i = default_counter_t(n) - 1; i >= 0; --i)
#define rrep2(i, n) \
for (default_counter_t i = default_counter_t(n) - 1; i >= 0; --i)
#define rrep3(i, a, b) \
for (default_counter_t i = default_counter_t(b) - 1, \
begin_##i = default_counter_t(a); \
i >= begin_##i; --i)
#define rrep4(i, a, b, c) \
for (default_counter_t \
i = (default_counter_t(b) - default_counter_t(a) - 1) / \
default_counter_t(c) * default_counter_t(c) + \
default_counter_t(a), \
begin_##i = default_counter_t(a); \
i >= begin_##i; i -= default_counter_t(c))
#define rrep(...) \
overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__)
#define ALL(f, c, ...) \
(([&](decltype((c)) cccc) { \
return (f)(std::begin(cccc), std::end(cccc), ##__VA_ARGS__); \
})(c))
// function
template <class C>
constexpr C& Sort(C& a) {
std::sort(std::begin(a), std::end(a));
return a;
}
template <class C>
constexpr auto& Min(C const& a) {
return *std::min_element(std::begin(a), std::end(a));
}
template <class C>
constexpr auto& Max(C const& a) {
return *std::max_element(std::begin(a), std::end(a));
}
template <class C>
constexpr auto Total(C const& c) {
return std::accumulate(std::begin(c), std::end(c), C(0));
}
template <typename T>
auto CumSum(std::vector<T> const& v) {
std::vector<T> a(v.size() + 1, T(0));
for (std::size_t i = 0; i < a.size() - size_t(1); ++i) a[i + 1] = a[i] + v[i];
return a;
}
template <typename T>
constexpr bool ChMax(T& a, T const& b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T>
constexpr bool ChMin(T& a, T const& b) {
if (b < a) {
a = b;
return true;
}
return false;
}
void In(void) { return; }
template <typename First, typename... Rest>
void In(First& first, Rest&... rest) {
cin >> first;
In(rest...);
return;
}
template <class T, typename I>
void VectorIn(vector<T>& v, const I n) {
v.resize(size_t(n));
rep(i, v.size()) cin >> v[i];
}
void Out(void) {
cout << "\n";
return;
}
template <typename First, typename... Rest>
void Out(First first, Rest... rest) {
cout << first << " ";
Out(rest...);
return;
}
constexpr auto yes(const bool c) { return c ? "yes" : "no"; }
constexpr auto Yes(const bool c) { return c ? "Yes" : "No"; }
constexpr auto YES(const bool c) { return c ? "YES" : "NO"; }
#ifdef USE_STACK_TRACE_LOGGER
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Weverything"
#include <glog/logging.h>
#pragma clang diagnostic pop
#endif //__clang__
#endif // USE_STACK_TRACE_LOGGER
signed main(int argc, char* argv[]) {
(void)argc;
#ifdef USE_STACK_TRACE_LOGGER
google::InitGoogleLogging(argv[0]);
google::InstallFailureSignalHandler();
#else
(void)argv;
#endif // USE_STACK_TRACE_LOGGER
int64_t n;
In(n);
vector<int64_t> a(n);
rep(i, n) In(a[i]);
bool sn = true;
int64_t sum = 0;
int64_t cost1 = 0;
rep(i, n) {
if (sn) {
int64_t k = max(int64_t(0), 1 - sum - a[i]);
cost1 += k;
sum += a[i] + k;
} else {
int64_t k = max(int64_t(0), sum + a[i] + 1);
cost1 += k;
sum += a[i] - k;
}
sn = (!sn);
}
sn = false;
sum = 0;
int64_t cost2 = 0;
rep(i, n) {
if (sn) {
int64_t k = max#include <stdint.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
using default_counter_t = int64_t;
// macro
#define let auto const&
#define overload4(a, b, c, d, name, ...) name
#define rep1(n) \
for (default_counter_t i = 0, end_i = default_counter_t(n); i < end_i; ++i)
#define rep2(i, n) \
for (default_counter_t i = 0, end_##i = default_counter_t(n); i < end_##i; \
++i)
#define rep3(i, a, b) \
for (default_counter_t i = default_counter_t(a), \
end_##i = default_counter_t(b); \
i < end_##i; ++i)
#define rep4(i, a, b, c) \
for (default_counter_t i = default_counter_t(a), \
end_##i = default_counter_t(b); \
i < end_##i; i += default_counter_t(c))
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep1(n) \
for (default_counter_t i = default_counter_t(n) - 1; i >= 0; --i)
#define rrep2(i, n) \
for (default_counter_t i = default_counter_t(n) - 1; i >= 0; --i)
#define rrep3(i, a, b) \
for (default_counter_t i = default_counter_t(b) - 1, \
begin_##i = default_counter_t(a); \
i >= begin_##i; --i)
#define rrep4(i, a, b, c) \
for (default_counter_t \
i = (default_counter_t(b) - default_counter_t(a) - 1) / \
default_counter_t(c) * default_counter_t(c) + \
default_counter_t(a), \
begin_##i = default_counter_t(a); \
i >= begin_##i; i -= default_counter_t(c))
#define rrep(...) \
overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__)
#define ALL(f, c, ...) \
(([&](decltype((c)) cccc) { \
return (f)(std::begin(cccc), std::end(cccc), ##__VA_ARGS__); \
})(c))
// function
template <class C>
constexpr C& Sort(C& a) {
std::sort(std::begin(a), std::end(a));
return a;
}
template <class C>
constexpr auto& Min(C const& a) {
return *std::min_element(std::begin(a), std::end(a));
}
template <class C>
constexpr auto& Max(C const& a) {
return *std::max_element(std::begin(a), std::end(a));
}
template <class C>
constexpr auto Total(C const& c) {
return std::accumulate(std::begin(c), std::end(c), C(0));
}
template <typename T>
auto CumSum(std::vector<T> const& v) {
std::vector<T> a(v.size() + 1, T(0));
for (std::size_t i = 0; i < a.size() - size_t(1); ++i) a[i + 1] = a[i] + v[i];
return a;
}
template <typename T>
constexpr bool ChMax(T& a, T const& b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T>
constexpr bool ChMin(T& a, T const& b) {
if (b < a) {
a = b;
return true;
}
return false;
}
void In(void) { return; }
template <typename First, typename... Rest>
void In(First& first, Rest&... rest) {
cin >> first;
In(rest...);
return;
}
template <class T, typename I>
void VectorIn(vector<T>& v, const I n) {
v.resize(size_t(n));
rep(i, v.size()) cin >> v[i];
}
void Out(void) {
cout << "\n";
return;
}
template <typename First, typename... Rest>
void Out(First first, Rest... rest) {
cout << first << " ";
Out(rest...);
return;
}
constexpr auto yes(const bool c) { return c ? "yes" : "no"; }
constexpr auto Yes(const bool c) { return c ? "Yes" : "No"; }
constexpr auto YES(const bool c) { return c ? "YES" : "NO"; }
#ifdef USE_STACK_TRACE_LOGGER
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Weverything"
#include <glog/logging.h>
#pragma clang diagnostic pop
#endif //__clang__
#endif // USE_STACK_TRACE_LOGGER
signed main(int argc, char* argv[]) {
(void)argc;
#ifdef USE_STACK_TRACE_LOGGER
google::InitGoogleLogging(argv[0]);
google::InstallFailureSignalHandler();
#else
(void)argv;
#endif // USE_STACK_TRACE_LOGGER
int64_t n;
In(n);
vector<int64_t> a(n);
rep(i, n) In(a[i]);
bool sn = true;
int64_t sum = 0;
int64_t cost1 = 0;
rep(i, n) {
if (sn) {
int64_t k = max(int64_t(0), 1 - sum - a[i]);
cost1 += k;
sum += a[i] + k;
} else {
int64_t k = max(int64_t(0), sum + a[i] + 1);
cost1 += k;
sum += a[i] - k;
}
sn = (!sn);
}
sn = false;
sum = 0;
int64_t cost2 = 0;
rep(i, n) {
if (sn) {
int64_t k = max(int64_t(0), 1 - sum - a[i]);
cost2 += k;
sum += a[i] + k;
} else {
int64_t k = max(int64_t(0), sum + a[i] + 1);
cost2 += k;
sum += a[i] - k;
}
sn = (!sn);
}
cout << min(cost1, cost2) << endl;
return EXIT_SUCCESS;
}#include <stdint.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
using default_counter_t = int64_t;
// macro
#define let auto const&
#define overload4(a, b, c, d, name, ...) name
#define rep1(n) \
for (default_counter_t i = 0, end_i = default_counter_t(n); i < end_i; ++i)
#define rep2(i, n) \
for (default_counter_t i = 0, end_##i = default_counter_t(n); i < end_##i; \
++i)
#define rep3(i, a, b) \
for (default_counter_t i = default_counter_t(a), \
end_##i = default_counter_t(b); \
i < end_##i; ++i)
#define rep4(i, a, b, c) \
for (default_counter_t i = default_counter_t(a), \
end_##i = default_counter_t(b); \
i < end_##i; i += default_counter_t(c))
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep1(n) \
for (default_counter_t i = default_counter_t(n) - 1; i >= 0; --i)
#define rrep2(i, n) \
for (default_counter_t i = default_counter_t(n) - 1; i >= 0; --i)
#define rrep3(i, a, b) \
for (default_counter_t i = default_counter_t(b) - 1, \
begin_##i = default_counter_t(a); \
i >= begin_##i; --i)
#define rrep4(i, a, b, c) \
for (default_counter_t \
i = (default_counter_t(b) - default_counter_t(a) - 1) / \
default_counter_t(c) * default_counter_t(c) + \
default_counter_t(a), \
begin_##i = default_counter_t(a); \
i >= begin_##i; i -= default_counter_t(c))
#define rrep(...) \
overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__)
#define ALL(f, c, ...) \
(([&](decltype((c)) cccc) { \
return (f)(std::begin(cccc), std::end(cccc), ##__VA_ARGS__); \
})(c))
// function
template <class C>
constexpr C& Sort(C& a) {
std::sort(std::begin(a), std::end(a));
return a;
}
template <class C>
constexpr auto& Min(C const& a) {
return *std::min_element(std::begin(a), std::end(a));
}
template <class C>
constexpr auto& Max(C const& a) {
return *std::max_element(std::begin(a), std::end(a));
}
template <class C>
constexpr auto Total(C const& c) {
return std::accumulate(std::begin(c), std::end(c), C(0));
}
template <typename T>
auto CumSum(std::vector<T> const& v) {
std::vector<T> a(v.size() + 1, T(0));
for (std::size_t i = 0; i < a.size() - size_t(1); ++i) a[i + 1] = a[i] + v[i];
return a;
}
template <typename T>
constexpr bool ChMax(T& a, T const& b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T>
constexpr bool ChMin(T& a, T const& b) {
if (b < a) {
a = b;
return true;
}
return false;
}
void In(void) { return; }
template <typename First, typename... Rest>
void In(First& first, Rest&... rest) {
cin >> first;
In(rest...);
return;
}
template <class T, typename I>
void VectorIn(vector<T>& v, const I n) {
v.resize(size_t(n));
rep(i, v.size()) cin >> v[i];
}
void Out(void) {
cout << "\n";
return;
}
template <typename First, typename... Rest>
void Out(First first, Rest... rest) {
cout << first << " ";
Out(rest...);
return;
}
constexpr auto yes(const bool c) { return c ? "yes" : "no"; }
constexpr auto Yes(const bool c) { return c ? "Yes" : "No"; }
constexpr auto YES(const bool c) { return c ? "YES" : "NO"; }
#ifdef USE_STACK_TRACE_LOGGER
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Weverything"
#include <glog/logging.h>
#pragma clang diagnostic pop
#endif //__clang__
#endif // USE_STACK_TRACE_LOGGER
signed main(int argc, char* argv[]) {
(void)argc;
#ifdef USE_STACK_TRACE_LOGGER
google::InitGoogleLogging(argv[0]);
google::InstallFailureSignalHandler();
#else
(void)argv;
#endif // USE_STACK_TRACE_LOGGER
int64_t n;
In(n);
vector<int64_t> a(n);
rep(i, n) In(a[i]);
bool sn = true;
int64_t sum = 0;
int64_t cost1 = 0;
rep(i, n) {
if (sn) {
int64_t k = max(int64_t(0), 1 - sum - a[i]);
cost1 += k;
sum += a[i] + k;
} else {
int64_t k = max(int64_t(0), sum + a[i] + 1);
cost1 += k;
sum += a[i] - k;
}
sn = (!sn);
}
sn = false;
sum = 0;
int64_t cost2 = 0;
rep(i, n) {
if (sn) {
int64_t k = max(int64_t(0), 1 - sum - a[i]);
cost2 += k;
sum += a[i] + k;
} else {
int64_t k = max(int64_t(0), sum + a[i] + 1);
cost2 += k;
sum += a[i] - k;
}
sn = (!sn);
}
cout << min(cost1, cost2) << endl;
return EXIT_SUCCESS;
}#include <stdint.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
using default_counter_t = int64_t;
// macro
#define let auto const&
#define overload4(a, b, c, d, name, ...) name
#define rep1(n) \
for (default_counter_t i = 0, end_i = default_counter_t(n); i < end_i; ++i)
#define rep2(i, n) \
for (default_counter_t i = 0, end_##i = default_counter_t(n); i < end_##i; \
++i)
#define rep3(i, a, b) \
for (default_counter_t i = default_counter_t(a), \
end_##i = default_counter_t(b); \
i < end_##i; ++i)
#define rep4(i, a, b, c) \
for (default_counter_t i = default_counter_t(a), \
end_##i = default_counter_t(b); \
i < end_##i; i += default_counter_t(c))
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep1(n) \
for (default_counter_t i = default_counter_t(n) - 1; i >= 0; --i)
#define rrep2(i, n) \
for (default_counter_t i = default_counter_t(n) - 1; i >= 0; --i)
#define rrep3(i, a, b) \
for (default_counter_t i = default_counter_t(b) - 1, \
begin_##i = default_counter_t(a); \
i >= begin_##i; --i)
#define rrep4(i, a, b, c) \
for (default_counter_t \
i = (default_counter_t(b) - default_counter_t(a) - 1) / \
default_counter_t(c) * default_counter_t(c) + \
default_counter_t(a), \
begin_##i = default_counter_t(a); \
i >= begin_##i; i -= default_counter_t(c))
#define rrep(...) \
overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__)
#define ALL(f, c, ...) \
(([&](decltype((c)) cccc) { \
return (f)(std::begin(cccc), std::end(cccc), ##__VA_ARGS__); \
})(c))
// function
template <class C>
constexpr C& Sort(C& a) {
std::sort(std::begin(a), std::end(a));
return a;
}
template <class C>
constexpr auto& Min(C const& a) {
return *std::min_element(std::begin(a), std::end(a));
}
template <class C>
constexpr auto& Max(C const& a) {
return *std::max_element(std::begin(a), std::end(a));
}
template <class C>
constexpr auto Total(C const& c) {
return std::accumulate(std::begin(c), std::end(c), C(0));
}
template <typename T>
auto CumSum(std::vector<T> const& v) {
std::vector<T> a(v.size() + 1, T(0));
for (std::size_t i = 0; i < a.size() - size_t(1); ++i) a[i + 1] = a[i] + v[i];
return a;
}
template <typename T>
constexpr bool ChMax(T& a, T const& b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T>
constexpr bool ChMin(T& a, T const& b) {
if (b < a) {
a = b;
return true;
}
return false;
}
void In(void) { return; }
template <typename First, typename... Rest>
void In(First& first, Rest&... rest) {
cin >> first;
In(rest...);
return;
}
template <class T, typename I>
void VectorIn(vector<T>& v, const I n) {
v.resize(size_t(n));
rep(i, v.size()) cin >> v[i];
}
void Out(void) {
cout << "\n";
return;
}
template <typename First, typename... Rest>
void Out(First first, Rest... rest) {
cout << first << " ";
Out(rest...);
return;
}
constexpr auto yes(const bool c) { return c ? "yes" : "no"; }
constexpr auto Yes(const bool c) { return c ? "Yes" : "No"; }
constexpr auto YES(const bool c) { return c ? "YES" : "NO"; }
#ifdef USE_STACK_TRACE_LOGGER
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Weverything"
#include <glog/logging.h>
#pragma clang diagnostic pop
#endif //__clang__
#endif // USE_STACK_TRACE_LOGGER
signed main(int argc, char* argv[]) {
(void)argc;
#ifdef USE_STACK_TRACE_LOGGER
google::InitGoogleLogging(argv[0]);
google::InstallFailureSignalHandler();
#else
(void)argv;
#endif // USE_STACK_TRACE_LOGGER
int64_t n;
In(n);
vector<int64_t> a(n);
rep(i, n) In(a[i]);
bool sn = true;
int64_t sum = 0;
int64_t cost1 = 0;
rep(i, n) {
if (sn) {
int64_t k = max(int64_t(0), 1 - sum - a[i]);
cost1 += k;
sum += a[i] + k;
} else {
int64_t k = max(int64_t(0), sum + a[i] + 1);
cost1 += k;
sum += a[i] - k;
}
sn = (!sn);
}
sn = false;
sum = 0;
int64_t cost2 = 0;
rep(i, n) {
if (sn) {
int64_t k = max(int64_t(0), 1 - sum - a[i]);
cost2 += k;
sum += a[i] + k;
} else {
int64_t k = max(int64_t(0), sum + a[i] + 1);
cost2 += k;
sum#include <stdint.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
using default_counter_t = int64_t;
// macro
#define let auto const&
#define overload4(a, b, c, d, name, ...) name
#define rep1(n) \
for (default_counter_t i = 0, end_i = default_counter_t(n); i < end_i; ++i)
#define rep2(i, n) \
for (default_counter_t i = 0, end_##i = default_counter_t(n); i < end_##i; \
++i)
#define rep3(i, a, b) \
for (default_counter_t i = default_counter_t(a), \
end_##i = default_counter_t(b); \
i < end_##i; ++i)
#define rep4(i, a, b, c) \
for (default_counter_t i = default_counter_t(a), \
end_##i = default_counter_t(b); \
i < end_##i; i += default_counter_t(c))
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep1(n) \
for (default_counter_t i = default_counter_t(n) - 1; i >= 0; --i)
#define rrep2(i, n) \
for (default_counter_t i = default_counter_t(n) - 1; i >= 0; --i)
#define rrep3(i, a, b) \
for (default_counter_t i = default_counter_t(b) - 1, \
begin_##i = default_counter_t(a); \
i >= begin_##i; --i)
#define rrep4(i, a, b, c) \
for (default_counter_t \
i = (default_counter_t(b) - default_counter_t(a) - 1) / \
default_counter_t(c) * default_counter_t(c) + \
default_counter_t(a), \
begin_##i = default_counter_t(a); \
i >= begin_##i; i -= default_counter_t(c))
#define rrep(...) \
overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__)
#define ALL(f, c, ...) \
(([&](decltype((c)) cccc) { \
return (f)(std::begin(cccc), std::end(cccc), ##__VA_ARGS__); \
})(c))
// function
template <class C>
constexpr C& Sort(C& a) {
std::sort(std::begin(a), std::end(a));
return a;
}
template <class C>
constexpr auto& Min(C const& a) {
return *std::min_element(std::begin(a), std::end(a));
}
template <class C>
constexpr auto& Max(C const& a) {
return *std::max_element(std::begin(a), std::end(a));
}
template <class C>
constexpr auto Total(C const& c) {
return std::accumulate(std::begin(c), std::end(c), C(0));
}
template <typename T>
auto CumSum(std::vector<T> const& v) {
std::vector<T> a(v.size() + 1, T(0));
for (std::size_t i = 0; i < a.size() - size_t(1); ++i) a[i + 1] = a[i] + v[i];
return a;
}
template <typename T>
constexpr bool ChMax(T& a, T const& b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T>
constexpr bool ChMin(T& a, T const& b) {
if (b < a) {
a = b;
return true;
}
return false;
}
void In(void) { return; }
template <typename First, typename... Rest>
void In(First& first, Rest&... rest) {
cin >> first;
In(rest...);
return;
}
template <class T, typename I>
void VectorIn(vector<T>& v, const I n) {
v.resize(size_t(n));
rep(i, v.size()) cin >> v[i];
}
void Out(void) {
cout << "\n";
return;
}
template <typename First, typename... Rest>
void Out(First first, Rest... rest) {
cout << first << " ";
Out(rest...);
return;
}
constexpr auto yes(const bool c) { return c ? "yes" : "no"; }
constexpr auto Yes(const bool c) { return c ? "Yes" : "No"; }
constexpr auto YES(const bool c) { return c ? "YES" : "NO"; }
#ifdef USE_STACK_TRACE_LOGGER
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Weverything"
#include <glog/logging.h>
#pragma clang diagnostic pop
#endif //__clang__
#endif // USE_STACK_TRACE_LOGGER
signed main(int argc, char* argv[]) {
(void)argc;
#ifdef USE_STACK_TRACE_LOGGER
google::InitGoogleLogging(argv[0]);
google::InstallFailureSignalHandler();
#else
(void)argv;
#endif // USE_STACK_TRACE_LOGGER
int64_t n;
In(n);
vector<int64_t> a(n);
rep(i, n) In(a[i]);
bool sn = true;
int64_t sum = 0;
int64_t cost1 = 0;
rep(i, n) {
if (sn) {
int64_t k = max(int64_t(0), 1 - sum - a[i]);
cost1 += k;
sum += a[i] + k;
} else {
int64_t k = max(int64_t(0), sum + a[i] + 1);
cost1 += k;
sum += a[i] - k;
}
sn = (!sn);
}
sn = false;
sum = 0;
int64_t cost2 = 0;
rep(i, n) {
if (sn) {
int64_t k = max(int64_t(0), 1 - sum - a[i]);
cost2 += k;
sum += a[i] + k;
} else {
int64_t k = max(int64_t(0), sum + a[i] + 1);
cost2 += k;
sum += a[i] - k;
}
sn = (!sn);
}
cout << min(cost1, cost2) << endl;
return EXIT_SUCCESS;
} += a[i] - k;
}
sn = (!sn);
}
cout << min(cost1, cost2) << endl;
return EXIT_SUCCESS;
}(int64_t(0), 1 - sum - a[i]);
cost2 += k;
sum += a[i] + k;
} else {
int64_t k = max(int64_t(0), sum + a[i] + 1);
cost2 += k;
sum += a[i] - k;
}
sn = (!sn);
}
cout << min(cost1, cost2) << endl;
return EXIT_SUCCESS;
}
sn = (!sn);
}
cout << min(cost1, cost2) << endl;
return EXIT_SUCCESS;
}k;
}
sn = (!sn);
}
cout << min(cost1, cost2) << endl;
return EXIT_SUCCESS;
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n=int(input())
a=[int(i) for i in input().split()]
if a[0]>=0:
f=1
else:
f=-1
s=0
ans=0
for i in range(n):
s+=a[i]
if s*f<0:
ans+=abs(s)+1
s=f
elif s==0:
ans+=1
s=f
f*=-1
print(ans)
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.