Search is not available for this dataset
name
stringlengths 2
88
| description
stringlengths 31
8.62k
| public_tests
dict | private_tests
dict | solution_type
stringclasses 2
values | programming_language
stringclasses 5
values | solution
stringlengths 1
983k
|
---|---|---|---|---|---|---|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, i, j;
vector<int> a;
int sum, count, count2;
cin >> n;
a.resize(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
count = 0;
count2 = 0;
sum = 0;
if (a[0] < 0) {
sum = 1;
count += abs(a[0]) + 1;
} else {
sum = a[0];
}
for (int i = 0; i < n - 1; i++) {
if (sum + a[i + 1] == 0) {
count++;
if (sum < 0)
sum = 1;
else
sum = -1;
} else if (sum < 0 && (sum + a[i + 1]) < 0) {
j = 1 - sum;
count += j - a[i + 1];
sum = 1;
} else if (sum > 0 && (sum + a[i + 1]) > 0) {
j = sum + 1;
count += j + a[i + 1];
sum = -1;
} else {
sum += a[i + 1];
}
}
if (a[0] > 0) {
sum = -1;
count2 += abs(a[0]) + 1;
} else {
sum = a[0];
}
for (int i = 0; i < n - 1; i++) {
if (sum + a[i + 1] == 0) {
count2++;
if (sum < 0)
sum = 1;
else
sum = -1;
} else if (sum < 0 && (sum + a[i + 1]) < 0) {
j = 1 - sum;
count2 += j - a[i + 1];
sum = 1;
} else if (sum > 0 && (sum + a[i + 1]) > 0) {
j = sum + 1;
count2 += j + a[i + 1];
sum = -1;
} else {
sum += a[i + 1];
}
}
cout << min(count, count2);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int sum;
int input;
cin >> sum;
int ans = 0;
int reigai = 0;
int ans1, ans2, sum1, sum2;
if (sum == 0) {
ans1 = 1;
ans2 = 1;
sum1 = 1;
sum2 = -1;
reigai = 1;
}
for (int i = 1; i < n; ++i) {
cin >> input;
if (sum * (sum + input) >= 0) {
ans += abs(sum + input) + 1;
if (sum < 0)
sum = 1;
else
sum = -1;
} else
sum += input;
if (reigai == 1) {
if (sum1 * (sum1 + input) >= 0) {
ans1 += abs(sum1 + input) + 1;
if (sum1 < 0)
sum1 = 1;
else
sum1 = -1;
} else
sum1 += input;
if (sum2 * (sum2 + input) >= 0) {
ans2 += abs(sum2 + input) + 1;
if (sum2 < 0)
sum2 = 1;
else
sum2 = -1;
} else
sum2 += input;
}
}
if (reigai == 0)
cout << ans << endl;
else
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;
const long long INF = 1e+9 + 7;
long long n, m, l;
string s, t;
long long d[100000], dp[100][100];
int main() {
cin >> n;
for (long long i = (0); i < (n); i++) cin >> d[i];
int sum1 = 0, sum2 = 0;
int ans1 = 0, ans2 = 0;
for (long long i = (0); i < (n); i++) {
sum1 += d[i];
if (i % 2 == 0) {
if (sum1 <= 0) {
ans1 += 1 - sum1;
sum1 = 1;
}
} else {
if (sum1 >= 0) {
ans1 += sum1 + 1;
sum1 = -1;
}
}
}
for (long long i = (0); i < (n); i++) {
sum2 += d[i];
if (i % 2 != 0) {
if (sum2 <= 0) {
ans2 += 1 - sum2;
sum2 = 1;
}
} else {
if (sum2 >= 0) {
ans2 += sum2 + 1;
sum2 = -1;
}
}
}
cout << (min(ans1, ans2)) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n=int(input())
a=[int(i) for i in input().split()]
for i in range(1,n):
a[i]+=a[i-1]
if a[0]>0:
pm=0
c=0
for i in range(1,n):
a[i]+=pm
if i%2==1:
if a[i]>=0:
pm-=(a[i]+1)
c+=(a[i]+1)
else:
if a[i]<=0:
pm+=(-a[i]+1)
c+=(-a[i]+1)
print(c)
elif a[0]<0:
pm=0
c=0
for i in range(1,n):
a[i]+=pm
if i%2==0:
if a[i]>=0:
pm-=(a[i]+1)
c+=(a[i]+1)
else:
if a[i]<=0:
pm+=(-a[i]+1)
c+=(-a[i]+1)
print(c)
else:
pm1=1
c1=1
for i in range(1,n):
a[i]+=pm1
if i%2==1:
if a[i]>=0:
pm1-=(a[i]+1)
c1+=(a[i]+1)
else:
if a[i]<=0:
pm1+=(-a[i]+1)
c1+=(-a[i]+1)
pm2=-1
c2=1
for i in range(1,n):
a[i]+=pm2
if i%2==0:
if a[i]>=0:
pm2-=(a[i]+1)
c2+=(a[i]+1)
else:
if a[i]<=0:
pm2+=(-a[i]+1)
c2+=(-a[i]+1)
print(min(c1,c2))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
import numpy as np
ans = 0
sum0 = a[0]
sum1 = a[0]
for i in range(1, n):
sum1 += a[i]
if np.sign(sum0) != np.sign(sum1) and sum1 != 0: #合計の符号が逆となっており、0でない
sum0 = sum1
pass
elif sum1 == 0: #合計が0になった場合は、符号が逆になるよう1か-1を足す
sum1 -= 1 * np.sign(sum0)
ans += 1
sum0 = sum1
elif np.sign(sum0) == np.sign(sum1): #符号が同じ場合は、+1か-1になるまで足す
if np.sign(sum1) == 1: #sum0もsum1もプラスの場合
ans += (sum1 + 1) #
sum1 = -1
else:
ans += (abs(sum1) +1)
sum1 = 1
sum0 = sum1
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
import std.container;
alias sread = () => readln.chomp();
ulong bignum = 1_000_000_007;
alias Pair = Tuple!(long, "begin", long, "end");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void main()
{
auto n = lread();
auto a = aryread();
long c_sum = a[0], change;
foreach (e; a[1 .. $])
{
if (c_sum + e < 0)
{
if (c_sum > 0)
c_sum += e;
else
{
change += abs(1 - c_sum - e);
c_sum = 1;
}
}
else if(c_sum + e > 0)
{
if (c_sum < 0)
c_sum += e;
else
{
change += abs(-1 - c_sum - e);
c_sum = -1;
}
}
else
{
change += 1;
if(c_sum < 0)
c_sum = 1;
else
c_sum = -1;
}
// c_sum.writeln(" c_sum");
// change.writeln(" change");
}
change.writeln();
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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) {
long long n, i, j, sw, sw2, count = 0, add = 0;
cin >> n;
vector<long long> a(n);
for (i = 0; i < n; i++) cin >> a[i];
if (a[0] > 0)
sw = 1;
else
sw = -1;
add += a[0];
for (i = 1; i < n; i++) {
add += a[i];
if (sw == 1) {
if (add < 0) {
} else {
while (add != -1) {
a[i]--;
add--;
count++;
}
}
} else {
if (add > 0) {
} else {
while (add != 1) {
a[i]++;
add++;
count++;
}
}
}
if (a[i] > 0)
sw = 1;
else
sw = -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 | #include <bits/stdc++.h>
long long n, a[111111], hoge, huga, nyaa = 0, nyan = 0;
int main() {
scanf("%lld", &n);
for (int i = 0; i < n; i++) {
scanf("%lld", &a[i]);
}
if (!a[0]) {
hoge = 1;
huga = -1;
nyaa = nyan = 1;
} else {
hoge = ((a[0]) > (-a[0]) ? (a[0]) : (-a[0]));
huga = ((a[0]) > (-a[0]) ? (-a[0]) : (a[0]));
nyaa += abs(a[0] - hoge);
nyan += abs(a[0] - huga);
}
int p = 1;
for (int i = 1; i < n; i++) {
hoge += a[i];
if (p) {
if (hoge >= 0) {
nyaa += hoge + 1;
hoge = -1;
}
} else {
if (hoge <= 0) {
nyaa += 1 - hoge;
hoge = 1;
}
}
huga += a[i];
if (p) {
if (huga <= 0) {
nyan += 1 - huga;
huga = 1;
}
} else {
if (huga >= 0) {
nyan += huga + 1;
huga = -1;
}
}
p ^= 1;
}
printf("%lld\n", ((nyaa) > (nyan) ? (nyan) : (nyaa)));
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];
for (int i = 0; i < n; i++) cin >> a[i];
int cnt = 0;
int current_div0PlusSum = 0;
int current_div0MinusSum = 0;
int cnt_div0Plus = 0;
int cnt_div0Minus = 0;
for (int i = 0; i < n; i++) {
current_div0PlusSum += a[i];
if (i % 2 == 0) {
if (current_div0PlusSum > 0)
continue;
else {
while (current_div0PlusSum <= 0) {
current_div0PlusSum++;
cnt_div0Plus++;
}
}
} else {
if (current_div0PlusSum < 0)
continue;
else {
while (current_div0PlusSum >= 0) {
current_div0PlusSum--;
cnt_div0Plus++;
}
}
}
}
for (int i = 0; i < n; i++) {
current_div0MinusSum += a[i];
if (i % 2 == 0) {
if (current_div0MinusSum < 0)
continue;
else {
while (current_div0MinusSum >= 0) {
current_div0MinusSum--;
cnt_div0Minus++;
}
}
} else {
if (current_div0MinusSum > 0)
continue;
else {
while (current_div0MinusSum <= 0) {
current_div0MinusSum++;
cnt_div0Minus++;
}
}
}
}
cout << min(cnt_div0Plus, cnt_div0Minus) << 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 resolve():
N = int(input())
A = list(map(int, input().split()))
total = A[0]
ope = 0
positive = True if A[0] > 0 else False
for i in range(1, len(A)):
if positive:
# 次は負の数=正の数になるなら補正
if total + A[i] >= 0:
ope += abs(total + A[i]) + 1
total = total + A[i] - abs(total + A[i]) - 1
else:
total += A[i]
else:
# 次は正の数=負の数になるなら補正
if total + A[i] <= 0:
ope += abs(total + A[i]) + 1
total = total + A[i] + abs(total + A[i]) + 1
else:
total += A[i]
positive = (not positive)
print(ope)
if '__main__' == __name__:
resolve() |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
L = [0 for _ in range(n)]
L[0] = a[0]
for i in range(1, n):
L[i] = L[i-1] + a[i]
delay = 0
all_over = 0
if a[0] != 0:
sign = (a[0] > 0) - (a[0] < 0)
for i in range(1, n):
L[i] += delay
if L[i] <= 0 and sign == -1:
delay += 1 - L[i]
all_over += 1 - L[i]
L[i] = 1
elif L[i] >= 0 and sign == 1:
delay -= L[i] + 1
all_over += L[i] + 1
L[i] = -1
sign *= -1
print(all_over)
else:
posL = L[:]
negL = L[:]
pos_delay, neg_delay = 1, -1
pos_all_over, neg_all_over = 1, 1
sign = 1
for i in range(1, n):
posL[i] += pos_delay
if posL[i] <= 0 and sign == -1:
pos_delay += 1 - posL[i]
pos_all_over += 1 - posL[i]
posL[i] = 1
elif posL[i] >= 0 and sign == 1:
pos_delay -= posL[i] + 1
pos_all_over += posL[i] + 1
posL[i] = -1
sign *= -1
sign = -1
for i in range(1, n):
negL[i] += neg_delay
if negL[i] <= 0 and sign == -1:
neg_delay += 1 - negL[i]
neg_all_over += 1 - negL[i]
negL[i] = 1
elif negL[i] >= 0 and sign == 1:
neg_delay -= negL[i] + 1
neg_all_over += negL[i] + 1
negL[i] = -1
sign *= -1
print(min(pos_all_over, neg_all_over)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e12;
const int inf = 1e9;
const int mod = 1e9 + 7;
int main() {
cout << fixed << setprecision(10);
int n;
cin >> n;
vector<int> v(n, 0);
for (int i = 0; i < (n); i++) cin >> v[i];
int ans = inf;
for (int i = 0; i < (2); i++) {
int now = 0;
int sum = 0;
for (int j = 0; j < (n); j++) {
if (i == 0) {
sum += v[j];
if (j % 2 == 0) {
if (sum == 0) {
sum = 1;
now++;
} else if (sum < 0) {
now += 1 - sum;
sum = 1;
}
} else {
if (sum == 0) {
sum = -1;
now++;
} else if (sum > 0) {
now += sum + 1;
sum = -1;
}
}
} else {
sum += v[j];
if (j % 2 == 0) {
if (sum == 0) {
sum = -1;
now++;
} else if (sum > 0) {
now += sum + 1;
sum = -1;
}
} else {
if (sum == 0) {
sum = 1;
now++;
} else if (sum < 0) {
now += 1 - sum;
sum = 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 | UNKNOWN | using System;
class Program
{
static void Main()
{
int n = int.Parse(Console.ReadLine());
var inp = Array.ConvertAll(Console.ReadLine().Split(), long.Parse);
long ans = 0;
long cumsum = inp[0];
for (int i = 1; i < n; i++)
{
if (cumsum * inp[i] >= 0)
{
ans += Math.Abs(cumsum + inp[i]) + 1;
inp[i] = inp[i] > 0 ? -(cumsum + 1) : cumsum + 1;
}
else if (Math.Abs(cumsum) >= Math.Abs(inp[i]))
{
ans += Math.Abs(cumsum + inp[i]) + 1;
inp[i] = inp[i] > 0 ? Math.Abs(cumsum) + 1 : -(Math.Abs(cumsum) + 1);
}
cumsum += inp[i];
}
Console.WriteLine(ans);
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long INF = 10E9;
const long long MOD = 1000000007;
const long double PI = 3.1415926;
template <class T>
T &chmin(T &a, const T &b) {
return a = min(a, b);
}
template <class T>
T &chmax(T &a, const T &b) {
return a = max(a, b);
}
long long int n, m, k, ans = 0, sum = 0, cnt = 0;
string s;
int main() {
long long int n;
cin >> n;
vector<long long int> acc(n);
long long int x = 0;
for (long long int i = (long long int)(0); i < (long long int)(n); i++) {
cin >> acc[i];
acc[i] += x;
x = acc[i];
}
bool minus = true;
long long int tmp = 0;
for (long long int i = (long long int)(0); i < (long long int)(n); i++) {
if ((minus && acc[i] + tmp >= 0) || (!minus && acc[i] + tmp <= 0)) {
ans += llabs(acc[i] + tmp) + 1;
if (!minus)
tmp += (llabs(acc[i] + tmp) + 1);
else
tmp -= (llabs(acc[i] + tmp) + 1);
}
minus = !minus;
}
long long int ans1 = ans;
minus = false;
tmp = 0;
for (long long int i = (long long int)(0); i < (long long int)(n); i++) {
if ((minus && acc[i] + tmp >= 0) || (!minus && acc[i] + tmp <= 0)) {
ans += llabs(acc[i] + tmp) + 1;
if (!minus)
tmp += (llabs(acc[i] + tmp) + 1);
else
tmp -= (llabs(acc[i] + tmp) + 1);
}
minus = !minus;
}
cout << min(ans, ans1) << 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;
string divide[4] = {"dream", "dreamer", "erase", "eraser"};
int main() {
int N, C, K;
cin >> N >> C >> K;
vector<int> T(N);
for (int i = 0; i < N; i++) {
cin >> T.at(i);
}
int sum = 0;
int cnt1 = 0;
for (int i = 0; i < N; i++) {
sum += T.at(i);
if (i % 2 == 0) {
if (sum <= 0) {
cnt1 += -sum + 1;
sum = 1;
}
} else {
if (sum >= 0) {
cnt1 += sum + 1;
sum = -1;
}
}
}
int cnt2 = 0;
sum = 0;
for (int i = 0; i < N; i++) {
sum += T.at(i);
if (i % 2 == 1) {
if (sum <= 0) {
cnt2 += -sum + 1;
sum = 1;
}
} else {
if (sum >= 0) {
cnt2 += sum + 1;
sum = -1;
}
}
}
cout << min(cnt1, cnt2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << endl;
err(++it, args...);
}
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
int ans1 = 0;
int ans2 = 0;
int sum[n];
sum[0] = a[0];
for (int i = 1; i < n; i++) sum[i] = a[i] + sum[i - 1];
int sum1 = 0;
int sum2 = 0;
int i = 0;
while (i < n) {
sum1 += a[i];
sum2 += a[i];
if (i % 2 == 0 && sum1 <= 0) {
ans1 += 1 - sum1;
sum1 += 1 - sum1;
}
if (i % 2 == 1 && sum1 >= 0) {
ans1 += sum1 + 1;
sum1 -= sum1 + 1;
}
if (i % 2 == 0 && sum2 >= 0) {
ans2 += 1 + sum2;
sum2 -= 1 + sum2;
}
if (i % 2 == 1 && sum2 <= 0) {
ans2 += 1 - sum2;
sum2 += -sum2 + 1;
}
i++;
}
cout << min(ans1, ans2);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const unsigned long long MOD = 1000000000 + 7;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a.at(i);
}
int cnt0 = 0;
long long sum = 0;
for (int i = 0; i < n; i++) {
sum += a.at(i);
if (i % 2 == 0 && sum >= 0) {
cnt0 += 1 + sum;
sum = -1;
} else if (i % 2 == 1 && sum <= 0) {
cnt0 += 1 - sum;
sum = 1;
}
}
int cnt1 = 0;
sum = 0;
for (int i = 0; i < n; i++) {
sum += a.at(i);
if (i % 2 == 0 && sum <= 0) {
cnt1 += 1 - sum;
sum = 1;
} else if (i % 2 == 1 && sum >= 0) {
cnt1 += 1 + sum;
sum = -1;
}
}
cout << min(cnt0, cnt1) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | # -*- coding: utf-8 -*-
# 整数の入力
n=int(input())
a=input().split()
counter=0
# 出力
for i in range(1,n):
S=0
for j in range(0,i):
S+=int(a[j])
if S<0 and S+int(a[i])<=0:
counter+=-S-int(a[i])+1
a[i]=-S+1
elif S>0 and S+int(a[i])>=0:
counter+=S+int(a[i])+1
a[i]=-S-1
print(counter) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
int resp = 0;
int s = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
if (s + a[i] > 0) {
s += a[i];
} else {
resp += 1 - s - a[i];
s = 1;
}
} else {
if (s + a[i] < 0) {
s += a[i];
} else {
resp += s + a[i] + 1;
s = -1;
}
}
}
int resm = 0;
s = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 1) {
if (s + a[i] > 0) {
s += a[i];
} else {
resm += 1 - s - a[i];
s = 1;
}
} else {
if (s + a[i] < 0) {
s += a[i];
} else {
resm += s + a[i] + 1;
s = -1;
}
}
}
int res = min(resp, resm);
cout << res << "\n";
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n + 1);
for (int i = 0; i < n; i++) {
cin >> a.at(i);
}
vector<int> b(n + 1);
b[0] = a[0];
int ansa = 0, ansb = 0, sum = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
if (b[i] >= 0) {
ansa += 1 + b[i];
b[i] = -1;
b[i + 1] = b[i] + a[i + 1];
} else
b[i + 1] = b[i] + a[i + 1];
} else {
if (b[i] <= 0) {
ansa += 1 - b[i];
b[i] = 1;
b[i + 1] = b[i] + a[i + 1];
} else
b[i + 1] = b[i] + a[i + 1];
}
}
b[0] = a[0];
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
if (b[i] <= 0) {
ansb += 1 - b[i];
b[i] = 1;
b[i + 1] = b[i] + a[i + 1];
} else
b[i + 1] = b[i] + a[i + 1];
} else {
if (b[i] >= 0) {
ansb += 1 + b[i];
b[i] = -1;
b[i + 1] = b[i] + a[i + 1];
} else
b[i + 1] = b[i] + a[i + 1];
}
}
int ans = min(ansa, ansb);
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> v(n);
for (int i = 0; i < (int)(n); i++) cin >> v[i];
int ans = 0;
int crnt = v[0];
int pre = v[0];
if (v[0] < 0) {
ans += abs(v[0]) + 1;
crnt = 1;
pre = 1;
}
for (int i = 1; i < n; i++) {
pre = crnt;
crnt += v[i];
if (crnt * pre >= 0) {
if (pre > 0) {
ans += crnt + 1;
crnt -= crnt + 1;
} else {
ans += abs(crnt) + 1;
crnt += abs(crnt) + 1;
}
}
}
int fans = 0;
crnt = v[0];
pre = v[0];
if (crnt > 0) {
fans += v[0] + 1;
crnt = -1;
pre = -1;
}
for (int i = 1; i < n; i++) {
pre = crnt;
crnt += v[i];
if (crnt * pre >= 0) {
if (pre > 0) {
fans += crnt + 1;
crnt -= crnt + 1;
} else {
fans += abs(crnt) + 1;
crnt += abs(crnt) + 1;
}
}
}
cout << ans << " " << fans << endl;
cout << min(fans, 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;
const int MAX_N = (int)1e5 + 5;
int n, a[MAX_N];
int ans;
int main(void) {
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
}
int zans = 0, zcur = 0;
for (int i = 0; i < n; ++i) {
zcur += a[i];
if (i % 2 == 0) {
if (zcur <= 0) {
zans += (1 - zcur);
zcur = 1;
}
} else {
if (zcur >= 0) {
zans += (zcur - (-1));
zcur = -1;
}
}
}
int oans = 0, ocur = 0;
for (int i = 0; i < n; ++i) {
ocur += a[i];
if (i % 2 == 0) {
if (ocur >= 0) {
oans += (ocur - (-1));
ocur = -1;
}
} else {
if (ocur <= 0) {
oans += (1 - ocur);
ocur = 1;
}
}
}
ans = min(zans, oans);
printf("%d\n", ans);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
struct point {
int x;
int y;
};
int gcd(int m, int n) {
if ((0 == m) || (0 == n)) return 0;
while (m != n) {
if (m > n)
m = m - n;
else
n = n - m;
}
return m;
}
int lcm(int m, int n) {
if ((0 == m) || (0 == n)) return 0;
return ((m / gcd(m, n)) * n);
}
int input() {
int x;
cin >> x;
return x;
}
int moji(char in) {
int ans = (int)in - (int)'a';
if ((ans < 0) || (ans > 25)) {
ans = 26;
}
return ans;
}
const int VV = 10;
int cost[VV][VV];
int d[VV];
bool used[VV];
void dijkstra(int s) {
fill(d, d + VV, 100000);
fill(used, used + VV, false);
d[s] = 0;
while (true) {
int v = -1;
for (int u = 0; u < VV; u++) {
if (!used[u] && (v == -1 || d[u] < d[v])) v = u;
}
if (v == -1) break;
used[v] = true;
for (int u = 0; u < VV; u++) {
d[u] = min(d[u], d[v] + cost[v][u]);
}
}
}
int compare_int(const void* a, const void* b) { return *(int*)a - *(int*)b; }
int binary_searchh(long long x, long long k[], int n) {
int l = 0;
int r = n;
while (r - l >= 1) {
int i = (l + r) / 2;
if (k[i] == x)
return i;
else if (k[i] < x)
l = i + 1;
else
r = i;
}
return -1;
}
struct File {
int aa;
int bb;
File(const int& aa, const int& bb) : aa(aa), bb(bb) {}
};
bool operator<(const File& a, const File& b) {
return std::tie(a.aa, a.bb) < std::tie(b.aa, b.bb);
}
long long gcd(long long a, long long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
long long lcm(long long a, long long b) {
long long g = gcd(a, b);
return a / g * b;
}
long long kaijo(long long x) {
long long l = 10 * 10 * 10 * 10 * 10 * 10 * 10 * 10 * 10 + 7;
long long sum = 1;
for (int i = x; i > 0; i--) {
sum *= i;
if (sum > l) {
sum %= l;
}
}
return sum;
}
int main() {
long long n;
cin >> n;
long long a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long sum = 0;
long long tmp = a[0];
for (int i = 1; i < n; i++) {
if (tmp >= 0) {
tmp += a[i];
if (tmp > 0) {
sum += tmp + 1;
tmp = -1;
}
} else {
tmp += a[i];
if (tmp <= 0) {
sum += abs(tmp) + 1;
tmp = 1;
}
}
}
cout << sum << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | def resolve(List):
# L[0]!=0を起点とする
L = List
cnt = 0
s = L[0]
for i in range(1,len(L)):
a = L[i]
if(s>0 and s+a>=0):
L[i] = -s-1
cnt += (s+a+1)
s = -1
elif(s<0 and s+a<=0):
L[i] = -s+1
cnt += (-s-a+1)
s = 1
else:
s += a
return cnt
def ans(L):
a = L[0]
c0,c1=0,0
if (a>0):
c0 = resolve(L)
c1 = (a+1) + resolve([-1]+L[1:])
elif (a<0):
c0 = resolve(L)
c1 = (-a+1) + resolve([1]+L[1:])
else:
c0 = 1 + resolve([1]+L[1:])
c1 = 1 + resolve([-1]+L[1:])
return(min(c0,c1))
N = int(input())
L = [int(x) for x in input().split(' ')]
print(ans(L))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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);
cout.tie(NULL);
int n;
cin >> n;
int a[n];
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
long long int sum = 0;
int count = 0;
int prev;
for (int i = 1; i <= n; i++) {
sum += a[i];
if (i != 1) {
if (sum == 0) {
if (prev == 0) {
a[i] += 1;
prev = 1;
count++;
sum += 1;
} else {
a[i] -= 1;
prev = 0;
count++;
sum -= 1;
}
} else {
if (prev == 0) {
if (sum < 0) {
count += 1 - sum;
a[i] = a[i] + (1 - sum);
sum += (1 - sum);
}
prev = 1;
} else {
if (sum > 0) {
count += sum + 1;
a[i] = a[i] - (1 + sum);
sum -= (1 + sum);
}
prev = 0;
}
}
} else {
if (sum >= 0)
prev = 1;
else
prev = 0;
}
}
cout << count << '\n';
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, a[100000], ans, sumb = 0, suma = 0;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) {
sumb = suma;
suma += a[i];
if (suma == 0) {
if (sumb > 0)
suma--;
else
suma++;
ans++;
} else if (suma * sumb > 0) {
if (sumb > 0) {
ans += suma + 1;
suma -= suma + 1;
} else if (sumb < 0) {
ans += 1 - suma;
suma += 1 - suma;
}
} else {
}
}
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 | import qualified Data.ByteString.Char8 as BC
import Data.Maybe (fromJust)
main = do
n <- readLn :: IO Int
(a:as) <- getIntListBC
print $ solve a as
bsToInt :: BC.ByteString -> Int
bsToInt = fst . fromJust . BC.readInt
getIntListBC :: IO [Int]
getIntListBC = map bsToInt . BC.words <$> BC.getLine
solve :: Int -> [Int] -> Int
solve _ [] = 0
solve s (a:as)
| s > 0 = let n = negate $ s + 1
in if n > a then solve (s + a) as
else (abs $ a - n) + solve (s + n) as
| otherwise = let n = negate $ s - 1
in if n < a then solve (s + a) as
else (abs $ n - a) + solve (s + n) as
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
A = list(map(int, input().split()))
ans = 0
total = A[0]
if A[0] == 0:
ans += 1
total = -A[1] // A[1]
for i in range(1, len(A)):
if total > 0:
if total + A[i] >= 0:
ans += total + A[i] + 1
total = -1
else:
total += A[i]
else:
if total + A[i] <= 0:
ans += 1 - (total + A[i])
total = 1
else:
total += A[i]
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
typedef vector<vector<int> > vii;
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < (int)n; i++) cin >> a[i];
int sum = a[0], op_cnt = 0;
for (int i = (int)1; i < (int)n; i++) {
if (sum < 0 && sum + a[i] <= 0) {
op_cnt += (0 - sum - a[i]) + 1;
sum = 1;
} else if (sum >= 0 && sum + a[i] >= 0) {
op_cnt += sum + a[i] + 1;
sum = -1;
} else
sum += a[i];
}
cout << op_cnt << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void) {
long long n;
cin >> n;
long long a[n];
for (long long i = 0; i < (long long)(n); i++) {
cin >> a[i];
}
long long ans = 1000000000;
for (long long p = 0; p <= 1; p++) {
long long tmpans = 0;
long long sum = 0;
for (long long i = 0; i < (long long)(n); i++) {
if (i % 2 == p) {
if (a[i] + sum <= 0) {
tmpans += 1 - (a[i] + sum);
sum = 1;
} else {
sum = a[i] + sum;
}
} else {
if (a[i] + sum >= 0) {
tmpans += 1 + a[i] + sum;
sum = -1;
} else {
sum = a[i] + sum;
}
}
}
ans = min(ans, tmpans);
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long INF = (long long)1e18 + 1;
int n, m;
vector<int> a(100001);
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
long long ps = 0, ans = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
if (ps + a[i] == 0) {
ans++;
if (ps > 0)
ps = -1;
else
ps = 1;
} else if ((ps + a[i] < 0 && ps >= 0) || (ps + a[i] > 0 && ps <= 0)) {
ps += a[i];
} else if (ps + a[i] < 0 && ps < 0) {
ans += abs(ps + a[i]) + 1;
ps = 1;
} else {
ans += abs(ps + a[i]) + 1;
ps = -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 | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
int array[n];
cin >> n;
for (int i = 0; i < n; i++) {
cin >> array[i];
}
int answer = 0;
int sum = 0;
for (int i = 0; i < n; i++) {
if (sum == 0)
sum += array[0];
else if (sum < 0) {
if (sum + array[i] > 0) {
sum += array[i];
} else {
if (sum == -1) {
answer += abs(2 - array[i]);
sum += 2;
} else {
answer += abs((-1) * sum + 1 - array[i]);
sum = 1;
}
}
} else {
if (sum + array[i] < 0) {
sum += array[i];
} else {
if (sum == 1) {
answer += abs(-2 - array[i]);
sum += -2;
} else {
answer += abs((-1) * sum - 1 - array[i]);
sum = -1;
}
}
}
}
cout << answer << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<ll> v(n, 0);
for (int i = (int)(0); i < (int)(n); i++) cin >> v[i];
vector<int> p1(n + 1, 1);
for (int i = (int)(0); i < (int)(n + 1); i++) {
if (i % 2 == 0) p1[i] *= -1;
}
int c[2];
vector<ll> sum_until(n + 1, 0);
int cnt = 0;
for (int i = 1; i <= n; i++) {
sum_until[i] = sum_until[i - 1] + v[i - 1];
if (sum_until[i] * p1[i] < 0) {
int plus = abs(sum_until[i]);
sum_until[i] += plus * p1[i] + p1[i];
cnt += abs(plus * p1[i]) + 1;
} else if (sum_until[i] == 0) {
sum_until[i] = p1[i];
cnt += 1;
}
}
c[0] = cnt;
fill(sum_until.begin(), sum_until.end(), 0ll);
cnt = 0;
for (int i = (int)(0); i < (int)(n + 1); i++) {
if (i % 2 == 1)
p1[i] = -1;
else
p1[i] = 1;
}
for (int i = 1; i <= n; i++) {
sum_until[i] = sum_until[i - 1] + v[i - 1];
if (sum_until[i] * p1[i] < 0) {
int plus = abs(sum_until[i]);
sum_until[i] += plus * p1[i] + p1[i];
cnt += abs(plus * p1[i]) + 1;
} else if (sum_until[i] == 0) {
sum_until[i] = p1[i];
cnt += 1;
}
}
c[1] = cnt;
cout << min(c[1], c[0]) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; ++i) cin >> a[i];
int sum = a[0], befsum = a[0];
int ans = 0;
for (int i = 1; i < n; ++i) {
sum += a[i];
if (sum * befsum >= 0) {
if (sum > 0) {
ans += sum + 1;
sum = -1;
} else if (sum < 0) {
ans += -sum + 1;
sum = 1;
} else if (sum == 0) {
ans += 1;
if (befsum > 0)
sum = -1;
else
sum = 1;
}
}
befsum = sum;
}
int tmp = abs(a[0]) + 1;
if (a[0] > 0) {
sum = -1;
befsum = -1;
} else {
sum = 1;
befsum = 1;
}
for (int i = 1; i < n; ++i) {
sum += a[i];
if (sum * befsum >= 0) {
if (sum > 0) {
tmp += abs(sum) + 1;
sum = -1;
} else if (sum < 0) {
tmp += abs(sum) + 1;
sum = 1;
} else if (sum == 0) {
tmp += 1;
if (befsum > 0)
sum = -1;
else
sum = 1;
}
}
befsum = sum;
}
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 | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long int n;
cin >> n;
vector<long long int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long int ans = 0;
long long int cnt = a[0];
for (int i = 1; i < n; i++) {
if (i % 2 == 0) {
if (0 < cnt + a[i]) {
cnt += a[i];
} else {
ans += 1 - (cnt + a[i]);
cnt = 1;
}
} else {
if (cnt + a[i] < 0) {
cnt += a[i];
} else {
ans += 1 + (cnt + a[i]);
cnt = -1;
}
}
}
long long int ans2 = 0;
cnt = a[0];
for (int i = 1; i < n; i++) {
if (i % 2 == 1) {
if (0 < cnt + a[i]) {
cnt += a[i];
} else {
ans2 += 1 - (cnt + a[i]);
cnt = 1;
}
} else {
if (cnt + a[i] < 0) {
cnt += a[i];
} else {
ans2 += 1 + (cnt + a[i]);
cnt = -1;
}
}
}
if (ans < 0) {
ans = ans2;
} else if (ans > ans2) {
if (0 <= ans2) {
ans = ans2;
}
}
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.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.NoSuchElementException;
public class Main {
int N;
long[] a;
public long calc(long[] a){
long ans = 0;
long now = 0;
for(int i = 0;i < N-1;i++){
now += a[i];
if(now < 0 && now+a[i+1] <= 0){
long add = Math.abs(now+a[i+1])+1;
a[i+1] += add;
ans += add;
}else if(now > 0 && now+a[i+1] >= 0){
long add = now+a[i+1]+1;
a[i+1] -= add;
ans += add;
}
}
if(now + a[N-1] == 0){
ans++;
}
return ans;
}
public void solve() {
N = nextInt();
a = new long[N];
for(int i = 0;i < N;i++){
a[i] = nextInt();
}
if(a[0] == 0){
a[0] = 1;
long ans = calc(a);
a[0] = -1;
ans = Math.min(ans, calc(a));
out.println(ans);
return;
}
out.println(calc(a));
}
public static void main(String[] args) {
out.flush();
new Main().solve();
out.close();
}
/* Input */
private static final InputStream in = System.in;
private static final PrintWriter out = new PrintWriter(System.out);
private final byte[] buffer = new byte[2048];
private int p = 0;
private int buflen = 0;
private boolean hasNextByte() {
if (p < buflen)
return true;
p = 0;
try {
buflen = in.read(buffer);
} catch (IOException e) {
e.printStackTrace();
}
if (buflen <= 0)
return false;
return true;
}
public boolean hasNext() {
while (hasNextByte() && !isPrint(buffer[p])) {
p++;
}
return hasNextByte();
}
private boolean isPrint(int ch) {
if (ch >= '!' && ch <= '~')
return true;
return false;
}
private int nextByte() {
if (!hasNextByte())
return -1;
return buffer[p++];
}
public String next() {
if (!hasNext())
throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = -1;
while (isPrint((b = nextByte()))) {
sb.appendCodePoint(b);
}
return sb.toString();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | def main():
_ = input()
a = [int(s) for s in input().split()]
print(solve(a))
def solve(a):
if a[0] > 0:
return min(rec(a[0], a[1:], 0), rec(-1, a[1:], a[0] + 1))
else:
return min(rec(a[0], a[1:], 0), rec(1, a[1:], 1 - a[0]))
def rec(s, a, r):
if not a:
return r
elif s < 0:
n = max(s + a[0], 1)
return rec(n, a[1:], r + (n - (s + a[0])))
else:
n = min(s + a[0], -1)
return rec(n, a[1:], r + s + a[0] - n)
main()
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N=int(input())
A=list(map(int, input().split()))
ans=0
for i in range(N):
if i==0:
a=A[0]
if a==0:
if A[1]>0:
a=-1
ans+=1
else:
a=1
ans+=1
SUM=a
else:
a=A[i]
isplus=(SUM>0)
if isplus:
if a>0:
ans+=SUM+1
a=-1
else:
if SUM+a>=0:
ans+=1+SUM+a
SUM=-1
else:
SUM+=a
else:
if a>0:
if SUM+a<=0:
ans+=-(SUM+a)+1
SUM=1
else:
SUM+=a
else:
ans+=-a-SUM+1
SUM=1
#print(SUM)
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.Scanner;
public class Main {
public static double sequence(int a[], double start) {
double count = 0.0, presum = -1.0 * start, sum = 0.0;
for(int i : a) {
sum += (double)i;
if(i == 0)sum += start;
if(sum * presum > 0) {
double min = Math.abs(sum) + 1;
if(presum > 0)sum -= min;
else sum += min;
count += min;
}
if(sum == 0) {
if(presum > 0)sum--;
else sum++;
++count;
}
presum = sum;
}
return count;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n, a[];
double count = 0;
n = sc.nextInt();
a = new int[n];
for(int i = 0; i < n; ++i) a[i] = sc.nextInt();
sc.close();
if(a[0] == 0)a[0]++;
int tmp = Math.abs(a[0]) + 1;
if(a[0] > 0)tmp = a[0] - tmp;
else tmp = a[0] + tmp;
count = Math.min(sequence(a, (double)a[0]),sequence(a, tmp));
System.out.printf("%.0f\n", 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.Generic;
using System.Linq;
using static AtCoder.Io;
namespace AtCoder
{
class Program
{
static void Main()
{
var n = ReadInt();
var a = ReadIntArray();
long count = 0;
long sum = a[0];
var isPositive = sum > 0;
for (int i = 1; i < a.Length; i++)
{
isPositive = !isPositive;
if (sum + a[i] >= 0 && !isPositive)
{
var diff = -1 - (sum + a[i]);
count += Math.Abs(diff);
sum += a[i] + diff;
}
else if (sum + a[i] <= 0 && isPositive)
{
var diff = 1 - (sum + a[i]);
count += Math.Abs(diff);
sum += a[i] + diff;
}
else
{
sum += a[i];
}
}
Console.WriteLine($"{count}");
}
}
public static class Io
{
public static string ReadString() => Console.ReadLine();
public static string[] ReadStringArray() => ReadString().Split(' ');
public static int ReadInt() => int.Parse(ReadString());
public static long ReadLong() => long.Parse(ReadString());
public static int[] ReadIntArray() => ReadStringArray().ParseInt().ToArray();
public static long[] ReadLongArray() => ReadStringArray().ParseLong().ToArray();
public static IEnumerable<int> ParseInt(this IEnumerable<string> source) => source.Select(int.Parse);
public static IEnumerable<long> ParseLong(this IEnumerable<string> source) => source.Select(long.Parse);
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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 vll = vector<ll>;
const int INF = 1e9;
int main() {
int n;
cin >> n;
vll s(n, 0);
for (int i = 0; i < n; i++) {
ll a;
cin >> a;
if (i == 0) {
s[i] = a;
} else {
s[i] = s[i - 1] + a;
}
}
bool sgn;
ll tmp = 0LL, cnt = 0LL;
for (int i = 0; i < n; i++) {
if (i == 0) sgn = (s[0] > 0) ? true : false;
s[i] += tmp;
if (sgn) {
if (s[i] <= 0) {
while (s[i] < 1) {
s[i]++;
tmp++;
cnt++;
}
}
} else {
if (s[i] >= 0) {
while (s[i] > -1) {
s[i]--;
tmp--;
cnt++;
}
}
}
sgn = (sgn) ? false : true;
}
cout << cnt << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 |
def culc(a, sum, plus, ans):
for i in range(1, n):
plus = not(plus)
sum += a[i]
if plus:
if sum <= 0:
ans += abs(sum) + 1
sum = 1
else:
if sum >= 0:
ans += abs(sum) + 1
sum = -1
return ans
n = int(input())
a = list(map(int, input().split()))
plus = None
if a[0] > 0:
plus = True
else:
plus = False
ans1 = culc(a, a[0], plus, 0)
a[0] = a[0] // abs(a[0]) * -1
ans2 = abs(a[0]) + 1
ans2 = culc(a, a[0], not(plus), abs(a[0]) + 1)
print(min(ans1, ans2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #region using
using System;
using System.Collections.Generic;
using System.Linq;
using IEnumerable = System.Collections.IEnumerable;
using IEnumerator = System.Collections.IEnumerator;
using BitArray = System.Collections.BitArray;
using BigInteger = System.Numerics.BigInteger;
using TextReader = System.IO.TextReader;
using System.Text;
#endregion
namespace AtCoderProject
{
public class Program
{
public object Calc()
{
var N = consoleReader.Int;
a = consoleReader.Split.Int;
return Math.Min(CalcImpl(true), CalcImpl(false));
}
long CalcImpl(bool startPositive)
{
long count = 0;
long sum = 0;
bool isPositive = startPositive;
for (int i = 0; i < a.Length; i++)
{
sum += a[i];
if (isPositive && sum <= 0)
sum += count += 1 - sum;
else if (!isPositive && sum >= 0)
sum -= count += sum + 1;
isPositive = !isPositive;
}
return count;
}
int[] a;
#region いつもの
#pragma warning disable
private ConsoleReader consoleReader;
public Program(ConsoleReader consoleReader) { this.consoleReader = consoleReader; }
static void Main() => Console.WriteLine(new Program(new ConsoleReader(Console.In)).Calc()); static string AllLines<T>(IEnumerable<T> source) => string.Join("\n", source);
}
static class Ext
{
public static Dictionary<TKey, int> GroupCount<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector) => source.GroupBy(keySelector).ToDictionary(g => g.Key, g => g.Count());
public static Dictionary<TKey, int> GroupCount<TKey>(this IEnumerable<TKey> source) => source.GroupCount(i => i);
}
public class ConsoleReader { private string[] ReadLineSplit() => textReader.ReadLine().Split(Array.Empty<char>(), StringSplitOptions.RemoveEmptyEntries); private string[] line = Array.Empty<string>(); private int linePosition; private TextReader textReader; public ConsoleReader(TextReader tr) { textReader = tr; } public int Int => int.Parse(String); public long Long => long.Parse(String); public double Double => double.Parse(String); public string String { get { if (linePosition >= line.Length) { linePosition = 0; line = ReadLineSplit(); } return line[linePosition++]; } } public class SplitLine { private string[] splited; public SplitLine(ConsoleReader cr) { splited = cr.ReadLineSplit(); cr.line = Array.Empty<string>(); } public int[] Int => String.Select(x => int.Parse(x)).ToArray(); public int[] Int0 => String.Select(x => int.Parse(x) - 1).ToArray(); public long[] Long => String.Select(x => long.Parse(x)).ToArray(); public double[] Double => String.Select(x => double.Parse(x)).ToArray(); public string[] String => splited; } public SplitLine Split => new SplitLine(this); public class RepeatReader : IEnumerable<ConsoleReader> { ConsoleReader cr; int count; public RepeatReader(ConsoleReader cr, int count) { this.cr = cr; this.count = count; } public IEnumerator<ConsoleReader> GetEnumerator() => Enumerable.Repeat(cr, count).GetEnumerator(); System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); public IEnumerable<string> String => this.Select(cr => cr.String); public IEnumerable<int> Int => this.Select(cr => cr.Int); public IEnumerable<int> Int0 => this.Select(cr => cr.Int - 1); public IEnumerable<long> Long => this.Select(cr => cr.Long); public IEnumerable<double> Double => this.Select(cr => cr.Double); } public RepeatReader Repeat(int count) => new RepeatReader(this, count); }
#endregion
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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 mod = 1000000007;
const int INF = 1001001001;
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (long long(i) = 0; (i) < (n); (i)++) cin >> a[i];
long long s = a[0];
long long ans = 0;
for (int i = 1; i < n; ++i) {
long long cur = s + a[i];
if (s > 0) {
if (cur >= 0) {
ans += abs(cur) + 1;
s = -1;
} else {
s += a[i];
}
} else {
if (cur <= 0) {
ans += abs(cur) + 1;
s = 1;
} else {
s += a[i];
}
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #!/usr/bin/env ruby
STDIN.gets.chomp.to_i
array = STDIN.gets.chomp.split(' ').map(&:to_i)
def get_answer(first, array)
ans = 0
sum = first
array.each do |a|
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
end
return ans
end
first = array.shift
if first == 0
ans = [get_answer(1, array), get_answer(-1, array)].min + 1
else
ans1 = get_answer(first, array)
ans2 = get_answer((-1 * first/first), array) + first.abs + 1
ans = [ans1, ans2].min
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 pint = pair<int, int>;
using pll = pair<ll, ll>;
template <typename T>
auto compare = [](T x, T y) -> bool { return (x < y); };
const int MOD = 1000000007;
ll N, a[100010];
ll solve(ll s) {
ll sum = a[0] + s, ans = abs(s);
if (sum == 0) return LONG_LONG_MAX;
for (int(i) = (1); (i) < (N); ++(i)) {
if (sum * (sum + a[i]) < 0) {
sum += a[i];
} else {
if (sum < 0) {
ans += 1 - (sum + a[i]);
sum = 1;
} else if (sum > 0) {
ans += 1 + (sum + a[i]);
sum = -1;
}
}
}
return ans;
}
signed main() {
cin >> N;
for (int(i) = 0; (i) < (N); ++(i)) cin >> a[i];
cout << (min(solve(0), min(solve(1), solve(-1)))) << "\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;
int a[n];
for (int i = 0; i < n; ++i) cin >> a[i];
int sum = 0;
int plus = 0;
for (int i = 0; i < n; ++i) {
if (i % 2 == 0) {
if (sum + a[i] < 1) {
plus += 1 - (sum + a[i]);
sum = 1;
} else
sum += a[i];
} else {
if (sum + a[i] > -1) {
plus += 1 + (sum + a[i]);
sum = -1;
} else
sum += a[i];
}
}
sum = 0;
int minus = 0;
for (int i = 0; i < n; ++i) {
if (i % 2 == 1) {
if (sum + a[i] < 1) {
minus += 1 - (sum + a[i]);
sum = 1;
} else
sum += a[i];
} else {
if (sum + a[i] > -1) {
minus += 1 + (sum + a[i]);
sum = -1;
} else
sum += a[i];
}
}
cout << min(plus, minus) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | program ec12;
var
ans:int64;
a,s:array[0..100000] of int64;
n,m,i,j:longint;
begin
readln(n);
ans:=0;
s[0]:=0;
for i:=1 to n do
begin
read(a[i]);
s[i]:=s[i-1]+a[i];
if i>1 then
begin
if s[i-1]<0 then
begin
if s[i]<=0 then
begin
if s[i]=0 then
begin
inc(ans);
s[i]:=1;
end
else
inc(ans,(-s[i])+1);
end;
end
else
begin
if s[i]>=0 then
begin
if s[i]=0 then
begin
inc(ans);
s[i]:=-1;
end
else
begin
inc(ans,s[i]+1);
s[i]:=-1;
end;
end;
end;
end;
end;
writeln(ans);
end. |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
int main() {
size_t N;
std::cin >> N;
std::vector<int64_t> A(N);
for (size_t n = 0; n < N; ++n) {
std::cin >> A[n];
}
int64_t a[2] = {0, 0};
for (size_t i = 0; i < 2; ++i) {
int64_t c = 0;
if (i == 0) {
a[i] = 0;
c = A[0];
} else {
a[i] = abs(A[0]) + 1;
c = (A[0] < 0) ? 1 : -1;
}
for (size_t n = 1; n < N; ++n) {
if (c + A[n] == 0) {
a[i] += abs(c + A[n]) + 1;
c = (c < 0) ? 1 : -1;
}
if ((c < 0) == (c + A[n] < 0)) {
a[i] += abs(c + A[n]) + 1;
c = (c < 0) ? 1 : -1;
} else {
c += A[n];
}
}
}
std::cout << std::min(a[0], a[1]) << std::endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int,input().split()))
s = []
for i in range(n):
s.append(sum(a[0:i+1]))
count = 0
ans1 = 0
for i in range(n):
if i%2 == 0:
if s[i]+count > 0:
continue
else:
ans1 += 1-(s[i]+count)
count += 1-(s[i]+count)
else:
if s[i]+count < 0:
continue
else:
ans1 += (s[i]+count) + 1
count -= (s[i]+count) + 1
count1 = 0
ans2 = 0
for i in range(n):
if i%2 == 1:
if s[i]+count1 > 0:
continue
else:
ans2 += 1-(s[i]+count1)
count1 += 1-(s[i]+count1)
else:
if s[i]+count1 < 0:
continue
else:
ans2 += (s[i]+count1) + 1
count1 -= (s[i]+count1) + 1
print(min(ans1,ans2))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException{
Sequence solver = new Sequence();
solver.readInput();
solver.solve();
solver.writeOutput();
}
static class Sequence {
private int n;
private int a[];
private int output;
private Scanner scanner;
public Sequence() {
this.scanner = new Scanner(System.in);
}
public void readInput() {
n = Integer.parseInt(scanner.next());
a = new int[n];
for(int i=0; i<n; i++) {
a[i] = Integer.parseInt(scanner.next());
}
}
private int count(boolean sign) {
int count=0;
long sum=0;
for(int i=0; i<n; i++) {
sum += a[i];
if((i%2==0) == sign) {
// a[i]までの合計を正にするとき
if(sum<=0) {
count += Math.abs(sum)+1;
sum = 1;
}
} else {
// a[i]までの合計を負にするとき
if(0<=sum) {
count += Math.abs(sum)+1;
sum = -1;
}
}
}
return count;
}
public void solve() {
output = Math.min(this.count(true), this.count(false));
}
public void writeOutput() {
System.out.println(output);
}
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n=int(input())
A=list(map(int,input().split()))
W=[]
wa=0
for i in range(n):
wa=A[i]+wa
W.append(wa)
counter=0
for i in range(n):
if i==n-1:
break
elif W[i]<0 and W[i+1]<0:
counter=abs(W[i+1])+1-abs(W[i])+counter
elif W[i]>0 and W[i+1]>0:
counter=abs(W[i+1])+1+counter-abs(W[i])
print(counter) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
l = len(a)
ans = 0
summary = a[0]
if(summary == 0):
if(a[1] > 0):
summary = -1
ans+= 1
else:
summary = 1
ans+= 1
for i in range(1, l):
if(summary* (summary+ a[i])>= 0):
if(summary > 0):
ans+= a[i]+ summary+ 1
a[i] = -summary- 1
summary= -1
else:
ans+= -summary+ 1- a[i]
a[i] = -summary+ 1
summary= 1
else:
summary+= a[i]
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
A = list(map(int, input().split()))
cnt = 0
w = A[0]
for i in range(n - 1):
nw = w + A[i + 1]
if w > 0:
if nw >= 0:
cnt += nw + 1
chg = -(nw + 1)
else:
if nw <= 0:
cnt += 1 - nw
chg = 1 - nw
w = nw + chg
chg = 0
print(cnt) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)
n, *a = map(int, read().split())
now = a[0]
ans = 0
for i, aa in enumerate(a[1:]):
if now > 0:
if now + aa + 1 < 0:
now += aa
else:
ans += max(0, now + aa + 1)
now = -1
else:
if -(now + aa) + 1 < 0:
now += aa
else:
ans += max(0, -(now + aa) + 1)
now = 1
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | # sys.stdin.readline
import sys
input = sys.stdin.readline
class AtCoder:
def main(self):
n = int(input())
a = list(map(int, input().split()))
ans = 0
if a[0] == 0:
if a[1] < 0:
a[0] = 1
else:
a[0] = -1
ans += 1
for i in range(1, n):
a[i] = a[i] + a[i - 1]
if a[i - 1] > 0 and a[i] >= 0:
ans += a[i] + 1
a[i] = - 1
elif a[i - 1] < 0 and a[i] < 0:
ans += -1 * a[i] + 1
a[i] = 1
elif a[i] == 0:
a[i] = 1
ans += 1
print(ans)
# Run main
if __name__ == '__main__':
AtCoder().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 = 999999999;
const int MOD = (int)1e9 + 7;
const int EPS = 1e-9;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, a;
cin >> n;
vector<int> A;
for (int i = (0); i < (n); ++i) {
cin >> a;
A.push_back(a);
}
int mn = INF;
for (int i = (0); i < (2); ++i) {
int sum = 0;
int op = 0;
for (int j = (0); j < (n); ++j) {
sum += A[j];
if ((i + j) % 2 == 0 && sum >= 0) {
op += (sum + 1);
sum = -1;
} else if ((i + j) % 2 == 1 && sum <= 0) {
op += (-sum + 1);
sum = 1;
}
}
mn = min(mn, op);
}
cout << mn << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T>
inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
template <class T>
inline T sqr(T x) {
return x * x;
}
const double EPS = 1e-10;
const double PI = acos(-1.0);
pair<long long, long long> maxP(vector<long long> a, long long size) {
pair<long long, long long> p;
long long Max = a[0];
long long place = 0;
for (int i = (0); i < (size); ++i) {
if (a[i] > Max) {
Max = a[i];
place = i;
}
}
p.first = Max;
p.second = place;
return p;
}
pair<long long, long long> minP(vector<long long> a, long long size) {
pair<long long, long long> p;
long long min = a[0];
long long place = 0;
for (int i = (0); i < (size); ++i) {
if (a[i] < min) {
min = a[i];
place = i;
}
}
p.first = min;
p.second = place;
return p;
}
long long sumL(vector<long long> a, long long size) {
long long sum = 0;
for (int i = (0); i < (size); ++i) {
sum += a[i];
}
return sum;
}
long long counT(vector<long long> a, long long t) {
sort(a.begin(), a.end());
return upper_bound(a.begin(), a.end(), t) -
lower_bound(a.begin(), a.end(), t);
}
long long DIV[1000 + 1][1000 + 1];
void divide(long long n, long long m) {
DIV[0][0] = 1;
for (int i = (1); i < (n + 1); ++i) {
DIV[i][0] = 0;
}
for (int i = (0); i < (n + 1); ++i) {
DIV[i][1] = 1;
}
for (int i = (1); i < (m + 1); ++i) {
for (int t = (0); t < (n + 1); ++t) {
if (DIV[t][i] > 0) continue;
if (t >= i) {
DIV[t][i] = DIV[t - i][i] + DIV[t][i - 1];
} else {
DIV[t][i] = DIV[t][i - 1];
}
}
}
}
bool IsPrime(int num) {
if (num < 2)
return false;
else if (num == 2)
return true;
else if (num % 2 == 0)
return false;
double sqrtNum = sqrt(num);
for (int i = 3; i <= sqrtNum; i += 2) {
if (num % i == 0) {
return false;
}
}
return true;
}
class UnionFind {
public:
vector<long long> par;
vector<long long> rank;
UnionFind(long long N) : par(N), rank(N) {
for (int i = (0); i < (N); ++i) par[i] = i;
for (int i = (0); i < (N); ++i) rank[i] = 0;
}
~UnionFind() {}
long long root(long long x) {
if (par[x] == x)
return x;
else {
par[x] = root(par[x]);
return par[x];
}
}
void unite(long long x, long long y) {
long long rx = root(x);
long long ry = root(y);
if (rx == ry) return;
if (rank[rx] < rank[ry]) {
par[rx] = ry;
} else {
par[ry] = rx;
if (rank[rx] == rank[ry]) {
rank[rx]++;
}
}
}
bool same(long long x, long long y) {
long long rx = root(x);
long long ry = root(y);
return rx == ry;
}
};
class BFS_shortestDistance {
public:
BFS_shortestDistance(vector<vector<char> > p_, long long h_, long long w_) {
p = p_;
h = h_;
w = w_;
initial_number = h * w * 2;
for (int i = (0); i < (h); ++i) {
vector<long long> k(w);
for (int t = (0); t < (w); ++t) k[t] = initial_number;
field.push_back(k);
}
}
vector<vector<char> > p;
long long h;
long long w;
long long initial_number;
vector<vector<long long> > field;
pair<long long, long long> plus(pair<long long, long long> &a,
pair<long long, long long> &b) {
pair<long long, long long> p;
p.first = a.first + b.first;
p.second = a.second + b.second;
return p;
}
bool equal(pair<long long, long long> &a, pair<long long, long long> &b) {
return (a.first == b.first && a.second == b.second);
}
bool is_in_field(int h, int w, const pair<long long, long long> &point) {
const int c = point.second;
const int r = point.first;
return (0 <= c && c < w) && (0 <= r && r < h);
}
void init() {
for (int i = (0); i < (field.size()); ++i) {
for (int t = (0); t < (field[i].size()); ++t) {
field[i][t] = initial_number;
}
}
}
void shortest(long long sy, long long sx) {
init();
pair<long long, long long> c[4];
c[0].first = 0;
c[0].second = 1;
c[1].first = 0;
c[1].second = -1;
c[2].first = 1;
c[2].second = 0;
c[3].first = -1;
c[3].second = 0;
queue<pair<long long, long long> > Q;
pair<long long, long long> s;
s.first = sy;
s.second = sx;
field[sy][sx] = 0;
Q.push(s);
while (Q.empty() == false) {
pair<long long, long long> now = Q.front();
Q.pop();
for (int u = 0; u < 4; u++) {
pair<long long, long long> x = c[u];
pair<long long, long long> next = plus(now, x);
if (is_in_field(h, w, next)) {
if (p[next.first][next.second] == '.') {
if (field[next.first][next.second] == initial_number) {
field[next.first][next.second] = field[now.first][now.second] + 1;
Q.push(next);
} else {
}
}
}
}
}
}
};
bool Ischanged(long long a, long long b) {
if (a * b < 0) {
return true;
} else {
return false;
}
}
int main() {
long long n;
cin >> n;
vector<long long> a(n);
for (int i = (0); i < (n); ++i) cin >> a[i];
long long sum = 0;
long long count = 0;
for (int i = (0); i < (n); ++i) {
if (i == 0) {
sum += a[i];
if (sum == 0 && n != 1) {
if (a[1] >= 0) {
sum = -1;
} else {
sum = 1;
}
}
count++;
} else {
long long was = sum;
sum += a[i];
if (Ischanged(was, sum)) {
continue;
} else {
if (sum < 0) {
count += abs(sum) + 1;
sum = 1;
} else {
count += abs(sum) + 1;
sum = -1;
}
}
}
}
cout << count;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(int(i) for i in input().split())
b = []
for i in range(0,len(a)):
b.append(a[i])
def solve(cnt,A,N):
for i in range(1, N):
if sum(A[0:i])>0:
while sum(A[0:i+1])>=0:
A[i]-=1
cnt+=1
else:
while sum(A[0:i+1])<=0:
A[i]+=1
cnt+=1
return cnt
cnt1=0
if b[0]<=0:
while b[0]<=0:
b[0]+=1
cnt1+=1
ans1=solve(cnt1,b,n)
cnt2=0
if a[0]>=0:
while a[0]>=0:
a[0]-=1
cnt2+=1
ans2=solve(cnt2,a,n)
print(min(ans1,ans2))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import numpy as np
n=int(input())
a=[int(i) for i in input().split()]
ap = np.array(a)
an = np.array(a)
#+-+-...
if ap[0] <= 0:
ap[0] = 1
for i in range(1, n):
if sum(ap[:i+1]) <= 0 and i%2 == 0:
ap[i] = 1 - sum(ap[:i])
if sum(ap[:i+1]) >= 0 and i%2 == 1:
ap[i] = -1 - sum(ap[:i])
#-+-+...
if an[0] >= 0:
an[0] = -1
for i in range(1, n):
if sum(an[:i+1]) <= 0 and i%2 == 1:
an[i] = 1 - sum(an[:i])
if sum(an[:i+1]) >= 0 and i%2 == 0:
an[i] = -1 - sum(an[:i])
print(min(sum(abs(np.array(a) - ap)), sum(abs(np.array(a) - an)))) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
void fnInput(vector<int>& rvnNum) {
int nSize;
cin >> nSize;
rvnNum.resize(nSize);
for (int& rnElm : rvnNum) cin >> rnElm;
}
int fnSignChgTimes(const vector<int>& cnrvnNum) {
vector<int> vnTimes(2);
for (int nParity = 0; nParity < 2; nParity++) {
int nTimes = 0;
int nSum = 0;
for (int n = 0; n < cnrvnNum.size(); n++) {
nSum += cnrvnNum[n];
if (n % 2 == nParity)
if (nSum > 0)
;
else {
nTimes += 1 - nSum;
nSum = 1;
}
else if (nSum >= 0) {
nTimes += 1 + nSum;
nSum = -1;
} else
;
}
vnTimes[nParity] = nTimes;
}
auto itElm = min_element(begin(vnTimes), end(vnTimes));
return *itElm;
}
int main() {
vector<int> vnNum;
fnInput(vnNum);
cout << fnSignChgTimes(vnNum) << 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 a, b = 0, c = 0;
int n;
cin >> n >> a;
b += a;
for (int i = 1; i < n; i++) {
cin >> a;
while ((a + b) * b >= 0) {
if (a + b > 0)
a--;
else if (a + b < 0)
a++;
else if (b > 0)
a--;
else if (b < 0)
a++;
c++;
}
b += a;
}
cout << c << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> x;
int temp, ans = 0;
for (int i = 0; i != n; ++i) {
cin >> temp;
x.push_back(temp);
}
if (!x[0]) {
x[0] = 1;
++ans;
int val, ind;
for (int i = 1; i != n; ++i) {
if (!x[i]) {
val = x[i];
ind = i;
break;
}
}
if ((val > 0 && ind % 2) || (val < 0 && !(ind % 2))) x[0] = -1;
}
int sum = x[0];
for (int i = 1; i != n; ++i) {
int sum2 = sum + x[i];
if (sum * sum2 >= 0) {
ans += abs(sum2) + 1;
if (sum < 0)
sum2 = 1;
else
sum2 = -1;
}
sum = sum2;
}
cout << ans;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | n = gets.to_i
as = gets.split(' ').map { |e| e.to_i }
x = 0
bs = []
as.each { |e|
x += e
bs << x
}
# p bs
memo = 0
ans = 0
for i in (1..(n - 1))
a, b = bs[i - 1], bs[i]
a += memo
b += memo
if a >= 0 && b >= 0
d = b + 1
memo -= d
ans += d
elsif a <= 0 && b <= 0
d = -1 * b + 1
memo += d
ans += d
end
end
puts ans |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
const int inf = (1 << 30);
const int mod = 1000000007;
using ll = long long;
using namespace std;
int main() {
ll n;
cin >> n;
vector<ll> a(n);
for (auto &k : a) cin >> k;
ll zcnt = 0;
ll ans = 0;
ll sum = 0;
ll sign = 0;
for (int i = 0; i < n; ++i) {
if (a[i] == 0)
++zcnt;
else
break;
}
if (zcnt == n) {
cout << 2 * n - 1 << endl;
return 0;
}
if (zcnt > 0) {
ans += 2 * zcnt - 1;
sum = sign = (a[zcnt] > 0) ? -1 : 1;
} else {
sum = a[0];
sign = (sum > 0) ? 1 : -1;
zcnt = 1;
}
for (int i = zcnt; i < n; ++i) {
sign *= -1;
ll tempsum = sum + a[i];
ll sumsign = (tempsum > 0) ? 1 : -1;
if (sumsign != sign) {
ans += abs(abs(sum - sign) * sign - a[i]);
sum = sign;
} else {
sum += a[i];
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
int number[100001];
int N;
int sum[100001];
int main() {
int mul = 0;
long long int answer = 0;
int fugou = 0;
scanf("%d", &N);
for (int i = 0; i < N; i++) {
scanf("%d", &(number[i]));
sum[0] = number[0];
mul += number[i];
if (i > 0) {
if (sum[i - 1] * mul >= 0) {
answer += (long long int)abs(mul) + 1;
if (sum[i - 1] < 0) {
mul += abs(mul) + 1;
sum[i] += abs(mul) + 1;
}
if (sum[i - 1] > 0) {
mul -= abs(mul) + 1;
sum[i] -= abs(mul) + 1;
}
} else {
sum[i] = mul;
}
}
}
printf("%lld\n", answer);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import copy
n = int(input())
a = [int(ai) for ai in input().split()]
def search(a, flip=False):
if flip:
count = 0
else:
count = abs(a[0]) + 1
a[0] = 1 if a[0] < 0 else -1
a_sum = a[0]
for ai in a[1:]:
tmp_sum = a_sum + ai
if tmp_sum < 0 and a_sum < 0:
c = +1 - tmp_sum
a_sum = 1
elif tmp_sum > 0 and a_sum > 0:
c = -1 - tmp_sum
a_sum = -1
elif tmp_sum == 0 and a_sum < 0:
c = 1
a_sum = 1
elif tmp_sum == 0 and a_sum > 0:
c = 1
a_sum = -1
else:
c = 0
count += abs(c)
a_sum = tmp_sum + c
return count
print(min(search(a, False), search(a, True))) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | def rec(ary, n, i, sum, cnt)
return cnt if i == n
if sum < 0
sum += ary[i]
if sum <= 0
diff = -sum+1
cnt += diff
sum += diff
end
elsif sum > 0
sum += ary[i]
if sum >= 0
diff = sum+1
cnt += diff
sum -= diff
end
elsif sum == 0 # never
if ary[i+1] > 0
sum -= 1
cnt += 1
else
sum += 1
cnt += 1
end
end
rec(ary, n, i+1, sum, cnt)
end
# main
n = gets.to_i
ary = gets.split(' ').map(&:to_i)
puts rec(ary, n, 1, ary[0], 0) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; ++i) cin >> a[i];
int cnt = 0;
if (a[0] >= 0) {
for (int i = 0; i < n; ++i) {
if (i % 2 == 0) {
int sum = 0;
for (int j = 0; j <= i; ++j) {
sum += a[j];
}
if (sum <= 0) {
cnt += abs(sum) + 1;
a[i] += abs(sum) + 1;
}
} else {
int sum = 0;
for (int j = 0; j <= i; ++j) {
sum += a[j];
}
if (sum >= 0) {
cnt += abs(sum) + 1;
a[i] += -(abs(sum) + 1);
}
}
}
} else {
for (int i = 0; i < n; ++i) {
if (i % 2 == 0) {
int sum = 0;
for (int j = 0; j <= i; ++j) {
sum += a[j];
}
if (sum >= 0) {
cnt += abs(sum) + 1;
a[i] += -(abs(sum) + 1);
}
} else {
int sum = 0;
for (int j = 0; j <= i; ++j) {
sum += a[j];
}
if (sum <= 0) {
cnt += abs(sum) + 1;
a[i] += abs(sum) + 1;
}
}
}
}
cout << cnt << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | # input = sys.stdin.readline
from bisect import *
from collections import *
from heapq import *
# import functools
# import itertools
# import math
n=int(input())
A=list(map(int,input().split()))
temp=A[0]
if temp<0:
flag=0
if temp>0:
flag=1
count=0
for i in range(1,n):
temp+=A[i]
if flag:
if temp>=0:
count+=temp-(-1)
temp=-1
flag=0
else:
if temp<=0:
count+=1-temp
temp=1
flag=1
#print(temp,flag)
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 | python3 | # -*- coding: utf-8 -*-
n = int(input())
an = list(map(int, input().split()))
sum = an[0]
ans = 0
for i in range(1,n):
if sum * (sum + an[i]) < 0:
sum += an[i]
else:
if sum > 0:
ans += abs(sum + an[i] + 1)
sum = -1
else:
ans += abs(sum + an[i] - 1)
sum = 1
ans1 = ans
if an[0] > 0:
sum = -1
else:
sum = 1
ans = abs(an[0])+1
for i in range(1, n):
if sum * (sum + an[i]) < 0:
sum += an[i]
else:
if sum > 0:
ans += abs(sum + an[i] + 1)
sum = -1
else:
ans += abs(sum + an[i] - 1)
sum = 1
print(ans1, ans)
print(min([ans1, ans]))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> A(N);
for (int x = 0; x < (N); x++) {
cin >> A.at(x);
}
long long sign = A.at(0);
long long ans = 0;
for (int x = 0; x < (N - 1); x++) {
if (sign < 0) {
if (sign + A.at(x + 1) <= 0) {
while (sign + A.at(x + 1) <= 0) {
A.at(x + 1)++;
ans++;
}
}
} else {
if (sign + A.at(x + 1) >= 0) {
while (sign + A.at(x + 1) >= 0) {
A.at(x + 1)--;
ans++;
}
}
}
sign += A.at(x + 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() {
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;
sum[0] = 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 | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pint = pair<int, int>;
using pll = pair<ll, ll>;
const long long MOD = 1000000007;
ll N, a[100010];
signed main() {
cin >> N;
for (int(i) = 0; (i) < (N); ++(i)) cin >> a[i];
ll ans = 0;
if (a[0] == 0) {
ll i = 0;
while (i < N && a[i] == 0) i++;
if (i == N || a[i] < 0)
a[0] = 1;
else
a[0] = -1;
ans++;
}
ll sum = a[0];
for (int(i) = (1); (i) < (N); ++(i)) {
if (sum > 0) {
ans += max(0LL, sum + a[i] + 1);
a[i] -= max(0LL, sum + a[i] + 1);
} else if (sum < 0) {
ans += max(0LL, -a[i] - sum + 1);
a[i] += max(0LL, -a[i] - sum + 1);
}
sum += a[i];
}
cout << (ans) << "\n";
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, ansa = 0, ansb = 0, suma = 0, sumb = 0;
cin >> n;
bool plus = true;
for (int i = 0; i < (n); i++) {
int a, b;
cin >> b;
a = b;
while (plus && suma + a <= 0) {
a++;
ansa++;
}
while (!plus && suma + a >= 0) {
a--;
ansa++;
}
while (plus && sumb + b >= 0) {
b--;
ansb++;
}
while (!plus && sumb + b <= 0) {
b++;
ansb++;
}
suma += a;
sumb += b;
plus = !plus;
}
cout << min(ansa, ansb) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
A = [int(_) for _ in input().split()]
ans = 0
s = A[0]
i = 1
while i < N:
a = A[i]
# print(i, s, ans)
sign = (s // abs(s))
abs_min = abs(s) + 1
if a == 0:
ans += abs_min
s = -sign
i += 1
continue
abs_a = abs(a)
a_sign = (a // abs_a)
if a_sign == sign:
A[i] = 0
ans += abs_a
else:
if abs_min <= abs_a:
s = (abs_a - abs(s)) * a_sign
else:
ans += abs_min - abs_a
s = -sign
i += 1
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
int count = 0;
vector<int> a(100000);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<long long int> sum(100000);
sum[0] = a[0];
for (int i = 1; i < n; i++) {
sum[i] = sum[i - 1] + a[i];
if (sum[i] * sum[i - 1] >= 0) {
if (sum[i - 1] < 0) {
count += 1 - sum[i];
sum[i] = 1;
} else {
count += sum[i] + 1;
sum[i] = -1;
}
}
}
cout << count;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <bits/stdc++.h>
int main() {
int n, f = 1, pn = 0, mn = 0, p = 0, m = 0, i, t;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &t);
pn += t;
mn += t;
if (pn * f <= 0) {
p += (pn * f * -1) + 1;
pn = f;
}
if (mn * f >= 0) {
m += (mn * f) + 1;
mn = f * -1;
}
f *= -1;
}
printf("%d", p < m ? p : m);
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int MOD = 1000000007;
const int mod = 1000000007;
const int INF = 1000000000;
const long long LINF = 1e18;
const int MAX = 510000;
bool code(long long int n) {
if (n < 0)
return 1;
else if (n > 0)
return 0;
}
int main() {
int n;
long long int sum = 0;
long long int ans = 0;
long long int ans2 = 0;
cin >> n;
vector<long long int> a(n);
for (int i = 0; i < n; i++) {
cin >> a.at(i);
}
if (a.at(0) != 0) {
sum = a.at(0);
for (int i = 1; i < n; i++) {
if (sum + a.at(i) == 0) {
ans++;
if (sum > 0)
sum = -1;
else if (sum < 0)
sum = 1;
} else if (code(sum + a.at(i)) == code(sum)) {
ans += abs(sum + a.at(i)) + 1;
if (sum > 0)
sum = -1;
else if (sum < 0)
sum = 1;
} else {
sum = a.at(i) + sum;
}
}
cout << ans << endl;
return 0;
} else if (a.at(0) == 0) {
sum = -1;
ans = 1;
for (int i = 1; i < n; i++) {
if (sum + a.at(i) == 0) {
ans++;
if (sum > 0)
sum = -1;
else if (sum < 0)
sum = 1;
} else if (code(sum + a.at(i)) == code(sum)) {
ans += abs(sum + a.at(i)) + 1;
if (sum > 0)
sum = -1;
else if (sum < 0)
sum = 1;
} else {
sum = a.at(i) + sum;
}
}
long long int sum2 = 1;
ans2 = 1;
for (int i = 1; i < n; i++) {
if (sum2 + a.at(i) == 0) {
ans2++;
if (sum2 > 0)
sum2 = -1;
else if (sum2 < 0)
sum2 = 1;
} else if (code(sum2 + a.at(i)) == code(sum2)) {
ans2 += abs(sum2 + a.at(i)) + 1;
if (sum2 > 0)
sum2 = -1;
else if (sum2 < 0)
sum2 = 1;
} else {
sum2 = a.at(i) + sum2;
}
}
if (ans > ans2)
cout << ans2 << endl;
else {
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() {
ios::sync_with_stdio(false);
int n;
cin >> n;
long long a, sum1 = 0, sum2 = 0;
int ans1 = 0, ans2 = 0;
for (int i = 1; i <= n; ++i) {
cin >> a;
sum1 += a;
sum2 += a;
if (i % 2 != 0) {
if (sum1 <= 0) {
ans1 += 1 - sum1;
sum1 = 1;
}
if (sum2 >= 0) {
ans2 += 1 + sum2;
sum2 = -1;
}
} else {
if (sum1 >= 0) {
ans1 += 1 + sum1;
sum1 = -1;
}
if (sum2 <= 0) {
ans2 += 1 - sum2;
sum2 = 1;
}
}
}
cout << min(ans1, ans2) << '\n';
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n;
long long a[100000];
int main() {
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
long long sum = a[0];
long long count = 0;
bool f;
if (a[0] == 0) {
for (int i = 1; i < n; i++) {
if (a[i] != 0) {
if (a[i] > 0) {
if (i % 2 == 0) {
sum--;
count--;
break;
} else {
sum++;
count++;
break;
}
} else {
if (i % 2 == 0) {
sum++;
count++;
break;
} else {
sum--;
count--;
break;
}
}
}
}
}
if (sum == 0) {
sum++;
count++;
}
if (sum > 0)
f = true;
else
f = false;
for (int i = 1; i < n; i++) {
if (f) {
if (sum + a[i] < 0) {
sum += a[i];
f = false;
continue;
}
count += (sum + a[i]) + 1;
sum = -1;
f = false;
} else {
if (sum + a[i] > 0) {
sum += a[i];
f = true;
continue;
}
count -= (sum + a[i]);
count++;
sum = 1;
f = true;
}
}
cout << count << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long N, sum, ans, ans2;
cin >> N;
sum = 0;
vector<long long> x(N);
for (int i = 0; i < N; i++) {
cin >> x[i];
}
for (int i = 0; i < N; i++) {
sum += x[i];
if (i % 2 == 0 && sum <= 0) {
ans += 1 - sum;
sum = 1;
}
if (i % 2 == 1 && sum >= 0) {
ans += 1 + sum;
sum = -1;
}
}
ans2 = ans;
ans = 0;
sum = 0;
for (int i = 0; i < N; i++) {
sum += x[i];
if (i % 2 == 1 && sum <= 0) {
ans += 1 - sum;
sum = 1;
}
if (i % 2 == 0 && sum >= 0) {
ans += 1 + sum;
sum = -1;
}
}
cout << min(ans, ans2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
static const int INF = 2000000000;
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < (int)(n); i++) cin >> a[i];
long long ans = 0;
long long wa;
if (a[0] != 0) {
wa = a[0];
for (int i = 1; i < n; i++) {
if (wa > 0) {
wa += a[i];
if (wa < 0)
continue;
else {
ans += wa + 1;
wa = -1;
}
} else {
wa += a[i];
if (wa > 0)
continue;
else {
ans += 1 - wa;
wa = 1;
}
}
}
cout << ans << endl;
} else {
long long ans1 = 1, ans2 = 1;
wa = 1;
for (int i = 1; i < n; i++) {
if (wa > 0) {
wa += a[i];
if (wa < 0)
continue;
else {
ans1 += wa + 1;
wa = -1;
}
} else {
wa += a[i];
if (wa > 0)
continue;
else {
ans1 += 1 - wa;
wa = 1;
}
}
}
wa = -1;
for (int i = 1; i < n; i++) {
if (wa > 0) {
wa += a[i];
if (wa < 0)
continue;
else {
ans2 += wa + 1;
wa = -1;
}
} else {
wa += a[i];
if (wa > 0)
continue;
else {
ans2 += 1 - wa;
wa = 1;
}
}
}
if (ans1 < ans2)
cout << ans1 << endl;
else
cout << ans2 << endl;
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | n = gets.strip.to_i
a = gets.strip.split.map(&:to_i)
cum_a = [a.first]
(1..n-1).to_a.each do |index|
cum_a[index] = cum_a[index-1]+a[index]
end
# mins first
minus_first_result = 0
minus_first_a = cum_a.dup
(0..n-1).to_a.each do |index|
current = minus_first_a.shift
current_count=0
if index.even? && current>=0
current_count = (current+1)
minus_first_a = minus_first_a.map {|x| x-current_count }
elsif index.odd? && current<=0
current_count = (-1*current+1)
minus_first_a = minus_first_a.map {|x| x+current_count }
end
minus_first_result += current_count
end
plus_first_result = 0
plus_first_a = cum_a.dup
(0..n-1).to_a.each do |index|
current = plus_first_a.shift
current_count=0
if index.even? && current<=0
current_count = (-1*current+1)
plus_first_a = plus_first_a.map {|x| x+current_count }
elsif index.odd? && current>=0
current_count = (current+1)
plus_first_a = plus_first_a.map {|x| x-current_count }
end
plus_first_result += current_count
end
p [plus_first_result, minus_first_result].min
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < (int)(n); i++) cin >> a[i];
long long prevArraySum = a[0];
long long currentArraySum = a[0];
long long res = 0;
if (a[0] == 0) {
res = 1;
prevArraySum = 1;
currentArraySum = 1;
for (int i = (1); i < (n); ++i) {
if (prevArraySum > 0) {
currentArraySum = prevArraySum + a[i];
if (currentArraySum >= 0) {
res += abs(-1 - currentArraySum);
prevArraySum = -1;
} else {
prevArraySum = currentArraySum;
}
} else {
currentArraySum = prevArraySum + a[i];
if (currentArraySum <= 0) {
res += abs(1 - currentArraySum);
prevArraySum = 1;
} else {
prevArraySum = currentArraySum;
}
}
}
long long res1 = res;
res = 1;
for (int i = (1); i < (n); ++i) {
if (prevArraySum > 0) {
currentArraySum = prevArraySum + a[i];
if (currentArraySum >= 0) {
res += abs(-1 - currentArraySum);
prevArraySum = -1;
}
} else {
currentArraySum = prevArraySum + a[i];
if (currentArraySum <= 0) {
res += abs(1 - currentArraySum);
prevArraySum = 1;
} else {
prevArraySum = currentArraySum;
}
}
}
res = min(res, res1);
} else {
for (int i = (1); i < (n); ++i) {
if (prevArraySum > 0) {
currentArraySum = prevArraySum + a[i];
if (currentArraySum >= 0) {
res += abs(-1 - currentArraySum);
prevArraySum = -1;
} else {
prevArraySum = currentArraySum;
}
} else {
currentArraySum = prevArraySum + a[i];
if (currentArraySum <= 0) {
res += abs(1 - currentArraySum);
prevArraySum = 1;
} else {
prevArraySum = currentArraySum;
}
}
}
}
cout << res << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
typedef vector<vector<int> > vii;
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
long long n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < (int)n; i++) cin >> a[i];
long long sum = a[0], op_cnt = 0;
for (int i = (int)1; i < (int)n; i++) {
if (sum < 0 && sum + a[i] <= 0) {
op_cnt += (-1) * (sum + a[i]) + 1;
sum = 1;
} else if (sum > 0 && sum + a[i] >= 0) {
op_cnt += sum + a[i] + 1;
sum = -1;
} else
sum += a[i];
}
cout << op_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;
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;
}
long long gcd(long long a, long long b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
long long keta(long long n) {
string s = to_string(n);
long long num = s.size();
return num;
}
const long long INF = 1LL << 60;
const int dh[4] = {1, 0, -1, 0};
const int dw[4] = {0, 1, 0, -1};
struct Edge {
int to;
int weight;
Edge(int t, int w) : to(t), weight(w) {}
};
using Graph = vector<vector<Edge>>;
using P = pair<long long, int>;
class UnionFind {
public:
vector<int> Parent;
UnionFind(int n) { Parent = vector<int>(n, -1); }
int root(int a) {
if (Parent[a] < 0) return a;
return Parent[a] = root(Parent[a]);
}
bool issame(int a, int b) { return root(a) == root(b); }
int size(int a) { return -Parent[root(a)]; }
bool merge(int a, int b) {
a = root(a);
b = root(b);
if (a == b) return false;
if (size(a) < size(b)) swap(a, b);
Parent[a] += Parent[b];
Parent[b] = a;
return true;
}
};
vector<int> MP(string s) {
vector<int> A(s.size() + 1);
A[0] = -1;
int j = -1;
for (int i = 0; i < s.size(); i++) {
while (j >= 0 && s[i] != s[j]) j = A[j];
j++;
A[i + 1] = j;
}
return A;
}
vector<int> Manacher(string s) {
vector<int> R(s.size());
int 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;
int 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;
}
vector<int> Z_algorithm(string& s) {
vector<int> Z(s.size());
Z[0] = s.size();
int i = 1, j = 0;
while (i < s.size()) {
while (i + j < s.size() && s[j] == s[i + j]) j++;
Z[i] = j;
if (j == 0) {
++i;
continue;
}
int k = 1;
while (i + k < s.size() && k + Z[k] < j) Z[i + k] = Z[k], ++k;
i += k;
j -= k;
}
return Z;
}
const int MAX = 1e6 + 1;
long long fac[MAX], finv[MAX], inv[MAX];
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % (1000000007);
inv[i] = (1000000007) -
inv[(1000000007) % i] * ((1000000007) / i) % (1000000007);
finv[i] = finv[i - 1] * inv[i] % (1000000007);
}
}
long long COM(long long n, long long k) {
if (n >= MAX) {
long long tmp = 1;
for (int i = 0; i < k; i++) {
tmp *= (n - i);
tmp %= (1000000007);
}
return tmp * finv[k] % (1000000007);
}
if (n < k) return 0;
if (n < 0 || k < 0) return 0;
return fac[n] * (finv[k] * finv[n - k] % (1000000007)) % (1000000007);
}
long long POW(long long x, long long n) {
long long ret = 1;
if (n < 0) {
n *= -1;
x = inv[x];
}
while (0 < n) {
if (n % 2 == 0) {
x = x * x % (1000000007);
n /= 2;
} else {
ret = ret * x % (1000000007);
n--;
}
}
return ret;
}
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
vector<long long> s(n);
long long ans1 = 0, ans2 = 0;
for (int i = 0; i < n; i++) {
if (i == 0)
s[i] = a[i];
else
s[i] = s[i - 1] + a[i];
if (i % 2 == 0) {
if (s[i] < 0) {
ans1 += 1 - s[i];
s[i] = 1;
}
} else {
if (s[i] > 0) {
ans1 += s[i] + 1;
s[i] = -1;
}
}
}
for (int i = 0; i < n; i++) {
if (i == 0)
s[i] = a[i];
else
s[i] = s[i - 1] + a[i];
if (i % 2 == 1) {
if (s[i] <= 0) {
ans2 += 1 - s[i];
s[i] = 1;
}
} else {
if (s[i] >= 0) {
ans2 += s[i] + 1;
s[i] = -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 | python3 | n = int(input())
a = list(map(int, input().split()))
c = 0
s = a[0]
fugo = a[0]
# print(a)
for i in range(1, n):
s = s + a[i]
while fugo * s >= 0: # 符号が一緒だったら
if a[i - 1] < 0:
s += 1
else:
s -= 1
c += 1
# print(s, c)
fugo = s
# print(s, c, fugo)
print(c)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const long long mod2 = 998244353;
long long bin(long long bina) {
long long ans = 0;
for (long long i = 0; bina > 0; i++) {
ans = ans + (bina % 2) * pow(10, i);
bina = bina / 2;
}
return ans;
}
bool prime(long long n) {
for (long long i = 2; i <= sqrt(n); i++) {
if (n % i == 0) return false;
}
return n != 1;
}
long long gcd(long long x, long long y) {
if (y == 0) return x;
return gcd(y, x % y);
}
long long lcm(long long x, long long y) { return x * y / gcd(x, y); }
long long kai(long long x) {
if (x == 0) return 1;
return kai(x - 1) * x % mod;
}
long long mod_pow(long long x, long long y, long long m) {
long long res = 1;
while (y > 0) {
if (y & 1) {
res = res * x % m;
}
x = x * x % m;
y >>= 1;
}
return res;
}
long nCr(long long n, long long r) {
long long ans = 1;
for (long long i = n; i > n - r; --i) {
ans = ans * i;
}
for (long long i = 1; i < r + 1; ++i) {
ans = ans / i;
}
return ans;
}
struct union_find {
long long par[200010], size_[200010];
void init(long long x) {
for (long long i = 0; i < x; ++i) {
par[i] = i;
size_[i] = 1;
}
}
long long find(long long x) {
if (par[x] == x) return x;
return par[x] = find(par[x]);
}
void unite(long long x, long long y) {
x = find(x);
y = find(y);
if (x == y) return;
if (size_[x] < size_[y]) {
par[x] = y;
size_[y] += size_[x];
} else {
par[y] = x;
size_[x] += size_[y];
}
}
};
long long a[100010];
signed main() {
long long n;
cin >> n;
for (long long i = 0; i < n; ++i) {
cin >> a[i];
}
long long ans = 0, ans2 = 0;
long long sum = 0;
for (long long i = 0; i < n; ++i) {
sum += a[i];
if (i % 2 == 0) {
if (sum <= 0) {
ans += 1 - sum;
sum = 1;
}
} else {
if (sum > 0) {
ans += sum + 1;
sum = -1;
}
}
}
sum = 0;
for (long long i = 0; i < n; ++i) {
sum += a[i];
if (i % 2 != 0) {
if (sum <= 0) {
ans2 += 1 - sum;
sum = 1;
}
} else {
if (sum > 0) {
ans2 += sum + 1;
sum = -1;
}
}
}
cout << min(ans, ans2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <cstdio>
#include <iostream>
#define ll long long
const int MAXN = 1e5 + 7;
const ll Inf = 1ll << 60;
using namespace std;
ll a[MAXN];
ll sum, ans = INF, res, n;
int main() {
bool flag = 0;
cin>>n;
for(int i = 1; i <= n; i++)
cin>>a[i];
for(int i = 1; i <= n; i++) {
sum += a[i];
if(!flag) { // if flag is 0 , means sum is postive
if(sum <= 0) {
res += 1 - sum;
sum = 1;
}
}else {
if(sum >= 0) {
res += sum + 1;
sum = -1;
}
}
flag ^= 1;
}
if(res < ans) ans = res;
flag = 0, res = sum = 0;
for(int i = 1; i <= n; i++) {
sum += a[i];
if(!flag) { // if flag is 0 , means sum is negative
if(sum >= 0) {
res += sum + 1;
sum = -1;
}
}else {
if(sum <= 0) {
res += 1 - sum;
sum = 1;
}
}
flag ^= 1;
}
if(res < ans) ans = res;
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, ansa = 0, ansb = 0, suma = 0, sumb = 0;
cin >> n;
for (int i = 0; i < (n); i++) {
int c;
cin >> c;
suma += c;
sumb += c;
if (i % 2 == 0) {
if (suma <= 0) {
ansa += 1 - suma;
suma = 1;
}
if (sumb >= 0) {
ansb += sumb + 1;
sumb = -1;
}
} else {
if (suma >= 0) {
ansa += suma + 1;
suma = -1;
}
if (sumb <= 0) {
ansb += 1 - sumb;
sumb = 1;
}
}
}
cout << min(ansa, ansb) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n;
long long func(vector<long long int>& s, vector<int>& hugo, long long int k) {
long long int ret = 0;
for (int i = 1; i < n; i++) {
if (s[i] == k) {
if (hugo[i - 1] == 0) {
hugo[i] = 1;
ret++;
k--;
} else {
hugo[i] = 0;
ret++;
k++;
}
} else if (s[i] > k) {
if (hugo[i - 1] == 0) {
hugo[i] = 1;
ret += s[i] - k + 1;
k += s[i] - k + 1;
} else {
hugo[i] = 0;
}
} else {
if (hugo[i - 1] == 0) {
hugo[i] = 1;
} else {
hugo[i] = 0;
ret += k - s[i] + 1;
k -= k - s[i] + 1;
}
}
}
return ret;
}
void solve() {
cin >> n;
vector<long long int> v(n), sum(n), sum2(n);
for (int i = 0; i < n; i++) {
cin >> v[i];
if (i == 0)
sum[i] = v[i];
else
sum[i] = sum[i - 1] + v[i];
}
vector<int> hugo(n);
sum2 = sum;
long long int ans = 0;
if (sum[0] == 0) {
vector<int> hugo2(n);
hugo[0] = 0;
ans = min(func(sum, hugo, -1), func(sum2, hugo2, 1));
} else if (sum[0] > 0) {
hugo[0] = 0;
ans = func(sum, hugo, 0);
} else {
hugo[0] = 1;
ans = func(sum, hugo, 0);
}
cout << ans << endl;
return;
}
int main() {
solve();
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = [int(i) for i in input().split()]
tmp1 = 0
tmp2 = 0
S = [0]*n
if a[0] > 0:
S[0] = a[0]
else:
S[0] = 1
tmp1 += abs(1-a[0])
for i in range(1,n):
tmpS = a[i] + S[i-1]
if tmpS*S[i-1] < 0:
S[i] = tmpS
continue
else:
if i%2 == 0:
S[i] = 1
tmp1 += abs(1 - tmpS)
else:
S[i] = -1
tmp1 += abs(-1-tmpS)
if a[0] < 0:
S[0] = a[0]
else:
S[0] = -1
tmp2 += abs(-1-a[0])
for i in range(1,n):
tmpS = a[i] + S[i-1]
if tmpS*S[i-1] < 0:
continue
else:
if i%2 == 1:
S[i] = -1
tmp2 += abs(-1 - tmpS)
else:
S[i] = 1
tmp2 += abs(1-tmpS)
print(min(tmp1,tmp2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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 sign(X):
if X==0:
return 0
else:
return [-1,1][X>0]
N = int(input())
A = [int(T) for T in input().split()]
Count = 0
SumNow = 0
SumRes = 0
for TA in range(0,N):
SumNow += A[TA]
if SumNow==0:
if sign(SumRes)==1:
Count += 1
SumNow = -1
else:
Count += 1
SumNow = 1
else:
if sign(SumNow)==sign(SumRes)==1:
Count += SumNow+1
SumNow = -1
elif sign(SumNow)==sign(SumRes)==-1:
Count += 1-SumNow
SumNow = 1
SumRes = SumNow
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 | python3 |
import sys
input = sys.stdin.readline
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
# A = [ int(input()) for _ in range(N) ]
##############################
N = int(input())
A = list(map(int, input().split()))
def get_count(summary):
count = 0
for i in range(1, N):
# print(summary)
# 次はマイナス
if summary > 0:
# 条件を満たしてる?
if (summary + A[i]) < 0:
summary += A[i]
else:
# プラスになっちゃってるので修正
summary += A[i]
count += abs(-1-summary)
summary = -1
# 次はプラス
else:
if (summary + A[i]) > 0:
summary += A[i]
else:
# マイナスになっちゃってるので修正
summary += A[i]
count += abs(1-summary)
summary = 1
return count
if A[0] > 0:
plus = get_count(A[0])
minus = get_count(-1*A[0])
minus += (A[0]+1)
else:
minus = get_count(A[0])
plus = get_count(-1*A[0])
plus += abs(-1-A[0])
print(min(plus, minus)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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, type = 1;
long long a[100009], res;
cin >> N;
cin >> a[0];
res = 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 | UNKNOWN | package main
import (
"bufio"
"fmt"
"math"
"os"
"strconv"
)
const pi = math.Pi
var mod int = pow(10, 9) + 7
var Umod uint64 = 1000000007
var ans int64
func main() {
reader.Split(bufio.ScanWords)
n, _ := strconv.Atoi(read())
a := make([]int, n)
for i := 0; i < n; i++ {
a[i], _ = strconv.Atoi(read())
}
sum := make([]int64, n)
sum[0] = int64(a[0])
for i := 1; i < n; i++ {
sum[i] += int64(a[i]) + sum[i-1]
if (0 <= sum[i-1] && 0 <= sum[i]) || (sum[i-1] <= 0 && sum[i] <= 0) {
// NGパターン
if sum[i] < 0 {
ans += 1 - sum[i]
sum[i] = 1
} else if 0 < sum[i] {
ans += sum[i] + 1
sum[i] = -1
} else {
if sum[i-1] < 0 {
ans += 1 - sum[i]
sum[i] = 1
} else if 0 < sum[i-1] {
ans += sum[i] + 1
sum[i] = -1
}
}
}
}
fmt.Println(ans)
}
/* ---------------------------------------- */
var reader = bufio.NewScanner(os.Stdin)
func read() string {
reader.Scan()
return reader.Text()
}
func lcm(x, y int) int {
return (x / gcd(x, y)) * y
}
func gcd(x, y int) int {
if x%y == 0 {
return y
} else {
r := x % y
return gcd(y, r)
}
}
var fac [1000000]int
var finv [1000000]int
var inv [1000000]int
func combination_init() {
fac[0], fac[1] = 1, 1
finv[0], finv[1] = 1, 1
inv[1] = 1
// invは a^(-1) mod p
// pをaで割ることを考える
// p/a*(a) + p%a = p
// p/a*(a) + p%a = 0 (mod p)
// -p%a = p/a*(a) (mod p)
// -p%a *a^(-1)= p/a (mod p)
// a^(-1)= p/a * (-p%a)^(-1) (mod p)
// a^(-1) =
for i := 2; i < 1000000; i++ {
fac[i] = fac[i-1] * i % mod
inv[i] = mod - inv[mod%i]*(mod/i)%mod
finv[i] = finv[i-1] * inv[i] % mod
}
}
func combination(x, y int) int {
if x < y {
return 0
}
if fac[0] != 1 {
combination_init()
}
return fac[x] * (finv[y] * finv[x-y] % mod) % mod
//return fac[x] / (fac[y] * fac[x-y])
}
func permutation(x, y int) int {
if x < y {
return 0
}
if fac[0] != 1 {
combination_init()
}
return fac[x] * (finv[x-y] % mod) % mod
//return fac[x] / fac[x-y]
}
func max(x ...int) int {
var res int = x[0]
for i := 1; i < len(x); i++ {
res = int(math.Max(float64(x[i]), float64(res)))
}
return res
}
func min(x ...int) int {
var res int = x[0]
for i := 1; i < len(x); i++ {
res = int(math.Min(float64(x[i]), float64(res)))
}
return res
}
func pow(x, y int) int { return int(math.Pow(float64(x), float64(y))) }
func abs(x int) int { return int(math.Abs(float64(x))) }
func floor(x int) int { return int(math.Floor(float64(x))) }
func ceil(x int) int { return int(math.Ceil(float64(x))) }
type SortBy [][]int
func (a SortBy) Len() int { return len(a) }
func (a SortBy) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a SortBy) Less(i, j int) bool { return a[i][0] < a[j][0] }
type PriorityQueue []int
func (h PriorityQueue) Len() int { return len(h) }
func (h PriorityQueue) Less(i, j int) bool { return h[i] < h[j] }
func (h PriorityQueue) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *PriorityQueue) Push(x interface{}) { *h = append(*h, x.(int)) }
func (h *PriorityQueue) Pop() interface{} {
old := *h
n := len(old)
x := old[n-1]
*h = old[0 : n-1]
return 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 | UNKNOWN | n = gets.to_i
a = gets.split.map(&:to_i)
b = a.dup
ans0 = 0
if b[0] < 0
ans0 += -b[0] + 1
b[0] = 1
end
(n-1).times do |i|
b[i+1] += b[i]
seki = b[i+1] * b[i]
if seki < 0
next
else
sgn = b[i] > 0 ? 1 : -1
ans0 += (b[i+1]).abs + 1
b[i+1] = -sgn
end
end
b = a.dup
ans1 = 0
if b[0] > 0
ans1 += b[0] + 1
b[0] = -1
end
(n-1).times do |i|
b[i+1] += b[i]
seki = b[i+1] * b[i]
if seki < 0
next
else
sgn = b[i] > 0 ? 1 : -1
ans1 += (b[i+1]).abs + 1
b[i+1] = -sgn
end
end
# p [ans0, ans1]
puts ans0 < ans1 ? ans0 : ans1
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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 abs(int a) {
if (a < 0)
return -a;
else
return a;
}
bool isDifAbs(long int a, long int b) {
if (a * b < 0) return true;
return false;
}
int main() {
int n, tmp, ttmp, ans = 0;
long int sum;
bool isOk;
cin >> n;
cin >> sum;
for (int i = 1; i < n; i++) {
isOk = true;
cin >> tmp;
if (!isDifAbs(sum, sum + tmp)) {
ttmp = tmp;
if (sum < 0)
tmp = abs(tmp) + 1;
else if (sum > 0)
tmp = -(abs(tmp) + 1);
ans += abs(tmp - ttmp);
}
if (sum + tmp == 0) {
if (sum < 0) tmp++;
if (sum > 0) tmp--;
ans++;
}
sum += tmp;
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
n = int(input())
a = list( map(int, input().split()))
from itertools import accumulate
acc = a
acc = list(accumulate(acc))
# a[i]+...+a[j]までは b[j+1]-b[i]
def dfs(ind,prev,tmp,ans):
if ind == n:
return ans
now = acc[ind]
if prev +tmp > 0:
if now + tmp >= 0:
ans +=abs(now + tmp +1)
tmp += -(now + tmp +1)
elif prev +tmp < 0:
if now + tmp <= 0:
ans +=abs(now + tmp -1)
tmp += -(now + tmp -1)
return dfs(ind+1, now+tmp,tmp,ans)
ansM = dfs(0,-1,0,0)
ansP = dfs(0,+1,0,0)
print(min(ansM,ansP))
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.