Search is not available for this dataset
name
stringlengths 2
88
| description
stringlengths 31
8.62k
| public_tests
dict | private_tests
dict | solution_type
stringclasses 2
values | programming_language
stringclasses 5
values | solution
stringlengths 1
983k
|
---|---|---|---|---|---|---|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n=int(input())
b=list(map(int,input().split()))
a=b[:]
condition=''
cnt=0
wa=0
for i in range(n):
wa+=a[i]
if i == 0:
if a[i]>0:
condition='minus'
else:
condition='plus'
elif condition == 'plus':
condition='minus'
if wa<=0:
cnt+=abs(wa)+1
a[i]+=abs(wa)+1
wa+=abs(wa)+1
elif condition == 'minus':
condition='plus'
if wa>=0:
cnt+=abs(wa)+1
a[i]-=abs(wa)+1
wa-=abs(wa)+1
cnt1=cnt
a=b[:]
condition=''
cnt=0
wa=0
for i in range(n):
wa+=a[i]
if i == 0:
a[i]=a[i]/abs(a[i])*(-1)
cnt+=abs(a[i])+1
wa=a[i]
if a[i]>0:
condition='minus'
else:
condition='plus'
elif condition == 'plus':
condition='minus'
if wa<=0:
cnt+=abs(wa)+1
a[i]+=abs(wa)+1
wa+=abs(wa)+1
elif condition == 'minus':
condition='plus'
if wa>=0:
cnt+=abs(wa)+1
a[i]-=abs(wa)+1
wa-=abs(wa)+1
cnt2=cnt
print(min(cnt1,cnt2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
class Program {
static void Main(string[] args) {
int n = int.Parse(Console.ReadLine());
string[] A = Console.ReadLine().Split();
int sum = int.Parse(A[0]);
int sign = Math.Sign(sum);
int ans = 0;
for (int i = 1; i < n; ++i) {
int a = int.Parse(A[i]);
sign *= -1;
if (sign * Math.Sign(a + sum) > 0) {
sum += a;
}
else {
ans += Math.Abs((Math.Abs(sum) + 1) * sign - a);
sum = sign;
}
}
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;
int main() {
int n;
cin >> n;
vector<int> v;
v.reserve(n);
for (int i = 0; i < n; ++i) {
int tmp;
cin >> tmp;
v.push_back(tmp);
}
int case1 = 0;
int pre = 0;
for (int i = 0; i < n; ++i) {
const auto k = pre + v[i];
if (i % 2) {
if (k >= 0) {
case1 += k + 1;
pre = -1;
} else {
pre = k;
}
} else {
if (k <= 0) {
case1 += 1 - k;
pre = 1;
} else {
pre = k;
}
}
}
int case2 = 0;
pre = 0;
for (int i = 0; i < n; ++i) {
const auto k = pre + v[i];
if (i % 2 == 0) {
if (k >= 0) {
case2 += k + 1;
pre = -1;
} else {
pre = k;
}
} else {
if (k <= 0) {
case2 += 1 - k;
pre = 1;
} else {
pre = k;
}
}
}
cout << min(case1, case2) << 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 | def c(ints):
for i in range(len(ints)):
if ints[i] != 0:
sig = 1 if ints[i] > 0 else -1
sig_ = -sig
total = ints[i]
total_ = -sig
mov = i
mov_ = abs(total) + 1
if i > 0:
mov += 1
mov_ += 2
j = i
break
if i == len(ints) - 1:
return i + 1
for i_ in ints[j+1:]:
tmp = total + i_
tmp_ = total_ + i_
if tmp == 0:
mov +=1
tmp = -sig
elif sig * tmp > 0:
mov += abs(tmp) + 1
tmp = -sig
if tmp_ == 0:
mov_ +=1
tmp_ = -sig_
elif sig_ * tmp_ > 0:
mov_ += abs(tmp_) + 1
tmp_ = -sig_
sig *= -1
total = tmp
sig_ *= -1
total_ = tmp_
return min(mov, mov_)
_ = input()
inp = input()
inp = inp.split(' ')
inp = [int(i_) for i_ in inp]
print(c(inp)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long sub_mod(long long a, long long b, long long x) {
long long tmp = (a - b) % x;
if (tmp < 0) tmp += x;
return tmp;
}
long long gcd(long long a, long long b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<int> nums(n, 0);
for (int i = 0; i < n; ++i) {
cin >> nums[i];
}
int sum = nums[0], ans = 0;
for (int i = 1; i < n; ++i) {
if ((sum > 0 && sum + nums[i] < 0) || (sum < 0 && sum + nums[i] > 0)) {
} else if (sum > 0) {
ans += abs(sum + nums[i] + 1);
nums[i] -= ans;
} else {
ans += abs(sum + nums[i] - 1);
nums[i] += ans;
}
sum += nums[i];
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include<bits/stdc++.h>
using namespace std;
/*int check(vector<int> a){
int time = 0;
int pre_sum = a.at(0);
int n = a.size();
if(a.at(0)<0){
for(int i = 1; i<n; i++){
int sum = pre_sum + a.at(i);
if(i%2==1 && sum <= 0){
time += abs(sum-1);
sum = 1;
}else if (i%2==0 && sum >=0){
time += abs(sum+1);
sum = -1;
}
pre_sum = sum;
}
}else if(a.at(0)>0){
for(int i = 1; i<n; i++){
int sum = pre_sum + a.at(i);
if(i%2==0 && sum <= 0){
time += abs(sum-1);
sum = 1;
}else if(i%2==1 && sum >=0){
time += abs(sum+1);
sum = -1;
}
pre_sum = sum;
}
}
return time;
}
int zerocheck(vector<int> a){
a.at(0) = 1;
int time1 = check(a)+1;
a.at(0) = -1;
int time2 = check(a)+1;
int time = min(time1, time2);
return time;
}
int main(){
int n;
cin >> n;
int time = 0;
vector<int> a(n);
for(auto& x:a){
cin >> x;
}
if(a.at(0) == 0){
time = zerocheck(a);
}else{
time = check(a);
}
cout << time << endl;
}*/
int main(){
int n;
cin >> n;
vector<int> a(n);
for(auto& x: a){
cin >> x;
}
int sum1 = 0;
int sum2 = 0;
int time1 = 0;
int time2 = 0;
for(int i =0;i<n;i++){
sum1 += a.at(i);
sum2 += a.at(i);
if(i%2 == 0){
if(sum1<=0){
time1 += sum1*(-1)+1;
sum1 = 1;
}
if(sum2>=0){
time2 += sum2+1;
sum2 = -1;
}
}
if(i%2==1){
if(sum1>=0){
time1 += sum1+1:
sum1 = -1;
}
if(sum2<=0){
time2 += sum2*(-1)+1;
sum2 = 1;
}
}
}
cout << min(time1, time2) << 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 a[100000];
int getTotal(int n, int dir) {
int total{}, sum{};
for (int i{0}; i < n; ++i) {
sum += a[i];
if (dir > 0 && sum <= 0) {
total += -sum + 1;
sum = 1;
} else if (dir < 0 && sum >= 0) {
total += sum + 1;
sum = -1;
}
dir *= -1;
}
return total;
}
int main() {
int n;
scanf("%d", &n);
for (int i{0}; i < n; ++i) scanf("%d", &a[i]);
int try1 = getTotal(n, 1);
int try2 = getTotal(n, -1);
printf("%d\n", ((try1) < (try2) ? (try1) : (try2)));
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
long long a[100000] = {0};
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long tmp[2] = {-1, 1};
long long ans = 0LL;
long long sum = a[0];
for (int i = 1; i < n; i++) {
if (sum * (sum + a[i]) > 0) {
if (sum > 0) {
ans += abs(sum + a[i] - tmp[0]);
sum = tmp[0];
} else if (sum < 0) {
ans += abs(sum + a[i] - tmp[1]);
sum = tmp[1];
}
} else if (sum + a[i] == 0) {
if (sum > 0) {
ans += 1;
sum = tmp[0];
} else if (sum < 0) {
ans += 1;
sum = tmp[1];
}
} else {
sum += a[i];
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
scanf("%d", &n);
int a[n];
for (int i = 0; i < n; i++) scanf(" %d", &a[i]);
int S = a[0];
int j = 0;
for (int i = 1; i < n; i++) {
if (S * (S + a[i]) < 0) {
S += a[i];
} else if (S + a[i] == 0) {
j += 1;
if (S > 0)
S = -1;
else if (S < 0)
S = 1;
} else {
j += abs(S + a[i]) + 1;
if (S < 0)
S = 1;
else if (S > 0)
S = -1;
}
}
printf("%d\n", j);
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a_list = list(map(int,input().split()))
def greeed(sum_a,now_a,next_a,count):
if now_a >= 0 and next_a >= 0:
next_a = -1
count += next_a + 1
elif now_a < 0 and next_a <0:
next_a = 0
count += next_a
if sum_a + next_a ==0:
if next_a >=0:
next_a += 1
count += 1
else :
next_a = next_a -1
count+= 1
return count
count = 0
if a_list[0] ==0:
if a_list[1]>=0:
a_list = -1
count+= 1
else :
a_list = 1
count +=1
sum_a = a_list[0]
for i in range(0,n-1):
now_a = a_list[i]
next_a = a_list[i + 1]
count = greeed(sum_a,now_a,next_a,count)
print(count)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long INF = (1ll << 60);
const long long MOD = (long long)1e9 + 7;
signed main() {
long long n;
long long aa[100001];
long long a[100001];
cin >> n;
for (long long i = 0; i < n; i++) {
cin >> aa[i];
a[i] = aa[i];
}
long long mn = INF;
{
long long cnt;
long long sum;
if (a[0] > 0) {
cnt = 0;
} else {
cnt = 1 - a[0];
a[0] = 1;
}
sum = 0;
for (long long i = 1; i < n; i++) {
if (i % 2 == 0) {
if (sum + a[i - 1] + a[i] <= 0) {
cnt += 1 - sum - a[i - 1] - a[i];
a[i - 1] = 1 - sum - a[i];
}
} else {
if (sum + a[i - 1] + a[i] >= 0) {
cnt += sum + a[i - 1] + a[i] + 1;
a[i - 1] = -1 - sum - a[i];
}
}
sum += a[i - 1];
}
mn = min(mn, cnt);
}
for (long long i = 0; i < n; i++) a[i] = aa[i];
{
long long cnt;
long long sum;
if (a[0] < 0) {
cnt = 0;
} else {
cnt = a[0] + 1;
a[0] = -1;
}
sum = 0;
for (long long i = 1; i < n; i++) {
if (i % 2 == 0) {
if (sum + a[i - 1] + a[i] >= 0) {
cnt += sum + a[i - 1] + a[i] + 1;
a[i - 1] = -1 - sum - a[i];
}
} else {
if (sum + a[i - 1] + a[i] <= 0) {
cnt += 1 + sum + a[i - 1] + a[i];
a[i - 1] = 1 - sum - a[i];
}
}
sum += a[i - 1];
}
mn = min(mn, cnt);
}
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;
template <typename T>
void showvector(vector<T> v) {
for (T x : v) cout << x << " ";
cout << "\n";
}
template <typename T>
void showvector1(vector<T> v) {
long long int n = v.size();
for (long long int i = 1; i <= n - 1; i++) cout << v[i] << "\n";
}
template <typename T>
void showset(set<T> s) {
for (T x : s) cout << x << " ";
cout << "\n";
}
template <class T>
void showvectorpair(vector<T> v) {
for (auto it = v.begin(); it != v.end(); it++)
cout << it->first << " " << it->second << "\n";
cout << "\n";
}
template <typename T, typename P>
void showmap(map<T, P> m) {
for (auto it = m.begin(); it != m.end(); it++)
cout << it->first << " " << it->second << "\n";
cout << "\n";
}
template <typename T>
bool comp(T a, T b) {
return (a > b);
}
template <class T>
bool comppair(T a, T b) {
if (a.first == b.first) return (a.second > b.second);
return (a.first > b.first);
}
bool sameparity(long long int a, long long int b) { return (a % 2 == b % 2); }
bool difparity(long long int a, long long int b) { return !(a % 2 == b % 2); }
bool isprime(long long int x) {
if (x <= 1) return false;
for (long long int i = 2; i <= sqrt(x); i++) {
if (x % i == 0) return false;
}
return true;
}
bool iseven(long long int x) { return !(x % 2); }
bool isodd(long long int x) { return (x % 2); }
void vfun() {
long long int n, k;
cin >> n;
vector<long long int> v(n);
for (long long int i = 0; i < n; i++) cin >> v[i];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long int test = 1;
while (test--) {
long long int n;
cin >> n;
vector<long long int> v(n);
for (long long int i = 0; i < n; i++) cin >> v[i];
long long int sum = v[0], psum = v[0], cnt = 0;
if (v[0] == 0) {
cnt = 1;
if (v[1] > 0)
sum = psum = -1;
else
sum = psum = 1;
}
for (long long int i = 1; i <= n - 1; i++) {
sum += v[i];
if (psum > 0) {
if (sum >= 0) {
cnt += sum + 1;
sum = -1;
}
} else {
if (sum <= 0) {
cnt += abs(sum) + 1;
sum = 1;
}
}
psum = sum;
}
cout << cnt << "\n";
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
now_a = a[0]
r, tmp, count = 0, 0, 0
# for i in range(1, n):
# tmp = t + a[i]
# if t < 0 and tmp < 0:
# r = 1 - tmp
# elif t > 0 and tmp > 0:
# r = -tmp - 1
# elif tmp == 0:
# if t < 0:
# r = 1 - t - a[i]
# else:
# r = -1 - t - a[i]
# else:
# r = 0
# print(t,tmp, r)
# count += abs(r)
# t = tmp + r
# print(t)
# print(count)
'''
True = positive
False = negative
'''
sign = True
if now_a < 0:
sign = False
for i in range(1, n):
next_a = now_a + a[i]
if sign:
if next_a >= 0:
count += next_a + 1
now_a = -1
else:
now_a = next_a
sign = False
else:
if next_a <= 0:
count += abs(next_a) + 1
now_a = 1
else:
now_a = next_a
sign = True
print(count)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
A = list(map(int,input().split()))
a = [A,A]
res = [0,0]
sum = 0
for check in range(2):
sum = 0
if a[check][0] == 0:
if check == 0:
a[0][0] += 1
else:
a[1][0] -= 1
res[check] += 1
if check == 1:
if a[1][0] > 0:
temp = -1 - a[1][0]
a[1][0] += temp
res[1] += temp * -1
elif a[1][0] < 0:
temp = 1 - a[1][0]
a[1][0] += temp
res[1] += temp
for i in range(n-1):
sum += a[check][i]
if sum * (sum + a[check][i+1]) >= 0:
if sum > 0:
temp = -1 - sum - a[check][i+1]
a[check][i+1] += temp
res[check] += temp * -1
else:
temp = 1 - sum - a[check][i+1]
a[check][i+1] += temp
res[check] += temp
print(min(res[0],res[1])) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n;
int a[100000], c[2];
int main() {
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int C = 0; C < 2; C++) {
int sign = C * 2 - 1;
int sum = 0;
for (int i = 0; i < n; i++) {
if ((sum + a[i]) * sign < 1) {
c[C] += abs(sum + a[i] - sign);
sum = sign;
} else
sum += a[i];
sign *= -1;
}
}
cout << min(c[0], c[1]);
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python2 | n = int(raw_input())
a = map(int, raw_input().split())
count = 0
if a[0] == 0:
count += 1
if 0 < a[1]:
a[0] = 1
else:
a[0] = -1
for i in range(1, n):
SUM = [sum(a[:i]), sum(a[:(i + 1)])]
if 0 <= SUM[0] * SUM[1]:
if 0 < SUM[0]:
a[i] -= (SUM[1] + 1)
count += (SUM[1] + 1)
else:
a[i] += (-SUM[1] + 1)
count += (-SUM[1] + 1)
print(count) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
cin.sync_with_stdio(false);
int n, a;
cin >> n;
int odd_sum = 0, even_sum = 0;
int odd_cost = 0, even_cost = 0;
for (int i = (int)(0); i < (int)(n); i++) {
cin >> a;
if (i % 2) {
if (odd_sum + a > 0)
odd_sum += a;
else {
odd_cost += abs(1 - (odd_sum + a));
odd_sum = 1;
}
if (even_sum + a < 0)
even_sum += a;
else {
even_cost += abs(-1 - (even_sum + a));
even_sum = -1;
}
} else {
if (odd_sum + a < 0)
odd_sum += a;
else {
odd_cost += abs(-1 - (odd_sum + a));
odd_sum = -1;
}
if (even_sum + a > 0)
even_sum += a;
else {
even_cost += abs(1 - (even_sum + a));
even_sum = 1;
}
}
}
cout << min(odd_cost, even_cost) << 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;
template <class T>
using V = vector<T>;
template <class T, class U>
using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
const ll MOD = (ll)1e9 + 7;
const ll HIGHINF = (ll)1e18;
int main() {
ll n;
cin >> n;
vll a(n);
for (ll i = 0; i < (ll)n; ++i) cin >> a.at(i);
ll ansp = 0, ansm = 0;
ll sump = 0, summ = 0;
if (a.at(0) < 0) {
ansp = -a.at(0) + 1;
sump = 1;
summ = a.at(0);
} else {
ansm = a.at(0) + 1;
summ = -1;
sump = a.at(0);
}
for (ll i = 1; i < (ll)n; ++i) {
if (sump > 0) {
if (sump + a.at(i) >= 0) {
ansp += a.at(i) + sump + 1;
sump = -1;
} else {
sump += a.at(i);
}
} else {
if (sump + a.at(i) <= 0) {
ansp += -(a.at(i) + sump) + 1;
sump = 1;
} else {
sump += a.at(i);
}
}
if (summ < 0) {
if (summ + a.at(i) <= 0) {
ansm += -(a.at(i) + summ) + 1;
summ = 1;
} else {
summ += a.at(i);
}
} else {
if (summ + a.at(i) >= 0) {
ansm += a.at(i) + summ + 1;
summ = -1;
} else {
summ += a.at(i);
}
}
}
cout << min(ansp, ansm) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] a = new int[n];
for(int i =0; i<n; i++) {
a[i] = sc.nextInt();
}
boolean isPositive = false;
if(a[0] > 0) {
isPositive = true;
}
int cnt = 0;
int tmp = a[0];
for(int i=0; i<n; i++) {
if(i+1 < n) {
if(isPositive) {
while(tmp+a[i+1] >= 0) {
a[i+1]--;
cnt++;
}
} else {
while(tmp+a[i+1] <= 0) {
a[i+1]++;
cnt++;
}
}
if(isPositive) {
isPositive = false;
} else {
isPositive = true;
}
tmp += a[i+1];
}
}
System.out.println(cnt);
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | def sequence(N: int, A: list) -> int:
def count_op(s: int) -> int:
op = 0
if s * A[0] > 0:
s = A[0]
else:
s = s / abs(s)
op = abs(1 - s)
for a in A[1:]:
if s < 0:
if s + a > 0:
# OK
s = s + a
continue
else:
op += 1 - (s + a)
s = 1
else: # s > 0
if s + a < 0:
# OK
s = s + a
continue
else:
op += (s + a) - (-1)
s = -1
return op
return min(count_op(1), count_op(-1))
if __name__ == "__main__":
N = int(input())
A = [int(s) for s in input().split()]
ans = sequence(N, A)
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() {
cin.tie(NULL);
ios::sync_with_stdio(false);
int n;
cin >> n;
long long a[n];
long long sum = 0;
long long count = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
if (i == 0) {
sum = a[i];
if ((sum == 0) && (i < n - 1)) {
if (a[i + 1] < 0) {
sum = 1;
} else {
sum = -1;
}
}
} else {
if (sum < 0) {
if (sum + a[i] <= 0) {
count += 1 - (sum + a[i]);
sum = 1;
} else {
sum = sum + a[i];
}
} else if (sum > 0) {
if (sum + a[i] >= 0) {
count += sum + a[i] - (-1);
sum = -1;
} else {
sum = sum + a[i];
}
}
}
}
cout << count << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> S(N + 1);
for (int i = 1; i <= N; ++i) {
cin >> S[i];
S[i] += S[i - 1];
}
int ans = 0;
int add = 0;
int sign = S[1] >> 31;
for (int i = 2; i <= N; ++i) {
S[i] += add;
int sign_i = ((S[i] >> 31) << 1) + 1;
if (sign_i == sign) {
ans += abs(-sign_i - S[i]);
add += -sign_i - S[i];
S[i] = -sign_i;
sign_i = -sign_i;
} else if (S[i] == 0) {
ans += 1;
add += -sign;
S[i] += -sign;
sign_i = -sign;
}
sign = sign_i;
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long sum = 0, num = 0;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
if (sum > 0 && sum + a >= 0) {
num += sum + a + 1;
a -= sum + a + 1;
} else if (sum < 0 && sum + a <= 0) {
num += abs(sum + a) + 1;
a += sum + a + 1;
}
sum += a;
}
cout << num << 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
import copy
import math
from _bisect import *
from collections import *
from operator import itemgetter
from math import factorial
"""
from fractions import gcd
def lcm(x, y):
return (x * y) // gcd(x, y)
"""
stdin = sys.stdin
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()
n = ni()
li = na()
ans = 0
if li[0] > 0:
code = 1
else:
code = 0
s = li[0]
for i in range(n - 1):
code = 1 - code
if code:
if s + li[i + 1] > 0:
s += li[i + 1]
continue
else:
ans += abs(s * (-1) + 1 - li[i + 1])
s = 1
else:
if s + li[i + 1] < 0:
s += li[i + 1]
continue
else:
ans += abs(s * (-1) - 1 - li[i + 1])
s = -1
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long a[n];
for (int i = 0; i < n; i++) cin >> a[i];
long long sum = a[0];
long long ans = 0;
if (sum == 0) {
sum = 1;
ans++;
}
for (int i = 1; i < n; i++) {
long long tmp = sum + a[i];
if (sum > 0 && tmp > 0) {
ans += tmp + 1;
sum = -1;
} else if (sum < 0 && tmp < 0) {
ans += -tmp + 1;
sum = 1;
} else if (tmp == 0) {
ans++;
if (sum < 0)
sum = 1;
else
sum = -1;
} else
sum = tmp;
}
long long sum2;
if (a[0] >= 0)
sum2 = -1;
else
sum2 = 1;
long long ans2 = a[0] + 1;
for (int i = 1; i < n; i++) {
long long tmp = sum2 + a[i];
if (sum2 > 0 && tmp > 0) {
ans2 += tmp + 1;
sum2 = -1;
} else if (sum2 < 0 && tmp < 0) {
ans2 += -tmp + 1;
sum2 = 1;
} else if (tmp == 0) {
ans2++;
if (sum2 < 0)
sum2 = 1;
else
sum2 = -1;
} else
sum2 = tmp;
}
cout << min(ans2, 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 | #
# abc059 c
#
import sys
from io import StringIO
import unittest
class TestClass(unittest.TestCase):
def assertIO(self, input, output):
stdout, stdin = sys.stdout, sys.stdin
sys.stdout, sys.stdin = StringIO(), StringIO(input)
resolve()
sys.stdout.seek(0)
out = sys.stdout.read()[:-1]
sys.stdout, sys.stdin = stdout, stdin
self.assertEqual(out, output)
def test_入力例_1(self):
input = """4
1 -3 1 0"""
output = """4"""
self.assertIO(input, output)
def test_入力例_2(self):
input = """5
3 -6 4 -5 7"""
output = """0"""
self.assertIO(input, output)
def test_入力例_3(self):
input = """6
-1 4 3 2 -5 4"""
output = """8"""
self.assertIO(input, output)
def resolve():
N = int(input())
A = list(map(int, input().split()))
ans = 0
s = A[0]
f = A[0] // abs(A[0])
for i in range(1, N):
a = A[i]
if f == 1 and s+a >= 0:
ans += abs(s+a) + 1
s = -1
elif f == -1 and s+a <= 0:
ans += abs(s+a) + 1
s = 1
else:
s += a
f = s // abs(s)
print(ans)
if __name__ == "__main__":
unittest.main()
# 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 | # coding: utf-8
# Your code here!
n = int(input())
a = list(map(int,input().split()))
def operate(ls,x):
sm = 0
ans = 0
ans += abs(ls[0]-x)
k = x
for i in range(1,len(ls)):
sm += k
if sm > 0:
if abs(ls[i]) <= sm or sm < ls[i]:
ans += abs(ls[i]+sm+1)
k = -sm-1
else:
k = ls[i]
if sm < 0:
if abs(ls[i]) <= abs(sm) or sm > ls[i]:
ans += abs(-sm+1-ls[i])
k = -sm+1
else:
k = ls[i]
return ans
anstot = []
if a[0] > 0:
inv = -1
if a[0] < 0:
inv = 1
if a[0] != 0:
anstot.append(operate(a,a[0]))
anstot.append(operate(a,inv))
if a[0] == 0:
a[0] = 1
anstot.append(1+operate(a,1))
anstot.append(1+operate(a,-1))
print(min(anstot)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> data(N);
int y;
cin >> y;
data.at(0) = y;
for (int i = 1; i < N; i++) {
int x;
cin >> x;
data.at(i) = data.at(i - 1) + x;
}
int sei_ans = 0;
int hu_ans = 0;
int zyoutai = 0;
for (int i = 0; i < N; i++) {
if (i % 2 == 0) {
int a = max(0, 1 - data.at(i) - zyoutai);
zyoutai += a;
sei_ans += a;
} else {
int a = max(0, 1 + data.at(i) + zyoutai);
zyoutai -= a;
sei_ans += a;
}
}
zyoutai = 0;
for (int i = 0; i < N; i++) {
if (i % 2 != 0) {
int a = max(0, 1 - data.at(i) - zyoutai);
zyoutai += a;
hu_ans += a;
} else {
int a = max(0, 1 + data.at(i) + zyoutai);
zyoutai -= a;
hu_ans += a;
}
}
cout << min(sei_ans, hu_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 | using System;
using System.Collections.Generic;
using static Assistant.Input;
using static Assistant.Debug;
using System.Linq;
using Assistant;
namespace ABC059C
{
class Program
{
static void Main(string[] args)
{
var n = RInt;
var a = RInts;
long sum = a[0];
long ans = 0;
for (int i = 1; i < n; i++)
{
if (sum < 0)
{
sum += a[i];
if (sum < 1)
{
ans += 1 - sum;
sum = 1;
}
}
else if (sum > 0)
{
sum += a[i];
if (sum > -1)
{
ans += sum + 1;
sum = -1;
}
}
}
Console.WriteLine(ans);
}
}
}
namespace Assistant
{
static class Input
{
static List<string> line = new List<string>();
static int index = 0;
static String RNext()
{
if (line.Count <= index) line.AddRange(Console.ReadLine().Split());
return line[index++];
}
public static int RInt => int.Parse(RNext());
public static long RLong => long.Parse(RNext());
public static int[] RInts => Console.ReadLine().Split().Select(int.Parse).ToArray();
public static long[] RLongs => Console.ReadLine().Split().Select(long.Parse).ToArray();
public static string RString => RNext();
//以下未テスト
public static int[] RIntsC(int c) => Enumerable.Repeat(0, c).Select(x => int.Parse(RNext())).ToArray();
public static long[] RLongsC(int c) => Enumerable.Repeat(0, c).Select(x => long.Parse(RNext())).ToArray();
public static char[][] RMap(int h) => Enumerable.Repeat(0, h).Select(x => Console.ReadLine().ToCharArray()).ToArray();
}
public struct Mlong
{
long _v;
const long mod = 1000000007;
public Mlong(long n = 0) : this() { _v = n >= mod ? n % mod : n; }
public static implicit operator Mlong(long _x) => new Mlong(_x);
public static implicit operator long(Mlong _x) => _x._v;
public static Mlong operator +(Mlong m1, Mlong m2) { long m = m1._v + m2._v; return m >= mod ? m - mod : m; }
public static Mlong operator -(Mlong m1, Mlong m2) { long m = m1._v - m2._v; return m >= 0 ? m : m + mod; }
public static Mlong operator *(Mlong m1, Mlong m2) => m1._v * m2._v % mod;
public static Mlong operator /(Mlong m1, Mlong m2) => m1._v * ModPow(m2._v, mod - 2) % mod;
public static long ModPow(long a, long n)
{
if (n == 0) return 1;
else if (n % 2 == 1) return a * ModPow(a, n - 1) % mod;
else return ModPow(a * a % mod, n / 2);
}
static Mlong[] fac, finv, inv;
public static void nCkInit(int max)
{
fac = new Mlong[max]; finv = new Mlong[max]; inv = new Mlong[max];
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;
inv[i] = mod - inv[mod % i] * (mod / i);
finv[i] = finv[i - 1] * inv[i];
}
}
public static Mlong nCk(int n, int k)
{
if (n < k) return 0;
if (n < 0 || k < 0) return 0;
return fac[n] * finv[k] * finv[n - k];
}
}
static class Debug
{
static public void Draw2D<T>(T[,] map, int mode = 1)
{
#if DEBUG
int W = map.GetLength(0);
int H = map.GetLength(1);
string[,] map2 = new string[W + 1, H + 1];
for (int i = 0; i < W + 1; i++)
{
for (int j = 0; j < H + 1; j++)
{
if (i == 0 && j == 0) map2[i, j] = 0.ToString();
else if (i == 0) map2[i, j] = (j - 1).ToString();
else if (j == 0) map2[i, j] = (i - 1).ToString();
else map2[i, j] = map[i - 1, j - 1].ToString();
}
}
for (int i = 0; i < W + 1; i++)
{
for (int j = 0; j < H + 1; j++)
{
if (mode == 0) Console.Write(map2[i, j].Last());
if (mode == 1) Console.Write(map2[i, j] + " ");
}
Console.WriteLine();
}
Console.WriteLine();
#endif
}
public static void Draw1D<T>(T[] array, int mode = 0)
{
#if DEBUG
Console.WriteLine(string.Join(" ", array));
#endif
}
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
i = input()
i = i.split()
for item in range(len(i)):
i[item] = int(i[item])
totn = 0
totp = 0
countp = 0
countn = 0
for x in range(len(i)):
totp += i[x]
totn += i[x]
'''
if x == 0:
if totp == 0:
totn = -1
countn = 1
totp = 1
countp = 1
elif totp < 0:
countp = abs(totp) + 1
totp = 1
elif totp > 0:
countn = abs(totn) + 1
totn = -1
'''
if x %2 == 1:
if totn == 0:
countn += 1
totn = 1
elif totn < 0:
countn += abs(totn) + 1
totn = 1
if totp == 0:
countp += 1
totp = -1
elif totp > 0:
countp += abs(totp) + 1
totp = -1
if x %2 == 0:
if totn == 0:
countn += 1
totn = -1
elif totn > 0:
countn += abs(totn) + 1
totn = -1
if totp == 0:
countp += 1
totp = 1
elif totp < 0:
countp += abs(totp) + 1
totp = 1
'''print('totn', totn)
print('countn', countn)
print('totp', totp)
print('countp', countp) '''
count = min(countn, countp)
print(count)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
int sign(long long A) {
if (A > 0)
return 1;
else if (A < 0)
return -1;
else
return 0;
}
int main(void) {
int N;
cin >> N;
long long ans = 0;
long long diff = 0;
long long sss;
vector<long long> arr;
for (int i = 0; i < N; i++) {
cin >> sss;
arr.push_back(sss);
}
vector<int> s(N + 1, 0);
for (int i = 0; i < N; i++) s[i + 1] = s[i] + arr[i];
if (s[1] == 0) {
diff++;
ans++;
}
long long a = 0, b = 0;
for (int i = 1; i <= N - 1; i++) {
a = s[i] + diff;
b = s[i + 1] + diff;
if (sign(a) == sign(b)) {
if (sign(a) == 1) {
diff += (-1 - b);
ans += (1 + b);
} else if (sign(a) == -1) {
diff += (1 - b);
ans += (1 - b);
}
}
if (sign(b) == 0) {
if (sign(a) == 1)
diff--;
else if (sign(a) == -1)
diff++;
ans++;
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
using System.Linq;
namespace A
{
class Program
{
static void Main(string[] args)
{
var n = int.Parse(Console.ReadLine());
var aList = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
var total = 0;
var count = 0;
foreach (var a in aList)
{
var preTotal = total;
total += a;
while (preTotal > 0 && total >= 0)
{
total--;
count++;
}
while (preTotal < 0 && total <= 0)
{
total++;
count++;
}
}
Console.WriteLine(count);
}
static int NumberOfDigits(long x)
{
if (x < 10) return 1;
var result = 0;
while (x > 0)
{
result++;
x /= 10;
}
return result;
}
static int SumOfDigits(int x)
{
var sum = 0;
while (x > 0)
{
sum += x % 10;
x /= 10;
}
return sum;
}
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
cnt=0
for i in range(1,n):
# 条件満たすまでループ
while True:
#print(a[:i])
now_tmp = sum(a[:i])
next_tmp = sum(a[:i+1])
#print(now_tmp, next_tmp)
# 符号が逆転していればOK かつ 現在までの総和が0でない
# 異なる符号を掛けるとマイナスになる
if now_tmp * next_tmp <0 and now_tmp !=0:
break
else:
# 現在の合計がマイナスの場合
if sum(a[:i]) < 0 :
a[i] +=1
cnt +=1
# 現在の合計がプラスの場合
elif sum(a[:i]) > 0 :
a[i] -=1
cnt +=1
# 現在の合計が0の場合
elif sum(a[:i]) == 0 :
a[i] +=1
cnt +=1
print(cnt) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
using System.Collections.Generic;
using System.Linq;
class MainClass
{
static void Main(string[] args)
{
{
int n = int.Parse(Console.ReadLine());
int[] arr = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
int[] arrSum = new int[n];
int sum_Plus = 0;
int sum_minus = 0;
bool isChange = false;
bool isChange_minus = false;
int nPreSum_Plus = 0;
int nPreSum_minus = 0;
for (int i = 0; i < n; i++)
{
if (i == 0)
{
arrSum[0] = arr[0];
nPreSum_minus = arr[0];
nPreSum_Plus = arr[0];
}
else
{
arrSum[i] = (arrSum[i - 1] + arr[i]);
}
#region //plusStart
if (i % 2 == 0)
{
if (!isChange)
{
//一回も変更していない
if (arrSum[i] > 0)
{
//isChange = false;
}
else if (arrSum[i] < 0)
{
int tmpcnt = (1 - arrSum[i]);
sum_Plus += tmpcnt;
nPreSum_Plus = 1;
isChange = true;
}
else
{
sum_Plus += 1;
nPreSum_Plus = 1;
isChange = true;
}
}
else
{
//一回でも変更したとき arrSum[i-1]=-1
int tmpsum = (nPreSum_Plus) + arr[i];
if (0<tmpsum)
{
//isChange = false;
nPreSum_Plus = tmpsum;
}
else if (tmpsum < 0)
{
int tmpcnt = Math.Abs(1 - (nPreSum_Plus));
sum_Plus += tmpcnt;
nPreSum_Plus = nPreSum_Plus + arr[i];
}
else
{
sum_Plus += 1;
nPreSum_Plus = 1;
}
}
}
else
{
//奇数インデックス⇒マイナスにする
if (!isChange)
{
//一回も変更していない
if (arrSum[i] < 0)
{
//isChange = false;
}
else if (arrSum[i] > 0)
{
int tmpcnt = (arrSum[i] - (-1));
sum_Plus += tmpcnt;
nPreSum_Plus = -1;
isChange = true;
}
else
{
sum_Plus += 1;
nPreSum_Plus = -1;
isChange = true;
}
}
else
{
//一回でも変更したとき⇒arrSum[i-1]=1;
int tmpSum = nPreSum_Plus + arr[i];
if (tmpSum < 0)
{
//isChange = false;
nPreSum_Plus= tmpSum;
}
else if (0<tmpSum)
{
int tmpcnt = (Math.Abs((-1)-nPreSum_Plus));
sum_Plus += tmpcnt;
nPreSum_Plus = nPreSum_Plus + arr[i];
}
else
{
sum_Plus += 1;
nPreSum_Plus = -1;
}
}
}
#endregion
#region //minusStart
if (i % 2 == 1)
{
if (!isChange_minus)
{
//一回も変更していない
if (arrSum[i] > 0)
{
//isChange_minus = false;
}
else if (arrSum[i] < 0)
{
int tmpcnt = (1 - arrSum[i]);
sum_minus += tmpcnt;
nPreSum_minus = 1;
isChange_minus = true;
}
else
{
sum_minus += 1;
nPreSum_minus = 1;
isChange_minus = true;
}
}
else
{
//一回でも変更したとき arrSum[i-1]=-1
int tmpsum = (nPreSum_minus) + arr[i];
if (0 < tmpsum)
{
//isChange_minus = false;
nPreSum_minus= tmpsum;
}
else if (tmpsum < 0)
{
int tmpcnt = Math.Abs(1 - (nPreSum_minus));
sum_minus += tmpcnt;
nPreSum_minus = nPreSum_minus + arr[i];
}
else
{
sum_minus += 1;
nPreSum_minus = 1;
}
}
}
else
{
//奇数インデックス⇒マイナスにする
if (!isChange_minus)
{
//一回も変更していない
if (arrSum[i] < 0)
{
//isChange_minus = false;
}
else if (arrSum[i] > 0)
{
int tmpcnt = (arrSum[i] - (-1));
sum_minus += tmpcnt;
nPreSum_minus = -1;
isChange_minus = true;
}
else
{
sum_minus += 1;
nPreSum_minus = -1;
isChange_minus = true;
}
}
else
{
//一回でも変更したとき⇒arrSum[i-1]=1;
int tmpSum = nPreSum_minus + arr[i];
if (tmpSum < 0)
{
//isChange_minus = false;
nPreSum_minus = tmpSum;
}
else if (0 < tmpSum)
{
int tmpcnt = (Math.Abs((-1) - nPreSum_minus));
sum_minus += tmpcnt;
nPreSum_minus = nPreSum_minus + arr[i];
}
else
{
sum_Plus += 1;
nPreSum_minus = -1;
}
}
}
#endregion
}
var ans = sum_minus <= sum_Plus ? sum_minus : sum_Plus;
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 | python3 |
N = int(input())
A = list(map(int, input().split()))
def solve(isPosi,N,A):
ans = 0
sm = A[0]
for a in A[1:]:
if isPosi:
sm += a
if sm >= 0:
ans += abs(-1-sm)
sm = -1
isPosi = False
else:
sm += a
if sm <= 0:
ans += abs(1-sm)
sm = 1
isPosi = True
return ans
ans_p = solve(True,N,A)
ans_n = solve(False,N,A)
print(min(ans_p,ans_n)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import sys
sys.setrecursionlimit(2000000)
def re(r,k,l):
if k==0:return r
while(k<l[0]):
l=l[1:]
return re(r+1,k-l[0],l)
n=int(input())
i=1
l6=[]
ans=10**5
while(i<n):
l6.append(i)
i*=6
i=1
l9=[]
while(i<n):
l9.append(i)
i*=9
l6.sort(reverse=True)
l9.sort(reverse=True)
for i in range(n+1):
ans=min(ans,re(0,n-i,l6)+re(0,i,l9))
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 | #include <bits/stdc++.h>
int main() {
int n;
scanf("%d", &n);
long long a[n];
for (int i = 0; i < n; i++) {
scanf("%lld", &a[i]);
}
long long mans = 0, pans = 0;
long long msub[n], psub[n];
if (a[0] == 0) {
pans = 1;
mans = 1;
psub[0] = 1;
msub[0] = -1;
}
if (a[0] > 0) {
psub[0] = a[0];
msub[0] = -1;
mans = a[0] + 1;
} else {
psub[0] = 1;
msub[0] = a[0];
pans = 1;
}
for (int i = 1; i < n; i++) {
if (i % 2 == 1 && psub[i - 1] + a[i] >= 0) {
pans += psub[i - 1] + a[i] + 1;
psub[i] = -1;
} else if (i % 2 == 0 && psub[i - 1] + a[i] <= 0) {
pans += 1 - (psub[i - 1] + a[i]);
psub[i] = 1;
} else {
psub[i] = psub[i - 1] + a[i];
}
if (i % 2 == 1 && msub[i - 1] + a[i] <= 0) {
mans += 1 - (msub[i - 1] + a[i]);
msub[i] = 1;
} else if (i % 2 == 0 && msub[i - 1] + a[i] >= 0) {
mans += msub[i - 1] + a[i] + 1;
msub[i] = -1;
} else {
msub[i] = msub[i - 1] + a[i];
}
}
printf("%lld", mans < pans ? mans : pans);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int d[n];
for (int i = 0; i < n; i++) {
cin >> d[i];
}
int count = 0;
int sum = d[0];
int f = 0;
if (d[0] > 0) {
f = -1;
}
if (d[0] < 0) {
f = 1;
}
for (int i = 1; i < n; i++) {
sum += d[i];
if (sum == 0) {
if (f == 1) {
count++;
f = -1;
sum = 1;
continue;
}
if (f == -1) {
count++;
f = 1;
sum = -1;
continue;
}
}
if (sum > 0) {
if (f == 1) {
f = -1;
continue;
}
if (f == -1) {
count += sum + 1;
sum = -1;
f = 1;
continue;
}
}
if (sum < 0) {
if (f == -1) {
f = 1;
continue;
}
if (f == 1) {
count += 1 - sum;
sum = 1;
f = -1;
continue;
}
}
}
int ccount = 0;
int ssum;
int ff = 0;
if (d[0] > 0) {
ff = 1;
ccount = -1 - d[0];
ssum = -1;
}
if (d[0] < 0) {
ff = -1;
ccount = 1 - d[0];
ssum = 1;
}
for (int i = 1; i < n; i++) {
sum += d[i];
if (ssum == 0) {
if (ff == 1) {
ccount++;
ff = -1;
ssum = 1;
continue;
}
if (ff == -1) {
ccount++;
ff = 1;
ssum = -1;
continue;
}
}
if (ssum > 0) {
if (f == 1) {
ff = -1;
continue;
}
if (ff == -1) {
ccount += sum + 1;
ssum = -1;
ff = 1;
continue;
}
}
if (ssum < 0) {
if (ff == -1) {
ff = 1;
continue;
}
if (ff == 1) {
ccount += 1 - sum;
ssum = 1;
ff = -1;
continue;
}
}
}
cout << min(count, ccount) << 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 signed wait() { return 0; }
inline void dout(const char *arg, ...) {}
template <typename T>
inline void SWAP(T &a, T &b) {
T t = a;
a = b;
b = t;
}
inline void CSWAP(char *&a, char *&b) {
char *t = a;
a = b;
b = t;
}
void CombSort(int N, int *ar, int order_ascending = 1) {
if (N <= 1) return;
int h = int(N / 1.3);
int flag;
int i;
while (true) {
flag = 0;
for (i = 0; i + h < N; ++i) {
if ((order_ascending && ar[i] > ar[i + h]) ||
(!order_ascending && ar[i] < ar[i + h])) {
swap<int>(ar[i], ar[i + h]);
flag = 1;
}
}
if (h == 1 && !flag) break;
if (h == 9 || h == 10) h = 11;
if (h > 1) h = int(h / 1.3);
}
}
void CombSort(int N, long long int *ar, int order_ascending = 1) {
if (N <= 1) return;
int h = int(N / 1.3);
int flag;
int i;
while (true) {
flag = 0;
for (i = 0; i + h < N; ++i) {
if ((order_ascending && ar[i] > ar[i + h]) ||
(!order_ascending && ar[i] < ar[i + h])) {
swap<long long int>(ar[i], ar[i + h]);
flag = 1;
}
}
if (h == 1 && !flag) break;
if (h == 9 || h == 10) h = 11;
if (h > 1) h = int(h / 1.3);
}
}
int EuclideanAlgorithm(int N, int *ar) {
for (int n = 0; n < N - 1; ++n) {
while (true) {
if (ar[n] % ar[n + 1] == 0 || ar[n + 1] % ar[n] == 0) {
ar[n + 1] = ar[n] < ar[n + 1] ? ar[n] : ar[n + 1];
break;
}
if (ar[n] > ar[n + 1]) {
ar[n] %= ar[n + 1];
} else {
ar[n + 1] %= ar[n];
}
}
}
return ar[N - 1];
}
template <typename T>
void CombSort(int N, T *ar, int order_ascending = 1) {
if (N <= 1) return;
int i, flag;
int h = int(N / 1.3);
while (true) {
flag = 0;
for (i = 0; i + h < N; ++i) {
if (order_ascending && ar[i].SortValue > ar[i + h].SortValue ||
!order_ascending && ar[i].SortValue < ar[i + h].SortValue) {
swap<T>(ar[i], ar[i + h]);
flag = 1;
}
}
if (h > 1) {
h = int(h / 1.3);
if (h == 9 || h == 10) h = 11;
} else {
if (!flag) break;
}
}
}
struct UnionFind {
vector<int> par;
UnionFind(int N) : par(N) {
for (int i = 0; i < N; i++) par[i] = i;
}
int root(int x) {
if (par[x] == x) return x;
return par[x] = root(par[x]);
}
void unite(int x, int y) {
int rx = root(x);
int ry = root(y);
if (rx == ry) return;
par[rx] = ry;
}
bool same(int x, int y) {
int rx = root(x);
int ry = root(y);
return rx == ry;
}
};
void Replace(char *c, int len, char before, char after) {
for (int i = 0; i < len; ++i) {
if (c[i] == before) c[i] = after;
}
}
void Replace(char *c, char before, char after) {
int len = strlen(c);
Replace(c, len, before, after);
}
class csNode {
public:
csNode() {}
};
class csStack {
public:
csStack() { num = 0; }
void alloc(int size) { param = new int[size]; }
void sort(int order = 1) {
if (num > 1) CombSort(num, param, order);
}
int num;
int *param;
void push(int p) { param[num++] = p; }
};
class csPosition {
public:
csPosition() { x = y = 0; }
int x, y;
};
template <typename T>
class csPos {
public:
csPos() { x = y = 0; }
T x, y;
};
char s[200010], s2[1000];
signed main() {
long long int n;
scanf("%lld", &n);
long long int cnt = 0;
long long int i;
long long int a, b, sum = 0;
scanf("%lld", &b);
sum = b;
long long int flag;
b < 0 ? flag = -1 : flag = 1;
for (i = 1; i < n; ++i) {
scanf("%lld", &a);
sum += a;
if (flag == -1 && sum < 0) {
cnt += 1 - sum;
sum = 1;
} else if (flag == 1 && sum > 0) {
cnt += sum + 1;
sum = -1;
} else if (sum == 0) {
cnt++;
if (flag == 1) {
sum = -1;
} else {
sum = 1;
}
}
flag *= -1;
}
cout << cnt << endl;
;
return wait();
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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
n = int(input())
a = list(map(int,input().split()))
sum_odd = sum(a[1::2])
sum_eve = sum(a[::2])
ans = 0
sum_a = 0
for i in range(n):
sum_a = sum_a + a[i]
if sum_a *(-1)**(i+1) < 1:
kari = 1-sum_a *(-1)**(i+1)
a[i] += 1*(-1)**(i+1) *(kari)
sum_a += 1*(-1)**(i+1) *(kari)
ans = ans + abs(kari)
ans1 = 0
sum_a = 0
for i in range(n):
sum_a = sum_a + a[i]
if sum_a *(-1)**(i) < 1:
kari = (1-sum_a *(-1)**(i))
a[i] += 1*(-1)**(i) * kari
sum_a += 1*(-1)**(i) * kari
ans1 = ans1 + abs(kari)
print(min(ans,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 main() {
int n;
cin >> n;
vector<long long> a(n);
for (long long &x : a) cin >> x;
long long ans = 0;
if (a[0] == 0) {
a[0] = 1;
ans++;
}
long long sum = a[0];
if (a[0] > 0) {
for (int i = 1; i < n; i++) {
sum += a[i];
if (i % 2 == 1) {
while (sum >= 0) {
sum--;
ans++;
}
}
if (i % 2 == 0) {
while (sum <= 0) {
sum++;
ans++;
}
}
}
}
if (a[0] < 0) {
for (int i = 1; i < n; i++) {
sum += a[i];
if (i % 2 == 1) {
while (sum <= 0) {
sum++;
ans++;
}
}
if (i % 2 == 0) {
while (sum >= 0) {
sum--;
ans++;
}
}
}
}
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;
int64_t min(int64_t a, int64_t b) {
if (a > b) {
return b;
} else {
return a;
}
}
int64_t solve(vector<int> a, bool next) {
bool nextposi = next;
int64_t ans = 0;
int64_t sum = 0;
for (int i = 0; i < a.size(); i++) {
sum += a.at(i);
if (nextposi != (sum > 0)) {
if (nextposi == 1) {
ans += abs(sum - 1);
sum = 1;
} else {
ans += abs(sum + 1);
sum = -1;
}
}
nextposi = !nextposi;
}
return ans;
}
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a.at(i);
}
int64_t ans = 0;
{ ans = min(solve(a, 0), solve(a, 1)); }
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
a = list(map(int,input().split()))
presum = a[0]
summ = 0
ans = 0
if a[0] == 0:
if a[1] - a[0] > 0:
ans += a[1] + 1
a[0] = -a[1] - 1
else:
ans += 1 - a[1]
a[0] = 1 - a[1]
for i in range(1,N):
nsum = a[i] + presum
if presum * nsum >= 0:
if nsum == 0:
if presum >= 0:
a[i] -= 1
ans += 1
nsum = -1
elif presum < 0:
a[i] += 1
ans += 1
nsum = 1
elif nsum > 0:
ans += 1+presum+a[i]
a[i] = -1 - presum
nsum = -1
elif nsum < 0:
nsum = 1
ans += 1 - presum - a[i]
a[i] = 1-presum
presum = nsum
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;
long long a[100005], sum1 = 0, sum2 = 0, cnt1 = 0, cnt2 = 0;
bool f1 = 0, f2 = 1;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
if (f1 == 0) {
if (sum1 + a[i] <= 0) {
cnt1 += 1 - sum1 - a[i];
sum1 = 1;
} else {
sum1 += a[i];
}
f1 = 1;
} else {
if (sum1 + a[i] >= 0) {
cnt1 += a[i] - (-1 - sum1);
sum1 = -1;
} else {
sum1 += a[i];
}
f1 = 0;
}
if (f2 == 0) {
if (sum2 + a[i] <= 0) {
cnt2 += 1 - sum2 - a[i];
sum2 = 1;
} else {
sum2 += a[i];
}
f2 = 1;
} else {
if (sum2 + a[i] >= 0) {
cnt2 += a[i] - (-1 - sum2);
sum2 = -1;
} else {
sum1 += a[i];
}
f2 = 0;
}
}
printf("%lld", min(cnt1, cnt2));
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | input()
seq = list(map(int, input().rstrip("\n").split()))
iter_ = iter(seq)
ans1 = 0
for i, num in enumerate(iter_):
if i == 0:
sum_ = num
if sum_ <= 0:
ans1 += abs(sum_ - 1)
sum_ = 1
else:
sum_ += num
if i % 2 and sum_ >= 0:
ans1 += abs(sum_ + 1)
sum_ = -1
elif sum_ <= 0:
ans1 += abs(sum_ - 1)
sum_ = 1
iter_ = iter(seq)
ans2 = 0
for i, num in enumerate(iter_):
if i == 0:
sum_ = num
if sum_ >= 0:
ans2 += abs(sum_ + 1)
sum_ = -1
else:
sum_ += num
if i % 2 and sum_ <= 0:
ans2 += abs(sum_ - 1)
sum_ = 1
elif sum_ >= 0:
ans2 += abs(sum_ + 1)
sum_ = -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 | python3 | n = int(input())
a = list(map(int, input().split()))
a1 = [a[0]] * n
b = a[0]
ans = 0
def f(x):
if x == 0:
return 0
else:
return x // abs(x)
for i in range(1, n):
if a1[i - 1] * a[i] >= 0:
a1[i] = -a[i]
else:
a1[i] = a[i]
if b * (b + a1[i]) >= 0:
a1[i] = -f(a1[i - 1]) - b
if b + a1[i] == 0:
a1[i] += f(a1[i])
ans += abs(a1[i] - a[i])
b += a1[i]
a2 = [0] * n
ans1 = abs(-f(a2[0]) - a2[0])
a2[0] = -f(a2[0])
b1 = a2[0]
for i in range(1, n):
if a2[i - 1] * a[i] >= 0:
a2[i] = -a[i]
else:
a2[i] = a[i]
if b * (b + a2[i]) >= 0:
a2[i] = -f(a2[i - 1]) - b1
if b1 + a2[i] == 0:
a2[i] += f(a2[i])
ans1 += abs(a2[i] - a[i])
b1 += a2[i]
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 | java | import java.util.Scanner;
public class Main {
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
int count = 0;
int l[] = new int[scanner.nextInt()];
int x[] = new int[l.length];
int y[] = new int[l.length];
for (int i = 0;i < l.length;++i){
l[i] = Integer.valueOf(scanner.next());
y[i] = Integer.valueOf(l[i]);
if(i > 0){
x[i] = l[i] + x[i - 1];
}
else{
x[i] = l[i];
}
}
boolean flag = true;
while (true){
for (int i = 1;i < l.length;++i){
int p = x[i - 1];
int q = x[i];
if(q == 0||(q < 0&&p < 0)||(q > 0&&p > 0)){
flag = false;
int d = (p < 0&&q <= 0) ? 1 : -1;
l[i] += d;
for (int j = i;j < l.length;++j){
x[j] += d;
}
}
}
if(flag){
break;
}
flag = true;
}
for (int i = 1;i < l.length;++i){
System.out.println(l[i]);
count += Math.abs(l[i] - y[i]);
}
System.out.println(count);
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const 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)(1); 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)(1); 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;
#define uint unsigned int
#define llong long long int
#define ullong unsigned long long int
#define rep(i, n) for (int i = 0; i < n; ++i)
int main (int argc, char *argv[]) {
cin.tie(0);
ios::sync_with_stdio(false);
int n, a[100001];
cin >> n;
rep(i, n) {
cin >> a[i];
}
llong res_even = 0, sum_a = 0;
rep(i, n) {
sum_a += a[i];
if (i % 2 == 0 && sum_a <= 0) {
res_even += abs(sum_a) + 1;
sum_a = 1;
}
if (i % 2 == 1 && sum_a >= 0) {
res_even += sum_a + 1;
sum_a = -1;s
}
}
llong res_odd = 0;
sum_a = 0;
rep(i, n) {
sum_a += a[i];
if (i % 2 == 0 && sum_a >= 0) {
res_odd += sum_a + 1;
sum_a = -1;
}
if (i % 2 == 1 && sum_a <= 0) {
res_odd += abs(sum_a) + 1;
sum_a += 1;
}
}
cout << min(res_even, res_odd) << endl;
return 0;
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e18;
signed main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
long long n, ans1 = 0, ans2 = 0, sum1 = 0, sum2 = 0;
cin >> n;
vector<long long> a(n);
for (long long i = 0; i < n; i++) {
cin >> a[i];
}
sum1 = a[0];
if (a[0] == 0) {
sum1 = (a[1] > 0 ? -1 : 1);
ans1++;
}
for (long long i = 1; i < n; i++) {
if (sum1 + a[i] == 0) {
ans1++;
sum1 = (a[i] > 0 ? 1 : -1);
} else if (sum1 > 0 && sum1 + a[i] < 0) {
sum1 += a[i];
} else if (sum1 < 0 && sum1 + a[i] > 0) {
sum1 += a[i];
} else if (sum1 > 0 && a[i] + sum1 > 0) {
ans1 += sum1 + a[i] + 1;
sum1 = -1;
} else {
ans1 += -a[i] - sum1 + 1;
sum1 = 1;
}
}
a[0] *= -1;
if (a[0] == 0) {
sum2 = (a[1] > 0 ? -1 : 1);
ans2++;
}
for (long long i = 1; i < n; i++) {
if (sum2 + a[i] == 0) {
ans2++;
sum2 = (a[i] > 0 ? 1 : -1);
} else if (sum2 > 0 && sum2 + a[i] < 0) {
sum2 += a[i];
} else if (sum2 < 0 && sum2 + a[i] > 0) {
sum2 += a[i];
} else if (sum2 > 0 && a[i] + sum2 > 0) {
ans2 += sum2 + a[i] + 1;
sum2 = -1;
} else {
ans2 += -a[i] - sum2 + 1;
sum2 = 1;
}
}
cout << min(ans1, ans2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
template <class T>
inline T chmax(T& a, const T b) {
return a = (a < b) ? b : a;
}
template <class T>
inline T chmin(T& a, const T b) {
return a = (a > b) ? b : a;
}
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const ll MOD = 1000000007;
const ll INF = 1e18;
const double PI = acos(-1);
using namespace std;
ll N;
ll solve(vector<ll> A) {
ll ret = 0;
ll sum = 0;
for (ll i = 0; i < ll(N - 1); ++i) A[i + 1] += A[i];
for (ll i = (1); i < (N); ++i) {
A[i] += sum;
if (A[i] * A[i - 1] >= 0) {
if (A[i] == 0) {
if (A[i - 1] < 0) {
sum++;
ret++;
A[i] += 1;
} else {
sum--;
ret++;
A[i] -= 1;
}
} else if (A[i] > 0) {
sum -= A[i] + 1;
ret += A[i] + 1;
A[i] = -1;
} else {
sum += -A[i] + 1;
ret += -A[i] + 1;
A[i] = 1;
}
}
}
return ret;
}
signed main() {
cin >> N;
vector<ll> A(N);
for (ll i = 0; i < ll(N); ++i) cin >> A[i];
ll ans = 0;
if (A[0] != 0) {
ans = solve(A);
ll ret = abs(A[0]) + 1;
if (A[0] > 0) {
A[0] = -1;
} else {
A[0] = 1;
}
chmax(ans, solve(A) + ret);
} else {
A[0] = 1;
ans = solve(A) + 1;
A[0] = -1;
chmin(ans, solve(A) + 1);
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | // AtCoder-Template.cpp : このファイルには 'main' 関数が含まれています。プログラム実行の開始と終了がそこで行われます。
//
#include <iostream>
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
using namespace std;
using ll = long long;
#define fst first
#define snd second
#define FOR(i,N) for(auto i=0; i<N; ++i)
#define FORREV(i,N,_cnt) for(auto i=N-1,cnt=_cnt; cnt > 0; --i, --cnt)
#define ALL(x) x.begin(), x.end()
/* clang-format off */
template <class T, size_t D> struct _vec { using type = vector<typename _vec<T, D - 1>::type>; };
template <class T> struct _vec<T, 0> { using type = T; };
template <class T, size_t D> using vec = typename _vec<T, D>::type;
template <class T> vector<T> make_v(size_t size, const T& init) { return vector<T>(size, init); }
template <class... Ts> auto make_v(size_t size, Ts... rest) { return vector<decltype(make_v(rest...))>(size, make_v(rest...)); }
template <class T> inline void chmin(T& a, const T& b) { if (b < a) a = b; }
template <class T> inline void chmax(T& a, const T& b) { if (b > a) a = b; }
/* clang-format on */
int main() {
#ifdef DEBUG
ifstream ifs("in.txt");
cin.rdbuf(ifs.rdbuf());
#endif
ll N; cin >> N; vector<ll> A(N); FOR(i, N) cin >> A[i];
// RUISEKI
vector<ll> Rui(N); Rui[0] = A[0];
ll ans = 0;
if (Rui[0] == 0) {
ans += 1;
Rui[0] = 1;
}
FOR(i, N - 1) {
Rui[i + 1] += Rui[i] + A[i + 1];
if (i + 1 >= 1 and Rui[i+1] * Rui[i] >= 0) {
if (Rui[i + 1] * Rui[i] == 0 and Rui[i] > 0) {
ans += 1; Rui[i + 1] = -1;
}
else if (Rui[i + 1] * Rui[i] == 0 and Rui[i] < 0) {
ans += 1; Rui[i + 1] = 1;
}
else { // 両方とも同じ符号になった場合
if (Rui[i + 1] > 0) {
ans += Rui[i + 1] + 1;
Rui[i + 1] = -1;
}
else {
ans += - Rui[i + 1] + 1;
Rui[i + 1] = 1;
}
}
}
}
///////////////////////////////////////////////
///////// ///////////////
///////////////////////////////////////////////
ll tmp = 0;
if (Rui[0] == 0) {
tmp += 1;
Rui[0] = -1;
}
FOR(i, N - 1) {
Rui[i + 1] += Rui[i] + A[i + 1];
if (i + 1 >= 1 and Rui[i + 1] * Rui[i] >= 0) {
if (Rui[i + 1] * Rui[i] == 0 and Rui[i] > 0) {
tmp += 1; Rui[i + 1] = -1;
}
else if (Rui[i + 1] * Rui[i] == 0 and Rui[i] < 0) {
tmp += 1; Rui[i + 1] = 1;
}
else { // 両方とも同じ符号になった場合
if (Rui[i + 1] > 0) {
tmp += Rui[i + 1] + 1;
Rui[i + 1] = -1;
}
else {
tmp += -Rui[i + 1] + 1;
Rui[i + 1] = 1;
}
}
}
}
chmin(ans, tmp);
cout << ans << endl;
//cout << 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 | python3 | def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))
import numpy
n = ii()
A = iil()
cum = numpy.cumsum(A)
#print(cum)
#print(type(cum))
now = -1*cum[0]
ans = 0
ope = 0
for i,item in enumerate(cum):
num = item+ope
if num == 0:
ans += 1
ope += 1 if now < 0 else -1
num += 1 if now < 0 else -1
elif num*now > 0:
ans += abs(num)+1
ope -= (abs(num)+1)*num//abs(num)
num -= (abs(num)+1)*num//abs(num)
# print(ans,ope,num)
now = num
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;
cin >> n;
long long a[10002] = {};
long long b[10002] = {};
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
b[i] = a[i];
}
long long eve = 0, sum = 0;
for (int j = 0; j < n; j++) {
if (j % 2 == 0 && sum + a[j] <= 0) {
eve += abs(a[j] + sum) + 1;
a[j] = abs(sum) + 1;
}
if (j % 2 == 1 && sum + a[j] >= 0) {
eve += a[j] + sum + 1;
a[j] = -abs(sum) - 1;
}
sum += a[j];
}
sum = 0;
long long odd = 0;
for (int k = 0; k < n; k++) {
if (k % 2 == 0 && sum + b[k] >= 0) {
odd += abs(b[k] + sum) + 1;
b[k] = -abs(sum) - 1;
}
if (k % 2 == 1 && sum + b[k] <= 0) {
odd += abs(sum + b[k]) + 1;
b[k] = abs(sum) + 1;
}
sum += b[k];
}
cout << min(odd, eve) << 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 | require 'prime'
include Math
def max(a,b); a > b ? a : b end
def min(a,b); a < b ? a : b end
def swap(a,b); a, b = b, a end
def gif; gets.to_i end
def gff; gets.to_f end
def gsf; gets.chomp end
def gi; gets.split.map(&:to_i) end
def gf; gets.split.map(&:to_f) end
def gs; gets.chomp.split.map(&:to_s) end
def gc; gets.chomp.split('') end
def pr(num); num.prime_division end
def digit(num); num.to_s.length end
def array(s,ini=nil); Array.new(s){ini} end
def darray(s1,s2,ini=nil); Array.new(s1){Array.new(s2){ini}} end
def rep(num); num.times{|i|yield(i)} end
def repl(st,en,n=1); st.step(en,n){|i|yield(i)} end
def f(sum,a, count)
repl 1,a.size-1 do |i|
sum << a[i]+sum[i-1]
if sum[i-1] > 0
if sum[i] >= 0
count += sum[i]+1
sum[i] = -1
end
elsif sum[i-1] < 0
if sum[i] <= 0
count += 1-sum[i]
sum[i] = 1
end
end
end
return count
end
n = gif
a = gi
sum1 = []
sum2 = []
sum = []
ans1 = nil
ans2 = nil
ans3 = nil
if a[0] != 0
sum << a[0]
ans1 = f sum,a, 0
else
sum1 << 1
ans2 = f sum1,a,1
sum2 << -1
ans3 = f sum2,a,1
end
if ans1
puts ans1
else
puts min ans2,ans3
end |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | 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 long 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 long[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 += 1-sum;
sum = 1;
}
} else {
// a[i]までの合計を負にするとき
if(0<=sum) {
count += 1+sum;
sum = -1;
}
}
}
return count;
}
public void solve() {
output = Math.min(count(true), 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 | cpp | #include <bits/stdc++.h>
#include <cassert>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define ok() puts(ok?"Yes":"No");
#define chmax(x,y) x = max(x,y)
#define chmin(x,y) x = min(x,y)
using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using ii = pair<int, int>;
using vvi = vector<vi>;
using vii = vector<ii>;
using gt = greater<int>;
using minq = priority_queue<int, vector<int>, gt>;
using P = pair<ll,ll>;
const ll LINF = 1e18L + 1;
const int INF = 1e9 + 1;
//clang++ -std=c++11 -stdlib=libc++
int main() {
int n; cin >> n;
int a(n);
rep(i,n) cin>>a[i];
ll ans = LINF;
// even is positive
// odd is nevgative
ll sum=0;
ll num=0;
rep(i,n) {
sum+=a[i];
if (i&1) {
if (sum>=0) {
num += (sum + 1);
sum = -1;
}
} else {
if (sum <= 0) {
num += (-sum + 1);
sum = 1;
}
}
}
chmin(ans, num);
// even is negative
// odd is positive
num = 0;
sum = 0;
rep(i,n) {
sum+=a[i];
if (i&1) {
if (sum <= 0) {
num += (-sum + 1);
sum = 1;
}
} else {
if (sum>=0) {
num += (sum + 1);
sum = -1;
}
}
}
chmin(ans, num);
printf("%lld\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;
int main() {
int n;
cin >> n;
vector<int> a(n);
vector<int64_t> s(n);
for (int i = 0; i < n; i++) cin >> a[i];
s[0] = max(a[0], 1);
int retval_p = (a[0] <= 0) ? abs(a[0]) + 1 : 0;
for (int i = 1; i < n; i++) {
s[i] = a[i] + s[i - 1];
if (i % 2 == 1 && s[i] >= 0) {
retval_p += abs(s[i]) + 1;
s[i] = -1;
} else if (i % 2 == 0 && s[i] <= 0) {
retval_p += abs(s[i]) + 1;
s[i] = 1;
}
}
s[0] = min(a[0], -1);
int retval_m = (a[0] >= 0) ? abs(a[0]) + 1 : 0;
for (int i = 1; i < n; i++) {
s[i] = a[i] + s[i - 1];
if (i % 2 == 1 && s[i] <= 0) {
retval_m += abs(s[i]) + 1;
s[i] = 1;
} else if (i % 2 == 0 && s[i] >= 0) {
retval_m += abs(s[i]) + 1;
s[i] = -1;
}
}
cout << min(retval_p, retval_m) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.function.IntFunction;
import java.util.function.Supplier;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner();
int n=scanner.nextInt();
long[] a=new long[n+1];
for(int i=1;i<=n;i++){
a[i]=scanner.nextInt();
}
Arrays.parallelPrefix(a,(c,b)->c+b);
//put(Arrays.toString(a));
long ans=0;
long ruiseki=0;
for(int i=1;i<=n;i++){
//put(format("i=%d",i));
//put(format("ruiseki=%d",ruiseki));
long val=a[i]+ruiseki;
long val_=a[i-1]+ruiseki;
//put(format("val=%d",val));
//put(format("val_=%d",val_));
if(val==0){
int bit=a[i-1]/Math.abs(a[i-1]);
ruiseki+=bit*1;
ans+=Math.abs(bit);
}else if(val>0&&val_>0){
ruiseki-=(val+1);
ans+=Math.abs(val+1);
}else if(val<0&&val_<0){
ruiseki+=Math.abs(val)+1;
ans+=Math.abs(val)+1;
}
//put(ans);
//put();
}
put(ans);
}
public static void print(Object object){
System.out.print(object);
}
public static void put(Object object) {
System.out.println(object);
}
public static void put(){
System.out.println();
}
public static String format(String string, Object... args) {
return String.format(string, args);
}
}
final class Scanner {
private final InputStream in = System.in;
private final byte[] buffer = new byte[1024];
private int ptr = 0;
private int buflen = 0;
private boolean hasNextByte() {
if (ptr < buflen) {
return true;
} else {
ptr = 0;
try {
buflen = in.read(buffer);
} catch (IOException e) {
e.printStackTrace();
}
if (buflen <= 0) {
return false;
}
}
return true;
}
private int readByte() {
if (hasNextByte())
return buffer[ptr++];
else
return -1;
}
private boolean isPrintableChar(int c) {
return 33 <= c && c <= 126;
}
public boolean hasNext() {
while (hasNextByte() && !isPrintableChar(buffer[ptr]))
ptr++;
return hasNextByte();
}
public String next() {
if (!hasNext())
throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while (isPrintableChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public long nextLong() {
if (!hasNext())
throw new NoSuchElementException();
long n = 0;
boolean minus = false;
int b = readByte();
if (b == '-') {
minus = true;
b = readByte();
}
if (b < '0' || '9' < b) {
throw new NumberFormatException();
}
while (true) {
if ('0' <= b && b <= '9') {
n *= 10;
n += b - '0';
} else if (b == -1 || !isPrintableChar(b)) {
return minus ? -n : n;
} else {
throw new NumberFormatException();
}
b = readByte();
}
}
public int nextInt() {
long nl = nextLong();
if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE)
throw new NumberFormatException();
return (int) nl;
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
final class Pair {
final public int x, y;
Pair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int hashCode() {
return x+y;
}
@Override
public boolean equals(Object obj) {
boolean result=super.equals(obj);
if(obj.getClass()!=this.getClass()){
return false;
}
Pair pair=(Pair)obj;
if(this.x==pair.x&&this.y==pair.y) return true;
return false;
}
@Override
public String toString() {
return String.format("(%d,%d)", x, y);
}
}
final class Tuple<T,V>{
//immutabl1でないことに注意(T,Vがmutableの場合変更可能)
final public T t;
final public V v;
Tuple(T t,V v){
this.t=t;
this.v=v;
}
@Override
public int hashCode() {
return (t.hashCode()+v.hashCode());
}
@Override
public boolean equals(Object obj) {
if(obj.getClass()!=this.getClass()){
return false;
}
Tuple<T,V> tuple=(Tuple)obj;
return tuple.t.equals(this.t)&&tuple.v.equals(this.v);
}
@Override
public String toString() {
return String.format("<Tuple>=<%s,%s>",t,v);
}
}
final class LowerBoundComparator<T extends Comparable<? super T>>
implements Comparator<T>
{
public int compare(T x, T y)
{
return (x.compareTo(y) >= 0) ? 1 : -1;
}
}
final class UpperBoundComparator<T extends Comparable<? super T>>
implements Comparator<T>
{
public int compare(T x, T y)
{
return (x.compareTo(y) > 0) ? 1 : -1;
}
}
final class Util {
static long gcd(long a,long b){
if(a%b==0)return b;
return gcd(b,a%b);
}
static long lcm(long a,long b){
long gcd=gcd(a,b);
long result=b/gcd;
return a*result;
}
static int kaijoMod(int n,int mod){
if(n<1) return -1;
long result=1;
for(int i=n;i>1;i--){
result*=i;
result%=mod;
}
return (int)result;
}
static <T extends Comparable> Map<T,Integer> count(List<T> list){
//副作用
Collections.sort(list);
Map<T,Integer> result=new HashMap<>();
int l=0,r=0;
while(l<list.size()){
while(r<list.size()-1&&list.get(r).equals(r+1)){
r++;
}
result.put(list.get(r),r-l+1);
r++;
l=r;
}
return result;
}
static Map<Integer,Integer> count(int[] array){
//副作用
Arrays.sort(array);
Map<Integer,Integer> result=new HashMap<>();
int l=0,r=0;
while(l<array.length){
while(r<array.length-1&&array[r]==array[r+1]){
r++;
}
result.put(array[l],r-l+1);
r++;
l=r;
}
return result;
}
static String toStringBWS(Iterable iterable){
Iterator ite=iterable.iterator();
return toStringBWS(ite);
}
static String toStringBWS(Iterator ite){
StringBuilder sb=new StringBuilder();
sb.append(ite.next());
while(ite.hasNext()){
sb.append(" ");
sb.append(ite.next());
}
return sb.toString();
}
static String toStringBWS(int[] array){
StringBuilder sb=new StringBuilder();
for(int i=0;i<array.length-1;i++){
sb.append(array[i]);
sb.append(" ");
}
sb.append(array[array.length-1]);
return sb.toString();
}
static String toStringBWS(long[] array){
StringBuilder sb=new StringBuilder();
for(int i=0;i<array.length-1;i++){
sb.append(array[i]);
sb.append(" ");
}
sb.append(array[array.length-1]);
return sb.toString();
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
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 : 0;
}
for (size_t n = 1; n < N; ++n) {
if (c < 0) {
if (c + A[n] <= 0) {
a[i] += -(c + A[n]) + 1;
c = 1;
} else {
c += A[n];
}
} else {
if (c + A[n] >= 0) {
a[i] += c + A[n] + 1;
c = -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 | cpp | #include <bits/stdc++.h>
using namespace std;
bool isplus(long long a) { return (a > 0) ? true : false; }
int main() {
int n;
cin >> n;
long long a[n];
long long sum = 0;
long long ans = 0;
bool is_plus = true;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sum = a[0];
is_plus = isplus(sum);
for (int i = 1; i < n; i++) {
sum += a[i];
if (sum == 0) {
if (is_plus) {
sum--;
ans++;
} else {
sum++;
ans++;
}
}
if (is_plus) {
if (isplus(sum)) {
ans += sum + 1;
sum -= sum + 1;
}
} else {
if (!isplus(sum)) {
ans += -sum + 1;
sum += -sum + 1;
}
}
is_plus = !is_plus;
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
template <class T>
bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
int dy[] = {0, 1, 0, -1};
int dx[] = {1, 0, -1, 0};
int main() {
long long n;
cin >> n;
long long a[n];
for (long long i = (0); i < (n); i++) cin >> a[i];
long long csum[n];
csum[0] = a[0];
for (long long i = (1); i < (n); i++) {
csum[i] = csum[i - 1] + a[i];
}
if (csum[0] != 0) {
long long p1 = 0, m1 = 0;
bool flag = (csum[0] > 0);
for (long long i = (1); i < (n); i++) {
if ((flag ? -1 : 1) * (csum[i] + p1 - m1) > 0) {
flag = !flag;
continue;
} else {
if (flag) {
m1 += csum[i] + p1 - m1 + 1;
} else {
p1 += -(csum[i] + p1 - m1) + 1;
}
flag = !flag;
}
}
long long res = p1 + m1;
cout << res << endl;
} else {
long long p1 = 1, m1 = 0;
bool flag = true;
for (long long i = (1); i < (n); i++) {
if ((flag ? -1 : 1) * (csum[i] + p1 - m1) > 0) {
flag = !flag;
continue;
} else {
if (flag) {
m1 += csum[i] + p1 - m1 + 1;
} else {
p1 += -(csum[i] + p1 - m1) + 1;
}
flag = !flag;
}
}
long long res = p1 + m1;
p1 = 0, m1 = 1;
flag = false;
for (long long i = (1); i < (n); i++) {
if ((flag ? -1 : 1) * (csum[i] + p1 - m1) > 0) {
flag = !flag;
continue;
} else {
if (flag) {
m1 += csum[i] + p1 - m1 + 1;
} else {
p1 += -(csum[i] + p1 - m1) + 1;
}
flag = !flag;
}
}
res = ((res) < (p1 + m1) ? (res) : (p1 + m1));
cout << res << endl;
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
*A, = map(int,input().split())
ans1 = 0
S = A[0]
if S <= 0:
S = 1
ans1 = abs(S)+1
for a in A[1:]:
S1 = S+a
if S1*S >= 0:
ans1 += abs(S1)+1
S1 = -S//abs(S)
S = S1
ans2 = 0
S = A[0]
if S >= 0:
S = -1
ans2 = abs(S)+1
for a in A[1:]:
S1 = S+a
if S1*S >= 0:
ans2 += abs(S1)+1
S1 = -S//abs(S)
S = S1
print(min(ans1,ans2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void) {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
vector<long long> rev_a = a;
long long result = 0;
bool isPlus = a[0] > 0 ? true : false;
long long sum = a[0];
for (int i = 1; i < n; i++) {
long long temp_sum = sum + a[i];
if (isPlus) {
if (temp_sum >= 0) {
result += temp_sum + 1;
a[i] -= temp_sum + 1;
}
} else {
if (temp_sum <= 0) {
result += -temp_sum + 1;
a[i] += -temp_sum + 1;
}
}
isPlus = !isPlus;
sum += a[i];
}
sum = 0;
long long rev_result = 0;
isPlus = rev_a[0] > 0 ? true : false;
if (isPlus) {
rev_result += rev_a[0] + 1;
rev_a[0] -= rev_a[0] + 1;
isPlus = !isPlus;
} else {
rev_result -= rev_a[0] + 1;
rev_a[0] += rev_a[0] + 1;
isPlus = !isPlus;
}
for (int i = 1; i < n; i++) {
int temp_sum = sum + rev_a[i];
if (isPlus) {
if (temp_sum >= 0) {
rev_result += temp_sum + 1;
rev_a[i] -= temp_sum + 1;
}
} else {
if (temp_sum <= 0) {
rev_result += -temp_sum + 1;
rev_a[i] += -temp_sum + 1;
}
}
isPlus = !isPlus;
sum += rev_a[i];
}
if (rev_result < result)
cout << rev_result << endl;
else
cout << result << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
using System.Collections.Generic;
using static Assistant.Input;
using static Assistant.Debug;
using System.Linq;
using Assistant;
namespace ABC059C
{
class Program
{
static void Main(string[] args)
{
var n = RInt;
var a = RInts;
long ans = 0;
if (a[0] != 0)
{
ans = cand(a[0], a);
}
else
{
ans = Math.Min(cand(1, a), cand(-1, a)) + 1;
}
Console.WriteLine(ans);
}
static long cand(int sum, int[] a)
{
long ret = 0;
for (int i = 1; i < a.Length; i++)
{
if (sum < 0)
{
sum += a[i];
if (sum < 1)
{
ret += 1 - sum;
sum = 1;
}
}
else if (sum > 0)
{
sum += a[i];
if (sum > -1)
{
ret += sum + 1;
sum = -1;
}
}
}
return ret;
}
}
}
namespace Assistant
{
static class Input
{
static List<string> line = new List<string>();
static int index = 0;
static String RNext()
{
if (line.Count <= index) line.AddRange(Console.ReadLine().Split());
return line[index++];
}
public static int RInt => int.Parse(RNext());
public static long RLong => long.Parse(RNext());
public static int[] RInts => Console.ReadLine().Split().Select(int.Parse).ToArray();
public static long[] RLongs => Console.ReadLine().Split().Select(long.Parse).ToArray();
public static string RString => RNext();
//以下未テスト
public static int[] RIntsC(int c) => Enumerable.Repeat(0, c).Select(x => int.Parse(RNext())).ToArray();
public static long[] RLongsC(int c) => Enumerable.Repeat(0, c).Select(x => long.Parse(RNext())).ToArray();
public static char[][] RMap(int h) => Enumerable.Repeat(0, h).Select(x => Console.ReadLine().ToCharArray()).ToArray();
}
public struct Mlong
{
long _v;
const long mod = 1000000007;
public Mlong(long n = 0) : this() { _v = n >= mod ? n % mod : n; }
public static implicit operator Mlong(long _x) => new Mlong(_x);
public static implicit operator long(Mlong _x) => _x._v;
public static Mlong operator +(Mlong m1, Mlong m2) { long m = m1._v + m2._v; return m >= mod ? m - mod : m; }
public static Mlong operator -(Mlong m1, Mlong m2) { long m = m1._v - m2._v; return m >= 0 ? m : m + mod; }
public static Mlong operator *(Mlong m1, Mlong m2) => m1._v * m2._v % mod;
public static Mlong operator /(Mlong m1, Mlong m2) => m1._v * ModPow(m2._v, mod - 2) % mod;
public static long ModPow(long a, long n)
{
if (n == 0) return 1;
else if (n % 2 == 1) return a * ModPow(a, n - 1) % mod;
else return ModPow(a * a % mod, n / 2);
}
static Mlong[] fac, finv, inv;
public static void nCkInit(int max)
{
fac = new Mlong[max]; finv = new Mlong[max]; inv = new Mlong[max];
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;
inv[i] = mod - inv[mod % i] * (mod / i);
finv[i] = finv[i - 1] * inv[i];
}
}
public static Mlong nCk(int n, int k)
{
if (n < k) return 0;
if (n < 0 || k < 0) return 0;
return fac[n] * finv[k] * finv[n - k];
}
}
static class Debug
{
static public void Draw2D<T>(T[,] map, int mode = 1)
{
#if DEBUG
int W = map.GetLength(0);
int H = map.GetLength(1);
string[,] map2 = new string[W + 1, H + 1];
for (int i = 0; i < W + 1; i++)
{
for (int j = 0; j < H + 1; j++)
{
if (i == 0 && j == 0) map2[i, j] = 0.ToString();
else if (i == 0) map2[i, j] = (j - 1).ToString();
else if (j == 0) map2[i, j] = (i - 1).ToString();
else map2[i, j] = map[i - 1, j - 1].ToString();
}
}
for (int i = 0; i < W + 1; i++)
{
for (int j = 0; j < H + 1; j++)
{
if (mode == 0) Console.Write(map2[i, j].Last());
if (mode == 1) Console.Write(map2[i, j] + " ");
}
Console.WriteLine();
}
Console.WriteLine();
#endif
}
public static void Draw1D<T>(T[] array, int mode = 0)
{
#if DEBUG
Console.WriteLine(string.Join(" ", array));
#endif
}
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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, ans;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a.at(i);
}
for (int i = 0; i < n - 1; i++) {
int p = 0;
for (int j = i + 1; j >= 0; j--) p += a.at(j);
while (a.at(i) * a.at(i + 1) < 0 || p == 0) {
if (a.at(i) > 0) {
a.at(i + 1)--;
ans++;
if (p == 0) p++;
}
if (a.at(i) < 0) {
a.at(i + 1)++;
ans++;
if (p == 0) p++;
}
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long a[100010];
long long sum = 0, cnt = 0;
for (int i = 0; i < n; i++) cin >> a[i];
bool nextpo = false;
bool nextne = false;
sum = a[0];
if (sum < 0)
nextpo = true;
else if (sum > 0)
nextne = true;
for (int i = 1; i < n; i++) {
if (nextpo) {
nextpo = false;
nextne = true;
sum += a[i];
if (sum == 0) {
sum++;
cnt++;
} else if (sum < 0) {
cnt += abs(sum) + 1;
sum += abs(sum) + 1;
}
} else if (nextne) {
nextpo = true;
nextne = false;
sum += a[i];
if (sum == 0) {
sum--;
cnt++;
} else if (sum > 0) {
cnt += sum + 1;
sum -= sum + 1;
}
}
}
cout << cnt << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using ll = long long;
using itn = int;
using namespace std;
int GCD(int a, int b) { return b ? GCD(b, a % b) : a; }
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<ll> sum(n, 0);
for (int i = 0; i < n; i++) {
if (i == 0)
sum[i] = a[i];
else
sum[i] = a[i] + sum[i - 1];
}
int cnt1 = 0, cnt2 = 0;
ll add1 = 0, add2 = 0;
for (int i = 0; i < n; i++) {
ll tmp1 = sum[i] + add1;
ll tmp2 = sum[i] + add2;
if (i % 2 == 0) {
if (tmp1 <= 0) {
int b = 1 - tmp1;
cnt1 += b;
add1 += b;
}
if (tmp2 >= 0) {
int b = tmp2 + 1;
cnt2 += b;
add2 -= b;
}
} else {
if (tmp1 >= 0) {
int b = tmp1 + 1;
cnt1 += b;
add1 -= b;
}
if (tmp2 <= 0) {
int b = 1 - tmp2;
cnt2 += b;
add2 += b;
}
}
}
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;
int main() {
long long N;
cin >> N;
vector<long long> v(N);
bool check = false;
for (int i = 0; i < N; i++) {
cin >> v[i];
if (v[0] < 0) check = true;
if (check) v[i] = -v[i];
}
vector<long long> a(N), b(N);
int cntA = 0, cntB = 0;
a[0] = v[0];
cntB += v[0] + 1;
b[0] = -1;
if (v[0] == 0) {
cntA++;
cntB++;
a[0] = v[0] + 1;
b[0] = v[0] - 1;
}
for (int i = 1; i < N; i++) {
long long tmp_a = a[i - 1] + v[i];
if (tmp_a == 0) {
if (i % 2 == 0) {
a[i] = 1;
} else {
a[i] = -1;
}
cntA++;
} else if (i % 2 == 0 && tmp_a < 0) {
a[i] = 1;
cntA += (-tmp_a) + 1;
} else if (i % 2 == 1 && tmp_a > 0) {
a[i] = -1;
cntA += tmp_a + 1;
} else {
a[i] = tmp_a;
}
long long tmp_b = b[i - 1] + v[i];
if (tmp_b == 0) {
if (i % 2 == 0) {
b[i] = -1;
} else {
b[i] = 1;
}
cntB++;
} else if (i % 2 == 0 && tmp_b > 0) {
b[i] = -1;
cntB += tmp_b + 1;
} else if (i % 2 == 1 && tmp_b < 0) {
b[i] = 1;
cntB += (-tmp_b) + 1;
} else {
b[i] = tmp_b;
}
}
cout << min(cntA, cntB) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
using System.Linq;
using System.Collections.Generic;
using System.Numerics;
using static System.Console;
class Program {
static Scanner sc = new Scanner();
internal static void Main(string[] args) {
var N = sc.nextInt();
var a = sc.ArrayInt(N);
var sum = a[0];
var ans = 0;
bool bef = a[0] >= 0;
for (int i = 1; i < N; i++) {
if (sum + a[i] == 0 || (sum + a[i] > 0) == bef ) {
if (bef) {
ans += sum + a[i] + 1;
sum = -1;
} else {
ans += Math.Abs(sum + a[i]) + 1;
sum = 1;
}
} else {
sum += a[i];
}
bef = !bef;
}
WriteLine(ans);
}
}
class Scanner {
string[] s;
int i;
char[] cs = new char[] { ' ' };
public Scanner() {
s = new string[0];
i = 0;
}
public string next() {
if (i < s.Length) return s[i++];
string st = Console.ReadLine();
while (st == "") st = Console.ReadLine();
s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
if (s.Length == 0) return next();
i = 0;
return s[i++];
}
public int nextInt() {
return int.Parse(next());
}
public int[] ArrayInt(int N, int add = 0) {
int[] Array = new int[N];
for (int i = 0; i < N; i++) {
Array[i] = nextInt() + add;
}
return Array;
}
public long nextLong() {
return long.Parse(next());
}
public long[] ArrayLong(int N, long add = 0) {
long[] Array = new long[N];
for (int i = 0; i < N; i++) {
Array[i] = nextLong() + add;
}
return Array;
}
public double nextDouble() {
return double.Parse(next());
}
public double[] ArrayDouble(int N, double add = 0) {
double[] Array = new double[N];
for (int i = 0; i < N; i++) {
Array[i] = nextDouble() + add;
}
return Array;
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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
# -*- coding: utf-8 -*-
# 整数の入力
n=int(input())
a=list(map(int, input().split()))
b=a[:]
c=a[:]
# 無変更チェック
if a[0]!=0:
S=int(a[0])
for i in range(1,n):
if S<0 and S+a[i]<=0:
break
elif S>0 and S+a[i]>=0:
break
S+=a[i]
if i==n-1:
print(0)
sys.exit()
# a[0]を1に変えた場合の計算
counter_2=abs(b[0]-1)
b[0]=1
S=b[0]
for i in range(1,n):
if S<0 and S+b[i]<=0:
counter_2+=-S-b[i]+1
b[i]=-S+1
elif S>0 and S+b[i]>=0:
counter_2+=S+b[i]+1
b[i]=-S-1
S+=b[i]
# a[0]を-1に変えた場合の計算
counter_3=abs(c[0]+1)
c[0]=-1
S=c[0]
for i in range(1,n):
if S<0 and S+c[i]<=0:
counter_3+=-S-c[i]+1
c[i]=-S+1
elif S>0 and S+c[i]>=0:
counter_3+=S+c[i]+1
c[i]=-S-1
S+=c[i]
print(min(counter_1,counter_2,counter_3)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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()))
check = ''
if a[0] > 0:
check = '+'
else:
check = '-'
ans = 0
for i in range(1, n):
a[i] += a[i-1]
if check == '+':
if a[i] >= 0:
ans += abs(a[i]) + 1
a[i] -= abs(a[i]) + 1
check = '-'
else:
if a[i] <= 0:
ans += abs(a[i]) + 1
a[i] += abs(a[i]) + 1
check = '+'
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import sys
n = int(input())
a = [int(n) for n in input().split()]
sum = [0]*n
sum[0] = a[0]
ans = 0
for i in range(1,n):
sum[i] = sum[i-1]
if((sum[i]+a[i])*sum[i-1] >= 0):
if(sum[i-1] > 0):
ans+=sum[i-1] + a[i]+1
a[i]-=sum[i-1] + a[i]+1
else:
ans+=1 - sum[i-1] - a[i]
a[i]+=1 - sum[i-1] - a[i]
sum[i] += a[i]
print(ans)
# print(a)
# print(sum)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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[100000];
for (long long i = 0; i < n; i++) cin >> a[i];
long long ans = 1 << 62;
long long sum[100001] = {};
for (long long p = 0; p < 2; p++) {
long long cnt = 0;
for (long long i = 0; i < n; i++) {
int border = 1 + (p + i) % 2 * -2;
sum[i + 1] = sum[i] + a[i];
if (border == 1 && sum[i + 1] >= border) continue;
if (border == -1 && sum[i + 1] <= border) continue;
cnt += abs(border - sum[i + 1]);
sum[i + 1] = border;
}
ans = min(ans, cnt);
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
long long int s1 = 0, s2 = 0, ans[] = {0, 0};
long long int a, b;
for (int i = 0; i < N; ++i) {
cin >> a;
b = a;
if (i % 2) {
s1 += a;
s2 += a;
if (s1 >= 0) {
ans[1] += s1 + 1;
a -= s1 + 1;
s1 = -1;
}
if (!a) {
--a;
--s1;
++ans[1];
}
if (s2 <= 0) {
ans[0] -= s2 - 1;
b -= s2 - 1;
s2 = 1;
}
if (!b) {
++b;
++s2;
++ans[0];
}
} else {
s1 += a;
s2 += a;
if (s2 >= 0) {
ans[0] += s2 + 1;
a -= s2 + 1;
s2 = -1;
}
if (!a) {
++ans[0];
--a;
--s2;
}
if (s1 <= 0) {
ans[1] -= s1 - 1;
b -= s1 + 1;
s1 = 1;
}
if (!b) {
++ans[1];
++b;
++s1;
}
}
}
cout << min(ans[0], ans[1]) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
const int N = 100000;
int n;
long long a[N];
long long sum = 0, ans = 0;
bool sign1 = true;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sum = a[0];
if (sum >= 0)
sign1 = true;
else
sign1 = false;
for (int i = 1; i < n; i++) {
bool sign2 = true;
sum += a[i];
if (sum >= 0)
sign2 = true;
else
sign2 = false;
if (sign1 != sign2) {
sign1 = sign2;
if (sum == 0) ans++;
} else {
ans += abs(sum) + 1;
if (sign2)
sum = -1;
else
sum = 1;
sign1 = !sign2;
}
}
cout << ans << endl;
return (0);
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
long[] a=new long[n];
for(int i=0;i<n;i++)a[i]=sc.nextLong();
long sum=0;
long count=0;
for(int i=0;i<n-1;i++){
if(i==0){
if(a[i]==0){
for(int j=1;j<n;j++){
if(a[j]>0){
a[i]-=1;
count++;
break;
}else if(a[j]<0){
a[i]+=1;
count++;
break;
}
}
if(a[i]==0){
a[i]=1;
count++;
}
}
}
sum+=a[i];
if(sum>0){
if(sum+a[i+1]>=0){
count+=sum+a[i+1]+1;
a[i+1]-=sum+a[i+1]+1;
}
}else if(sum<0){
if(sum+a[i+1]<=0){
count+=-1*(sum+a[i+1])+1;
a[i+1]+=-1*(sum+a[i+1])+1;
}
}
}
System.out.println(count);
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long INF = (1LL << 62);
long long N;
vector<long long> A, W;
long long S[100002] = {INF * (-1)};
long long dp[100002] = {0};
void calcDP(int n) {
if (n == 1) {
if (W[1] != 0) {
dp[1] = 0;
} else {
dp[1] = 1;
if (W[2] <= 0) {
W[1] = 1;
} else {
W[1] = -1;
}
S[1] = W[1];
}
return;
} else {
S[n] = S[n - 1] + W[n];
if ((S[n - 1] < 0 && S[n] > 0) || (S[n - 1] > 0 && S[n] < 0)) {
dp[n] = dp[n - 1];
} else {
dp[n] = dp[n - 1] + abs(0 - S[n - 1] - W[n]) + 1;
W[n] = 0 - S[n - 1] - (abs(S[n - 1]) / S[n - 1]);
S[n] = S[n - 1] + W[n];
}
return;
}
}
int main(int argc, char* argv[]) {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> N;
W.push_back(0);
S[0] = 0;
for (int i = 1; i <= N; i++) {
long long a;
cin >> a;
A.push_back(a);
W.push_back(a);
if (i == 1) {
S[1] = a;
} else {
S[i] = S[i - 1] + a;
}
}
for (int i = 1; i <= N; i++) {
calcDP(i);
}
printf("%lld\n", dp[N]);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
as = list(map(int, input().split(" ")))
ope = 0
for i in range(0, n-1):
sum_be=sum(as[:i])
sum_af=sum(as[:i+1])
if sum_be<0<sum_af or sum_af<0<sum_be:
continue
elif sum_be > 0:
ope += as[i+1] + (sum_be+1)
as[i+1] = sum_be + 1
elif sum_be < 0:
ope += 1 - sum_be - sum_af
as[i+1] = 1 - sum_be
print(ope) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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 1;
}
return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
int a[N];
int sum = 0, cnt = 0;
for (long long i = 0; i < N; i++) {
cin >> a[i];
if (i == 0) {
sum += a[i];
continue;
}
if (sum > 0 && sum + a[i] > 0) {
cnt += sum + a[i] + 1;
sum = -1;
} else if (sum < 0 && sum + a[i] < 0) {
cnt += abs(sum + a[i]) + 1;
sum = 1;
} else if (sum + a[i] == 0) {
if (a[i] >= 0) {
sum++;
cnt++;
} else {
sum--;
cnt++;
}
} else
sum += a[i];
}
cout << cnt << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.*;
// ABC 6-C
// http://abc006.contest.atcoder.jp/tasks/abc006_3
public class Main {
public static void main (String[] args) throws java.lang.Exception {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = in.nextInt();
}
long answer = 0;
if (nums[0] == 0) {
answer = solve(nums, 0, 0);
} else {
answer = solve(nums, nums[0], 1);
}
System.out.println(answer);
//
// long sum = 0;
// long answer = 0;
//
// for (int i = 0; i < n; i++) {
// int a = in.nextInt();
//
// if (sum < 0 && sum + a < 0) {
// answer += 1 + Math.abs(sum + a);
// sum = 1;
// } else if (sum > 0 && sum + a > 0) {
// answer += 1 + sum + a;
// sum = -1;
// } else if (sum + a == 0) {
// answer++;
// if (sum < 0) {
// sum = 1;
// } else {
// sum = -1;
// }
// } else {
// sum += a;
// }
// }
// System.out.println(answer);
}
public static long solve(int[] nums, long sum, int index) {
if (index == nums.length) {
return 0;
}
if (sum < 0 && sum + nums[index] < 0) {
return 1 + Math.abs(sum + nums[index]) + solve(nums, 1, index + 1);
} else if (sum > 0 && sum + nums[index] > 0) {
return 1 + sum + nums[index] + solve(nums, -1, index + 1);
} else if (sum + nums[index] == 0) {
return 1 + Math.min(solve(nums, 1, index + 1), solve(nums, -1, index + 1));
} else {
return solve(nums, sum + nums[index], index + 1);
}
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 100;
const int mod = 1e9 + 7;
long long a[N];
int n;
int slove(int f) {
long long sum = 0, ans = 0;
for (int i = 1; i <= n; i++) {
sum += a[i];
if (sum * f <= 0) {
ans += abs(f - sum);
sum = f;
}
f = -f;
}
return ans;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
long long ans = min(slove(1), slove(-1));
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int n, cnt = 0;
cin >> n;
bool flag;
vector<ll> a;
ll x;
for (int i = 0; i < n; i++) {
cin >> x;
a.push_back(x);
}
if (a[0] >= 0) {
flag = true;
} else {
flag = false;
}
int sum = a[0];
for (int i = 1; i < n; i++) {
bool flag2;
int tmp = sum;
sum += a[i];
if (sum == 0) {
if (flag) {
sum -= 1;
flag = false;
cnt++;
} else {
sum += 1;
flag = true;
cnt++;
}
} else {
if (sum > 0) {
flag2 = true;
} else {
flag2 = false;
}
if (flag == flag2) {
if (flag2) {
while (sum >= 0) {
sum--;
cnt++;
}
flag2 = false;
} else {
while (sum <= 0) {
sum++;
cnt++;
}
flag2 = true;
}
}
flag = flag2;
}
}
if (sum == 0) cnt++;
cout << cnt << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | n = gets.to_i
arr = gets.split.map(&:to_i)
pre = arr[0]
count = 0
# binding.pry
(arr.size - 1).times do |i|
pre2 = arr[i + 1]
if pre > 0
if pre + pre2 >= 0
pre2 = -(pre + 1)
end
pre += pre2
count += (pre2 - arr[i + 1]).abs
elsif pre < 0
if pre + pre2 <= 0
pre2 = -(pre - 1)
end
pre += pre2
count += (pre2 - arr[i + 1]).abs
end
end
puts count |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define read_input freopen("in.txt","r", stdin)
#define print_output freopen("out.txt","w", stdout)
typedef long long ll;
typedef long double ld;
using namespace std;
int sign(int x) {
if(x > 0) return 1;
else if(x < 0) return 0;
else return -1;
}
ll f(ll t, int pre, int s) {
if(s == 0) return abs(-1-pre-t);
else return abs((1-t)-pre);
}
int main() {
int n, a[100005];
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
ll ans1 = 0, ans2 = 0;
int s = 1;
ll x = 0, tmp;
for(int i = 1; i <= n; i++, s ^= 1) {
tmp = x;
x += a[i];
if(sign(x) != s) ans1 += f(tmp, a[i], s);
if(not s) x = min(-1, x);
else x = max(1, x);
}
s = 0, x = 0;
for(int i = 1; i <= n; i++, s ^= 1) {
tmp = x;
x += a[i];
if(sign(x) != s) ans2 += f(tmp, a[i], s);
if(not s) x = min(-1, x);
else x = max(1, x);
}
cout << min(ans1, ans2) << endl;
return 0;
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int i;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (i = 0; i < n; i++) {
cin >> a[i];
}
int res = 0;
for (i = 0; i < n; i++) {
if (a[i] > 0) {
res += a[i + 1] - -abs(a[i] + 1);
} else {
res += a[i + 1] - abs(a[i] + 1);
}
}
cout << res;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n;
long long Num[111111];
long long Sum[111111];
long long Out1, Out2;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%lld", &Num[i]);
if (Num[1] == 0) Sum[1] = 1, Out1++;
for (int i = 1; i <= n; i++) {
Sum[i] = Sum[i - 1] + Num[i];
if (i % 2 == 0) {
if (Sum[i] >= 0) {
Out1 += (Sum[i] + 1);
Sum[i] = -1;
}
} else {
if (Sum[i] <= 0) {
Out1 += (1 - Sum[i]);
Sum[i] = 1;
}
}
}
memset(Sum, 0, sizeof(Sum));
if (Num[1] == 0) Sum[1] = -1, Out2++;
for (int i = 1; i <= n; i++) {
Sum[i] = Sum[i - 1] + Num[i];
if (i % 2 == 1) {
if (Sum[i] >= 0) {
Out2 += (Sum[i] + 1);
Sum[i] = -1;
}
} else {
if (Sum[i] <= 0) {
Out2 += (1 - Sum[i]);
Sum[i] = 1;
}
}
}
printf("%lld\n", min(Out1, Out2));
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
List<Integer> alist = new ArrayList<>();
for (int i = 0; i < n; i++) {
alist.add(sc.nextInt());
}
int cntOdd = 0;
int cntEvn = 0;
int sum = 0;
for (int i = 0; i < alist.size(); i++) {
sum += alist.get(i);
if(i%2 != 0) {
if(sum >= 0) {
while(sum >= 0) {
sum--;
cntOdd++;
}
} else {
continue;
}
} else {
if(sum <= 0) {
while(sum <= 0) {
sum++;
cntOdd++;
}
} else {
continue;
}
}
}
sum = 0;
for (int i = 0; i < alist.size(); i++) {
sum += alist.get(i);
if (i%2 != 0) {
if(sum <= 0) {
while(sum <= 0) {
sum ++;
cntEvn++;
}
} else {
continue;
}
} else {
if(sum >= 0) {
while(sum >= 0) {
sum --;
cntEvn++;
}
} else {
continue;
}
}
}
if(cntOdd <= cntEvn) {
System.out.println(cntOdd);
} else {
System.out.println(cntEvn);
}
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | import algorithm, tables, sets, lists, intsets, critbits, sequtils, strutils, math, future
var
N:int = stdin.readLine.parseInt
A = stdin.readLine.split.map(parseInt)
# ans:int = 0
# now:int = A[0]
proc dist(a, b:int):int =
if a > b:
return abs(a - b)
else:
return abs(b - a)
proc solve(L:seq[int]):int =
var
ans:int = 0
A = L
now = A[0]
for i in 1..<N:
if now < 0:
if now + A[i] <= 0:
var v = -now + 1
ans += dist(now, A[i])
A[i] = v
now += A[i]
elif now > 0:
if now + A[i] >= 0:
var v = -now - 1
ans += dist(now, A[i])
A[i] = v
now += A[i]
return ans
var a1 = solve(A)
var tmp:int
if A[0] > 0:
tmp = A[0]
A[0] = -1
else:
tmp = abs(A[0])
A[0] = 1
var a2 = solve(A) + tmp
echo min(a1, a2) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import numpy as np
import copy
N = int(input())
a = list(map(int,input().split()))
a = np.cumsum(a)
ans1 = 0
ans2 = 0
b = copy.copy(a)
for i in range(N):
if i % 2 == 0:
if a[i] > 0:
pass
else:
ans1 += abs(a[i]) + 1
a = list(map(lambda n:n+abs(a[i])+1, a))
else:
if a[i] < 0:
pass
else:
ans1 += abs(a[i]) + 1
a = list(map(lambda n:n-(abs(a[i])+1), a))
for i in range(N):
if i % 2 == 1:
if b[i] > 0:
pass
else:
ans2 += abs(b[i]) + 1
b = list(map(lambda n:n+abs(b[i])+1, a))
else:
if b[i] < 0:
pass
else:
ans2 += abs(b[i]) + 1
b = list(map(lambda n:n-(abs(b[i])+1), b))
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 | n = int(input())
a = list(map(int, input().split()))
ans = 10**10
# 1 -1 1 -1...
cost1 = 0
s = 0
for i in range(n):
t = 1 if i % 2 == 0 else -1
c = 0
if i % 2 == 0:
if s + a[i] > 0:
s += a[i]
else:
c = abs(s - t)
s += c
cost1 += abs(c - a[i])
else:
if s + a[i] < 0:
s += a[i]
else:
c = abs(s - t)
s -= c
cost1 += abs(c - a[i])
cost2 = 0
s = 0
for i in range(n):
t = -1 if i % 2 == 0 else 1
c = 0
if i % 2 == 1:
if s + a[i] > 0:
s += a[i]
else:
c = abs(s - t)
s += c
cost2 += abs(c - a[i])
else:
if s + a[i] < 0:
s += a[i]
else:
c = abs(s - t)
s -= c
cost2 += abs(- c - a[i])
print(cost1, cost2)
print(min(cost1, cost2))
# -1 1 -1 1..
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
a_lst = [int(x) for x in input().split()]
def my_sign(num):
return (num > 0) - (num < 0)
sum = 0
cnt = 0
sum_lst = []
for i in range(N):
if i == 0:
if a_lst[i] == 0:
a_lst[i] = 1
cnt += 1
sum_lst.append(a_lst[i])
else:
sum_lst.append(a_lst[i] + sum_lst[i - 1])
if my_sign(sum_lst[i]) == my_sign(sum_lst[i - 1]) or my_sign(sum_lst[i]) == 0:
cnt += max(-my_sign(sum_lst[i - 1]), sum_lst[i]) - min(-my_sign(sum_lst[i - 1]), sum_lst[i])
a_lst[i] += -my_sign(sum_lst[i - 1]) - sum_lst[i]
sum_lst[i] = -my_sign(sum_lst[i - 1])
#print(a_lst)
#print(sum_lst)
print(cnt)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
A = list(map(int,input().split()))
totals = [0] * N
totals[0] = A[0]
con = 0
for i in range(1,N):
totals[i] = totals[i - 1] + A[i]
#チェック
##符号が等しいか、ゼロなら操作
if totals[i - 1] * totals[i] >= 0:
if totals[i - 1] < 0:
con += abs(1 - totals[i])
totals[i] += abs(1 - totals[i])
else:
con += abs(-1 - totals[i])
totals[i] -= abs(-1 - totals[i])
print(con)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int N;
vector<int> a;
int solve(bool b) {
int count = 0;
int sum = a[0];
if (a[0] == 0) {
sum = b ? 1 : -1;
count++;
} else if (b && a[0] < 0) {
sum = 1;
count = 1 - a[0];
} else if (!b && a[0] > 0) {
sum = -1;
count = a[0] + 1;
}
for (int i = 1; i < N; i++) {
if (sum * a[i] < 0 && abs(sum) < abs(a[i])) {
sum += a[i];
} else {
if (sum > 0) {
count += a[i] + sum + 1;
sum = -1;
} else {
count += 1 - sum - a[i];
sum = 1;
}
}
}
return count;
}
int main() {
cin >> N;
a.resize(N);
for (int i = 0; i < N; i++) cin >> a[i];
cout << min(solve(true), solve(false)) << 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 | n = gets.to_i
nums = gets.split.map(&:to_i)
nonzero_index = nums.find_index { |i| i.nonzero? }
if nonzero_index.even? && nums[nonzero_index] > 0
next_minus = false
elsif nonzero_index.even? && nums[nonzero_index] < 0
next_minus = true
elsif nonzero_index.odd? && nums[nonzero_index] > 0
next_minus = true
else
next_minus = false
end
cumulative = 0
ans = 0
nums.each do |num|
cumulative += num
if next_minus
if cumulative >= 0
decrement = cumulative.abs + 1
ans += decrement
cumulative -= decrement
end
else
if cumulative <= 0
increment = cumulative.abs + 1
ans += increment
cumulative += increment
end
end
next_minus = !next_minus
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;
int main(int argc, char* argv[]) {
int n;
cin >> n;
vector<long long> a(n, 0);
vector<long long> s(n, 0);
int i;
for (i = 0; i < n; i++) {
cin >> a[i];
}
s[0] = a[0];
long long aw = 0;
for (i = 1; i < n; i++) {
s[i] = s[i - 1] + a[i];
if (s[i - 1] > 0) {
if (s[i] < 0) {
continue;
} else {
aw += abs(s[i - 1] + a[i] + 1);
a[i] = -1 - s[i - 1];
s[i] = s[i - 1] + a[i];
}
} else {
if (s[i] > 0) {
continue;
} else {
aw += abs(s[i - 1] + a[i] - 1);
a[i] = 1 - s[i - 1];
s[i] = s[i - 1] + a[i];
}
}
}
cout << aw << 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(void) {
long long n, b, c = 0;
cin >> n >> b;
for (int i = 0; i < n - 1; i++) {
int a;
cin >> a;
a += b;
if (a * b >= 0) {
if (b > 0) {
c += a + 1;
a = -1;
} else {
c += 1 - a;
a = 1;
}
}
b = a;
}
cout << c << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
const int INF = 1001001001;
int main() {
int n;
cin >> n;
vector<int> 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];
cout << s << " ";
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 << s << endl;
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
long long s[maxn];
long long ans[maxn];
int main() {
int n, j;
cin >> n;
long long sum = 0;
for (int i = 1; i <= n; i++) {
cin >> s[i];
}
for (int i = 1; i < n; i++) {
ans[i] = ans[i - 1] + s[i];
if (ans[i] > 0) {
if (s[i + 1] >= 0) {
sum += (s[i + 1] + ans[i] + 1);
s[i + 1] = -(ans[i] + 1);
} else {
if (abs(s[i + 1]) > ans[i]) {
} else {
sum += (s[i + 1] + ans[i] + 1);
s[i + 1] = -(ans[i] + 1);
}
}
} else if (ans[i] == 0) {
if (s[i + 1] <= 0) {
sum += -s[i + 1] + 1;
ans[i] = 2;
s[i + 1] = -1;
} else if (s[i + 1] > 0) {
sum += s[i + 1] + 1;
ans[i] = -2;
s[i + 1] = 1;
}
} else if (ans[i] < 0) {
if (s[i + 1] > 0) {
if (abs(ans[i]) < s[i + 1]) {
} else {
sum += (1 - ans[i] - s[i + 1]);
s[i + 1] = -ans[i] + 1;
}
} else {
sum += (1 - ans[i] - s[i + 1]);
s[i + 1] = -ans[i] + 1;
}
}
}
cout << sum << 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())
nums = list(map(int, input().split()))
ans = 10**5+1
for start in [-1, 1]:
before = start
cnt = 0
sum_n = 0
for num in nums:
sum_n += num
if before*sum_n >= 0:
if before < 0:
cnt += abs(1-sum_n)
sum_n = 1
else:
cnt += abs(-1-sum_n)
sum_n = -1
before = sum_n
ans = min(ans, cnt)
print(ans)
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.