Search is not available for this dataset
name
stringlengths 2
88
| description
stringlengths 31
8.62k
| public_tests
dict | private_tests
dict | solution_type
stringclasses 2
values | programming_language
stringclasses 5
values | solution
stringlengths 1
983k
|
---|---|---|---|---|---|---|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a_list = list(map(int, input().split()))
total = 0
count = 0
pos_or_neg = True # True = +, False = -
if sum(a_list) >= 0:
if n % 2 == 0:
pos_or_neg = False
else:
pos_or_neg = True
else:
if n % 2 == 0:
pos_or_neg = True
else:
pos_or_neg = False
for a in a_list:
total += a
if pos_or_neg:
if total <= 0:
count += -total + 1
total += -total + 1
pos_or_neg = False
else:
if total >= 0:
count += total + 1
total -= total + 1
pos_or_neg = True
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 | using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace C
{
public class Program
{
static void Main(string[] args)
{
var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
Console.SetOut(sw);
Solve();
Console.Out.Flush();
}
public static void Solve()
{
var N = int.Parse(Console.ReadLine());
var A = Console.ReadLine().Trim().Split(' ').Select(int.Parse).ToArray();
var answer = (long)1e18;
var t = 1;
for (var k = 0; k < 2; k++)
{
t ^= 1;
var step = 0;
var sum = 0;
var prev = t == 0;
for (var i = 0; i < N; i++)
{
sum += A[i];
if (sum == 0)
{
step++;
sum += prev ? -1 : 1;
}
if (prev == (sum > 0))
{
step += Math.Abs(sum) + 1;
sum = prev ? -1 : 1;
}
prev = sum > 0;
}
answer = Math.Min(answer, step);
}
Console.WriteLine(answer);
}
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <bits/stdc++.h>
long long sign(long long n) {
if (n > 0)
return 1;
else if (n < 0)
return -1;
return 0;
}
long long myabs(long long n) { return (n > 0) ? n : (-n); }
long long calc(long long *a, int n, long long bias, long long cost) {
int i;
assert(a[0] != 0);
long long sum = a[0];
long long sum_eval_prev = sum + bias;
for (i = 1; i < n; i++) {
sum += a[i];
long long sum_eval = sum + bias;
if (sign(sum_eval_prev) == sign(sum_eval) || sign(sum_eval) == 0) {
bias += -1 * sign(sum_eval_prev) - sum_eval;
cost += myabs(-1 * sign(sum_eval_prev) - sum_eval);
}
sum_eval_prev = sum + bias;
}
return cost;
}
int main(void) {
int n;
scanf("%d\n", &n);
long long a[n];
int i;
for (i = 0; i < n; i++) {
scanf("%lld ", &a[i]);
}
long long ret;
if (a[0] == 0) {
long long r0, r1;
a[0] = 1;
r0 = calc(a, n, 0, 1);
a[0] = -1;
r1 = calc(a, n, 0, 1);
if (r0 < r1)
ret = r0;
else
ret = 1;
} else {
ret = calc(a, n, 0, 0);
}
printf("%lld\n", ret);
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, = map(int,input().split())
ans1 = 0
S = A[0]
if S <= 0:
S = 1
ans1 = abs(S)+1
for a in A[1:]:
S1 = S+a
if S1*S >= 0:
ans1 += abs(S1)+1
S1 = -S//abs(S)
S = S1
ans2 = 0
S = A[0]
if S >= 0:
S = -1
ans2 = abn(S)+1
for a in A[1:]:
S1 = S+a
if S1*S >= 0:
ans2 += abs(S1)+1
S1 = -S//abs(S)
S = S1
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 | java | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long[] a = new long[n];
for(int i = 0; i < n; i++) {
a[i] = sc.nextLong();
}
long sum = a[0];
long x = 0;
if(a[0] == 0) {
if(a[1] >= 0) {
sum = -1;
x++;
} else {
sum = 1;
x++;
}
}
long count = 0;
for(int i = 1; i < n; i++) {
if((sum < 0 && sum + a[i] <= 0) || (sum > 0 && sum + a[i] >= 0)) {
if(sum < 0) {
count = -sum - a[i] + 1;
} else {
count = -sum - a[i] - 1;
}
}
sum += a[i] + count;
x += Math.abs(count);
count = 0;
}
System.out.println(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 | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
long long res1 = 0, sum1 = a[0], res2 = 0, sum2 = a[0];
for (int i = 1; i < n; i++) {
if (i % 2 == 0) {
if (sum1 > 0) {
sum1 += a[i];
continue;
}
res1 += 1 - sum1;
sum1 = 1;
} else {
if (sum1 < 0) {
sum1 += a[i];
continue;
}
res1 += sum1 + 1;
sum1 = -1;
}
}
for (int i = 1; i < n; i++) {
if (i % 2 == 0) {
if (sum2 > 0) {
sum2 += a[i];
continue;
}
res2 += 1 - sum2;
sum2 = 1;
} else {
if (sum2 < 0) {
sum2 += a[i];
continue;
}
res2 += sum2 + 1;
sum2 = -1;
}
}
cout << min(res1, res2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long a[100001] = {0};
for (int i = 0; i < n; i++) cin >> a[i];
bool sign = true;
if (a[0] <= 0) sign = false;
long long res = 0, sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (sign) {
if (sum <= 0) {
res += 1 - sum;
sum = 1;
}
sign = false;
} else {
if (sum >= 0) {
res += 1 + sum;
sum = -1;
}
sign = true;
}
}
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() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
uint64_t min_ans = INT64_MAX;
for (int s = -1; s < 2; s += 2) {
uint64_t ans = 0;
uint64_t sum = 0;
int sign;
for (int i = 1, sign = s; i < n; ++i, sign *= -1) {
sum += a[i];
if (sign * sum <= 0) {
ans += abs(sign - sum);
sum = sign;
}
}
min_ans = min(min_ans, ans);
}
cout << min_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;
string flag;
vector<int> a;
int str;
long long str_sum = 0, str_nsum = 0;
int num;
long long co = 0;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> num;
for (int i = 0; i < num; i++) {
cin >> str;
a.push_back(str);
}
if (a[0] > 0) {
flag = "up";
} else if (a[0] < 0) {
flag = "down";
} else {
if (a[1] >= 0) {
flag = "down";
co++;
} else {
flag = "up";
co++;
}
}
str_sum = a[0];
for (int i = 1; i < num; i++) {
str_sum += a[i];
if (flag == "up" && str_sum >= 0) {
co += str_sum + 1;
str_sum -= str_sum + 1;
} else if (flag == "down" && str_sum <= 0) {
co += 0 - str_sum + 1;
str_sum += 0 - str_sum + 1;
}
if (str_sum > 0) {
flag = "up";
} else if (str_sum < 0) {
flag = "down";
}
}
cout << co << "\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 dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int n;
cin >> n;
long long int b[100001];
for (int i = 0; i < (n); i++) {
long long int l;
cin >> l;
b[i] = l;
}
for (int i = 0; i < (n); i++) b[i + 1] += b[i];
long long int sm = 0;
for (int i = 1; i < n; i++) {
if (b[i - 1] * b[i] < 0) continue;
int target = (b[i - 1] > 0) ? -1 : 1;
sm += abs(b[i] - target);
long long int dif = -b[i] + target;
for (int j = i; j < n; j++) {
b[j] += dif;
}
}
cout << (sm) << "\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() {
ios::sync_with_stdio(false);
cin.tie(0);
long long n;
cin >> n;
long long a[n];
for (int i = 0; i < n; i++) cin >> a[i];
long long sum[n];
sum[0] = a[0];
long long ans = 0;
if (sum[0] == 0) {
ans = 1;
for (int i = 0; i < n; i++) {
if (a[i] != 0) {
sum[0] = abs(a[i]) / a[i] * pow(-1, (i % 2));
}
}
}
for (int i = 1; i <= (int)(n - 1); i++) {
long long t = sum[i - 1] + a[i];
if (t == 0) {
ans += 1;
sum[i] = -abs(sum[i - 1]) / sum[i - 1];
} else if (abs(t) / t != abs(sum[i - 1]) / sum[i - 1]) {
sum[i] = t;
continue;
} else {
ans += abs(t) + 1;
sum[i] = -abs(t) / t;
}
}
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 | python2 | n = int(raw_input())
a = map(int, raw_input().split())
count = 0
if a[0] == 0:
count += 1
if 0 < a[1]:
a[0] = 1
else:
a[0] = -1
SUM = a[0]
for i in range(1, n - 1):
SUM_next = SUM + a[i - 1]
if 0 <= SUM * SUM_next:
if 0 < SUM:
a[i] -= (SUM_next + 1)
count += (SUM_next + 1)
else:
a[i] += (-SUM_next + 1)
count += (-SUM_next + 1)
SUM = SUM_next
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;
const ll INF = 1LL << 60;
int main() {
ll n;
cin >> n;
ll a[n];
for (ll i = 0; i < n; i++) {
cin >> a[i];
}
ll sum = a[0];
ll ans = 0;
for (ll i = 1; i < n; i++) {
ll tmp_sum = sum + a[i];
if (sum > 0) {
while (tmp_sum >= 0) {
--tmp_sum;
++ans;
}
} else if (sum < 0) {
while (tmp_sum <= 0) {
++tmp_sum;
++ans;
}
}
sum = tmp_sum;
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n;
unsigned long long func(vector<long long int>& s, vector<int>& hugo,
long long int k) {
unsigned long long int ret = 0;
for (int i = 1; i < n; i++) {
if (s[i] == k) {
if (hugo[i - 1] == 0) {
hugo[i] = 1;
ret++;
k--;
} else {
hugo[i] = 0;
ret++;
k++;
}
} else if (s[i] > k) {
if (hugo[i - 1] == 0) {
hugo[i] = 1;
ret += s[i] - k + 1;
k += s[i] - k + 1;
} else {
hugo[i] = 0;
}
} else {
if (hugo[i - 1] == 0) {
hugo[i] = 1;
} else {
hugo[i] = 0;
ret += k - s[i] + 1;
k -= k - s[i] + 1;
}
}
}
return ret;
}
void solve() {
cin >> n;
vector<long long int> v(n), sum(n), sum2(n);
for (int i = 0; i < n; i++) {
cin >> v[i];
if (i == 0)
sum[i] = v[i];
else
sum[i] = sum[i - 1] + v[i];
}
vector<int> hugo(n);
sum2 = sum;
unsigned long long int ans;
if (sum[0] == 0) {
vector<int> hugo2(n);
hugo[0] = 0;
ans = min(func(sum, hugo, -1), func(sum2, hugo2, 1));
} else if (sum[0] > 0) {
hugo[0] = 0;
ans = func(sum, hugo, 0);
} else {
hugo[0] = 1;
ans = func(sum, hugo, 0);
}
cout << ans << endl;
return;
}
int main() {
solve();
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n=int(input())
a=list(map(int , input().split()))
res=[0]*n
res[0]=a[0]
c=0
for i in range(1,n):
res[i]=res[i-1]+a[i]
for i in range(1,n):
s=sum(a[:i])
if s*sum(a[:i+1])>=0:
c+=abs(a[i]+s)+1
if s>0:
a[i]=-s-1
else:
a[i]=-s+1
print(c) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
template <class T, class S>
void cmin(T &a, const S &b) {
if (a > b) a = b;
}
template <class T, class S>
void cmax(T &a, const S &b) {
if (a < b) a = b;
}
using namespace std;
signed main() {
long long int n;
cin >> n;
vector<long long int> v(n), sum(n);
for (long long int i = 0; i < n; i++) cin >> v[i];
bool flag = false;
long long int ans = 0;
for (long long int i = 0; i < n; i++) {
if (i == 0) {
sum[0] = v[0];
if (v[i] > 0) flag = true;
if (v[i] < 0)
flag = false;
else {
ans++;
flag = true;
sum[i] = 1;
}
continue;
}
sum[i] = v[i] + sum[i - 1];
if (flag) {
if (sum[i] < 0)
flag = false;
else if (sum[i] > 0) {
ans += abs(sum[i]) + 1;
sum[i] = -1;
} else {
ans++;
sum[i] = -1;
}
flag = false;
} else {
if (sum[i] > 0)
flag = true;
else if (sum[i] <= 0) {
ans += abs(sum[i]) + 1;
sum[i] = 1;
}
flag = true;
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, count = 0;
cin >> n;
long long a, first;
cin >> first;
long long sum = first, pre_sum;
for (int i = 1; i < n; ++i) {
cin >> a;
if (first == 0) {
if (a >= 0)
first--;
else
first++;
sum = first;
count++;
}
pre_sum = sum;
sum += a;
if (sum * pre_sum >= 0) {
count += (abs(sum) + 1);
if (pre_sum < 0)
sum += (abs(sum) + 1);
else
sum -= (abs(sum) + 1);
}
}
cout << count << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | import qualified Data.ByteString.Char8 as C
import Data.List
main = C.interact $ put . sol . get
get = unfoldr (C.readInt . C.dropWhile (==' ')) . last . C.lines
put = C.pack . show
sol (a:as) = fst $ foldl' f (0,a) as
f (c,s) b
| s*s'<0 = (c,s')
| s>0 = (c+1+abs s',-1)
| s<0 = (c+1+abs s',1)
where
s' = s+b |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> data(N);
for (int i = 0; i < N; i++) {
cin >> data[i];
}
int count_odd, count_even;
count_odd = 0;
count_even = 0;
int sum_odd = 0;
int sum_even = 0;
for (int i = 0; i < N; i++) {
sum_even += data[i];
if (i % 2 == 0) {
if (sum_even >= 0) {
count_even += (sum_even + 1);
sum_even = -1;
}
} else {
if (sum_even <= 0) {
count_even -= (sum_even - 1);
sum_even = 1;
}
}
}
for (int i = 0; i < N; i++) {
sum_odd += data[i];
if (i % 2 == 0) {
if (sum_odd <= 0) {
count_odd -= (sum_odd - 1);
sum_odd = 1;
}
} else {
if (sum_odd >= 0) {
count_odd += (sum_odd + 1);
sum_odd = 1;
}
}
}
cout << min(count_even, count_odd) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int,input().split()))
b = a
#1 = plus
cp = 0
cm = 0
sumscp = 0
sumscm = 0
for i in range(len(a)):
if i % 2 == 0:
while sumscp + a[i] < 1:
a[i] = a[i] + 1
cp = cp + 1
sumscp = sumscp + a[i]
else:
while sums + a[i] > -1:
a[i] = a[i] - 1
cp = cp + 1
sumscp = sumscp + a[i]
for i in range(len(b)):
if i % 2 == 0:
while sumscm + b[i] > -1:
b[i] = b[i] - 1
cm = cm + 1
sumscm = sumscm + b[i]
else:
while sumscm + a[i] < 1:
b[i] = b[i] + 1
cm = cm + 1
sumscm = sumscm + b[i]
print(min(cp,cm)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
template <typename T>
bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T>
bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
const long long mod = 1e9 + 7;
const int inf = 1 << 30;
const long long lnf = 1ll << 60;
int main() {
int n;
cin >> n;
vector<long long> a(n);
vector<long long> s(n);
for (int i = 0; i < (int)(n); i++) cin >> a[i];
s[0] = a[0];
for (int i = 0; i < (int)(n - 1); i++) {
s[i + 1] = a[i + 1] + s[i];
}
int b = -1;
bool p = 1;
for (int i = 0; i < (int)(n); i++) {
if (s[i] == 0) {
b = i;
if (i + 1 < n && s[i + 1] < 0) p = 0;
}
}
if (b == n - 1) {
cout << 1 + 2 * (n - 1) << endl;
return 0;
}
long long add = 0;
if (b != -1) {
if ((b + 1) % 2 == 0 && s[b + 1] < 0 || (b + 1) % 2 == 1 && s[b + 1] > 0)
add = -1;
else
add = 1;
}
long long ans = abs(add);
long long newadd = 0;
for (int i = 0; i < (int)(n - 1); i++) {
if ((s[i] + add) * (s[i + 1] + add) >= 0) {
if (s[i] + add < 0)
newadd = 1 - (s[i + 1] + add);
else
newadd = (-1) - (s[i + 1] + add);
add += newadd;
ans += abs(newadd);
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long[] a = new long[n];
long[] s = new long[n];
for(int i = 0; i < n; i++){
a[i] = sc.nextLong();
if(i == 0){
s[0] = a[0];
} else {
s[i] = s[i-1]+a[i];
}
}
sc.close();
long cnt1 = 0;
long cnt2 = 0;
long diff1 = 0;
long diff2 = 0;
for(int i = 0; i < n; i++){
if(i%2 == 0){
if(s[i]+diff1 <= 0){
cnt1 += 1-s[i]-diff1;
diff1 += 1-s[i];
}
} else {
if(s[i]+diff1 >= 0){
cnt1 += s[i] + diff1 + 1;
diff1 += -1-s[i];
}
}
}
for(int i = 0; i < n; i++){
if(i%2 == 0){
if(s[i]+diff2 >= 0){
cnt2 += s[i] + diff2 + 1;
diff2 += -1-s[i];
}
} else {
if(s[i]+diff2 <= 0){
cnt2 += 1-s[i]-diff2;
diff2 += 1-s[i];
}
}
}
System.out.println(Math.min(cnt1, cnt2));
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
int sum = 0, ans1 = 0, ans2 = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 != 0) {
if (sum <= 0) {
ans1 += abs(1 - sum);
sum = 1;
}
} else {
if (sum >= 0) {
ans1 += abs((-1) - sum);
sum = -1;
}
}
}
sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 0) {
if (sum <= 0) {
ans2 += abs(1 - sum);
sum = 1;
}
} else {
if (sum >= 0) {
ans2 += abs((-1) - sum);
sum = -1;
}
}
}
cout << min(ans1, ans2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner((System.in));
int N = sc.nextInt();
int[] n = new int[N + 1];
// 入力値を設定
for (int i = 0; i < N; i++) {
n[i] = sc.nextInt();
}
int s = n[0];
int m = 0;
for (int i = 1; i <= N; i++) {
while (Math.signum(s) != Math.pow(-1, i % 2)) {
if (Math.pow(-1, i % 2) > 0) {
s++;
} else {
s--;
}
m++;
}
s += n[i];
}
if (m == 0) {
sc.close();
return;
}
s = n[0];
int p = 0;
for (int i = 1; i <= N; i++) {
while (Math.signum(s) != Math.pow(-1, (i + 1) % 2)) {
if (Math.pow(-1, (i + 1) % 2) > 0) {
s++;
} else {
s--;
}
p++;
}
s += n[i];
}
if (m > p) {
System.out.print(p);
} else {
System.out.print(m);
}
sc.close();
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int a[1009];
int b[1009];
int main() {
int n, hh, sum = 0;
while (~scanf("%d", &n)) {
int s = 0;
for (int g = 0; g < n; g++) {
scanf("%d", &a[g]);
s += a[g];
b[g] = s;
}
int i;
for (i = 1; i < n; i++) {
if (b[i] > 0 && b[i - 1] > 0 || b[i] < 0 && b[i - 1] < 0 || b[i] == 0) {
hh = abs(b[i]) + 1;
if (b[i] < 0) {
int x = abs(b[i] + 1);
for (int j = i; j < n; j++) {
b[j] = b[j] + x;
}
} else if (b[i] > 0) {
int y = abs(b[i] + 1);
for (int t = i; t < n; t++) {
b[t] = b[t] - y;
}
} else if (b[i] == 0) {
if (b[i - 1] > 0) {
int c = 1;
for (int t = i; t < n; t++) {
b[t] = b[t] - c;
}
}
if (b[i - 1] < 0) {
int c = 1;
for (int t = i; t < n; t++) {
b[t] = b[t] + c;
}
}
}
sum += hh;
}
}
printf("%d\n", sum);
sum = 0;
}
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, p_m = 0, m_p = 0;
cin >> n;
int a[n], b[n], c[n];
for (int i = 0; i < n; i++) cin >> a[i];
if (a[0] == 0) {
b[0] = 1;
c[0] = 1;
}
if (0 <= a[0]) {
if (a[0] == 0) {
a[0] = 1;
p_m++;
}
int s = a[0];
for (int i = 1; i < n; i++) {
int k = 1 - 2 * (i % 2);
s += a[i];
if (s * k <= 0) {
p_m += 1 - k * s;
s = k;
}
}
if (a[0] != 0) {
cout << p_m << endl;
return 0;
}
}
if (a[0] <= 0) {
if (a[0] == 0) {
a[0] = -1;
m_p++;
}
int s = a[0];
for (int i = 1; i < n; i++) {
int k = 2 * (i % 2) - 1;
s += a[i];
if (s * k <= 0) {
m_p += 1 - k * s;
s = k;
}
}
if (a[0] != 0) {
cout << m_p << endl;
return 0;
}
}
cout << min(p_m, m_p) << 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 | from sys import stdin
if __name__ == "__main__":
_in = [_.rstrip() for _ in stdin.readlines()]
n = int(_in[0]) # type:int
a_arr = list(map(int,_in[1].split(' '))) # type:list(int)
# vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
cnt = 0
sum_ = 0
for i in range(0,n):
if i==0:
if a_arr[0]==0:
while a_arr[i]==0:
i+=1
else:
if a_arr[i]>0:
sum_ = (-1)**i
cnt += 1
else:
sum_ = (-1)**(i+1)
cnt += 1
else:
sum_ = a_arr[i]
else:
if sum_<0:
if sum_+a_arr[i]<=0:
cnt += 1-(sum_+a_arr[i])
sum_ = 1
else:
sum_ = sum_+a_arr[i]
else:
if sum_+a_arr[i]>=0:
cnt += 1+(sum_+a_arr[i])
sum_ = -1
else:
sum_ = sum_+a_arr[i]
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
print(cnt)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
using System.Linq;
class Program
{
static void Main(string[] args)
{
int n = int.Parse(Console.ReadLine());
int[] an = Console.ReadLine().Split(' ').Select(e => int.Parse(e)).ToArray();
long sum = 0;
long cs1 = 0;
int t = 1;
for (int i = 0; i < n; i++)
{
sum += an[i];
if (sum * t <= 0)
{
int tmp = Math.Abs(sum - t);
sum = t;
cs1 += tmp;
}
t *= -1;
}
long cs2 = 0;
t = -1;
sum = 0;
for (int i = 0; i < n; i++)
{
sum += an[i];
if (sum * t <= 0)
{
int tmp = Math.Abs(sum - t);
sum = t;
cs2 += tmp;
}
t *= -1;
}
Console.WriteLine(Math.Min(cs1, cs2));
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long n;
scanf("%d", &n);
long long a[n];
for (int i = 0; i < n; i++) scanf(" %d", &a[i]);
int S = a[0];
int j = 0;
for (int i = 1; i < n; i++) {
if (S * (S + a[i]) < 0) {
S += a[i];
} else {
if (S < 0) {
j += 1 - S - a[i];
S = 1;
} else {
j += S + a[i] + 1;
S = -1;
}
}
}
printf("%d\n", j);
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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;
int cnt_a0pos = 0, cnt_a0neg = 0;
long int a[100000];
long int sum[100000] = {0};
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
if (i == 0)
sum[i] = a[i];
else
sum[i] = sum[i - 1] + a[i];
if (i % 2 == 0) {
if (sum[i] <= 0) {
cnt_a0pos += abs(sum[i]) + 1;
sum[i] = 1;
}
}
if (i % 2 == 1) {
if (sum[i] >= 0) {
cnt_a0pos += abs(sum[i]) + 1;
sum[i] = -1;
}
}
}
for (int i = 0; i < n; i++) {
if (i == 0)
sum[i] = a[i];
else
sum[i] = sum[i - 1] + a[i];
if (i % 2 == 0) {
if (sum[i] >= 0) {
cnt_a0neg += abs(sum[i]) + 1;
sum[i] = -1;
}
}
if (i % 2 == 1) {
if (sum[i] <= 0) {
cnt_a0neg += abs(sum[i]) + 1;
sum[i] = 1;
}
}
}
cout << min(cnt_a0pos, cnt_a0neg) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long num_seq[100005];
int main() {
int N;
cin >> N;
for (int i = 0; i < N; i++) cin >> num_seq[i];
long long min_count = 1 << 30, sub_count = 0, count = 0;
for (int i = 0; i < N; i++) {
if (i == 0) {
if (num_seq[i] <= 0) {
count += 1 - num_seq[i];
sub_count = 1;
} else
sub_count += num_seq[i];
} else {
if (i % 2 == 0) {
if (num_seq[i] + sub_count <= 0) {
if (num_seq[i] + sub_count == 0)
count++;
else
count += 1 - (num_seq[i] + sub_count);
sub_count = 1;
} else
sub_count += num_seq[i];
} else {
if (num_seq[i] + sub_count >= 0) {
if (num_seq[i] + sub_count == 0)
count++;
else
count += 1 + (num_seq[i] + sub_count);
sub_count = -1;
} else
sub_count += num_seq[i];
}
}
}
if (min_count > count) min_count = count;
count = 0;
sub_count = 0;
for (int i = 0; i < N; i++) {
if (i == 0) {
if (num_seq[i] >= 0) {
if (num_seq[i] == 0)
count++;
else
count += 1 + num_seq[i];
sub_count = -1;
} else
sub_count += num_seq[i];
} else {
if (i % 2 == 0) {
if (num_seq[i] + sub_count >= 0) {
if (num_seq[i] + sub_count == 0)
count++;
else
count += 1 + (num_seq[i] + sub_count);
sub_count = -1;
} else
sub_count += num_seq[i];
} else {
if (num_seq[i] + sub_count <= 0) {
if (num_seq[i] + sub_count == 0)
count++;
else
count += 1 - (num_seq[i] + sub_count);
sub_count = 1;
} else
sub_count += num_seq[i];
}
}
}
if (min_count > count) min_count = count;
cout << min_count << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int N;
vector<int> a;
int main() {
cin >> N;
a.resize(N);
for (int i = 0; i < (N); i++) {
cin >> a[i];
}
int sum = a[0];
int ans = 0;
for (int i = (1); i < (N); i++) {
int b;
if (sum > 0) {
b = sum * -1 - 1;
if (b < a[i]) {
ans += a[i] - b;
a[i] = b;
}
} else {
b = sum * -1 + 1;
if (b > a[i]) {
ans += b - a[i];
a[i] = b;
}
}
sum += a[i];
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using pii = pair<int, int>;
using tiii = tuple<int, int, int>;
const int mod = 1000000007;
const double EPS = 1e-9;
const int INF = 1 << 30;
const ll INFLL = 1LL << 60;
template <class T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
int main() {
int n;
cin >> n;
vll a(n);
for (int i = (0); i < (n); ++i) cin >> a[i];
ll ans = 0;
ll sum = 0;
for (int i = (0); i < (n); ++i) {
if (sum * (sum + a[i]) >= 0) {
if (sum < 0) {
ans += 1 - (sum + a[i]);
a[i] += 1 - (sum + a[i]);
} else if (sum > 0) {
ans += -(-1 - (sum + a[i]));
a[i] += -1 - (sum + a[i]);
}
}
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 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 | cpp | #include <bits/stdc++.h>
using namespace std;
class C {
public:
int sgn(long long int val) {
if (val == 0) {
return 0;
}
if (val < 0) {
return -1;
}
return 1;
}
void solve(std::istream& in, std::ostream& out) {
ios::sync_with_stdio(false);
int n;
in >> n;
vector<long long int> a(n), p(n);
for (int i = 0; i < n; ++i) {
in >> a[i];
}
long long int steps = 0;
long long int steps2 = 0;
if (a[0] == 0) {
a[0] = 1;
++steps;
}
p[0] = a[0];
for (int i = 0; i < n - 1; ++i) {
p[i + 1] = p[i] + a[i + 1];
if (sgn(p[i]) != -sgn(p[i + 1])) {
steps += abs(p[i + 1]) + 1;
p[i + 1] = -sgn(p[i]);
}
}
steps2 = abs(a[0]) + 1;
p[0] = -sgn(p[0]);
for (int i = 0; i < n - 1; ++i) {
p[i + 1] = p[i] + a[i + 1];
if (sgn(p[i]) != -sgn(p[i + 1])) {
steps2 += abs(p[i + 1]) + 1;
p[i + 1] = -sgn(p[i]);
}
}
steps = min(steps, steps2);
out << steps << endl;
}
};
int main() {
C solver;
std::istream& in(std::cin);
std::ostream& out(std::cout);
solver.solve(in, out);
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.IO;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
namespace FertiLib.Contest.C
{
static class Program
{
public static void Solve(Scanner cin)
{
int n = cin.ReadInt();
var a = cin.ReadIntArray(n);
int ansa = 0;
var sum = new int[n];
bool isPositiveBefore = true;
for (int i = 0; i < n; i++)
{
if (i == 0) sum[0] = a[0];
else sum[i] = sum[i - 1] + a[i];
if (isPositiveBefore)
{
if (sum[i] >= 0)
{
ansa += sum[i] + 1;
sum[i] = -1;
}
}
else
{
if (sum[i] <= 0)
{
ansa += 1 - sum[i];
sum[i] = 1;
}
}
isPositiveBefore = !isPositiveBefore;
}
sum = new int[n];
isPositiveBefore = false;
int ansb = 0;
for (int i = 0; i < n; i++)
{
if (i == 0) sum[0] = a[0];
else sum[i] = sum[i - 1] + a[i];
if (isPositiveBefore)
{
if (sum[i] >= 0)
{
ansb += sum[i] + 1;
sum[i] = -1;
}
}
else
{
if (sum[i] <= 0)
{
ansb += 1 - sum[i];
sum[i] = 1;
}
}
isPositiveBefore = !isPositiveBefore;
}
Console.WriteLine(Math.Min(ansa, ansb));
}
public static void Main(string[] args)
{
var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
Console.SetOut(sw);
var cin = new Scanner();
Solve(cin);
Console.Out.Flush();
}
public static void YESNO(bool condition) => Console.WriteLine(condition ? "YES" : "NO");
public static void YesNo(bool condition) => Console.WriteLine(condition ? "Yes" : "No");
public static void yesno(bool condition) => Console.WriteLine(condition ? "yes" : "no");
public static bool Chmax<T>(ref T a, T b) where T : IComparable<T>
{
if (a.CompareTo(b) >= 0) return false;
a = b;
return true;
}
public static bool Chmin<T>(ref T a, T b) where T : IComparable<T>
{
if (a.CompareTo(b) <= 0) return false;
a = b;
return true;
}
}
static class Util
{
public static string Join<T>(this IEnumerable<T> x, string separator = "") => string.Join(separator, x);
}
class Scanner
{
string[] s;
int i;
char[] separator = new char[] { ' ' };
public Scanner()
{
s = new string[0];
i = 0;
}
public string Read() => ReadString();
public string ReadString()
{
if (i < s.Length) return s[i++];
string st = Console.ReadLine();
while (st == "") st = Console.ReadLine();
s = st.Split(separator, StringSplitOptions.RemoveEmptyEntries);
if (s.Length == 0) return ReadString();
i = 0;
return s[i++];
}
public string[] ReadStringArray(int N)
{
string[] Array = new string[N];
for (int i = 0; i < N; i++)
{
Array[i] = ReadString();
}
return Array;
}
public int ReadInt()
{
return int.Parse(ReadString());
}
public int[] ReadIntArray(int N, int add = 0)
{
int[] Array = new int[N];
for (int i = 0; i < N; i++)
{
Array[i] = ReadInt() + add;
}
return Array;
}
public long ReadLong()
{
return long.Parse(ReadString());
}
public long[] ReadLongArray(int N, long add = 0)
{
long[] Array = new long[N];
for (int i = 0; i < N; i++)
{
Array[i] = ReadLong() + add;
}
return Array;
}
public double ReadDouble()
{
return double.Parse(ReadString());
}
public double[] ReadDoubleArray(int N, double add = 0)
{
double[] Array = new double[N];
for (int i = 0; i < N; i++)
{
Array[i] = ReadDouble() + add;
}
return Array;
}
public T1 ReadValue<T1>() => (T1)Convert.ChangeType(ReadString(), typeof(T1));
public (T1, T2) ReadValue<T1, T2>()
{
var inputs = ReadStringArray(2);
var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1));
var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2));
return (v1, v2);
}
public (T1, T2, T3) ReadValue<T1, T2, T3>()
{
var inputs = ReadStringArray(3);
var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1));
var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2));
var v3 = (T3)Convert.ChangeType(inputs[2], typeof(T3));
return (v1, v2, v3);
}
public (T1, T2, T3, T4) ReadValue<T1, T2, T3, T4>()
{
var inputs = ReadStringArray(4);
var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1));
var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2));
var v3 = (T3)Convert.ChangeType(inputs[2], typeof(T3));
var v4 = (T4)Convert.ChangeType(inputs[3], typeof(T4));
return (v1, v2, v3, v4);
}
public (T1, T2, T3, T4, T5) ReadValue<T1, T2, T3, T4, T5>()
{
var inputs = ReadStringArray(5);
var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1));
var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2));
var v3 = (T3)Convert.ChangeType(inputs[2], typeof(T3));
var v4 = (T4)Convert.ChangeType(inputs[3], typeof(T4));
var v5 = (T5)Convert.ChangeType(inputs[4], typeof(T5));
return (v1, v2, v3, v4, v5);
}
public (T1, T2, T3, T4, T5, T6) ReadValue<T1, T2, T3, T4, T5, T6>()
{
var inputs = ReadStringArray(6);
var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1));
var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2));
var v3 = (T3)Convert.ChangeType(inputs[2], typeof(T3));
var v4 = (T4)Convert.ChangeType(inputs[3], typeof(T4));
var v5 = (T5)Convert.ChangeType(inputs[4], typeof(T5));
var v6 = (T6)Convert.ChangeType(inputs[5], typeof(T6));
return (v1, v2, v3, v4, v5, v6);
}
public (T1, T2, T3, T4, T5, T6, T7) ReadValue<T1, T2, T3, T4, T5, T6, T7>()
{
var inputs = ReadStringArray(7);
var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1));
var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2));
var v3 = (T3)Convert.ChangeType(inputs[2], typeof(T3));
var v4 = (T4)Convert.ChangeType(inputs[3], typeof(T4));
var v5 = (T5)Convert.ChangeType(inputs[4], typeof(T5));
var v6 = (T6)Convert.ChangeType(inputs[5], typeof(T6));
var v7 = (T7)Convert.ChangeType(inputs[6], typeof(T7));
return (v1, v2, v3, v4, v5, v6, v7);
}
public T1[] ReadValueArray<T1>(int N)
{
var v1 = new T1[N];
for (int i = 0; i < N; i++)
{
v1[i] = ReadValue<T1>();
}
return v1;
}
public (T1[], T2[]) ReadValueArray<T1, T2>(int N)
{
var (v1, v2) = (new T1[N], new T2[N]);
for (int i = 0; i < N; i++)
{
var (t1, t2) = ReadValue<T1, T2>();
v1[i] = t1;
v2[i] = t2;
}
return (v1, v2);
}
public (T1[], T2[], T3[]) ReadValueArray<T1, T2, T3>(int N)
{
var (v1, v2, v3) = (new T1[N], new T2[N], new T3[N]);
for (int i = 0; i < N; i++)
{
var (t1, t2, t3) = ReadValue<T1, T2, T3>();
v1[i] = t1;
v2[i] = t2;
v3[i] = t3;
}
return (v1, v2, v3);
}
public (T1[], T2[], T3[], T4[]) ReadValueArray<T1, T2, T3, T4>(int N)
{
var (v1, v2, v3, v4) = (new T1[N], new T2[N], new T3[N], new T4[N]);
for (int i = 0; i < N; i++)
{
var (t1, t2, t3, t4) = ReadValue<T1, T2, T3, T4>();
v1[i] = t1;
v2[i] = t2;
v3[i] = t3;
v4[i] = t4;
}
return (v1, v2, v3, v4);
}
public (T1[], T2[], T3[], T4[], T5[]) ReadValueArray<T1, T2, T3, T4, T5>(int N)
{
var (v1, v2, v3, v4, v5) = (new T1[N], new T2[N], new T3[N], new T4[N], new T5[N]);
for (int i = 0; i < N; i++)
{
var (t1, t2, t3, t4, t5) = ReadValue<T1, T2, T3, T4, T5>();
v1[i] = t1;
v2[i] = t2;
v3[i] = t3;
v4[i] = t4;
v5[i] = t5;
}
return (v1, v2, v3, v4, v5);
}
public (T1[], T2[], T3[], T4[], T5[], T6[]) ReadValueArray<T1, T2, T3, T4, T5, T6>(int N)
{
var (v1, v2, v3, v4, v5, v6) = (new T1[N], new T2[N], new T3[N], new T4[N], new T5[N], new T6[N]);
for (int i = 0; i < N; i++)
{
var (t1, t2, t3, t4, t5, t6) = ReadValue<T1, T2, T3, T4, T5, T6>();
v1[i] = t1;
v2[i] = t2;
v3[i] = t3;
v4[i] = t4;
v5[i] = t5;
v6[i] = t6;
}
return (v1, v2, v3, v4, v5, v6);
}
public (T1[], T2[], T3[], T4[], T5[], T6[], T7[]) ReadValueArray<T1, T2, T3, T4, T5, T6, T7>(int N)
{
var (v1, v2, v3, v4, v5, v6, v7) = (new T1[N], new T2[N], new T3[N], new T4[N], new T5[N], new T6[N], new T7[N]);
for (int i = 0; i < N; i++)
{
var (t1, t2, t3, t4, t5, t6, t7) = ReadValue<T1, T2, T3, T4, T5, T6, T7>();
v1[i] = t1;
v2[i] = t2;
v3[i] = t3;
v4[i] = t4;
v5[i] = t5;
v6[i] = t6;
v7[i] = t7;
}
return (v1, v2, v3, v4, v5, v6, v7);
}
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const int MAX_N = int(1e5);
long long n, a[MAX_N], dp[MAX_N], dp1[MAX_N];
void solve() {
long long even_plus = 0, even_minus = 0;
long long diffs = 0;
for (long long i = 0; i < (long long)(n); i++) {
long long diff = 0;
dp1[i] += diffs;
if (i % 2 == 0 && dp1[i] < 0) {
diff = 1 - dp1[i];
diffs += diff;
dp1[i] = 1;
} else if (i % 2 != 0 && dp1[i] >= 0) {
diff = -1 - dp1[i];
diffs += diff;
dp1[i] = -1;
}
even_plus += abs(diff);
}
diffs = 0;
for (long long i = 0; i < (long long)(n); i++) {
long long diff = 0;
dp[i] += diffs;
if (i % 2 == 0 && dp[i] >= 0) {
diff = -1 - dp[i];
diffs += diff;
dp[i] = -1;
} else if (i % 2 != 0 && dp[i] < 0) {
diff = 1 - dp[i];
diffs += diff;
dp[i] = 1;
}
even_minus += abs(diff);
}
cout << (even_minus < even_plus ? even_minus : even_plus) << endl;
}
int main() {
cin >> n;
for (long long i = 0; i < (long long)(n); i++) {
cin >> a[i];
if (i == 0)
dp[0] = a[0];
else
dp[i] = dp[i - 1] + a[i];
}
memcpy(dp1, dp, sizeof(dp));
solve();
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long a[100010] = {};
long long calc(long long *, long long);
long long get_sign(long long);
int main() {
long long n, ans;
cin >> n;
for (long long i = 0; i < n; i++) {
cin >> a[i];
}
ans = calc(a, n);
cout << ans << endl;
return 0;
}
long long calc(long long *a, long long n) {
long long cnt = 0;
long long sign = 0;
long long tmp;
if (a[0] > 0) {
sign = 1;
}
tmp = a[0];
for (long long i = 0; i < n - 1; i++) {
tmp = tmp + a[i + 1];
if (sign == 1) {
if (tmp >= 0) {
cnt += tmp + 1;
tmp = -1;
}
} else {
if (tmp <= 0) {
cnt += (-1 * tmp) + 1;
tmp = 1;
}
}
sign = get_sign(tmp);
}
return cnt;
}
long long get_sign(long long tmp) {
if (tmp < 0)
return 0;
else
return 1;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AtCoder
{
class Code3
{
static void Main(string[] args)
{
string s1 = Console.ReadLine();
string s2 = Console.ReadLine();
Console.WriteLine(funcMain(s1,s2));
}
static private string funcMain(string arg1, string arg2)
{
long ret = 0;
long ret1 = 0;
long ret2 = 0;
long sum = 0;
for(int i = 0; i <= 1; i++) // 0はそのまま、1は逆符号
{
sum = ret = 0;
foreach (string buf in arg2.Split())
{
if (sum == 0)
{
sum = long.Parse(buf);
if (i == 1)
{
if (sum >= 0)
{
ret += sum + 1;
sum = -1;
}
else
{
ret += (sum * -1) + 1;
sum = 1;
}
}
}
else
{
if (sum > 0)
{
sum += long.Parse(buf);
if (sum >= 0)
{
ret += sum + 1;
sum = -1;
}
}
else
{
sum += long.Parse(buf);
if (sum <= 0)
{
ret += (sum * -1) + 1;
sum = 1;
}
}
}
}
if (i == 0)
ret1 = ret;
else
ret2 = ret;
}
if (ret1 >= ret2)
ret = ret2;
else
ret = ret1;
return ret.ToString();
}
static private void test()
{
string arg1, arg2;
arg1 = "4";
arg2 = "1 -3 1 0";
Console.WriteLine("4" == funcMain(arg1, arg2));
arg1 = "5";
arg2 = "3 -6 4 -5 7";
Console.WriteLine("0" == funcMain(arg1, arg2));
arg1 = "6";
arg2 = "-1 4 3 2 -5 4";
Console.WriteLine("8" == funcMain(arg1, arg2));
arg1 = "6";
arg2 = "-1 -2 -3 -4 -5 -6";
Console.WriteLine("16" == funcMain(arg1, arg2));
arg1 = "3";
arg2 = "1 10 -100";
Console.WriteLine("2" == funcMain(arg1, arg2));
Console.ReadKey();
}
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #!/usr/bin/env ruby
n = STDIN.gets.chomp.to_i
array = STDIN.gets.chomp.split(' ').map(&:to_i)
ans = 0
sum = 0
array.each_with_index do |a, i|
if i == 0
sum = a
next
end
if sum >= 0
if sum + a < 0
sum += a
else
ans += (-1 - (sum + a)).abs
sum = -1
end
else # sumがマイナス
if sum + a > 0
sum += a
else
ans += (1 - (sum + a)).abs
sum = 1
end
end
#puts "#{i}: sum = #{sum}, ans = #{ans}"
end
puts ans
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1000000007;
int main() {
ll N, sum = 0, ans = 0;
cin >> N;
vector<ll> A(N);
for (long long i = 0; i < (N); ++i) cin >> A.at(i);
if (A.at(0) == 0) {
A.at(0) = 1;
ans++;
for (long long i = 0; i < (N); ++i) {
if (i == 0) {
sum += A.at(i);
continue;
}
if (sum > 0) {
if (sum + A.at(i) < 0) {
sum += A.at(i);
} else {
ans += sum + A.at(i) + 1;
sum = -1;
}
} else {
if (sum + A.at(i) > 0) {
sum += A.at(i);
} else {
ans += 1 - (sum + A.at(i));
sum = 1;
}
}
}
A.at(0) = -1;
ll ans1 = 1;
sum = 0;
for (long long i = 0; i < (N); ++i) {
if (i == 0) {
sum += A.at(i);
continue;
}
if (sum > 0) {
if (sum + A.at(i) < 0) {
sum += A.at(i);
} else {
ans1 += sum + A.at(i) + 1;
sum = -1;
}
} else {
if (sum + A.at(i) > 0) {
sum += A.at(i);
} else {
ans1 += 1 - (sum + A.at(i));
sum = 1;
}
}
}
cout << min(ans, ans1) << endl;
return 0;
}
for (long long i = 0; i < (N); ++i) {
if (i == 0) {
sum += A.at(i);
continue;
}
if (sum > 0) {
if (sum + A.at(i) < 0) {
sum += A.at(i);
} else {
ans += sum + A.at(i) + 1;
sum = -1;
}
} else {
if (sum + A.at(i) > 0) {
sum += A.at(i);
} else {
ans += 1 - (sum + A.at(i));
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<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
int ans1 = 0;
int ans2 = 0;
int sum1 = 0;
int sum2 = 0;
for (int i = 0; i < n; i++) {
sum1 += a[i];
sum2 += a[i];
if (i % 2 == 0) {
if (sum1 > 0) {
} else {
ans1 += abs(sum1) + 1;
sum1 = 1;
}
if (sum2 < 0) {
} else {
ans2 += abs(sum2) + 1;
sum2 = -1;
}
} else {
if (sum1 < 0) {
} else {
ans1 += abs(sum1) + 1;
sum1 = -1;
}
if (sum2 > 0) {
} else {
ans2 += abs(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 | python3 | #
# Written by NoKnowledgeGG @YlePhan
# ('ω')
#
#import math
#mod = 10**9+7
#import itertools
#import fractions
#import numpy as np
#mod = 10**4 + 7
"""def kiri(n,m):
r_ = n / m
if (r_ - (n // m)) > 0:
return (n//m) + 1
else:
return (n//m)"""
""" n! mod m 階乗
mod = 1e9 + 7
N = 10000000
fac = [0] * N
def ini():
fac[0] = 1 % mod
for i in range(1,N):
fac[i] = fac[i-1] * i % mod"""
"""mod = 1e9+7
N = 10000000
pw = [0] * N
def ini(c):
pw[0] = 1 % mod
for i in range(1,N):
pw[i] = pw[i-1] * c % mod"""
"""
def YEILD():
yield 'one'
yield 'two'
yield 'three'
generator = YEILD()
print(next(generator))
print(next(generator))
print(next(generator))
"""
"""def gcd_(a,b):
if b == 0:#結局はc,0の最大公約数はcなのに
return a
return gcd_(a,a % b) # a = p * b + q"""
"""def extgcd(a,b,x,y):
d = a
if b!=0:
d = extgcd(b,a%b,y,x)
y -= (a//b) * x
print(x,y)
else:
x = 1
y = 0
return d"""
def readInts():
return list(map(int,input().split()))
mod = 10**9 + 7
def main():
n = int(input())
A = readInts()
# 符号 positive?
#po_ = True
# 変わったか変わってないか
if A[0] >= 0: # if positive
po_ = True
else: # negative
po_ = False
Cost = 0
ANS = [0] * (n+1)
ANS[0] = A[0]
#print(ANS[0])
for i in range(1,n):
#print(sum(A[:i+1]),A[i],po_)
if ANS[i-1]+A[i] >= 0 and not po_: # sumがpositiveで前がnegativeだった
po_ = True # positiveに
ANS[i] = ANS[i-1] + A[i]
# 3
if ANS[i-1]+A[i] == 0:
ANS[i] = 1
Cost += 1
# これで終わり
elif ANS[i-1]+A[i] >= 0 and po_: # posi : posi ?
# 負にしなければならない
Cost += abs(-1 - (ANS[i-1]+A[i])) # 先にこれやれ
A[i] += -1 - (ANS[i-1] + A[i])
# -4
ANS[i] = ANS[i-1] + A[i]
po_ = False
elif ANS[i-1]+A[i] < 0 and not po_: #nega : nega
# -1 はここ
# print(A[i])
Cost += abs(1 - (ANS[i-1]+A[i])) # 先にこれやれ
A[i] += 1 - (ANS[i-1] + A[i])
ANS[i] = ANS[i-1] + A[i]
po_ = True
else: # nega: pos
po_ = False
ANS[i] = ANS[i-1] + A[i]
if ANS[i-1]+A[i] == 0:
ANS[i] = -1
Cost +=1
#print(A[i])
#print(ANS[i])
print(Cost)
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;
const int INF = 1001001001;
const long long LINF = 1e18;
const string endstr = "\n";
template <typename T>
T gcd(T a, T b) {
return (a == 0) ? b : gcd(b % a, a);
}
template <typename T>
T lcm(T a, T b) {
return a / gcd(a, b) * b;
}
bool p_comp_fs(const pair<int, int> p1, const pair<int, int> p2) {
return p1.first < p2.first;
};
bool p_comp_fg(const pair<int, int> p1, const pair<int, int> p2) {
return p1.first > p2.first;
};
bool p_comp_ss(const pair<int, int> p1, const pair<int, int> p2) {
return p1.second < p2.second;
};
bool p_comp_sg(const pair<int, int> p1, const pair<int, int> p2) {
return p1.second > p2.second;
};
template <typename T>
vector<T> uniquen(vector<T> vec) {
vec.erase(unique(vec.begin(), vec.end()), vec.end());
return vec;
}
int main() {
long long N, s = 0;
cin >> N;
vector<long long> as(N, 0);
for (long long i = 0; i < N; i++) {
long long a;
cin >> a;
s += a;
as[i] = s;
}
long long ret = 0, mv = 0;
for (long long i = (1); i < N; i++) {
if ((as[i] + mv) * (as[i - 1] + mv) >= 0) {
long long a_pre = as[i - 1] + mv;
long long t_mv = a_pre > 0 ? -(as[i] + mv + 1) : 1 - (as[i] + mv);
mv += t_mv;
ret += abs(t_mv);
}
}
cout << ret << endstr;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
template <typename T1, typename T2>
inline void chmin(T1& a, T2 b) {
if (a > b) a = b;
}
template <typename T1, typename T2>
inline void chmax(T1& a, T2 b) {
if (a < b) a = b;
}
using namespace std;
std::mt19937 mt((long long)time(0));
long long dx[4] = {0, 1, 0, -1};
long long dy[4] = {1, 0, -1, 0};
using Weight = long long;
using Flow = long long;
struct Edge {
long long src, dst;
Weight weight;
Flow cap;
Edge() : src(0), dst(0), weight(0) {}
Edge(long long s, long long d, Weight w) : src(s), dst(d), weight(w) {}
};
using Edges = std::vector<Edge>;
using Graph = std::vector<Edges>;
using Array = std::vector<Weight>;
using Matrix = std::vector<Array>;
void add_edge(Graph& g, long long a, long long b, Weight w = 1) {
g[a].emplace_back(a, b, w);
g[b].emplace_back(b, a, w);
}
void add_arc(Graph& g, long long a, long long b, Weight w = 1) {
g[a].emplace_back(a, b, w);
}
struct uf_tree {
std::vector<long long> parent;
long long __size;
uf_tree(long long size_) : parent(size_, -1), __size(size_) {}
void unite(long long x, long long y) {
if ((x = find(x)) != (y = find(y))) {
if (parent[y] < parent[x]) std::swap(x, y);
parent[x] += parent[y];
parent[y] = x;
__size--;
}
}
bool is_same(long long x, long long y) { return find(x) == find(y); }
long long find(long long x) {
return parent[x] < 0 ? x : parent[x] = find(parent[x]);
}
long long size(long long x) { return -parent[find(x)]; }
long long size() { return __size; }
};
template <signed M, unsigned T>
struct mod_int {
constexpr static signed MODULO = M;
constexpr static unsigned TABLE_SIZE = T;
signed x;
mod_int() : x(0) {}
mod_int(long long y)
: x(static_cast<signed>(y >= 0 ? y % MODULO : MODULO - (-y) % MODULO)) {}
mod_int(signed y) : x(y >= 0 ? y % MODULO : MODULO - (-y) % MODULO) {}
mod_int& operator+=(const mod_int& rhs) {
if ((x += rhs.x) >= MODULO) x -= MODULO;
return *this;
}
mod_int& operator-=(const mod_int& rhs) {
if ((x += MODULO - rhs.x) >= MODULO) x -= MODULO;
return *this;
}
mod_int& operator*=(const mod_int& rhs) {
x = static_cast<signed>(1LL * x * rhs.x % MODULO);
return *this;
}
mod_int& operator/=(const mod_int& rhs) {
x = static_cast<signed>((1LL * x * rhs.inv().x) % MODULO);
return *this;
}
mod_int operator-() const { return mod_int(-x); }
mod_int operator+(const mod_int& rhs) const { return mod_int(*this) += rhs; }
mod_int operator-(const mod_int& rhs) const { return mod_int(*this) -= rhs; }
mod_int operator*(const mod_int& rhs) const { return mod_int(*this) *= rhs; }
mod_int operator/(const mod_int& rhs) const { return mod_int(*this) /= rhs; }
bool operator<(const mod_int& rhs) const { return x < rhs.x; }
mod_int inv() const {
assert(x != 0);
if (x <= static_cast<signed>(TABLE_SIZE)) {
if (_inv[1].x == 0) prepare();
return _inv[x];
} else {
signed a = x, b = MODULO, u = 1, v = 0, t;
while (b) {
t = a / b;
a -= t * b;
std::swap(a, b);
u -= t * v;
std::swap(u, v);
}
return mod_int(u);
}
}
mod_int pow(long long t) const {
assert(!(x == 0 && t == 0));
mod_int e = *this, res = mod_int(1);
for (; t; e *= e, t >>= 1)
if (t & 1) res *= e;
return res;
}
mod_int fact() {
if (_fact[0].x == 0) prepare();
return _fact[x];
}
mod_int inv_fact() {
if (_fact[0].x == 0) prepare();
return _inv_fact[x];
}
mod_int choose(mod_int y) {
assert(y.x <= x);
return this->fact() * y.inv_fact() * mod_int(x - y.x).inv_fact();
}
static mod_int _inv[TABLE_SIZE + 1];
static mod_int _fact[TABLE_SIZE + 1];
static mod_int _inv_fact[TABLE_SIZE + 1];
static void prepare() {
_inv[1] = 1;
for (long long i = 2; i <= (long long)TABLE_SIZE; ++i) {
_inv[i] = 1LL * _inv[MODULO % i].x * (MODULO - MODULO / i) % MODULO;
}
_fact[0] = 1;
for (unsigned i = 1; i <= TABLE_SIZE; ++i) {
_fact[i] = _fact[i - 1] * signed(i);
}
_inv_fact[TABLE_SIZE] = _fact[TABLE_SIZE].inv();
for (long long i = (long long)TABLE_SIZE - 1; i >= 0; --i) {
_inv_fact[i] = _inv_fact[i + 1] * (i + 1);
}
}
};
template <signed M, unsigned F>
std::ostream& operator<<(std::ostream& os, const mod_int<M, F>& rhs) {
return os << rhs.x;
}
template <signed M, unsigned F>
std::istream& operator>>(std::istream& is, mod_int<M, F>& rhs) {
long long s;
is >> s;
rhs = mod_int<M, F>(s);
return is;
}
template <signed M, unsigned F>
mod_int<M, F> mod_int<M, F>::_inv[TABLE_SIZE + 1];
template <signed M, unsigned F>
mod_int<M, F> mod_int<M, F>::_fact[TABLE_SIZE + 1];
template <signed M, unsigned F>
mod_int<M, F> mod_int<M, F>::_inv_fact[TABLE_SIZE + 1];
template <signed M, unsigned F>
bool operator==(const mod_int<M, F>& lhs, const mod_int<M, F>& rhs) {
return lhs.x == rhs.x;
}
template <long long M, unsigned F>
bool operator!=(const mod_int<M, F>& lhs, const mod_int<M, F>& rhs) {
return !(lhs == rhs);
}
const signed MF = 1000010;
const signed MOD = 1000000007;
using mint = mod_int<MOD, MF>;
mint binom(long long n, long long r) {
return (r < 0 || r > n || n < 0) ? 0 : mint(n).choose(r);
}
mint fact(long long n) { return mint(n).fact(); }
mint inv_fact(long long n) { return mint(n).inv_fact(); }
template <typename T, typename E>
struct SegmentTree {
typedef function<T(T, T)> F;
typedef function<T(T, E)> G;
typedef function<E(E, E)> H;
typedef function<E(E, long long)> P;
long long n;
F f;
G g;
H h;
P p;
T d1;
E d0;
vector<T> dat;
vector<E> laz;
SegmentTree(
long long n_, F f, G g, H h, T d1, E d0, vector<T> v = vector<T>(),
P p = [](E a, long long b) { return a; })
: f(f), g(g), h(h), d1(d1), d0(d0), p(p) {
init(n_);
if (n_ == (long long)v.size()) build(n_, v);
}
void init(long long n_) {
n = 1;
while (n < n_) n *= 2;
dat.clear();
dat.resize(2 * n - 1, d1);
laz.clear();
laz.resize(2 * n - 1, d0);
}
void build(long long n_, vector<T> v) {
for (long long i = 0; i < n_; i++) dat[i + n - 1] = v[i];
for (long long i = n - 2; i >= 0; i--)
dat[i] = f(dat[i * 2 + 1], dat[i * 2 + 2]);
}
inline void eval(long long len, long long k) {
if (laz[k] == d0) return;
if (k * 2 + 1 < n * 2 - 1) {
laz[k * 2 + 1] = h(laz[k * 2 + 1], laz[k]);
laz[k * 2 + 2] = h(laz[k * 2 + 2], laz[k]);
}
dat[k] = g(dat[k], p(laz[k], len));
laz[k] = d0;
}
T update(long long a, long long b, E x, long long k, long long l,
long long r) {
eval(r - l, k);
if (r <= a || b <= l) return dat[k];
if (a <= l && r <= b) {
laz[k] = h(laz[k], x);
return g(dat[k], p(laz[k], r - l));
}
return dat[k] = f(update(a, b, x, k * 2 + 1, l, (l + r) / 2),
update(a, b, x, k * 2 + 2, (l + r) / 2, r));
}
T update(long long a, long long b, E x) { return update(a, b, x, 0, 0, n); }
T query(long long a, long long b, long long k, long long l, long long r) {
eval(r - l, k);
if (r <= a || b <= l) return d1;
if (a <= l && r <= b) return dat[k];
T vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
return f(vl, vr);
}
T query(long long a, long long b) { return query(a, b, 0, 0, n); }
};
class compress {
public:
static const long long MAP = 10000000;
map<long long, long long> zip;
long long unzip[MAP];
compress(vector<long long>& x) {
sort(x.begin(), x.end());
x.erase(unique(x.begin(), x.end()), x.end());
for (long long i = 0; i < x.size(); i++) {
zip[x[i]] = i;
unzip[i] = x[i];
}
}
};
unsigned euclidean_gcd(unsigned a, unsigned b) {
while (1) {
if (a < b) swap(a, b);
if (!b) break;
a %= b;
}
return a;
}
template <class T>
struct CumulativeSum2D {
vector<vector<T>> data;
CumulativeSum2D(long long W, long long H)
: data(W + 1, vector<long long>(H + 1, 0)) {}
void add(long long x, long long y, T z) {
++x, ++y;
if (x >= data.size() || y >= data[0].size()) return;
data[x][y] += z;
}
void build() {
for (long long i = 1; i < data.size(); i++) {
for (long long j = 1; j < data[i].size(); j++) {
data[i][j] += data[i][j - 1] + data[i - 1][j] - data[i - 1][j - 1];
}
}
}
T query(long long sx, long long sy, long long gx, long long gy) {
return (data[gx][gy] - data[sx][gy] - data[gx][sy] + data[sx][sy]);
}
};
long long nC2(long long n) { return n * (n - 1) / 2; }
class node {
public:
long long depth;
long long num;
node(long long d, long long n) {
depth = d;
num = n;
}
};
CumulativeSum2D<long long> sumB(4001, 4001);
template <class T>
struct CumulativeSum {
vector<T> data;
CumulativeSum(long long sz) : data(sz, 0){};
void add(long long k, T x) { data[k] += x; }
void build() {
for (long long i = 1; i < data.size(); i++) {
data[i] += data[i - 1];
}
}
T query(long long k) {
if (k < 0) return (0);
return (data[min(k, (long long)data.size() - 1)]);
}
T query(long long left, long long right) {
return query(right) - query(left - 1);
}
};
std::vector<bool> IsPrime;
void sieve(size_t max) {
if (max + 1 > IsPrime.size()) {
IsPrime.resize(max + 1, true);
}
IsPrime[0] = false;
IsPrime[1] = false;
for (size_t i = 2; i * i <= max; ++i)
if (IsPrime[i])
for (size_t j = 2; i * j <= max; ++j) IsPrime[i * j] = false;
}
vector<int64_t> divisor(int64_t n) {
vector<int64_t> ret;
for (int64_t i = 1; i * i <= n; i++) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n) ret.push_back(n / i);
}
}
sort(begin(ret), end(ret));
return (ret);
}
long long binary_search(function<bool(long long)> isOk, long long ng,
long long ok) {
while (abs(ok - ng) > 1) {
long long mid = (ok + ng) / 2;
if (isOk(mid))
ok = mid;
else
ng = mid;
}
return ok;
}
std::pair<std::vector<Weight>, bool> bellmanFord(const Graph& g, long long s) {
long long n = g.size();
const Weight inf = std::numeric_limits<Weight>::max() / 8;
Edges es;
for (long long i = 0; i < n; i++)
for (auto& e : g[i]) es.emplace_back(e);
std::vector<Weight> dist(n, inf);
dist[s] = 0;
bool negCycle = false;
for (long long i = 0;; i++) {
bool update = false;
for (auto& e : es) {
if (dist[e.src] != inf && dist[e.dst] > dist[e.src] + e.weight) {
dist[e.dst] = dist[e.src] + e.weight;
update = true;
}
}
if (!update) break;
if (i > n) {
negCycle = true;
break;
}
}
return std::make_pair(dist, !negCycle);
}
std::pair<std::vector<Weight>, bool> bellmanFord(const Graph& g, long long s,
long long d) {
long long n = g.size();
const Weight inf = std::numeric_limits<Weight>::max() / 8;
Edges es;
for (long long i = 0; i < n; i++)
for (auto& e : g[i]) es.emplace_back(e);
std::vector<Weight> dist(n, inf);
dist[s] = 0;
bool negCycle = false;
for (long long i = 0; i < n * 2; i++) {
bool update = false;
for (auto& e : es) {
if (dist[e.src] != inf && dist[e.dst] > dist[e.src] + e.weight) {
dist[e.dst] = dist[e.src] + e.weight;
update = true;
if (e.dst == d && i == n * 2 - 1) negCycle = true;
}
}
if (!update) break;
}
return std::make_pair(dist, !negCycle);
}
vector<long long> Manachar(string S) {
long long len = S.length();
vector<long long> R(len);
long long i = 0, j = 0;
while (i < S.size()) {
while (i - j >= 0 && i + j < S.size() && S[i - j] == S[i + j]) ++j;
R[i] = j;
long long k = 1;
while (i - k >= 0 && i + k < S.size() && k + R[i - k] < j)
R[i + k] = R[i - k], ++k;
i += k;
j -= k;
}
return R;
}
std::vector<long long> tsort(const Graph& g) {
long long n = g.size(), k = 0;
std::vector<long long> ord(n), in(n);
for (auto& es : g)
for (auto& e : es) in[e.dst]++;
std::queue<long long> q;
for (long long i = 0; i < n; ++i)
if (in[i] == 0) q.push(i);
while (q.size()) {
long long v = q.front();
q.pop();
ord[k++] = v;
for (auto& e : g[v]) {
if (--in[e.dst] == 0) {
q.push(e.dst);
}
}
}
return *std::max_element(in.begin(), in.end()) == 0
? ord
: std::vector<long long>();
}
std::vector<Weight> dijkstra(const Graph& g, long long s) {
const Weight INF = std::numeric_limits<Weight>::max() / 8;
using state = std::tuple<Weight, long long>;
std::priority_queue<state> q;
std::vector<Weight> dist(g.size(), INF);
dist[s] = 0;
q.emplace(0, s);
while (q.size()) {
Weight d;
long long v;
std::tie(d, v) = q.top();
q.pop();
d *= -1;
if (dist[v] < d) continue;
for (auto& e : g[v]) {
if (dist[e.dst] > dist[v] + e.weight) {
dist[e.dst] = dist[v] + e.weight;
q.emplace(-dist[e.dst], e.dst);
}
}
}
return dist;
}
Matrix WarshallFloyd(const Graph& g) {
auto const INF = std::numeric_limits<Weight>::max() / 8;
long long n = g.size();
Matrix d(n, Array(n, INF));
for (long long i = (0); i < (long long)(n); i++) d[i][i] = 0;
for (long long i = (0); i < (long long)(n); i++)
for (auto& e : g[i]) d[e.src][e.dst] = std::min(d[e.src][e.dst], e.weight);
for (long long k = (0); k < (long long)(n); k++)
for (long long i = (0); i < (long long)(n); i++)
for (long long j = (0); j < (long long)(n); j++) {
if (d[i][k] != INF && d[k][j] != INF) {
d[i][j] = std::min(d[i][j], d[i][k] + d[k][j]);
}
}
return d;
}
const long long BLACK = 1, WHITE = 0;
bool isValid(vector<vector<long long>>& mapData, long long gyo,
long long retu) {
bool f = true;
for (long long i = (0); i < (long long)(gyo); i++) {
for (long long j = (0); j < (long long)(retu); j++) {
long long colorCnt = 0;
if (j > 0 && mapData[i][j] == mapData[i][j - 1]) {
colorCnt++;
}
if (i > 0 && mapData[i][j] == mapData[i - 1][j]) {
colorCnt++;
}
if (i < gyo - 1 && mapData[i][j] == mapData[i + 1][j]) {
colorCnt++;
}
if (j < retu - 1 && mapData[i][j] == mapData[i][j + 1]) {
colorCnt++;
}
if (colorCnt > 1) {
f = false;
}
}
}
return f;
}
void getNext(long long nowX, long long nowY, long long* pOutX, long long* pOutY,
long long gyo, long long retu) {
if (nowX == retu - 1) {
*pOutY = nowY + 1;
*pOutX = 0;
return;
}
*pOutX = nowX + 1;
*pOutY = nowY;
}
void dfs(vector<vector<long long>> mapData, long long nowX, long long nowY,
long long gyo, long long retu, long long* outCnt) {
if (nowX == retu - 1 && nowY == gyo - 1) {
mapData[nowY][nowX] = BLACK;
if (isValid(mapData, gyo, retu)) {
*outCnt = *outCnt + 1;
}
mapData[nowY][nowX] = WHITE;
if (isValid(mapData, gyo, retu)) {
*outCnt = *outCnt + 1;
}
return;
}
mapData[nowY][nowX] = BLACK;
long long nextX, nextY;
getNext(nowX, nowY, &nextX, &nextY, gyo, retu);
dfs(mapData, nextX, nextY, gyo, retu, outCnt);
mapData[nowY][nowX] = WHITE;
getNext(nowX, nowY, &nextX, &nextY, gyo, retu);
dfs(mapData, nextX, nextY, gyo, retu, outCnt);
}
void dec(map<long long, long long>& ma, long long a) {
ma[a]--;
if (ma[a] == 0) {
ma.erase(a);
}
}
long long N;
long long solve(long long ans, vector<long long> A, vector<long long> cu) {
for (long long i = (0); i < (long long)(N); i++) {
if (cu[i] == 0) {
ans++;
if (i == 0) {
if (cu[i + 1] < 0) {
cu[i] = 1;
} else {
cu[i] = -1;
}
} else {
if (cu[i - 1] < 0) {
cu[i] = 1;
} else {
cu[i] = -1;
}
}
}
if (i == N - 1) {
break;
}
if (cu[i] < 0 == cu[i + 1] < 0) {
if (cu[i + 1] > 0) {
ans += cu[i + 1] + 1;
cu[i + 1] -= cu[i + 1] + 1;
} else {
ans += -cu[i + 1] + 1;
cu[i + 1] += -cu[i + 1] + 1;
}
}
cu[i + 2] = cu[i + 1] + A[i + 2];
}
return ans;
}
signed main() {
cin >> N;
vector<long long> A(N + 2);
vector<long long> cu(N + 2);
long long su = 0;
for (long long i = (0); i < (long long)(N); i++) {
cin >> A[i];
su += A[i];
cu[i] = su;
}
long long ans1 = 0, ans2 = 0;
ans1 = solve(ans1, A, cu);
if (A[0] < 0) {
ans2 = -A[0] + 1;
A[0] = 1;
} else {
ans2 = A[0] + 1;
A[0] = -1;
}
su = 0;
for (long long i = (0); i < (long long)(N); i++) {
su += A[i];
cu[i] = su;
}
ans2 = solve(ans2, A, cu);
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 | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ll N = 0;
cin >> N;
vector<ll> A(N, 0);
for (ll i = 0; i < N; i++) {
cin >> A.at(i);
}
ll ans = 0;
vector<ll> sum(N, 0);
sum.at(0) = A.at(0);
ll ansa = abs(A.at(0) + (abs(A.at(0)) / A.at(0)));
vector<ll> suma(N, 0);
suma.at(0) = -1 * (abs(A.at(0)) / A.at(0));
for (size_t i = 1; i < N; i++) {
sum.at(i) = sum.at(i - 1) + A.at(i);
if (sum.at(i) * sum.at(i - 1) < 0) {
continue;
} else {
ans += abs(sum.at(i) + (abs(A.at(i - 1)) / A.at(i - 1)));
sum.at(i) = -1 * (abs(A.at(i - 1)) / A.at(i - 1));
}
}
for (size_t i = 1; i < N; i++) {
suma.at(i) = suma.at(i - 1) + A.at(i);
if (suma.at(i) * suma.at(i - 1) < 0) {
continue;
} else {
ansa += abs(suma.at(i) + (abs(A.at(i - 1)) / A.at(i - 1)));
suma.at(i) = -1 * (abs(A.at(i - 1)) / A.at(i - 1));
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | main :: IO ()
main = do
_ <- readLn :: IO Int
(a0:as) <- (map read . words) <$> getLine
print $ solve (0, a0, as)
solve :: (Int, Int, [Int]) -> Int
solve (x, 0, 0:as) = min (solve (x+1, 1, as)) (solve (x+1, -1, as))
solve (x, _, []) = x
solve (x, s, a0:as) = solve (x+k, s + a0 + k, as)
where
m = - sign s
k = if s * a0 > 0 then m - (s + a0) else 0
sign :: Int -> Int
sign x = x `div` abs 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 | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using R = double;
const ll inf = 1LL << 50;
const ll MOD = 1e9 + 7;
int main() {
int n;
cin >> n;
vector<int> v(n);
for (int i = 0; i < n; ++i) cin >> v[i];
for (int i = 1; i < n; ++i) v[i] += v[i - 1];
int ans1 = 0;
int all = 0;
bool fl = 0;
for (int i = 0; i < n; ++i) {
if (fl == 0 && v[i] + all <= 0) {
ans1 += 1 - (v[i] + all);
all += 1 - (v[i] + all);
fl = 1;
} else if (fl == 1 && v[i] + all >= 0) {
ans1 += 1 + (v[i] + all);
all += -1 - (v[i] + all);
fl = 0;
} else if (fl == 1) {
fl = 0;
} else {
fl = 1;
}
}
int ans2 = 0;
all = 0;
fl = 1;
for (int i = 0; i < n; ++i) {
if (fl == 0 && v[i] + all <= 0) {
ans2 += 1 - (v[i] + all);
all += 1 - (v[i] + all);
fl = 1;
} else if (fl == 1 && v[i] + all >= 0) {
ans2 += 1 + (v[i] + all);
all += -1 - (v[i] + all);
fl = 0;
} else if (fl == 1) {
fl = 0;
} else {
fl = 1;
}
}
cout << min(ans1, ans2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
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 n;
vector<long long> a;
int main() {
cin >> n;
a.resize(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long sum = a[0];
long long cost = 0;
for (int i = 1; i < n; i++) {
if ((sum < 0 && sum + a[i] > 0) || (sum > 0 && sum + a[i] < 0)) {
sum += a[i];
} else {
if (sum > 0) {
cost += abs(sum + a[i] - (-1));
sum = -1;
} else {
cost += abs(sum + a[i] - 1);
sum = 1;
}
}
}
cout << cost << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ll acc;
ll N;
vector<ll> A;
cin >> N;
for (ll i = 0; i < N; i++) {
ll tmp;
cin >> tmp;
A.push_back(tmp);
}
ll ans_pos = 0;
acc = A[0];
if (acc == 0) {
acc = 1;
ans_pos += 1;
}
for (ll i = 1; i < N; i++) {
ll next = acc + A[i];
if (acc * next >= 0) {
ans_pos += abs(next) + 1;
next = -1 * (acc / abs(acc));
}
acc = next;
}
ll ans_neg = 0;
acc = A[0];
if (acc == 0) {
acc = 1;
ans_neg += 1;
}
for (ll i = 1; i < N; i++) {
ll next = acc + A[i];
if (acc * next >= 0) {
ans_neg += abs(next) + 1;
next = -1 * (acc / abs(acc));
}
acc = next;
}
ll ans = min(ans_pos, ans_neg);
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long f(int a[], int n, bool plus) {
long long ans = 0;
int sum = a[0];
if (sum == 0) {
ans++;
if (plus) {
sum++;
} else {
sum--;
}
} else {
plus = (sum > 0);
}
for (int i = 1; i < n; i++) {
sum += a[i];
if ((plus && sum < 0) || (!plus && sum > 0)) {
plus = !plus;
continue;
}
if (plus) {
ans += (sum + 1);
sum = -1;
} else {
ans += (-sum + 1);
sum = 1;
}
plus = !plus;
}
return ans;
}
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
long long ans1 = f(a, n, true);
long long ans2 = f(a, n, false);
cout << min(ans1, ans2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = [int(x) for x in input().split()]
seq_sum = a[0]
times = 0
for i in range(1, n):
if seq_sum > 0 and seq_sum + a[i] >= 0:
times += seq_sum + a[i] + 1
seq_sum = -1
elif seq_sum < 0 and seq_sum + a[i] <= 0:
times += abs(seq_sum + a[i]) + 1
seq_sum = 1
else:
seq_sum += a[i]
print(times) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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()]
count = 0
if a[0] == 0:
for i in range(n):
if a[i] != 0:
if a[i] % 2 == 0:
a[0] = a[i]//abs(a[i])
else:
a[0] = -a[i]//abs(a[i])
count = 1
break
if not(count):
a[0] = 1
count += 1
s = [0]*n
s[0] = a[0]
for i in range(1,n):
s[i] = s[i-1] + a[i]
if s[i] == 0:
tmp = -s[i-1]//abs(s[i-1])
s[i] += tmp
count += abs(tmp)
elif s[i] * s[i-1] > 0:
tmp = (-s[i]) + (-s[i]//abs(s[i]))
s[i] += tmp
count += abs(tmp)
print(count)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
unsigned long op = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
if (a[0] == 0) {
for (int i = 1; i < n; i++) {
if (a[i] == 0) {
continue;
} else if (a[i] > 0) {
if (i % 2 == 0) {
a[0] = 1;
} else {
a[0] = -1;
}
op = 1;
break;
} else {
if (i % 2 == 0) {
a[0] = -1;
} else {
a[0] = 1;
}
op = 1;
break;
}
}
}
long sum = a[0];
for (int i = 1; i < n; i++) {
if (sum > 0) {
if (sum + a[i] >= 0) {
op += abs(-1 - sum - a[i]);
sum = -1;
} else {
sum += a[i];
}
} else {
if (sum + a[i] <= 0) {
op += abs(1 - sum - a[i]);
sum = 1;
} else {
sum += a[i];
}
}
}
cout << op << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
int sum[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sum[0] = a[0];
int count = 0;
for (int i = 0; i < n - 1; i++) {
sum[i + 1] = sum[i] + a[i + 1];
if (sum[0] == 0) {
if (a[1] > 0) {
sum[0]--;
a[0]--;
count++;
}
if (a[1] < 0) {
sum[0]++;
a[0]++;
count++;
}
}
if (sum[i] > 0) {
while (sum[i + 1] >= 0) {
sum[i + 1]--;
a[i + 1]--;
count++;
}
}
if (sum[i] < 0) {
while (sum[i + 1] <= 0) {
sum[i + 1]++;
a[i + 1]++;
count++;
}
}
}
cout << count << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N, type = 1;
long long count = 0;
long long a[100009];
cin >> N;
cin >> a[0];
if (a[0] == 0) {
a[0] = 1;
count++;
} else if (a[0] < 0)
type = -1;
long long sum = a[0];
for (int i = 1; i < N; i++) {
cin >> a[i];
sum += a[i];
type = -type;
if (sum == 0) {
sum += type;
count++;
} else if ((sum > 0) && (type == -1)) {
count += sum + 1;
sum = -1;
} else if ((sum < 0) && (type == 1)) {
count += -sum + 1;
sum = 1;
}
}
cout << count << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long int check(long int sum, long int ans, vector<int> T, int N, bool pre_pm) {
for (int i = 1; i < N; i++) {
if (pre_pm) {
sum += T.at(i);
while (0 <= sum) {
sum--;
ans++;
}
pre_pm = false;
} else {
sum += T.at(i);
while (sum <= 0) {
sum++;
ans++;
}
pre_pm = true;
}
}
return ans;
}
int main() {
int N;
vector<int> T;
cin >> N;
for (int i = 0; i < N; i++) {
int tmp;
cin >> tmp;
T.push_back(tmp);
}
long int ans = 0;
long int sum = 0;
bool pre_pm;
sum = T.at(0);
if (0 <= sum) {
pre_pm = true;
long int tmp1, tmp2;
if (!sum) {
sum++;
ans++;
tmp1 = check(sum, ans, T, N, pre_pm);
}
pre_pm = false;
tmp2 = check(-1, 1 + sum, T, N, pre_pm);
cout << min(tmp1, tmp2) << endl;
} else {
pre_pm = false;
long int tmp1 = check(sum, ans, T, N, pre_pm);
pre_pm = true;
long int tmp2 = check(1, 1 + sum, T, N, pre_pm);
cout << min(tmp1, tmp2) << endl;
}
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
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;
}
} else {
if (i == n - 1) {
++ans;
a[0] = 1;
}
}
}
}
if (a[0] > 0) {
signed long long sum = a[0];
for (int i = 1; i < n; ++i) {
if (i % 2 == 1) {
if (sum + a[i] < 0) {
sum += a[i];
} else {
ans += sum + a[i] + 1;
sum = -1;
}
} else {
if (sum + a[i] > 0) {
sum += a[i];
} else {
ans += abs(sum + a[i] - 1);
sum = 1;
}
}
}
} else {
signed long long sum = a[0];
for (int i = 1; i < n; ++i) {
if (i % 2 == 1) {
if (sum + a[i] > 0) {
sum += a[i];
} else {
ans += abs(sum + a[i] - 1);
sum = 1;
}
} else {
if (sum + a[i] < 0) {
sum += a[i];
} else {
ans += sum + a[i] + 1;
sum = -1;
}
}
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
cnt=0
for i in range(1,n):
# 条件満たすまでループ
for _ in range(3):
print(a)
now_tmp = sum(a[:i])
next_tmp = sum(a[:i+1])
print(i, now_tmp, next_tmp)
# 符号が逆転していればOK かつ 現在までの総和が0でない
# 異なる符号を掛けるとマイナスになる
if now_tmp * next_tmp <0 and now_tmp !=0:
break
else:
# 現在の合計がマイナスの場合
if now_tmp < 0:
a[i] += -next_tmp+1
cnt +=abs(next_tmp+1)
# 現在の合計がプラスの場合
elif now_tmp > 0 :
a[i] += -next_tmp-1
cnt +=abs(next_tmp+1)
# 現在の合計が0の場合
elif now_tmp == 0 :
# 1個前がプラスの場合、
if sum(a[:i-1]) > 0:
a[i] += -next_tmp+1
cnt +=abs(next_tmp+1)
# 1個前がマイナスの場合
else:
a[i] += -next_tmp+1
cnt +=abs(next_tmp+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;
struct __ {
__() {
ios_base::Init i;
ios_base::sync_with_stdio(0);
cin.tie(0);
}
} __;
int main() {
int n;
cin >> n;
vector<long long> v(n);
for (int i = 0; i < n; ++i) {
cin >> v[i];
if (i) {
v[i] = v[i] + v[i - 1];
}
}
vector<long long> a = v;
int ans = 0;
int curr = 0;
for (int i = 0; i < n; ++i) {
v[i] = curr + v[i];
if (i % 2 == 0 && v[i] <= 0) {
curr += abs(v[i]) + 1;
ans += abs(v[i]) + 1;
}
if (i % 2 == 1 && v[i] >= 0) {
curr -= v[i] + 1;
ans += v[i] + 1;
}
}
v = a;
int ans2 = ans;
ans = 0;
curr = 0;
for (int i = 0; i < n; ++i) {
v[i] = curr + v[i];
if (i % 2 == 1 && v[i] <= 0) {
curr += abs(v[i]) + 1;
ans += abs(v[i]) + 1;
}
if (i % 2 == 0 && v[i] >= 0) {
curr -= v[i] + 1;
ans += v[i] + 1;
}
}
cout << min(ans, ans2);
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AtCoder
{
class Code3
{
static void Main(string[] args)
{
string s1 = Console.ReadLine();
string s2 = Console.ReadLine();
Console.WriteLine(funcMain(s1,s2));
}
static private string funcMain(string arg1, string arg2)
{
int ret = 0;
int sum = 0;
foreach (string buf in arg2.Split())
{
if (sum == 0)
sum = int.Parse(buf);
else
{
if (sum > 0)
{
sum += int.Parse(buf);
if (sum >= 0)
{
ret += sum + 1;
sum = -1;
}
}
else
{
sum += int.Parse(buf);
if (sum <= 0)
{
ret += (sum * -1) + 1; // 絶対値の関数探すのがめんどくさかった
sum = 1;
}
}
}
}
return ret.ToString();
}
static private void test()
{
string arg1, arg2;
arg1 = "4";
arg2 = "1 -3 1 0";
Console.WriteLine("4" == funcMain(arg1, arg2));
arg1 = "5";
arg2 = "3 -6 4 -5 7";
Console.WriteLine("0" == funcMain(arg1, arg2));
arg1 = "6";
arg2 = "-1 4 3 2 -5 4";
Console.WriteLine("8" == funcMain(arg1, arg2));
Console.ReadKey();
}
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)
N = int(input())
As = list(mapint())
cum = As.pop(0)
if not cum==0:
cum2 = cum
ans = 0
for a in As:
if cum*(cum+a)>=0:
ans += abs(cum+a)+1
cum = -1 if cum>0 else 1
else:
cum += a
ans2 = abs(cum2)+1
cum2 = 1 if cum2<0 else -1
for a in As:
if cum2*(cum2+a)>=0:
ans2 += abs(cum2+a)+1
cum2 = -1 if cum2>0 else 1
else:
cum2 += a
print(min(ans, ans2))
else:
cum2 = -1
ans = 1
for a in As:
if cum*(cum+a)>=0:
ans += abs(cum+a)+1
cum = -1 if cum>0 else 1
else:
cum += a
ans2 = 1
cum2 = 1
for a in As:
if cum2*(cum2+a)>=0:
ans2 += abs(cum2+a)+1
cum2 = -1 if cum2>0 else 1
else:
cum2 += a
print(min(ans, ans2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long solve(vector<long> A) {
long res = 0;
long 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<long> A(N);
for (int i = 0; i < N; i++) {
cin >> A[i];
}
long res;
if (A[0] != 0) {
res = solve(A);
cout << res << endl;
} else {
long 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>
int inf = 1000000007;
using namespace std;
int main() {
int n;
cin >> n;
vector<int> data(n);
int ans = 0;
for (int i = 0; i < n; i++) {
cin >> data.at(i);
}
int64_t sum = data.at(0);
int64_t sump = sum;
for (int i = 1; i < n; i++) {
sump += data.at(i);
if (sum * sump > 0) {
int c = sump;
if (c < 0) c *= -1;
c++;
ans += c;
if (sump > 0) {
data.at(i) -= c;
sump -= c;
} else {
data.at(i) += c;
sump += c;
}
}
sum += data.at(i);
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
long long ans = 1000000007;
long long sum = 0;
long long tmp = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 0 && sum >= 0) {
tmp += sum + 1;
sum = -1;
} else if (i % 2 == 1 && sum <= 0) {
tmp += 1 - sum;
sum = 1;
}
}
ans = min(ans, tmp);
tmp = 0;
sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 0 && sum <= 0) {
tmp += 1 - sum;
sum = 1;
} else if (i % 2 == 1 && sum >= 0) {
tmp += 1 + sum;
sum = -1;
}
}
ans = min(ans, tmp);
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = [int(i) for i in input().split()]
ans = 0
tmp = a[0]
if a[0] == 0:
tmp = 1
ans += 1
for i in range(1,n):
#print(tmp,ans)
if tmp > 0:
if tmp + a[i] >= 0:
ans += tmp + a[i] + 1
tmp = -1
else:
tmp += a[i]
else:
if tmp + a[i] <= 0:
ans += abs(tmp + a[i]) + 1
tmp = 1
else:
tmp += a[i]
#print(ans)
ans2 = 0
if a[0] > 0:
ans2 += a[0]+1
tmp = -1
elif a[0] < 0:
ans2 += -a[0]+1
else:
tmp = -1
ans2 += 1
for i in range(1,n):
#print(tmp,ans)
if tmp > 0:
if tmp + a[i] >= 0:
ans2 += tmp + a[i] + 1
tmp = -1
else:
tmp += a[i]
else:
if tmp + a[i] <= 0:
ans2 += abs(tmp + a[i]) + 1
tmp = 1
else:
tmp += a[i]
print(min(ans,ans2))
exit()
ans2 = 0
tmp = -a[0]
ans2 += abs(a[0])*2
for i in range(1,n):
#print(tmp,ans)
if tmp > 0:
if tmp + a[i] >= 0:
ans2 += tmp + a[i] + 1
tmp = -1
else:
tmp += a[i]
else:
if tmp + a[i] <= 0:
ans2 += abs(tmp + a[i]) + 1
tmp = 1
else:
tmp += a[i]
print(min(ans,ans2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using i64 = long long;
using P = pair<i64, i64>;
int main() {
i64 n;
cin >> n;
vector<i64> a(n);
for (int i = 0; i < n; ++i) cin >> a[i];
i64 ans = 0;
if (a[0] == 0) {
for (int i = 1; i < n; ++i) {
if (a[i] == 0) continue;
if (a[i] > 0) {
if (i % 2 == 0)
a[0]++;
else
a[0]--;
} else {
if (i % 2 == 0)
a[0]--;
else
a[0]++;
}
++ans;
}
if (a[0] == 0) a[0] = 1;
}
i64 wa = a[0];
for (int i = 1; i < n; ++i) {
if (wa > 0) {
if (wa + a[i] >= 0) {
ans += wa + a[i] + 1;
a[i] -= wa + a[i] + 1;
}
} else {
if (wa + a[i] <= 0) {
ans += -1 * (wa + a[i]) + 1;
a[i] += -1 * (wa + a[i]) + 1;
}
}
wa += a[i];
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import sys
input=sys.stdin.readline
input = open(sys.argv[1], "r").readline
def main():
N = int(input())
A = list(map(int, input().split()))
tmp = A.copy()
mi = -1
for j in range(2):
A = tmp.copy()
s = 0
n = 0
if j == 0: # 偶数番目までの和が正 ⇒ A[0] を負にする
if A[0] > 0:
n += abs(A[0] +1)
A[0] = -1
else: # 奇数番目までの和が正 ⇒ A[0] を正にする
if A[0] < 0:
n += abs(A[0] -1)
A[0] = 1
s = A[0]
# print(A)
for i in range(1,N):
if s * (s+A[i]) >= 0:
if s < 0:
n += abs(-s+1 -A[i])
A[i] = -s+1
else:
n += abs(-s-1 -A[i])
A[i] = -s-1
s += A[i]
if mi == -1:
mi = n
else:
mi = min(mi, n)
print(mi)
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 | UNKNOWN | import Control.Applicative
import Data.List
main = do
_ <- getLine
as <- getLine >>= return . map read . words :: IO [Integer]
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 | UNKNOWN | using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
class Program
{
static int n;
static int[] a;
public static void Main(string[] args)
{
n = Input.NextInt();
a = Input.LineInt();
int minmove = int.MaxValue;
minmove = Calc(a);
for (int i = 0; i < a.Length; i++)
{
a[i] = -a[i];
}
minmove = Math.Min(minmove, Calc(a));
Console.WriteLine(minmove);
}
private static int Calc(int[] a)
{
checked
{
int curr = 0;
int move = 0;
for (int i = 0; i < a.Length; i++)
{
int dif = 0;
switch (i % 2)
{
case 0:
{
var newc = curr + a[i];
dif = newc <= 0 ? 1 - newc : 0;
}
break;
case 1:
{
var newc = curr + a[i];
dif = newc >= 0 ? -1 - newc : 0;
}
break;
}
curr += a[i] + dif;
move += Math.Abs(dif);
}
return move;
}
}
}
public static class Input
{
private static Queue<string> q = new Queue<string>();
private static void Confirm() { if (q.Count == 0) foreach (var s in Console.ReadLine().Split(' ')) q.Enqueue(s); }
public static int NextInt() { Confirm(); return int.Parse(q.Dequeue()); }
public static long NextLong() { Confirm(); return long.Parse(q.Dequeue()); }
public static string NextString() { Confirm(); return q.Dequeue(); }
public static double NextDouble() { Confirm(); return double.Parse(q.Dequeue()); }
public static int[] LineInt() { return Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); }
public static long[] LineLong() { return Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); }
public static string[] LineString() { return Console.ReadLine().Split(' ').ToArray(); }
public static double[] LineDouble() { return Console.ReadLine().Split(' ').Select(double.Parse).ToArray(); }
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1000000007;
int main() {
ll N, sum = 0, ans = 0;
cin >> N;
vector<ll> A(N);
for (long long i = 0; i < (N); ++i) cin >> A.at(i);
for (long long i = 0; i < (N); ++i) {
if (i == 0) {
if (A.at(i) > 0) {
sum += A.at(i);
continue;
} else {
sum = 1;
ans += 1 - A.at(i);
continue;
}
}
if (sum > 0) {
if (sum + A.at(i) < 0) {
sum += A.at(i);
} else {
ans += sum + A.at(i) + 1;
sum = -1;
}
} else {
if (sum + A.at(i) > 0) {
sum += A.at(i);
} else {
ans += 1 - (sum + A.at(i));
sum = 1;
}
}
}
ll ans1 = 0;
sum = 0;
for (long long i = 0; i < (N); ++i) {
if (i == 0) {
if (A.at(i) < 0) {
sum += A.at(i);
continue;
} else {
sum = -1;
ans += 1 + A.at(i);
continue;
}
}
if (sum > 0) {
if (sum + A.at(i) < 0) {
sum += A.at(i);
} else {
ans1 += sum + A.at(i) + 1;
sum = -1;
}
} else {
if (sum + A.at(i) > 0) {
sum += A.at(i);
} else {
ans1 += 1 - (sum + A.at(i));
sum = 1;
}
}
}
cout << min(ans, ans1) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | def op(pos, n, a):
if pos:
S = 1 if a[0] == 0 else a[0]
else:
S = -1 if a[0] == 0 else a[0]
count = 1 if a[0] == 0 else 0
for i in a[1:]:
if S * (S + i) > 0:
count += abs(S + i) + 1
S = -1 if S > 0 else 1
elif S + i == 0:
count += 1
S = -1 if S > 0 else 1
else:
S += i
return count
def main():
n = int(input())
a = list(map(int, input().split()))
c1 = op(True, n, a)
c2 = op(False, n, a)
print(min(c1, c2))
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>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define IO(i, o) freopen(i, "r", stdin), freopen(o, "w", stdout)
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
int n, a[100000];
int main(){
//IO("input.txt", "output.txt");
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
for(int i = 0; i < n; i++) cin >> a[i];
long long sum = 0, mn = INT_MAX;
for(int i = 0; i < n; i++){
sum += a[i];
if(i % 2 == 0 && sum <= 0) mn += 1 - sum, sum = 1;
else if(i % 2 == 1 && sum >= 0) mn += 1 + sum, sum = -1;
}
sum = 0;
long long cnt = 0;
for(int i = 0; i < n; i++){
sum += a[i];
if(i % 2 == 0 && sum >= 0) cnt += 1 + sum, sum = -1;
else if(i % 2 == 1 && sum <= 0) cnt += 1 - sum, sum = 1;
}
mn = min(mn, cnt);
cout << mn << "\n";
return 0;
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int N, A[100000];
int main() {
scanf("%d", &N);
for (int i = 0; i < N; i++) scanf("%d", &A[i]);
long long cnt = 0, sum = A[0];
if (sum == 0) sum++;
for (int i = 1; i < N; i++) {
long long prev = sum;
sum += A[i];
if (sum < 0 && prev < 0)
cnt += sum + 1, sum = 1;
else if (sum > 0 && prev > 0)
cnt += sum + 1, sum = -1;
else if (sum == 0)
cnt++, sum = 1;
}
printf("%lld\n", cnt);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long int n;
cin >> n;
long long int a[n];
for (long long int i = 0; i < n; i++) {
cin >> a[i];
};
bool is_plus;
if (a[0] > 0) {
is_plus = true;
} else if (a[0] < 0) {
is_plus = false;
} else {
long long int x = -1;
for (long long int i = 1; i < n; i++) {
if (a[i] != 0) {
x = i;
break;
}
}
if (x == -1) {
a[0]++;
is_plus = true;
} else {
if (x % 2 == 1) {
a[0] = -1;
is_plus = false;
} else {
a[0] = 1;
is_plus = true;
}
}
}
long long int sum = a[0];
long long int ans = 0;
is_plus = !is_plus;
for (long long int i = 1; i < n; i++) {
sum += a[i];
if (is_plus) {
if (sum <= 0) {
ans += (-sum) + 1;
sum = 1;
}
} else {
if (sum >= 0) {
ans += sum + 1;
sum = -1;
}
}
is_plus = !is_plus;
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> vector;
long long temp;
for(int i=0; i<n; i++) {
cin >> temp;
vector.push_back(temp);
}
long long answer1=0;
long long answer2=0;
long long sum1=0;
long long sum2=0;
for(int i=0; i<n; i++) {
if(i == 0) {
sum1 = vector[0]; //初項
}
else if(sum1 < 0) {
if(sum1 + vector[i] > 0){ //和の符号がデフォルトで異なるとき
// answer -> そのまま
sum1 += vector[i];
}
else {
answer1 += abs((-1)*sum1+1 - vector[i]); // vector[i] -> -sum1+1 までincrimentすると和は1
sum1 = 1;
}
}
else {
if(sum1 + vector[i] < 0) {
//answer->そのまま
sum1 += vector[i];
}
else {
answer1 += abs((-1)*sum1-1 - vector[i]); // vector[i] -> -sum1-1 までincrimentすると和は-1
sum1 = -1;
}
}
}
for(int i=0; i<n; i++) {
if(i==0) {
if(vector[0] > 0) {
sum2 = -1;
answer2 += abs(-1-vector[0]);
}
else {
sum2 = 1;
answer2 += abs(1-vector[0]);
}
}
}
else if(sum2 < 0) {
if(sum2 + vector[i] > 0){ //和の符号がデフォルトで異なるとき
// answer-> そのまま
sum2 += vector[i];
}
else {
answer2 += abs((-1)*sum2+1 - vector[i]); // vector[i] -> -sum1+1 までincrimentすると和は1
sum2 = 1;
}
}
else {
if(sum2 + vector[i] < 0) {
//answer->そのまま
sum2 += vector[i];
}
else {
answer2 += abs((-1)*sum2-1 - vector[i]); // vector[i] -> -sum1-1 までincrimentすると和は-1
sum2 = -1;
}
}
cout << min(answer1,answer2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | 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 | cpp | #include<bits/stdc++.h>
using namespace std;
int main()
{ int n;
cin>>n;
long int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
long int sum=arr[0];
long int ans=0;
for(int i=1;i<n;i++0
{ if(sum<0)
{
sum=sum+arr[i];
if(sum>0)
continue;
else
{ ans+=abs(sum)+1;
sum=1;
}
}
else if
{
sum+=arr[i];
if(sum<0)
continue;
else
{ ans+=sum+1;
sum=-1;
}
}
}
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 | import numpy as np
n = input()
a_s = [int(x) for x in input().split()]
sum_i = 0
manipulate_num = 0
for a in a_s:
new_sum_i = sum_i + a
# print(f"a:{a}, sum_i:{sum_i}, tmp_sum_i:{new_sum_i}")
if new_sum_i == 0:
manipulate_num += 1
new_sum_i = 1
if new_sum_i * sum_i > 0:
new_sum_i = np.sign(new_sum_i)*(-1)
manipulate_num += abs(a - (new_sum_i - sum_i))
sum_i = new_sum_i
# print(f"a:{a}, new_sum_i:{sum_i}, num:{manipulate_num}")
# print()
print(manipulate_num) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("avx")
using namespace std;
const int cm = 1 << 17;
char cn[cm], *ci = cn + cm, ct;
inline char getcha() {
if (ci - cn == cm) {
fread_unlocked(cn, 1, cm, stdin);
ci = cn;
}
return *ci++;
}
inline int getint() {
int A = 0;
int pn = 1;
if (ci - cn + 16 > cm) {
if ((ct = getcha()) == '-') {
pn = -1;
ct = getcha();
}
A = ct - '0';
while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';
;
return pn * A;
} else {
if ((ct = *ci++) == '-') {
pn = -1;
ct = *ci++;
}
A = ct - '0';
while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';
;
return pn * A;
}
}
int main() {
int N = getint();
int s1 = 0;
long long kotae1 = 0;
int s2 = 0;
long long kotae2 = 0;
int a = 0;
for (int i = 0; i < (N / 2); i++) {
a = getint();
s1 += a;
s2 += a;
if (s1 <= 0) {
kotae1 += 1 - s1;
s1 = 1;
}
if (s2 >= 0) {
kotae2 += s2 + 1;
s2 = -1;
}
a = getint();
s1 += a;
s2 += a;
if (s2 <= 0) {
kotae2 += 1 - s2;
s2 = 1;
}
if (s1 >= 0) {
kotae1 += s1 + 1;
s1 = -1;
}
}
if (N & 1) {
a = getint();
s1 += a;
s2 += a;
if (s1 <= 0) {
kotae1 += 1 - s1;
s1 = 1;
}
if (s2 >= 0) {
kotae2 += s2 + 1;
s2 = -1;
}
}
printf("%d", min(kotae1, kotae2));
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
int main(int argc, char const *argv[]) {
int n;
std::cin >> n;
std::vector<int> v(n);
std::vector<ll> sums(2, 0);
for (size_t i = 0; i < n; i++) {
std::cin >> v[i];
sums[i % 2] += v[i];
}
ull ans = 0;
if (sums[0] > sums[1] && v[0] <= 0) {
ans = ans + abs(v[0]) + 1;
v[0] = 1;
} else if (sums[0] < sums[1] && v[0] >= 0) {
ans = ans + abs(v[0]) + 1;
v[0] = -1;
} else if (v[0] == 0) {
ans += 1;
v[0] = 1;
}
ll now, pre;
now = pre = v[0];
for (size_t i = 1; i < n; i++) {
now = v[i] + pre;
if (pre * now >= 0) {
if (pre > 0) {
ans = ans + abs(now) + 1;
now = -1;
} else if (pre < 0) {
ans = ans + abs(now) + 1;
now = 1;
}
}
pre = now;
}
std::cout << ans << '\n';
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
long long cost = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
if (a[i] <= 0) cost += 1 - a[i];
} else {
if (a[i] >= 0) cost += a[i] + 1;
}
}
long long cost2 = 0;
for (int i = 0; i < n; i++) {
if (i % 2 != 0) {
if (a[i] <= 0) cost2 += 1 - a[i];
} else {
if (a[i] >= 0) cost2 += a[i] + 1;
}
}
cout << min(cost, cost2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int n;
cin >> n;
ll a[n];
for (int i = 0; i < n; i++) cin >> a[i];
ll ans = 4e18;
if (a[0] < 0)
for (int i = 0; i < n; i++) a[i] *= -1;
ll sum = a[0], now = 0;
for (int i = 0; i < n - 1; i++) {
if (i % 2) {
if (sum + a[i + 1] <= 0) {
now += 1 - (sum + a[i + 1]);
sum = 1;
} else {
sum += a[i + 1];
}
} else {
if (sum + a[i + 1] >= 0) {
now += sum + a[i + 1] + 1;
sum = -1;
} else {
sum += a[i + 1];
}
}
}
ans = min(ans, now);
sum = -1, now = a[0] + 1;
for (int i = 0; i < n - 1; i++) {
if (i % 2 == 0) {
if (sum + a[i + 1] <= 0) {
now += 1 - a[i + 1] - sum;
sum = 1;
} else {
sum += a[i + 1];
}
} else {
if (sum + a[i + 1] >= 0) {
now += sum + a[i + 1] + 1;
sum = -1;
} else {
sum += a[i + 1];
}
}
}
ans = min(ans, now);
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n][2];
for (int i = 0; i < n; i++) {
cin >> a[i][0];
a[i][1] = a[i][0];
}
int sum = 0;
int res[2];
for (int check = 0; check < 2; check++) {
sum = 0;
if (check) {
if (a[0][check] > 0) {
int temp = -1 - a[0][check];
a[0][check] += temp;
res[check] += temp * -1;
} else if (a[0][check] < 0) {
int temp = 1 - a[0][check];
a[0][check] += temp;
res[check] += temp;
}
if (a[0][check] == 0) {
if (!check) {
a[0][check]++;
} else {
a[0][check]--;
}
res[check]++;
}
}
for (int i = 0; i < n - 1; i++) {
sum += a[i][check];
if (sum * (sum + a[i + 1][check]) >= 0) {
if (sum > 0) {
int temp = -1 - sum - a[i + 1][check];
a[i + 1][check] += temp;
res[check] += temp * -1;
} else {
int temp = 1 - sum - a[i + 1][check];
a[i + 1][check] += temp;
res[check] += temp;
}
}
}
}
cout << min(res[0], res[1]) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
long long int sum = 0, in, ans = 0;
cin >> n >> sum;
for (int i = 1; i < n; i++) {
cin >> in;
if (sum * in < 0 && abs(sum) < abs(in)) {
sum += in;
continue;
} else if (sum * in < 0) {
ans += abs(sum) - abs(in) + 1;
if (sum > 0)
sum = -1;
else
sum = 1;
continue;
}
ans += abs(sum) + abs(in) + 1;
if (sum < 0) {
sum = 1;
} else {
sum = -1;
}
}
if (sum == 0) ans++;
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
A = list(map(int,input().split()))
ans = 0
s = A[0]
if s > 0:
flag = 1
elif s < 0:
flag = -1
elif A[1] < 0:
flag = 1
ans += 1
else:
flag = -1
ans += 1
for i in range(1,N):
s += A[i]
if flag == 1 and s >= 0:
ans += s + 1
s = -1
elif flag == -1 and s <= 0:
ans += 1 - s
s = 1
flag *= -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 | UNKNOWN | import std.stdio, std.algorithm, std.conv, std.array, std.string;
long check(long op, long sum, long[] as)
{
foreach (a; as) {
if (sum < 0) {
if ((sum + a) <= 0) {
op += (1 - (sum + a));
sum = 1;
} else {
sum += a;
}
} else {
if ((sum + a) >= 0) {
op += sum + a + 1;
sum = -1;
} else {
sum += a;
}
}
}
return op;
}
void main()
{
readln;
auto as = readln.chomp.split(" ").map!(to!long).array;
auto op1 = check(0, as[0], as[1..$]);
auto op2 = check((as[0] - 1).abs, 1, as[1..$]);
auto op3 = check((as[0] + 1).abs, -1, as[1..$]);
writeln(min(op1, op2, op3));
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 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 <string>
using namespace std;
int main(){
int n,i,c=0;
cin >> n;
int a[n],s[2];
//入力
for (i=0;i<n;i++){
cin >> a[i];
}
//操作
s[0] = a[0],s[1]=0;
for (i=1;i<n;i++){
s[1] = s[0]+a[i];
if (s[0]*s[1] >=0){//符号一致(または0)していたら符号を一致させない方向へ動かす
for (;s[0]*s[1]>=0;){
if (s[0]>0) s[1] -=1;
else s[1] +=1;
c += 1;//カウントを増やす
}
}
s[0] = s[1];
// cout <<"Sum=" <<s[0]<<endl;
}
cout <<c<<;
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 sum1 = 0, ans1 = 0;
for (int i = 0; i < n; i++) {
sum1 += a[i];
if (i % 2 == 0) {
if (sum1 >= 0) {
ans1 += sum1 + 1;
sum1 = -1;
}
} else {
if (sum1 <= 0) {
ans1 += abs(sum1) + 1;
sum1 = 1;
}
}
}
int sum2 = 0, ans2 = 0;
for (int i = 0; i < n; i++) {
sum2 += a[i];
if (i % 2 == 0) {
if (sum2 <= 0) {
ans2 += abs(sum2) + 1;
sum2 = 1;
}
} else {
if (sum2 >= 0) {
ans2 += sum2 + 1;
sum2 = -1;
}
}
}
cout << min(ans1, ans2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int M[100][100];
int color[100];
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {-1, 0, 1, 0};
static const int NIL = -1;
int n;
void printArray(int *array, int);
void printDimention(vector<vector<int> >);
int main(int argc, char const *argv[]) {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> n;
int a[n];
for (int i = (0); i < (n); ++i) cin >> a[i];
bool flg = true;
int tmp = 0;
int d;
int res1 = 0;
for (int i = (0); i < (n); ++i) {
tmp += a[i];
if (flg) {
if (tmp <= 0) {
d = 1 - tmp;
res1 += d;
tmp = 1;
}
flg = false;
} else {
if (tmp >= 0) {
d = 1 + tmp;
res1 += d;
tmp = -1;
}
flg = true;
}
}
flg = false;
tmp = 0;
int res2 = 0;
for (int i = (0); i < (n); ++i) {
tmp += a[i];
if (flg) {
if (tmp <= 0) {
d = 1 - tmp;
res2 += d;
tmp = 1;
}
flg = false;
} else {
if (tmp >= 0) {
d = 1 + tmp;
res2 += d;
tmp = -1;
}
flg = true;
}
}
cout << min(res1, res2) << endl;
}
void printArray(int array[], int n) {
for (int i = (0); i < (n); ++i) {
if (i) cout << " ";
cout << array[i];
}
cout << endl;
}
void printDimention(vector<vector<int> > &dv) {
for (int i = (0); i < ((int)dv.size()); ++i) {
for (int j = (0); j < ((int)dv[i].size()); ++j) {
if (j) cout << " ";
cout << dv[i][j];
}
cout << endl;
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | # -*- coding: utf-8 -*-
n = int(input())
a = [int(n) for n in input().split()]
count_a = 0
count_b = 0
nowsum = a[0]
if nowsum != 0:
for n in range(1, n):
if nowsum * (nowsum + a[n]) >= 0:
count_a += abs(nowsum + a[n]) + 1
if nowsum < 0:
nowsum = 1
else:
nowsum = -1
else:
nowsum += a[n]
print(count_a)
else:
a[0] = 1
count_a += 1
nowsum = 1
for n in range(1, n):
if nowsum * (nowsum + a[n]) >= 0:
count_a += abs(nowsum + a[n]) + 1
if nowsum < 0:
nowsum = 1
else:
nowsum = -1
else:
nowsum += a[n]
a[0] = -1
count_b += 1
nowsum = -1
for n in range(1, n):
if nowsum * (nowsum + a[n]) >= 0:
count_b += abs(nowsum + a[n]) + 1
if nowsum < 0:
nowsum = 1
else:
nowsum = -1
else:
nowsum += a[n]
print(min(count_a, count_b)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using int64 = long long;
int64 max(int64 a, int64 b) {
if (b > a)
return b;
else
return a;
}
int64 min(int64 a, int64 b) {
if (b < a)
return b;
else
return a;
}
signed main() {
int64 N;
cin >> N;
vector<int64> a(N);
int64 sum1 = 0, sum2 = 0;
int64 res1 = 0, res2 = 0;
for (int64 i = 0; i < N; i++) {
cin >> a[i];
sum1 += a[i];
sum2 += a[i];
if (i % 2 == 0) {
if (sum1 <= 0) {
res1 += abs(sum1) + 1;
sum1 = 1;
}
if (sum2 > 0) {
res2 += abs(sum2) + 1;
sum2 = -1;
}
} else {
if (sum1 > 0) {
res1 += abs(sum1) + 1;
sum1 = -1;
}
if (sum2 <= 0) {
res2 += abs(sum2) + 1;
sum2 = 1;
}
}
}
cout << min(res1, res2) << 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 s1, s2, c1, c2, a;
for (int i = 1; i <= n; i++) {
cin >> a;
s1 += a;
s2 += a;
if (i % 2) {
if (s1 <= 0) c1 += 1 - s1, s1 = 1;
if (s2 >= 0) c2 += 1 + s2, s2 = -1;
} else {
if (s1 >= 0) c1 += 1 + s1, s1 = -1;
if (s2 <= 0) c2 += 1 - s2, s2 = 1;
}
}
cout << min(c1, c2) << 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> List(n);
for (int i = 0; i < n; i++) {
cin >> List.at(i);
}
int cnt = 0;
long Sign = 0;
for (int i = 0; i < n; i++) {
if (Sign == 0) {
if (List.at(i) > 0) {
Sign = List.at(i);
} else if (List.at(i) < 0) {
Sign = List.at(i);
}
continue;
}
if (Sign > 0) {
if (Sign + List.at(i) >= 0) {
cnt += abs(Sign + List.at(i)) + 1;
Sign = -1;
} else {
Sign += List.at(i);
}
continue;
}
if (Sign < 0) {
if (List.at(i) + Sign <= 0) {
cnt += abs(Sign + List.at(i)) + 1;
Sign = 1;
} else {
Sign += List.at(i);
}
continue;
}
}
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;
const long long mod = 1e9 + 7;
const long long INF = 1e18;
const double pi = acos(-1.0);
int main(void) {
long long n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < (n); ++i) cin >> a[i];
long long ans = 0;
for (int i = 0; i < (n); ++i) {
if (i + 1 < n && a[i] < 0) {
while (i + 1 < n && a[i] + a[i + 1] <= 0) {
a[i + 1]++;
ans++;
}
} else if (i + 1 < n && a[i] > 0) {
while (i + 1 < n && a[i] + a[i + 1] >= 0) {
a[i + 1]--;
ans++;
}
}
a[i + 1] += 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 dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int n;
cin >> n;
int a[100000];
for (int i = 0; i < (n); i++) {
cin >> a[i];
}
long long int sm = 0;
for (int i = 1; i < n; i++) {
long long int b = 0;
for (int j = 0; j < (i); j++) b += a[j];
if (b * a[i] < 0 && abs(a[i]) > abs(b)) continue;
long long int t = (b > 0) ? -b - 1 : -b + 1;
sm += abs(t - a[i]);
a[i] = t;
}
cout << (sm) << "\n";
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
total = 0
fugo = 0
count = 0
for i in a:
if(fugo == 0):
total = i
if(total > 0):
fugo = 1
else:
fugo = -1
continue
total += i
if(fugo > 0):
fugo = -1
if(total >= 0):
while(total>=0):
count += 1
total -= 1
elif(fugo < 0):
fugo = 1
if(total <= 0):
while(total<=0):
count += 1
total += 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;
int main() {
int n, i;
cin >> n;
long long a[100010];
long long ans = 0;
long long cnt = 0;
int flag = 1;
for (i = 0; i < n; i++) cin >> a[i];
if (a[0] > 0)
flag = 1;
else if (a[0] < 0)
flag = -1;
for (i = 1; i < n; i++)
if (a[i] != 0) break;
if (a[i] > 0) {
if (i % 2)
flag = -1;
else
flag = 1;
} else {
if (i % 2) flag = 1;
flag = -1;
}
cnt = a[0];
for (i = 1; i < n; i++) {
cnt += a[i];
if (cnt * flag >= 0) {
ans += abs(cnt) + 1;
if (flag == -1) {
cnt = 1;
} else {
cnt = -1;
}
}
if (flag == -1) {
flag = 1;
} else {
flag = -1;
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
long long X, Y;
cin >> X >> Y;
if ((X + Y <= 1) || (X == 1 && Y == 1))
cout << "Brown" << endl;
else if ((X + Y) % 2 == 0) {
if (((X + Y) / 2) % 2 == 0)
cout << "Alice" << endl;
else
cout << "Brown" << endl;
} else {
X++;
if (((X + Y) / 2) % 2 == 0)
cout << "Brown" << endl;
else
cout << "Alice" << endl;
}
return 0;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.