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 | UNKNOWN | #include <bits/stdc++.h>
int main(void) {
int n;
long int a[100000] = {0}, b[100001] = {0}, fugo, ans = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%ld", &a[i]);
}
if (a[0] < 0)
fugo = 0;
else
fugo = 1;
for (int i = 1; i <= n; i++) {
b[i] = b[i - 1] + a[i - 1];
if (i % 2 == fugo) {
if (b[i] <= 0) ans += 1 - b[i], b[i] += ans;
} else {
if (b[i] >= 0) ans += b[i] + 1, b[i] -= ans;
}
}
printf("%d\n", ans);
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> a(N);
for (int i = 0; i < N; i++) cin >> a.at(i);
bool fla = false;
for (int i = 0; i < N; i++) {
if (a.at(i) != 0) {
if ((a.at(i) > 0) && (i % 2 == 0))
fla = true;
else if (i % 2 == 1)
fla = true;
break;
}
}
int64_t t = 0LL, res = 0LL;
for (int i = 0; i < N; i++) {
int b = a.at(i);
if (fla) {
if (t + b <= 0) {
b = t * -1 + 1;
res += b - a.at(i);
}
} else {
if (t + b >= 0) {
b = t * -1 - 1;
res += abs(b - a.at(i));
}
}
t += b;
fla = !fla;
}
cout << res << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | #!/usr/bin/env python3
#ABC59 C
n = int(input())
a = list(map(int,input().split()))
ans1 = 0
s1 = a[0]
if s1 < 0:
ans1 += abs(s1) + 1
for i in range(1,n):
if i % 2 == 0:
if s1 + a[i] > 0:
s1 += a[i]
else:
ans1 += abs(s1 + a[i]) + 1
s1 = 1
else:
if s1 + a[i] < 0:
s1 += a[i]
else:
ans1 += abs(s1 + a[i]) + 1
s1 = -1
ans2 = 0
s2 = a[0]
if s2 > 0:
ans2 += abs(s2) + 1
for i in range(1,n):
if i % 2 == 0:
if s2 + a[i] < 0:
s2 += a[i]
else:
ans2 += abs(s2 + a[i]) + 1
s2 = -1
else:
if s2 + a[i] > 0:
s2 += a[i]
else:
ans2 += abs(s2 + a[i]) + 1
s2 = 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()))
prv_total =0
cnt = 0
if a[0] == 0:
if a[1]>= 0:
a[0] = -1
else:
a[0] = -1
cnt += 1
for i in range(n-1):
total = prv_total + a[i]
nxt_total = total+a[i+1]
if total > 0 and nxt_total >= 0:
a[i+1] -= nxt_total+1
cnt += nxt_total+1
nxt_total -= nxt_total+1
elif total < 0 and nxt_total <=0:
a[i+1] += abs(nxt_total)+1
cnt += abs(nxt_total)+1
nxt_total += abs(nxt_total)+1
prv_total = total
total = prv_total + a[-1]
if total == 0:
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 | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
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, 0, 1, -1};
int dx[] = {1, -1, 0, 0};
int main() {
ll n;
cin >> n;
vector<ll> a(n);
(i, a, b) for (ll(i) = a; (i) < (b); ++(i))(i, 0, n) cin >> a[i];
vector<ll> sum(n);
sum[0] = a[0];
(i, a, b) for (ll(i) = a; (i) < (b); ++(i))(i, 0, n - 1) sum[i + 1] =
sum[i] + a[i + 1];
ll c = 0;
ll d = 0;
if (sum[0] == 0) {
(i, a, b) for (ll(i) = a; (i) < (b); ++(i))(i, 1, n) {
if (sum[i] == 0 and i != n - 1) continue;
c = 1;
d = (sum[i] < 0 ? 1 : -1);
break;
}
}
(i, a, b) for (ll(i) = a; (i) < (b); ++(i))(i, 0, n - 1) {
if ((d + sum[i]) * (d + sum[i + 1]) < 0) continue;
c += abs(d + sum[i + 1]) + 1;
if (d + sum[i + 1] >= 0)
d -= d + sum[i + 1] + 1;
else
d += abs(d + sum[i + 1]) + 1;
}
cout << c << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
count = 0
for i in range(1,n):
if a[0]>0:
if i%2==1 and sum(a[0:i+1])>=0:
count += abs(sum(a[0:i+1])+1)
a[i] = -(sum(a[0:i])+1)
elif i%2==0 and sum(a[0:i+1])<=0:
count += abs(sum(a[0:i+1])-1)
a[i] = a[i]+abs(sum(a[0:i+1])-1)
else:
if i%2==1 and sum(a[0:i+1])<=0:
count += abs(sum(a[0:i+1])-1)
a[i] = a[i]+abs(sum(a[0:i+1])-1)
elif i%2==0 and sum(a[0:i+1])>=0:
count += abs(sum(a[0:i+1])+1)
a[i] = -(sum(a[0:i])+1)
print(count) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | n = gets.to_i
input = gets.chomp.split(" ").map { |n| n.to_i }
if input[0] == 0
input[0] += 1
end
total = input[0]
cost = 0
flag = input[0] > 0 ? true : false
for i in 1..n-1
total += input[i]
if flag == true
if total > 0
#p total
cost += (total.abs) +1
total = -1
elsif total == 0
cost += 1
total = -1
end
flag = false
else
if total < 0
#p total
cost += (total.abs) +1
total = 1
elsif total == 0
cost += 1
total = 1
end
flag = true
end
end
puts cost |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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{0}, sum{0};
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;
cin >> n;
for (int i{0}; i < n; ++i) cin >> a[i];
int try1 = getTotal(n, 1);
int try2 = getTotal(n, -1);
cout << ((try1) < (try2) ? (try1) : (try2)) << "\n";
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
using namespace std;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
#define pint pair<int,int>
#define pll pair<ll,ll>
using ll = long long;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;
int main(){
int n;
cin >> n;
int a[n];
rep(i,n)cin >> a[i];
int sum[n]={},sum2[n]={};
int temp=0,temp2=0;
rep(i,n){
if(i==0){
sum[i]=a[i];
if(a[i]<=0){
temp+=-a[i]+1;
sum[i]=1;
}
}
else{
sum[i]=a[i]+sum[i-1];
if(sum[i]*sum[i-1]>=0){
if(i%2==0){
temp+=-sum[i]+1;
sum[i]=1;
}else{
temp+=sum[i]+1;
sum[i]=-1;
}
}
}
}
rep(i,n){
if(i==0){
sum2[i]=a[i];
if(sum2[i]>=0){
temp2+=a[i]+1;
sum2[i]=-1;
}
}
else{
sum2[i]=a[i]+sum2[i-1];
if(sum2[i]*sum2[i-1]>=0){
if(i%2==0){
temp2+=sum2[i]+1;
sum2[i]=-1;
}else{
temp2+=-sum2[i]+1;
sum[i]=1;
}
}
}
}
// cout << temp << ' ' << temp2 << endl;
cout << min(temp,temp2) << endl;
return 0;} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | # coding: utf-8
# Here your code
N = int(input())
a = [int(i) for i in input().split()]
result_1 = 0
before_sum = a[0]
if a[0] <= 0:
before_sum = 1
result_1 += 1 + abs(a[0])
after_sum = before_sum
for i in range(1,N):
before_sum = after_sum
after_sum = before_sum + a[i]
if i % 2 == 0:
if after_sum <= 0:
result_1 += 1 + abs(after_sum)
after_sum = 1
else:
if after_sum >= 0:
result_1 += 1 + abs(after_sum)
after_sum = -1
result_2 = 0
before_sum = a[0]
if a[0] >= 0:
before_sum = -1
result_2 += 1 + abs(a[0])
after_sum = before_sum
for i in range(1,N):
before_sum = after_sum
after_sum = before_sum + a[i]
if i % 2 == 1:
if after_sum >= 0:
result_2 += 1 + abs(after_sum)
after_sum = -1
else:
if after_sum <= 0:
result_2 += 1 + abs(after_sum)
after_sum = 1
print(min(result_1,result_2))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> a(N);
for (auto &i : a) cin >> i;
int64_t sum = 0;
int64_t cnt = 0;
int sign = a.at(0) / abs(a.at(0));
for (int i = 0; i < N; i++) {
sum += a.at(i);
if (sign * sum <= 0) {
cnt += abs(sum) + 1;
sum = sign;
}
cout << sum << endl;
sign *= -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 | UNKNOWN | 'use strict'
let input = require("fs").readFileSync("/dev/stdin", "utf8");
let Nums = input.split('\n');
let amount = Nums[0]*1;
let arr = Nums[1].split(" ").map(x => x*1);
// 最初がプラスかどうかの判定
let isFirstPlus = arr[0] > 0? true: false;
let sum = 0;
let ans = 0;
// とりあえず偶数で奇数がプラスの時に正常動作するものを書く
for(let i = 0; i < amount; i++){
sum += arr[i];
if((sum > 0) != isFirstPlus){
// 不備の時の処理(+1なのは0からどちらかに1つ増やしたいから)
ans += Math.abs(sum) + 1;
// 場合わけでsumを1か-1に戻す処理
if(sum >= 0){
sum = -1;
} else {
sum = 1;
}
} else if(sum == 0) {
ans += 1;
if(isFirstPlus){
sum = 1;
}else {
sum = -1;
}
}
// 偶奇で判定を逆転する
isFirstPlus = !isFirstPlus;
}
if(sum == 0){
ans += 1;
}
console.log(ans);
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vs = vector<string>;
using vll = vector<long long int>;
const int MOD = 1e9 + 7;
int main() {
int n;
cin >> n;
vll a(n + 1);
for (int i = 0; i < n; i++) cin >> a[i];
vll sum(n + 1);
sum[0] = a[0];
long long int even = 0;
long long int odd = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
if (sum[i] <= 0) {
even += 1 - sum[i];
sum[i] = 1;
}
} else if (i % 2 == 1) {
if (sum[i] >= 0) {
even += 1 + sum[i];
sum[i] = -1;
}
}
sum[i + 1] = sum[i] + a[i + 1];
}
vll summ(n + 1);
summ[0] = a[0];
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
if (summ[i] >= 0) {
odd += 1 + summ[i];
summ[i] = -1;
}
} else if (i % 2 == 1) {
if (summ[i] <= 0) {
odd += 1 - summ[i];
summ[i] = 1;
}
}
summ[i + 1] = sum[i + 1] + a[i + 1];
}
long long int ans = min(odd, even);
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()))
b = a
#1 = plus
cp = 0
cm = 0
sumscp = 0
sumscm = 0
for i in range(len(a)):
if i % 2 == 0:
while sumscp + a[i] < 1:
a[i] = a[i] + 1
cp = cp + 1
sumscp = sumscp + a[i]
else:
while sumscp + a[i] > -1:
a[i] = a[i] - 1
cp = cp + 1
sumscp = sumscp + a[i]
for i in range(len(b)):
if i % 2 == 0:
while sumscm + b[i] > -1:
b[i] = b[i] - 1
cm = cm + 1
sumscm = sumscm + b[i]
else:
while sumscm + a[i] < 1:
b[i] = b[i] + 1
cm = cm + 1
sumscm = sumscm + b[i]
print(min(cp,cm)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
long long sum = a[0];
long long cnt1 = 0, cnt2 = 0;
for (int i = 1; i < n; i++) {
long long nsum = sum + a[i];
if (sum > 0 && nsum < 0 || sum < 0 && nsum > 0) {
sum = nsum;
continue;
}
sum = (sum > 0 ? -1 : 1);
cnt1 += (nsum == 0 ? 1 : abs(nsum) + 1);
}
for (int i = 1; i < n; i++) {
long long nsum = sum + a[i];
if (sum > 0 && nsum < 0 || sum < 0 && nsum > 0) {
sum = nsum;
continue;
}
sum = (sum < 0 ? -1 : 1);
cnt2 += (nsum == 0 ? 1 : abs(nsum) + 1);
}
cout << min(cnt1, cnt2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
#define forx(i,a,b) for(int i=(a);i<(b);i++)
#define rep(j,n) for(int j=0;j<(n);j++)
typedef long long ll;
int main()
{
int n,ansa=0,ansb=0,suma=0,sumb=0;
cin>>n;
rep(i,n){
int a,b;
cin>>b;
a=b;
if(i%2==0){
if(suma+a<=0){
ansa=1-a-suma
a=1-suma;
}
if(sumb+b>=0){
ansb=sumb+b+1;
b=-1-sumb;
}
}
else{
if(suma+a>=0){
ansa=suma+a+1;
a=-1-suma;
}
if(sumb+b<=0){
ansb=1-b-sumb;
b=1-sumb;
}
}
suma+=a;
sumb+=b;
}
cout<<min(ansa,ansb)<<endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int change_num(long p[], int N) {
int res = 0;
long sum = p[0];
for (int i = 1; i < N; i++) {
if (sum * (sum + p[i]) < 0) {
sum += p[i];
continue;
}
if (sum > 0 && sum + p[i] >= 0) {
sum += p[i];
while (sum >= 0) {
res++;
sum--;
}
continue;
}
if (sum < 0 && sum + p[i] <= 0) {
sum += p[i];
while (sum <= 0) {
res++;
sum++;
}
continue;
}
}
return res;
}
int main() {
int N;
cin >> N;
long a[N];
for (int i = 0; i < N; i++) cin >> a[i];
int ans = 0;
long sum = a[0];
if (a[0] == 0) {
int plus_ans;
a[0] = 1;
plus_ans = change_num(a, N) + 1;
int minus_ans = 1;
a[0] = -1;
minus_ans = change_num(a, N) + 1;
if (plus_ans < minus_ans) {
ans = plus_ans;
} else {
ans = minus_ans;
}
} else {
ans = change_num(a, N);
}
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 <cstdio>
using namespace std;
int main(){
long double n;
scanf("%Lf", &n);
long double a[n];
for (int i = 0; i < n; i++) scanf(" %Lf", &a[i]);
int S = a[0];
int j = 0;
for (int i = 1; i < n; i++){
if (S * (S+a[i]) < 0){
S += a[i];
}
else {
if (S < 0){
j += 1 - S - a[i];
S = 1;
} else{
j += S + a[i] + 1;
S = -1;
}
}
}
printf("%d\n", j);
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | # -*- coding:utf-8 -*-
def solve():
N = int(input())
A = list(map(int, input().split()))
ans = 0
"""
(1) A = [0, 0, 0, 0, 0, 100, ...] みたいなときは
A = [-1, 2, -2, 2, -2, 100, ...] にしたい
(2) A = [0, 0, 0, 0, 100, ...] みたいなときは
A = [1, -2, 2, -2, 100, ...] にしたい
"""
if A[0] == 0:
hugou = 1 # 最後の符号(1:+, -1:-)
for i in range(1, N):
if A[i] == 0:
continue
if A[i] > 0:
hugou = 1
break
else:
hugou = -1
break
for j in range(i-1, 0, -1):
if hugou == -1:
A[j] = 2
else:
A[j] = -2
hugou *= -1
ans += 2
if hugou > 0:
A[0] = -1
else:
A[0] = 1
ans += 1
# ruiseki[i] := i番目までの累積和
ruiseki = [0] * N
ruiseki[0] = A[0]
for i in range(1, N):
i_sum = A[i] + ruiseki[i-1]
if ruiseki[i-1] > 0:
# ruiseki[i]をマイナス値にする必要がある
if i_sum < 0:
ruiseki[i] = i_sum
else:
diff = abs(i_sum - (-1))
ans += diff
A[i] -= diff
i_sum -= diff
ruiseki[i] = i_sum
elif ruiseki[i-1] < 0:
# ruiseki[i]をプラス値にする必要がある
if i_sum > 0:
ruiseki[i] = i_sum
else:
diff = abs(i_sum - 1)
ans += diff
A[i] += diff
i_sum += diff
ruiseki[i] = i_sum
print(ans)
if __name__ == "__main__":
solve()
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | package main
import (
"bufio"
"fmt"
"os"
"strconv"
)
func out(x ...interface{}) {
// fmt.Println(x...)
}
var sc = bufio.NewScanner(os.Stdin)
func getInt() int {
sc.Scan()
i, e := strconv.Atoi(sc.Text())
if e != nil {
panic(e)
}
return i
}
func getString() string {
sc.Scan()
return sc.Text()
}
func sign(a int) int {
if a > 0 {
return 1
} else if a < 0 {
return -1
}
return 0
}
func main() {
sc.Split(bufio.ScanWords)
n := getInt()
a := make([]int, n)
for i := 0; i < n; i++ {
a[i] = getInt()
}
sum0 := 0
sum1 := a[0]
ans := 0
for i := 1; i < n; i++ {
sum0 += a[i-1]
sum1 += a[i]
s0 := sign(sum0)
s1 := sign(sum1)
out(a, "i", i, "sum", sum0, sum1, "sign", s0, s1, ans)
if s1 == 0 {
if s0 == -1 {
a[i]++
sum1++
ans++
} else {
a[i]--
sum1--
ans++
}
out("same", ans)
}
if s0 == s1 {
if s1 == -1 {
ans += 1 - sum1
a[i] += 1 - sum1
sum1 = 1
} else {
ans += 1 + sum1
a[i] -= 1 + sum1
sum1 = -1
}
out("modify", a, "i", i, "sum", sum0, sum1, "ams", ans)
}
}
fmt.Println(ans)
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n=int(input())
a=list(map(int,input().split()))
import sys
sum=a[0]
cnt=0
if a[0]==0:
sum+=1
cnt+=1
for i in range(1,n):
if sum<0:
z=sum+a[i]
if z>0:
sum=z
elif z<0:
cnt+=(1-z)
sum=1
else:
if sum>0:
sum=-1
cnt+=1
elif sum<0:
sum=1
cnt+=1
elif sum>0:
z=sum+a[i]
if z>0:
cnt+=(z+1)
sum=-1
elif z<0:
sum=z
else:
if sum>0:
sum=-1
cnt+=1
elif sum<0:
sum=1
cnt+=1
cnt_plus=cnt
sum=-1
cnt=1
for i in range(1,n):
if sum<0:
z=sum+a[i]
if z>0:
sum=z
elif z<0:
cnt+=(1-z)
sum=1
else:
if sum>0:
sum=-1
cnt+=1
elif sum<0:
sum=1
cnt+=1
elif sum>0:
z=sum+a[i]
if z>=0:
cnt+=(z+1)
sum=-1
elif z<0:
sum=z
else:
if sum>0:
sum=-1
cnt+=1
elif sum<0:
sum=1
cnt+=1
cnt_sbst=cnt
print(min(cnt_plus,cnt_sbst))
sys.exit()
for i in range(1,n):
if sum<0:
z=sum+a[i]
if z>0:
sum=z
elif z<0:
cnt+=(1-z)
sum=1
else:
if sum>0:
sum=-1
cnt+=1
elif sum<0:
sum=1
cnt+=1
elif sum>0:
z=sum+a[i]
if z>0:
cnt+=(z+1)
sum=-1
elif z<0:
sum=z
else:
if sum>0:
sum=-1
cnt+=1
elif sum<0:
sum=1
cnt+=1
print(cnt)
# 6
# 0 0 -1 3 5 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 minuss(int n, vector<int> a) {
long long x = 0;
long long sum = 0;
long long counter = 0;
while (x < n) {
if (x % 2 == 0 && sum + a.at(x) >= 0) {
long long olda = a.at(x);
a.at(x) = -1 - sum;
counter += abs(a.at(x) - olda);
}
if (x % 2 == 1 && sum + a.at(x) <= 0) {
long long olda = a.at(x);
a.at(x) = 1 - sum;
counter += abs(a.at(x) - olda);
}
sum += a.at(x);
x++;
}
return counter;
}
int pluss(int n, vector<int> a) {
long long x = 0;
long long sum = 0;
long long counter = 0;
while (x < n) {
if (x % 2 == 0 && sum + a.at(x) <= 0) {
long long olda = a.at(x);
a.at(x) = 1 - sum;
counter += abs(a.at(x) - olda);
}
if (x % 2 == 1 && sum + a.at(x) >= 0) {
long long olda = a.at(x);
a.at(x) = -1 - sum;
counter += abs(a.at(x) - olda);
}
sum += a.at(x);
x++;
}
return counter;
}
int main() {
long long n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a.at(i);
}
cout << min(minuss(n, a), pluss(n, a)) << 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 sum = 0, prev = 0;
long long int ans = 0;
for (int i = 0; i < n; i++) {
long long int a;
cin >> a;
sum += a;
if (prev > 0) {
if (sum >= 0) {
ans += abs(sum) + 1;
sum = -1;
}
} else if (prev < 0) {
if (sum <= 0) {
ans += abs(sum) + 1;
sum = 1;
}
}
prev = sum;
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n=int(input())
A=list(map(int,input().split()))
ans=10**9
for t in range(2):
a=0
s=0
for i in range(n):
a+=A[i]
if t and a<=0:
s+=-a+1
a=1
t=0
elif not(t) and a>=0:
s+=a+1
a=-1
t=1
else:
t=1-t
ans=min(s,ans)
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long linf = 1001002003004005006ll;
const int inf = 1001001001;
const int mod = 1000000007;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < (n); ++i) cin >> a[i];
long long tot = 0;
long long res1 = 0;
{
for (int i = 0; i < (n); ++i) {
if (i % 2 == 0) {
if (tot + a[i] > 0)
tot += a[i];
else {
res1 += 1 - tot - a[i];
tot = 1;
}
} else {
if (tot + a[i] < 0)
tot += a[i];
else {
res1 += 1 + tot + a[i];
tot = -1;
}
}
}
}
tot = 0;
long long res2 = 0;
{
for (int i = 0; i < (n); ++i) {
if (i % 2 != 0) {
if (tot + a[i] > 0)
tot += a[i];
else {
res2 += 1 - tot - a[i];
tot = 1;
}
} else {
if (tot + a[i] < 0)
tot += a[i];
else {
res2 += 1 + tot + a[i];
tot = -1;
}
}
}
}
int ans = min(res1, res2);
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = [int(ai) for ai in input().split()]
count = 0
a_sum = 0
for i, ai in enumerate(a):
if i == 0:
a_sum = ai
else:
tmp_sum = a_sum + ai
if tmp_sum < 0 and a_sum < 0:
c = +1 - tmp_sum
a_sum = 1
elif tmp_sum > 0 and a_sum > 0:
c = -1 - tmp_sum
a_sum = -1
elif tmp_sum == 0 and a_sum < 0:
c = +1
a_sum = 1
elif tmp_sum == 0 and a_sum > 0:
c = -1
a_sum = 1
else:
c = 0
count += abs(c)
a_sum = tmp_sum + c
print(count)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | public class Main {
private static java.util.Scanner scanner = new java.util.Scanner(System.in);
public static void main(String[] args) {
int n = scanner.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = scanner.nextInt();
long m1 = 0, m2 = 0, s1 = 0, s2 = 0;
for (int i = 0; i < n; i++) {
s1 += a[i];
s2 += a[i];
if ((i & 1) == 0) {
if (s1 <= 0) {
m1 += Math.abs(s1) + 1;
s1 = 1;
} if (s2 >= 0) {
m2 += Math.abs(s2) + 1;
s2 = -1;
}
} else {
if (s1 >= 0) {
m1 += Math.abs(s1) + 1;
s1 = 1;
} if (s2 <= 0) {
m2 += Math.abs(s2) + 1;
s2 = -1;
}
}
}
System.out.println(Math.min(m1, m2));
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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()))
ruisekiwa = []
tmp = 0
for x in a:
tmp += x
ruisekiwa.append(tmp)
ans = cnt = 0
for x, y in zip(ruisekiwa, ruisekiwa[1:]):
if (x == abs(x) and y != abs(y)) or (x != abs(x) and y == abs(y)):
cnt += 1
else:
ans += 1
if len(ruisekiwa) - 1 == cnt:
print('0')
else:
print(ans*2)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int f(int isum, vector<int>& a, int n, int ans) {
for (int i = 1; i < n; i++) {
int temp = isum;
isum += a[i];
if (temp > 0) {
if (isum >= 0) {
ans += (1 + isum);
isum = -1;
}
} else if (temp < 0) {
if (isum <= 0) {
ans += (1 + abs(isum));
isum = 1;
}
}
}
return ans;
}
void solve() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int p = 1;
int ne = -1;
int pans = 1;
int nans = 1;
if (a[0] > 0) {
p = a[0];
nans = a[0] + 1;
pans--;
} else if (a[0] < 0) {
ne = a[0];
nans--;
pans = abs(a[0]) + 1;
}
cout << min(f(p, a, n, pans), f(ne, a, n, nans));
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
solve();
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int64_t d[n];
for (int i = 0; i < n; i++) {
cin >> d[i];
}
int64_t 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;
}
}
}
cout << count << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | 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_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 | import copy
N=int(input())
l=list(map(int, input().split())) #リスト入力
cp = copy.copy(l)
#c=0
for k in range(1,N):
if sum(l[:k])==0:
#c=c+1
if l[k]>0:
l[k]=l[k]+1
else:
l[k]=l[k]-1
if sum(l[:k])*sum(l[:k+1])>0:
if sum(l[:k+1])>0:
l[k]=l[k]-(sum(l[:k+1])-(-1))
#c=c+abs(sum(l[:k+1])-(-1))
else:
l[k]=l[k]+(1-sum(l[:k+1]))
#c=c+abs(1-sum(l[:k+1]))
if sum(l)==0:
c=c+1
l[-1]=l[-1]+1
#print(l)
print(sum([abs(l[n]-cp[n]) for n in range(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()))
cusm = [a[0]]
ans = 0
for i, ai in enumerate(a):
if i == 0: continue
if (cusm[-1] + ai) * cusm[-1] < 0: #符号が逆なら
cusm.append(cusm[-1] + ai)
elif cusm[-1] > 0: #操作の必要があって前が正なら
ans += abs(-1 - (cusm[-1] + ai))
cusm.append(-1)
else: #操作の必要があって前が負なら
ans += abs(1 - (cusm[-1] + ai))
cusm.append(1)
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <bits/stdc++.h>
int main(int argc, char *argv[]) {
int i, n, sign;
long long a, c = 0, t = 0;
scanf("%d", &n);
scanf("%lld", &t);
sign = t > 0 ? -1 : 1;
for (i = 1; i < n; i++) {
scanf("%lld", &a);
if (sign < 0) {
if (t + a >= 0) {
c += llabs(-1 - t - a);
t = -1;
} else {
t += a;
}
} else {
if (t + a <= 0) {
c += llabs(1 - t - a);
t = 1;
} else {
t += a;
}
}
sign *= -1;
}
printf("%lld\n", c);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using i8 = int8_t;
using u8 = uint8_t;
using i16 = int16_t;
using u16 = uint16_t;
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
template <class T>
using V = vector<T>;
namespace tuple_utils {
template <size_t...>
struct seq {};
template <size_t N, size_t... Is>
struct gen_seq : gen_seq<N - 1, N - 1, Is...> {};
template <size_t... Is>
struct gen_seq<0, Is...> : seq<Is...> {};
template <class Tuple, size_t... Is>
void read(istream &stream, Tuple &t, seq<Is...>) {
static_cast<void>((int[]){0, (void(stream >> get<Is>(t)), 0)...});
}
template <class Tuple, size_t... Is>
void print(ostream &stream, Tuple const &t, seq<Is...>) {
static_cast<void>(
(int[]){0, (void(stream << (Is == 0 ? "" : ", ") << get<Is>(t)), 0)...});
}
} // namespace tuple_utils
template <class F, class S>
istream &operator>>(istream &stream, pair<F, S> &pair) {
stream >> pair.first;
stream >> pair.second;
return stream;
}
template <class... Args>
istream &operator>>(istream &stream, tuple<Args...> &tuple) {
tuple_utils::read(stream, tuple, tuple_utils::gen_seq<sizeof...(Args)>());
return stream;
}
template <class T>
T read() {
T t;
cin >> t;
return t;
}
template <class F, class S>
pair<F, S> read() {
pair<F, S> p;
cin >> p;
return p;
}
template <class T1, class T2, class T3, class... Args>
tuple<T1, T2, T3, Args...> read() {
tuple<T1, T2, T3, Args...> t;
cin >> t;
return t;
}
template <class T>
V<T> read(const int length) {
V<T> ts(length);
for (auto &t : ts) {
cin >> t;
}
return ts;
}
template <class F, class S>
V<pair<F, S>> read(const int length) {
V<pair<F, S>> ps(length);
for (auto &p : ps) {
cin >> p;
}
return ps;
}
template <class T1, class T2, class T3, class... Args>
V<tuple<T1, T2, T3, Args...>> read(const int length) {
V<tuple<T1, T2, T3, Args...>> ts(length);
for (auto &t : ts) {
cin >> t;
}
return ts;
}
namespace debug {
template <class F, class S>
ostream &operator<<(ostream &stream, const pair<F, S> &pair) {
stream << "{" << pair.first << ", " << pair.second << "}";
return stream;
}
template <class... Args>
ostream &operator<<(ostream &stream, const tuple<Args...> &tuple) {
stream << "{";
tuple_utils::print(stream, tuple, tuple_utils::gen_seq<sizeof...(Args)>());
stream << "}";
return stream;
}
template <class T, class Alloc>
ostream &operator<<(ostream &stream, const vector<T, Alloc> &vector) {
stream << "[";
for (auto i = 0; i < vector.size(); i++) {
stream << vector[i];
if (i != vector.size() - 1) {
stream << "," << ((i % 10 == 9) ? "\n " : "\t");
}
}
stream << "]";
return stream;
}
} // namespace debug
void body() {
auto n = read<i32>();
auto as = read<i64>(n);
i64 total = 0;
u64 retval = 0;
for (auto &a : (as)) {
if (total != 0 && total * (total + a) >= 0) {
retval += abs(total + a) + 1;
a -= (total + a);
if (total > 0) {
a -= 1;
} else {
a += 1;
}
}
total += a;
}
cout << retval << endl;
}
int main() {
ios_base::sync_with_stdio(false);
body();
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = [int(i) for i in input().split()]
ans = 0
tmp = a[0]
if a[0] == 0:
tmp = 1
ans += 1
for i in range(1,n):
#print(tmp,ans)
if tmp > 0:
if tmp + a[i] >= 0:
ans += tmp + a[i] + 1
tmp = -1
else:
tmp += a[i]
else:
if tmp + a[i] <= 0:
ans += abs(tmp + a[i]) + 1
tmp = 1
else:
tmp += a[i]
#print(ans)
ans2 = 0
if a[0] > 0:
ans2 += a[0]+1
tmp = -1
elif a[0] < 0:
ans2 += -a[0]+1
else:
tmp = 1
ans2 += 1
for i in range(1,n):
#print(tmp,ans)
if tmp > 0:
if tmp + a[i] >= 0:
ans2 += tmp + a[i] + 1
tmp = -1
else:
tmp += a[i]
else:
if tmp + a[i] <= 0:
ans2 += abs(tmp + a[i]) + 1
tmp = 1
else:
tmp += a[i]
print(min(ans,ans2))
exit()
ans2 = 0
tmp = -a[0]
ans2 += abs(a[0])*2
for i in range(1,n):
#print(tmp,ans)
if tmp > 0:
if tmp + a[i] >= 0:
ans2 += tmp + a[i] + 1
tmp = -1
else:
tmp += a[i]
else:
if tmp + a[i] <= 0:
ans2 += abs(tmp + a[i]) + 1
tmp = 1
else:
tmp += a[i]
print(min(ans,ans2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n + 1];
for (int i = 0; i < n; i++) cin >> a[i];
int sum = a[0];
int c = sum > 0 ? -1 : 1;
int ans = 0;
for (int i = 1; i < n; i++) {
sum += a[i];
if (sum * c < 1) {
ans += c * (c - sum);
sum = c;
}
c *= -1;
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int sign(long long int num) {
if (num == 0) {
return 0;
} else {
return num / abs(num);
}
}
int main() {
int n;
cin >> n;
vector<long long int> list(n);
for (int i = 0; i < n; i++) {
cin >> list.at(i);
}
long long int count = 0, sum = 0;
bool flag = true;
for (int i = 0; i < n; i++) {
if (flag) {
sum = list.at(i);
if (sum == 0) {
if (i == 0) {
count += 1;
} else {
count += 2;
}
} else {
flag = false;
if (i != 0) {
sum = sign(sum) * (abs(sum) - 1);
}
}
} else {
long long int temp_sum = sum;
sum += list.at(i);
if (sign(sum) * sign(temp_sum) >= 0) {
count += abs(sum) + 1;
if (temp_sum > 0) {
sum = -1;
} else {
sum = 1;
}
}
}
}
cout << count << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;
int d[n];
for(int i=0;i<n;i++) {
cin >> d[i];
}
int count=0;
int sum=d[0];
int f =0;
if(d[0]>0){
f=-1;
}
if(d[0]<0){
f=1;
}
for(int i=1;i<n;i++){
sum+=d[i];
if(sum==0){
if(f==1){
count++;
f=-1;
sum=1;
continue;
}
if(f==-1){
count++;
f=1;
sum=-1;
continue;
}
}
if(sum>0){
if(f==1){
f=-1;
continue;
}
if(f==-1){
count+=sum+1;
sum=-1;
f=1;
continue;
}
}
if(sum<0){
if(f==-1){
f=1;
continue;
}
if(f==1){
count+=1-sum;
sum=1;
f=-1;
continue;
}
}
}
int ccount=0;
int ssum;
int ff =0;
if(d[0]>0){
ff=1;
ccount=1+d[0];
ssum=-1;
}
if(d[0]<0){
ff=-1;
ccount=1-d[0];
ssum=1;
}
for(int i=1;i<n;i++){
ssum+=d[i];
if(ssum==0){
if(ff==1){
ccount++;
ff=-1;
ssum=1;
continue;
}
if(ff==-1){
ccount++;
ff=1;
ssum=-1;
continue;
}
}
if(ssum>0){
if(ff==1){
ff=-1;
continue;
}
if(ff==-1){
ccount+=ssum+1;
ssum=-1;
ff=1;
continue;
}
}
if(ssum<0){
if(ff==-1){
ff=1;
continue;
}
if(ff==1){
ccount+=1-ssum;
ssum=1;
ff=-1;
continue;
}
}
}
int s= min(count,ccount)
cout << s << endl;
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
arr = list(map(int,input().split()))
def S1(a,b):
c,last,ans = 0,0,0
for t in range(N):
c += 1
if c % 2 == a:
while last+arr[t] <= 0:
ans += 1
last += 1
elif c % 2 == b:
while last +arr[t] >= 0:
ans +=1
last -= 1
last += arr[t]
return ans
aa = S1(1,0)
bb = S1(0,1)
print(min(aa,bb)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
s = a[0]
count = 0
total_delta = 0
delta = 0
if a[0] == 0:
i = 0
while i < len(a):
if a[i] != 0:
break
i += 1
if i == len(a):
delta = 1
total_delta += delta
count += 1
else:
delta = -1 * (a[i] // abs(a[i])) * ((1, -1)[(i + 1) % 2])
total_delta += delta
count += 1
for i in range(1, n):
sign = (s + total_delta) // abs(s + total_delta)
if (s + a[i] + total_delta) * sign > 0 :
delta = (sign * -1) - (s + a[i] + total_delta)
total_delta += delta
count += abs(delta)
elif (s + a[i] + total_delta) == 0:
total_delta += sign * -1
count += 1
s += a[i]
print(count)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<sstream>
#include<cmath>
#include<numeric>
#include<map>
#include<stack>
#include<queue>
using namespace std;
int inf = 1e9;
int main() {
int n; cin >> n;
vector<int> a(n);
for(int i=0; i<n; i++) cin >> a[i];
int ans = inf;
long long int tmp = 0;
long long int cnt = 0;
for(int i=0; i<n; i++){
tmp += a[i];
if( i % 2 == 0 && tmp <= 0 ){
cnt += (1 - tmp);
tmp = 1;
}else if( i % 2 == 1 && tmp >= 0 ){
cnt += (1 + tmp);
tmp = -1;
}
}
ans = min(ans, cnt);
tmp = 0;
cnt = 0;
for(int i=0; i<n; i++){
tmp += a[i];
if( i % 2 == 1 && tmp <= 0 ){
cnt += (1 - tmp);
tmp = 1;
}else if( i % 2 == 0 && tmp >= 0 ){
cnt += (1 + tmp);
tmp = -1;
}
}
ans = min(ans, cnt);
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import itertools
def sign(num):
if num < 0:
return -1
elif num > 0:
return 1
else:
return 0
N = input()
a_i = list(map(int, input().split()))
signs = [-1,1]
a_sum = 0
changes_1 = 0
for i, a in enumerate(a_i):
a_sum += a
if sign(a_sum) != signs[i%2]:
changes += abs(a_sum) + 1
a_sum = signs[i%2]
signs = [1,-1]
a_sum = 0
changes_2 = 0
for i, a in enumerate(a_i):
a_sum += a
if sign(a_sum) != signs[i%2]:
changes += abs(a_sum) + 1
a_sum = signs[i%2]
print(min(changes_1,changes_2))
#
# for i, sum_i in enumerate(a_sum):
# if i == 0:
# signs = [sign(sum_i), -sign(sum_i)]
# elif sign(sum_i) != signs[i%2]:
# a_sum[i:] = [num + (abs(sum_i) + 1) * signs[i%2] for num in a_sum[i:]]
# changes += abs(sum_i) + 1
# # print(a_sum)
# print(changes)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
const int mod = 1e9 + 7;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
long long a[n];
for (int i = 0; i < n; i++) cin >> a[i];
long long s_i = a[0];
long long s_i_1;
long long d = 0;
long long c = 0;
for (int i = 1; i < n; i++) {
if (s_i == 0) {
s_i += 1;
c++;
}
s_i_1 = s_i + a[i];
if ((s_i_1 > 0 && s_i > 0) || (s_i < 0 && s_i_1 < 0)) {
d = abs(s_i_1 - s_i);
if (s_i > 0) {
if (s_i_1 != 0) {
s_i_1 -= d + 1;
c += d + 1;
} else {
s_i_1 -= 1;
d += 1;
}
} else {
if (s_i_1 != 0) {
s_i_1 += d + 1;
c += d + 1;
} else {
s_i_1 += 1;
c += 1;
}
}
}
s_i = s_i_1;
}
if (s_i == 0) {
c += 1;
}
cout << c << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll ts = 1000000007;
ll sum, sum2, ans, i;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
ll n;
cin >> n;
vector<ll> a(n);
for (ll i = 0; i < n; i++) cin >> a[i];
bool can = false;
ll ans = 0, sum = a[0], nextSum = a[0];
for (int i = 1; i < n; i++) {
nextSum += a[i];
if (sum < 0 && nextSum < 0 || sum > 0 && nextSum > 0 || nextSum == 0) {
ll N;
if (nextSum >= 0) N = nextSum + 1;
if (nextSum < 0) N = nextSum - 1;
ans += abs(N);
if (a[0] >= 0 && i % 2 == 1 || a[0] <= 0 && i % 2 == 0)
nextSum = -1;
else
nextSum = 1;
sum = nextSum;
} else {
sum = nextSum;
}
}
cout << ans << "\n";
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | 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];
}
assert(a[0] != 0);
long long op = 0LL;
long long sum = 0LL;
sum = a[0];
for (int i = 1; i < n; i++) {
if (!(sum * (sum + a[i]) < 0)) {
long long tmp_a = sum < 0 ? abs(sum) + 1 : -1 * (abs(sum) + 1);
op += abs(tmp_a - a[i]);
sum = sum + tmp_a;
} else {
sum += a[i];
}
}
long long op_m = op;
if (a[0] > 0) {
sum = -1LL;
op = a[0] + 1;
} else {
sum = 1LL;
op = -1 * a[0] + 1;
}
for (int i = 1; i < n; i++) {
if (!(sum * (sum + a[i]) < 0)) {
long long tmp_a = sum < 0 ? abs(sum) + 1 : -1 * (abs(sum) + 1);
op += abs(tmp_a - a[i]);
sum = sum + tmp_a;
} else {
sum += a[i];
}
}
op = min(op, op_m);
cout << op << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a;
for (int i = 0; i < n; i++) {
int num;
cin >> num;
a.push_back(num);
}
int sumPlus = 0, sumMinus = 0;
int ansPlus = 0, ansMinus = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
if (sumPlus + a[i] <= 0) {
ansPlus += abs(sumPlus + a[i]) + 1;
sumPlus = 1;
} else
sumPlus += a[i];
if (sumMinus + a[i] >= 0) {
ansMinus += abs(sumMinus + a[i]) + 1;
sumMinus = -1;
} else
sumMinus += a[i];
} else {
if (sumPlus >= 0 && sumPlus + a[i] >= 0) {
ansPlus += abs(sumPlus + a[i]) + 1;
sumPlus = -1;
} else
sumPlus += a[i];
if (sumMinus <= 0 && sumMinus + a[i] <= 0) {
ansMinus += abs(sumMinus + a[i]) + 1;
sumMinus = 1;
} else
sumMinus += a[i];
}
}
cout << min(ansPlus, ansMinus) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long MOD = (1e+9) + 7;
const long long INF = 2e+9 + 10;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
long long res = 0;
if (a[0] == 0) {
a[0]++;
res++;
}
long long sum = a[0];
bool sign = sum > 0;
for (int i = 1; i < n; i++) {
sum += a[i];
if (sign && sum >= 0) {
res += (sum + 1);
sum -= (sum + 1);
} else if (!sign && sum <= 0) {
res += abs(sum) + 1;
sum += abs(sum) + 1;
}
sign = sum > 0;
}
cout << res << "\n";
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | n = gets.to_i
A = gets.split.map(&:to_i)
x = A[0]
answer = 0
for i in 0..n-2
s = x + A[i+1]
if x * s >= 0
if x > 0
answer = answer - s + 1
A[i+1] = A[i+1] - s + 1
else
answer = answer + s + 1
A[i+1] = A[i+1] - s - 1
end
end
x = x + A[i+1]
end
puts answer |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import numpy as np
input()
a = list(map(int, input().split()))
value = a[0]
sign = value >= 0
result = 0
for i in a[1:]:
value += i
diff = 0
if sign and value >= 0:
diff = value + 1
elif not sign and value < 0:
diff = value - 1
result += abs(diff)
sign = not sign
value -= diff
if value == 0:
diff += sign
value += diff
result += diff
print(result)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -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 calc(bool firstPositive, int a[], int n) {
bool positive = firstPositive;
int cost = 0, sum = 0;
for (int i = 0; i < n; ++i) {
bool sumpos = (sum + a[i]) >= 0;
if (sumpos != positive) {
while (((sum + a[i]) >= 0) == sumpos) {
a[i] += sumpos ? -1 : 1;
++cost;
}
}
if ((sum + a[i]) == 0) {
a[i] += sumpos ? -1 : 1;
++cost;
}
sum += a[i];
positive = !positive;
}
return cost;
}
int main(int argc, char *argv[]) {
int n;
std::cin >> n;
int a[1 << 11], b[1 << 11];
for (int i = 0; i < n; ++i) {
std::cin >> a[i];
b[i] = a[i];
}
std::cout << std::min(calc(true, a, n), calc(false, b, n)) << std::endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
long long sum = a[0];
long long ans = 0;
for (int i = 1; i < n; i++) {
if (sum + a[i] == 0) {
ans++;
if (sum > 0)
sum = -1;
else
sum = 1;
} else {
if (sum > 0 && sum + a[i] > 0) {
sum += a[i];
ans += -(-1 - sum);
sum = -1;
} else if (sum < 0 && sum + a[i] < 0) {
sum += a[i];
ans += 1 - sum;
sum = 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 | java | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[]a =new int[n];
int[]sig = new int[n];
int[]acopy = new int[n];
for(int i=0;i<n;i++){
a[i]=scan.nextInt();
acopy[i]=a[i];
/* if(i==0){
siga[i]=a[i];
}else{
siga[i]=siga[i-1]+a[i];
}*/
}
//第一項目が正の場合と負の場合
//a[0]が0の場合を考える
long cnt=0;
if(a[0]==0){
a[0]++;
cnt++;
}
sig[0]=a[0];
for(int i=1;i<n;i++){
sig[i]=sig[i-1]+a[i];
//積が0なら符号が異なるように+-1
if(sig[i]*sig[i-1]==0){
if(sig[i-1]>0){
a[i]--;
cnt++;
}else{
a[i]++;
cnt++;
}
sig[i]=sig[i-1]+a[i];
}
//積が負なら条件を満たしているのでok
else if(sig[i]*sig[i-1]<0){
}
//積が正なら負になるまで変える
else{
if(sig[i-1]>0){
a[i]-=(sig[i]+1);
cnt+=sig[i]+1;
}else{
a[i]+=-sig[i]+1;
cnt+=-sig[i]+1;
}
sig[i]=sig[i-1]+a[i];
}
}
long cnt2=0;
if(acopy[0]==0){
acopy[0]--;
cnt2++;
}else if(acopy[0]>0){
cnt2 +=acopy[0]+1;
acopy[0]=-1;
}else{
cnt2+=-acopy[0]+1;
acopy[0]=1;
}
sig[0]=acopy[0];
for(int i=1;i<n;i++){
sig[i]=sig[i-1]+acopy[i];
//積が0なら符号が異なるように+-1
if(sig[i]*sig[i-1]==0){
if(sig[i-1]>0){
acopy[i]--;
cnt2++;
}else{
acopy[i]++;
cnt2++;
}
sig[i]=sig[i-1]+acopy[i];
}
//積が負なら条件を満たしているのでok
else if(sig[i]*sig[i-1]<0){
}
//積が正なら負になるまで変える
else{
if(sig[i-1]>0){
acopy[i]-=(sig[i]+1);
cnt2+=sig[i]+1;
}else{
acopy[i]+=-sig[i]+1;
cnt2+=-sig[i]+1;
}
sig[i]=sig[i-1]+acopy[i];
}
}
System.out.println(Math.min(cnt, cnt2));
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, ans = 0, d = 0;
cin >> n;
int a[n], sum[n];
cin >> a[0];
sum[0] = a[0];
for (int i = 1; i < n; i++) {
cin >> a[i];
sum[i] = sum[i - 1] + a[i];
}
for (int i = 1; i < n; i++) {
if ((sum[i - 1] + d) * (sum[i] + d) >= 0) {
ans += abs(sum[i] + d) + 1;
if (sum[i - 1] + d < 0)
d -= sum[i] + d - 1;
else
d -= sum[i] + d + 1;
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void) {
long long n;
cin >> n;
long long a[n];
long long even = 0;
for (long long i = 0; i < n; i++) {
cin >> a[i];
even += i % 2 == 0 ? abs(a[i]) : -1 * abs(a[i]);
}
long long sum = 0;
long long ans = 0;
for (long long i = 0; i < n; i++) {
if (sum == 0) {
if (a[i] == 0) {
sum += even < 0 ? -1 : 1;
ans++;
} else {
sum += a[i];
}
} else if (sum > 0) {
if (sum + a[i] >= 0) {
ans += abs(sum + a[i]) + 1;
sum = -1;
} else {
sum += a[i];
}
} else {
if (sum + a[i] <= 0) {
ans += abs(sum + a[i]) + 1;
sum = 1;
} else {
sum += a[i];
}
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n;
int func(vector<long long> a, int fugo) {
long long ans = 0;
long long offset = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == fugo) {
if (a[i] <= offset) {
ans += offset - (a[i] - 1);
offset = a[i] - 1;
}
} else {
if (a[i] >= offset) {
ans += (a[i] + 1) - offset;
offset = a[i] + 1;
}
}
}
return ans;
}
int main() {
cin >> n;
vector<long long> a;
int sum_tmp = 0;
for (int i = 0; i < n; i++) {
int tmp;
cin >> tmp;
sum_tmp += tmp;
a.push_back(sum_tmp);
}
int ans = min(func(a, 0), func(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 | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
long long l1[n + 1];
long long x = 0, s = 0;
for (int i = 1; i <= n; i++) {
cin >> l1[i];
x += l1[i];
if (i == 1 && l1[i] == 0 && l1[i + 1] <= 0)
x++, s++, l1[i] = 1;
else if (i == 1 && l1[i] == 0 && l1[i + 1] > 0)
x--, s++, l1[i] = -1;
if (i >= 2) {
if (x - l1[i] <= 0 && x <= 0) {
s += abs((-x + l1[i] + 1) - l1[i]);
l1[i] = l1[i] - x + 1;
x = 1;
} else if (x - l1[i] >= 0 && x >= 0) {
s += abs(-(x - l1[i] + 1) - l1[i]);
l1[i] = -(x - l1[i] + 1);
x = -1;
}
}
}
cout << s << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
inline long long toLongLong(string s) {
long long v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T>
inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
inline vector<char> toVC(string s) {
vector<char> data(s.begin(), s.end());
return data;
}
template <typename List>
void SPRIT(const std::string &s, const std::string &delim, List &result) {
result.clear();
string::size_type pos = 0;
while (pos != string::npos) {
string::size_type p = s.find(delim, pos);
if (p == string::npos) {
result.push_back(s.substr(pos));
break;
} else {
result.push_back(s.substr(pos, p - pos));
}
pos = p + delim.size();
}
}
string TRIM(const string &str, const char *trimCharacterList = " \t\v\r\n") {
string result;
string::size_type left = str.find_first_not_of(trimCharacterList);
if (left != string::npos) {
string::size_type right = str.find_last_not_of(trimCharacterList);
result = str.substr(left, right - left + 1);
}
return result;
}
template <typename T>
bool VECTOR_EXISTS(vector<T> vec, T data) {
auto itr = std::find(vec.begin(), vec.end(), data);
size_t index = distance(vec.begin(), itr);
if (index != vec.size()) {
return true;
} else {
return 0;
}
}
double ceil_n(double dIn, int nLen) {
double dOut;
dOut = dIn * pow(10.0, nLen);
dOut = (double)(int)(dOut + 0.9);
return dOut * pow(10.0, -nLen);
}
double floor_n(double dIn, int nLen) {
double dOut;
dOut = dIn * pow(10.0, nLen);
dOut = (double)(int)(dOut);
return dOut * pow(10.0, -nLen);
}
double round_n(double dIn, int nLen) {
double dOut;
dOut = dIn * pow(10.0, nLen);
dOut = (double)(int)(dOut + 0.5);
return dOut * pow(10.0, -nLen);
}
int take_a_n(int num, int n) {
string str = toString(num);
return str[str.length() - n] - '0';
}
int strbase_2to10(const std::string &s) {
int out = 0;
for (int i = 0, size = s.size(); i < size; ++i) {
out *= 2;
out += ((int)s[i] == 49) ? 1 : 0;
}
return out;
}
int strbase_10to2(const std::string &s) {
int binary = toInt(s);
int out = 0;
for (int i = 0; binary > 0; i++) {
out = out + (binary % 2) * pow(static_cast<int>(10), i);
binary = binary / 2;
}
return out;
}
int strbase_16to10(const std::string &s) {
int out = stoi(s, 0, 16);
return out;
}
int intbase_2to10(int in) {
string str = toString(in);
return strbase_2to10(str);
}
int intbase_10to2(int in) {
string str = toString(in);
return strbase_10to2(str);
}
int intbase_16to10(int in) {
string str = toString(in);
return strbase_16to10(str);
}
string intbase_10to16(unsigned int val, bool lower = true) {
if (!val) return std::string("0");
std::string str;
const char hc = lower ? 'a' : 'A';
while (val != 0) {
int d = val & 15;
if (d < 10)
str.insert(str.begin(), d + '0');
else
str.insert(str.begin(), d - 10 + hc);
val >>= 4;
}
return str;
}
long long bitcount64(long long bits) {
bits = (bits & 0x5555555555555555) + (bits >> 1 & 0x5555555555555555);
bits = (bits & 0x3333333333333333) + (bits >> 2 & 0x3333333333333333);
bits = (bits & 0x0f0f0f0f0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f0f0f0f0f);
bits = (bits & 0x00ff00ff00ff00ff) + (bits >> 8 & 0x00ff00ff00ff00ff);
bits = (bits & 0x0000ffff0000ffff) + (bits >> 16 & 0x0000ffff0000ffff);
return (bits & 0x00000000ffffffff) + (bits >> 32 & 0x00000000ffffffff);
}
const double EPS = 1e-10;
const double PI = acos(-1.0);
template <typename T>
inline bool BETWEEN(const T aim, const T min, const T max) {
if (min <= aim && aim <= max) {
return true;
} else {
return false;
}
}
template <class T>
inline T SQR(const T x) {
return x * x;
}
template <class T1, class T2>
inline T1 POW(const T1 x, const T2 y) {
if (!y)
return 1;
else if ((y & 1) == 0) {
return SQR(POW(x, y >> 1));
} else
return POW(x, y ^ 1) * x;
}
template <typename T>
constexpr T ABS(T x) {
static_assert(is_signed<T>::value, "ABS(): argument must be signed");
return x < 0 ? -x : x;
}
template <class BidirectionalIterator>
bool next_partial_permutation(BidirectionalIterator first,
BidirectionalIterator middle,
BidirectionalIterator last) {
reverse(middle, last);
return next_permutation(first, last);
}
template <class BidirectionalIterator>
bool next_combination(BidirectionalIterator first1, BidirectionalIterator last1,
BidirectionalIterator first2,
BidirectionalIterator last2) {
if ((first1 == last1) || (first2 == last2)) {
return false;
}
BidirectionalIterator m1 = last1;
BidirectionalIterator m2 = last2;
--m2;
while (--m1 != first1 && !(*m1 < *m2)) {
}
bool result = (m1 == first1) && !(*first1 < *m2);
if (!result) {
while (first2 != m2 && !(*m1 < *first2)) {
++first2;
}
first1 = m1;
std::iter_swap(first1, first2);
++first1;
++first2;
}
if ((first1 != last1) && (first2 != last2)) {
m1 = last1;
m2 = first2;
while ((m1 != first1) && (m2 != last2)) {
std::iter_swap(--m1, m2);
++m2;
}
std::reverse(first1, m1);
std::reverse(first1, last1);
std::reverse(m2, last2);
std::reverse(first2, last2);
}
return !result;
}
template <typename T>
constexpr bool ODD(T x) {
return x % 2 != 0;
}
template <typename T>
constexpr bool EVEN(T x) {
return x % 2 == 0;
}
template <class T>
inline T GCD(const T x, const T y) {
if (x < 0) return GCD(-x, y);
if (y < 0) return GCD(x, -y);
return (!y) ? x : GCD(y, x % y);
}
template <class T>
inline T LCM(const T x, const T y) {
if (x < 0) return LCM(-x, y);
if (y < 0) return LCM(x, -y);
return x * (y / GCD(x, y));
}
template <class T>
inline T EXTGCD(const T a, const T b, T &x, T &y) {
if (a < 0) {
T d = EXTGCD(-a, b, x, y);
x = -x;
return d;
}
if (b < 0) {
T d = EXTGCD(a, -b, x, y);
y = -y;
return d;
}
if (!b) {
x = 1;
y = 0;
return a;
} else {
T d = EXTGCD(b, a % b, x, y);
T t = x;
x = y;
y = t - (a / b) * y;
return d;
}
}
template <class T>
inline bool ISPRIME(const T x) {
if (x <= 1) return false;
for (T i = 2; SQR(i) <= x; i++)
if (x % i == 0) return false;
return true;
}
template <class T>
vector<bool> ERATOSTHENES(const T n) {
vector<bool> arr(n, true);
for (int i = 2; i < SQR(n); i++) {
if (arr[i]) {
for (int j = 0; i * (j + 2) < n; j++) {
arr[i * (j + 2)] = false;
}
}
}
return arr;
}
template <typename T>
vector<bool> ERATOSTHENES(const T a, const T b) {
vector<bool> small = ERATOSTHENES(b);
vector<bool> prime(b - a, true);
for (int i = 2; (T)(SQR(i)) < b; i++) {
if (small[i]) {
for (T j = max(2, (a + i - 1) / i) * i; j < b; j += i) {
prime[j - a] = false;
}
}
}
return prime;
}
template <class T>
vector<T> DIVISOR(T n) {
vector<T> v;
for (int i = 1; i * i <= n; ++i) {
if (n % i == 0) {
v.push_back(i);
if (i != n / i) {
v.push_back(n / i);
}
}
}
sort(v.begin(), v.end());
return v;
}
template <typename T>
T NCR(T n, T r) {
T ans = 1;
for (T i = n; i > n - r; --i) {
ans = ans * i;
}
for (T i = 1; i < r + 1; ++i) {
ans = ans / i;
}
return ans;
}
int MATRIZ_CHAIN(vector<int> &p, vector<vector<int> > &s) {
const static int INF = 1 << 20;
const int n = p.size() - 1;
vector<vector<int> > X(n, vector<int>(n, INF));
s.resize(n, vector<int>(n));
for (int i = 0; i < n; ++i) X[i][i] = 0;
for (int w = 1; w < n; ++w)
for (int i = 0, j; j = i + w, j < n; ++i)
for (int k = i; k < j; ++k) {
int f = p[i] * p[k + 1] * p[j + 1];
if (X[i][k] + X[k + 1][j] + f < X[i][j]) {
X[i][j] = X[i][k] + X[k + 1][j] + f;
s[i][j] = k;
}
}
return X[0][n - 1];
}
vector<int> LIS(const vector<int> &a) {
const static int INF = 99999999;
const int n = a.size();
vector<int> A(n, INF);
vector<int> id(n);
for (int i = 0; i < n; ++i) {
id[i] = distance(A.begin(), lower_bound(A.begin(), A.end(), a[i]));
A[id[i]] = a[i];
}
int m = *max_element(id.begin(), id.end());
vector<int> b(m + 1);
for (int i = n - 1; i >= 0; --i)
if (id[i] == m) b[m--] = a[i];
return b;
}
template <typename T>
vector<T> LCS(const vector<T> &a, const vector<T> &b) {
const int n = a.size(), m = b.size();
vector<vector<int> > X(n + 1, vector<int>(m + 1));
vector<vector<int> > Y(n + 1, vector<int>(m + 1));
for (int i = (0); i < (n); ++i) {
for (int j = (0); j < (m); ++j) {
if (a[i] == b[j]) {
X[i + 1][j + 1] = X[i][j] + 1;
Y[i + 1][j + 1] = 0;
} else if (X[i + 1][j] < X[i][j + 1]) {
X[i + 1][j + 1] = X[i][j + 1];
Y[i + 1][j + 1] = +1;
} else {
X[i + 1][j + 1] = X[i + 1][j];
Y[i + 1][j + 1] = -1;
}
}
}
vector<T> c;
for (int i = n, j = m; i > 0 && j > 0;) {
if (Y[i][j] > 0)
--i;
else if (Y[i][j] < 0)
--j;
else {
c.push_back(a[i - 1]);
--i;
--j;
}
}
reverse((c).begin(), (c).end());
return c;
}
vector<int> money_change(int C, vector<int> &cs) {
const int INF = 99999999;
int n = cs.size();
vector<int> xs(C + 1, INF);
vector<int> ys(C + 1);
xs[0] = 0;
for (int i = 0; i < n; ++i) {
for (int c = 0; c + cs[i] <= C; ++c) {
if (xs[c + cs[i]] > xs[c] + 1) {
xs[c + cs[i]] = xs[c] + 1;
ys[c + cs[i]] = c;
}
}
}
vector<int> zs;
for (int c = C; c > 0; c = ys[c]) {
zs.push_back(c - ys[c]);
}
return zs;
}
int main() {
int N;
cin >> N;
vector<long long> a(N);
for (int i = (0); i < (N); ++i) {
cin >> a[i];
}
long long cost = 0;
long long sum = a[0];
long long cost2 = 0;
long long sum2 = -a[0];
for (int i = (0); i < (N - 1); ++i) {
if (sum > 0) {
if (sum + a[i + 1] < 0) {
sum += a[i + 1];
} else {
cost += (1 + (sum + a[i + 1]));
sum = -1;
}
} else {
if (sum + a[i + 1] > 0) {
sum += a[i + 1];
} else {
cost += (1 + ABS(sum + a[i + 1]));
sum = 1;
}
}
}
for (int i = (0); i < (N - 1); ++i) {
if (sum2 > 0) {
if (sum2 + a[i + 1] < 0) {
sum2 += a[i + 1];
} else {
cost2 += (1 + (sum2 + a[i + 1]));
sum2 = -1;
}
} else {
if (sum2 + a[i + 1] > 0) {
sum2 += a[i + 1];
} else {
cost2 += (1 + ABS(sum2 + a[i + 1]));
sum2 = 1;
}
}
}
std::cout << (((cost) < (cost2) ? (cost) : (cost2))) << endl;
;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
void print(std::string s1, std::string s2) {
for (int i = 0; i < s1.length(); i++) {
if (s1[i] == s2[i]) continue;
if (s1[i] > s2[i]) {
std::cout << "GREATER";
return;
}
if (s1[i] < s2[i]) {
std::cout << "LESS";
return;
}
}
std::cout << "EQUAL";
}
int main() {
std::string s1, s2;
std::cin >> s1 >> s2;
if (s1.length() > s2.length())
std::cout << "GREATER";
else if (s1.length() < s2.length())
std::cout << "LESS";
else if (s1.length() == s2.length())
print(s1, s2);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n, a[100000];
int even() {
int res = 0;
int sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 0) {
if (sum <= 0) {
res += -sum + 1;
sum = 1;
}
} else {
if (sum >= 0) {
res += sum + 1;
sum = -1;
}
}
}
return res;
}
int odd() {
int res = 0;
int sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 0) {
if (sum >= 0) {
res += sum + 1;
sum = -1;
}
} else {
if (sum <= 0) {
res += -sum + 1;
sum = 1;
}
}
}
return res;
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
cout << min(even(), odd()) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-9;
const int INF = 1 << 29;
int main() {
int n;
cin >> n;
long long s1, s2, c1, c2, a;
for (int i = 1; i < n + 1; i++) {
cin >> a;
s1 += a;
s2 += a;
if (i % 2) {
if (s1 <= 0) c1 += 1 - s1, s1 = 1;
if (s2 >= 0) c2 += 1 + s2, s2 = -1;
} else {
if (s1 >= 0) c1 += 1 + s1, s1 = -1;
if (s2 <= 0) c2 += 1 - s2, s2 = 1;
}
}
cout << min(c1, c2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using i_i = pair<int, int>;
using ll_ll = pair<ll, ll>;
using d_ll = pair<double, ll>;
using ll_d = pair<ll, double>;
using d_d = pair<double, double>;
template <class T>
using vec = vector<T>;
static constexpr ll LL_INF = 1LL << 60;
static constexpr int I_INF = 1 << 28;
static constexpr double PI =
static_cast<double>(3.14159265358979323846264338327950288);
static constexpr double EPS = numeric_limits<double>::epsilon();
static map<type_index, const char* const> scanType = {{typeid(int), "%d"},
{typeid(ll), "%lld"},
{typeid(double), "%lf"},
{typeid(char), "%c"}};
template <class T>
static void scan(vector<T>& v);
[[maybe_unused]] static void scan(vector<string>& v, bool isWord = true);
template <class T>
static inline bool chmax(T& a, T b);
template <class T>
static inline bool chmin(T& a, T b);
template <class T>
static inline T gcd(T a, T b);
template <class T>
static inline T lcm(T a, T b);
template <class A, size_t N, class T>
static void Fill(A (&arr)[N], const T& val);
template <class T>
T mod(T a, T m);
template <class Monoid>
struct SegmentTree {
using F = function<Monoid(Monoid, Monoid)>;
int sz;
vector<Monoid> seg;
const F f;
const Monoid M1;
SegmentTree(int n, const F f, const Monoid& M1) : f(f), M1(M1) {
sz = 1;
while (sz < n) sz <<= 1;
seg.assign(2 * sz, M1);
}
void set(int k, const Monoid& x) { seg[k + sz] = x; }
void build() {
for (int k = sz - 1; k > 0; k--) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
void update(int k, const Monoid& x) {
k += sz;
seg[k] = x;
while (k >>= 1) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
Monoid query(int a, int b) {
Monoid L = M1, R = M1;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1) L = f(L, seg[a++]);
if (b & 1) R = f(seg[--b], R);
}
return f(L, R);
}
Monoid operator[](const int& k) const { return seg[k + sz]; }
template <class C>
int find_subtree(int a, const C& check, Monoid& M, bool type) {
while (a < sz) {
Monoid nxt = type ? f(seg[2 * a + type], M) : f(M, seg[2 * a + type]);
if (check(nxt))
a = 2 * a + type;
else
M = nxt, a = 2 * a + 1 - type;
}
return a - sz;
}
template <class C>
int find_first(int a, const C& check) {
Monoid L = M1;
if (a <= 0) {
if (check(f(L, seg[1]))) return find_subtree(1, check, L, false);
return -1;
}
int b = sz;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1) {
Monoid nxt = f(L, seg[a]);
if (check(nxt)) return find_subtree(a, check, L, false);
L = nxt;
++a;
}
}
return -1;
}
template <class C>
int find_last(int b, const C& check) {
Monoid R = M1;
if (b >= sz) {
if (check(f(seg[1], R))) return find_subtree(1, check, R, true);
return -1;
}
int a = sz;
for (b += sz; a < b; a >>= 1, b >>= 1) {
if (b & 1) {
Monoid nxt = f(seg[--b], R);
if (check(nxt)) return find_subtree(b, check, R, true);
R = nxt;
}
}
return -1;
}
};
int main(int argc, char* argv[]) {
ll n;
cin >> n;
vec<ll> a(n);
scan(a);
SegmentTree<ll> seg(
n, [](ll x, ll y) { return x + y; }, 0LL);
for (int i = (0); i < (n); i++) {
seg.set(i, a[i]);
}
seg.build();
ll ans = 0;
bool next_sign = (a[0] < 0) ? true : false;
if (a[0] == 0) {
seg.update(0, 1LL);
ans++;
}
for (int i = (2); i < (n + 1); i++) {
ll sum = seg.query(0, i);
if ((next_sign && sum > 0) || (!next_sign && sum < 0)) {
next_sign = !next_sign;
continue;
}
ll to = (next_sign) ? 1 : -1;
ll diff = abs(sum - to);
ans += diff;
seg.update(i - 1, seg[i - 1] + (to - sum));
next_sign = !next_sign;
}
ll ans2 = 0;
for (int i = (0); i < (n); i++) {
seg.update(i, a[i]);
}
next_sign = (a[0] < 0) ? true : false;
if (a[0] == 0) {
seg.update(0, -1LL);
ans2++;
}
for (int i = (2); i < (n + 1); i++) {
ll sum = seg.query(0, i);
if ((next_sign && sum > 0) || (!next_sign && sum < 0)) {
next_sign = !next_sign;
continue;
}
ll to = (next_sign) ? 1 : -1;
ll diff = abs(sum - to);
ans2 += diff;
seg.update(i - 1, seg[i - 1] + (to - sum));
next_sign = !next_sign;
}
((cout) << (min(ans, ans2)) << (endl));
return 0;
}
template <class T>
static void scan(vector<T>& v) {
auto tFormat = scanType[typeid(T)];
for (T& n : v) {
scanf(tFormat, &n);
}
}
static void scan(vector<string>& v, bool isWord) {
if (isWord) {
for (auto& n : v) {
cin >> n;
}
return;
}
int i = 0, size = v.size();
string s;
getline(cin, s);
if (s.size() != 0) {
i++;
v[0] = s;
}
for (; i < size; ++i) {
getline(cin, v[i]);
}
}
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;
}
template <class T>
inline T gcd(T a, T b) {
return __gcd(a, b);
}
template <class T>
inline T lcm(T a, T b) {
T c = min(a, b), d = max(a, b);
return c * (d / gcd(c, d));
}
template <class A, size_t N, class T>
void Fill(A (&arr)[N], const T& val) {
std::fill((T*)arr, (T*)(arr + N), val);
}
template <class T>
T mod(T a, T m) {
return (a % m + m) % m;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long> a(n);
long long wa = 0;
int now_sign = 0;
int pre_sign = 0;
long long count = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
pre_sign = a[0] / abs(a[0]);
wa = a[0];
for (int i = 1; i < n; i++) {
wa += a[i];
if (wa != 0)
now_sign = wa / abs(wa);
else
now_sign = 0;
if (now_sign == pre_sign || now_sign == 0) {
count += abs(wa) + 1;
wa = -1 * pre_sign;
now_sign = -1 * pre_sign;
}
pre_sign = now_sign;
}
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>
int main(int argc, char const* argv[]) {
uint64_t n;
std::cin >> n;
int64_t sum, current;
std::cin >> current;
sum = current;
uint64_t result = 0;
bool is_sum_negative = sum < 0;
for (int i = 1; i < n; ++i) {
std::cin >> current;
sum += current;
auto tmp = std::abs(sum) + 1;
if (is_sum_negative) {
if (sum <= 0) {
sum += tmp;
result += tmp;
assert(sum == 1);
}
} else {
if (sum >= 0) {
sum -= tmp;
result += tmp;
assert(sum == -1);
}
}
is_sum_negative = !is_sum_negative;
}
std::cout << result << std::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 sign(int a) { return a / abs(a); }
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
long long int a[100001];
int n;
cin >> n;
for (int i = 0; i < (int)n; i++) cin >> a[i];
long long int sum = 0;
long long int c = 1;
long long int c1 = 0, c2 = 0;
for (int i = 0; i < (int)n; i++) {
sum += a[i];
if (sum * c <= 0) {
c1 += abs(sum) + 1;
sum = c;
}
c *= -1;
}
c = -1;
for (int i = 0; i < (int)n; i++) {
sum += a[i];
if (sum * c <= 0) {
c2 += abs(sum) + 1;
sum = c;
}
c *= -1;
}
cout << min(c1, c2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | package main
import (
"bufio"
"fmt"
"os"
"strings"
"strconv"
)
func main() {
sc := bufio.NewScanner(os.Stdin)
sc.Buffer(make([]byte, 64*1024*1024), 64*1024*1024)
sc.Scan()
n, _ := strconv.Atoi(sc.Text())
sc.Scan()
aArr := strings.Split(sc.Text(), " ")
a := make([]int, n)
for i := 0; i < n; i++ {
a[i], _ = strconv.Atoi(aArr[i])
}
cnt := 0
sum := a[0]
for i := 1; i < n; i++ {
if (sum+a[i])*sum < 0 {
sum += a[i]
continue
} else if sum+a[i] > 0 {
for j := 1; j < 10000000000; j++ {
if sum+a[i]-j < 0 {
cnt += j
sum += a[i]-j
break
}
}
} else {
for j := 1; j < 10000000000; j++ {
if sum+a[i]+j > 0 {
cnt += j
sum += a[i]+j
break
}
}
}
}
fmt.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 | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a;
for (int i = 0; i < n; i++) {
int ai;
cin >> ai;
a.push_back(ai);
}
int count = 0;
if (a.at(0) == 0) {
a.at(0) = 1;
count = 1;
}
int sum = a.at(0);
for (int i = 0; i < n - 1; i++) {
int sum_next = sum + a.at(i + 1);
if (sum > 0 && sum_next >= 0) {
int diff = sum_next + 1;
count += diff;
sum_next -= diff;
} else if (sum < 0 && sum_next <= 0) {
int diff = -sum_next + 1;
count += diff;
sum_next += diff;
}
sum = sum_next;
}
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> a(n);
for (int i = 0; i < (int)n; i++) cin >> a[i];
int c1 = 0, c2 = 0;
int s1 = 0, s2 = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0 && s1 + a[i] <= 0) {
c1 += -(s1 + a[i]) + 1;
s1 = 1;
} else if (i % 2 == 1 && s1 + a[i] >= 0) {
c1 += s1 + a[i] + 1;
s1 = -1;
} else {
s1 += a[i];
}
if (i % 2 == 1 && s2 + a[i] <= 0) {
c2 += -(s2 + a[i]) + 1;
s2 = 1;
} else if (i % 2 == 0 && s2 + a[i] >= 0) {
c2 += s2 + a[i] + 1;
s2 = -1;
} else {
s2 += a[i];
}
}
cout << min(c1, c2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | def resolve(SL):
# L[0]!=0を起点とする
cnt = 0
for i in range(len(SL)-1):
s0 = SL[i]
s1 = SL[i+1]
if(s0>0 and s1>=0):
SL[(i+1):] = [s-(s1+1) for s in SL[(i+1):]]
cnt += (s1+1)
elif(s0<0 and s1<=0):
SL[(i+1):] = [s+(-s1+1) for s in SL[(i+1):]]
cnt += (-s1+1)
return cnt
def ans(L):
SL = [sum(L[:(i+1)]) for i in range(len(L))]
c0,c1=0,0
if (L[0]>0):
c0 = resolve(SL)
c1 = (L[0]+1) + resolve(list(map(lambda x:x-(L[0]+1), SL)))
elif (L[0]<0):
c0 = resolve(SL)
c1 = (-L[0]+1) + resolve(list(map(lambda x:x+(-L[0]+1), SL)))
else:
c0 = 1 + resolve(list(map(lambda x:x+1, SL)))
c1 = 1 + resolve(list(map(lambda x:x-1, SL)))
return(min(c0,c1))
N = int(input())
L = [int(x) for x in input().split(' ')]
print(ans(L)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n, x = 0, a[100001], ans = 0;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; a[i] == 0; ++i) {
ans = 2 * (i + 1);
x = i + 1;
}
int sum1 = a[x], sum2 = a[x];
for (int i = x + 1; i < n; i++) {
sum2 += a[i];
if (sum2 >= 0 && sum1 > 0) {
ans += abs(sum2) + 1;
a[i] = a[i] - abs(sum2) - 1;
sum2 = sum2 - abs(sum2) - 1;
}
if (sum2 <= 0 && sum1 < 0) {
ans += abs(sum2) + 1;
a[i] = a[i] + abs(sum2) + 1;
sum2 = sum2 + abs(sum2) + 1;
}
sum1 = sum2;
}
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;
template <class T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
long n, i, a, sum, ans;
bool sign;
inline void solve() {
cin >> (n);
for ((i) = (0); (i) < (n); (i)++) {
cin >> (a);
if (i == 0) {
sum = a;
if (a < 0)
sign = false;
else
sign = true;
} else {
if (sign) {
if (sum + a >= 0) {
ans += sum + a + 1;
sum = -1;
} else {
sum += a;
}
sign = false;
} else {
if (sum + a <= 0) {
ans += abs(sum + a) + 1;
sum = 1;
} else {
sum += a;
}
sign = true;
}
}
}
cout << ans << endl;
}
int main(int argc, char** argv) {
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
return EXIT_SUCCESS;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Main main = new Main();
main.run();
}
public void run() {
Scanner sc = new Scanner(System.in);
int n= sc.nextInt();
int sum1=0;
int sum2=0;
int ans1=0;
int ans2=0;
for(int i=0; i<n; i++) {
int a = sc.nextInt();
sum1 += a;
sum2 += a;
if(i%2==0) {
if(sum1 <= 0) {
ans1 += (1-sum1);
sum1=1;
}
if(sum2 >= 0) {
ans2 += sum2+1;
sum2=-1;
}
}else {
if(sum1 >= 0) {
ans1 += sum1+1;
sum1 = -1;
}
if(sum2 <= 0) {
ans2 += (1-sum2);
sum2 = 1;
}
}
}
System.out.println(Math.min(ans1, ans2));
sc.close();
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
A = [int(i) for i in input().split()]
seq_sum = A[0]
sign = 1 if seq_sum > 0 else -1
count = 0
for i in range(1, n):
if A[i] * sign * (-1) > seq_sum * sign:
seq_sum += A[i]
sign = sign * (-1)
else:
count += abs(sign * (-1) - (seq_sum + A[i]))
A[i] += (sign * (-1) - (seq_sum + A[i])) * (sign * (-1))
seq_sum += A[i]
sign = sign * (-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 | python3 | #!/usr/bin/env python3
import sys
def solve(n: int, a: "List[int]"):
def _solve():
from itertools import cycle
ab = 0
for aa in map(lambda a_o: a_o[0]*a_o[1], zip(a, cycle([1, -1] if a[0] > 0 else [-1, 1]))):
ab -= aa
if ab >= 0:
yield ab + 1
ab = 1
ab = abs(ab)
return sum(_solve())
# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools (tips: You use the default template now. You can remove this line by using your custom template)
def main():
def iterate_tokens():
for line in sys.stdin:
for word in line.split():
yield word
tokens = iterate_tokens()
n = int(next(tokens)) # type: int
a = [int(next(tokens)) for _ in range(n)] # type: "List[int]"
print(solve(n, a))
if __name__ == '__main__':
main()
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
int sum = a[0], count = 0;
for (int i = 1; i < n; i++) {
if (sum > 0) {
if (sum + a[i] >= 0) {
count += abs(a[i] - (-1 - sum));
a[i] = -1 - sum;
}
} else {
if (sum + a[i] <= 0) {
count += abs(a[i] - (1 - sum));
a[i] = 1 - 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 | UNKNOWN | #!/usr/bin/env ruby
#
n = STDIN.gets.to_i
a = STDIN.gets.split(' ').map{|x| x.to_i}
s = Array.new(n,0)
output = 0
s[0] = a[0]
1.upto(n-1) do |i|
s[i] = s[i-1] + a[i]
if (s[i-1] > 0) ^ (s[i] > 0)
change = 0
else
change = s[i].abs+1
s[i] = (s[i-1] > 0) ? -1: 1
end
output += change
end
puts 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>
using namespace std;
using Graph = vector<vector<long long>>;
const long long mod = 1000000007;
long long digitsum(long long n, long long b) {
if (b < 2) return -1;
if (n < b) return n;
return digitsum(n / b, b) + n % b;
}
long long mpow(long long a, long long x);
long long m_inv(long long n);
vector<long long> split(long long n, long long a);
string xal_number(long long n, long long x);
long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; }
long long lcm(long long x, long long y) { return x * y / gcd(x, y); }
class Factorial {
private:
vector<long long> fac;
public:
Factorial(long long N) {
fac.push_back(1);
for (long long i = (0); i < (N); ++i) fac.push_back(fac[i] * (i + 1) % mod);
}
long long fact(long long a) { return fac[a]; }
long long ifac(long long a) { return m_inv(fac[a]); }
long long cmb(long long n, long long r);
};
struct UnionFind {
vector<long long> par;
UnionFind(long long n = 1) { init(n); }
void init(long long n = 1) {
par.resize(n);
for (long long i = (0); i < (n); ++i) par[i] = -1;
}
long long root(long long x) {
if (par[x] < 0)
return x;
else
return par[x] = root(par[x]);
}
long long size(long long x) { return -par[root(x)]; }
bool issame(long long x, long long y) { return root(x) == root(y); }
bool connect(long long x, long long y);
};
signed main() {
long long n;
cin >> n;
vector<long long> a(n);
for (long long i = (0); i < (n); ++i) cin >> a[i];
long long ans = 0;
vector<long long> S(n + 1);
S[0] = 0;
for (long long i = (0); i < (n); ++i) {
S[i + 1] = S[i] + a[i];
if (S[i + 1] * S[i] > 0) {
ans += abs(S[i + 1]) + 1;
S[i + 1] = (S[i] > 0) ? -1 : 1;
}
if (S[i + 1] == 0) {
ans += 1;
S[i + 1] += (S[i] > 0) ? -1 : 1;
}
}
long long ans2 = 0;
S[n] = 0;
for (long long i = n; i > 0; --i) {
S[i - 1] = S[i] + a[i];
if (S[i - 1] * S[i] > 0) {
ans2 += abs(S[i - 1]) + 1;
S[i - 1] = (S[i] > 0) ? -1 : 1;
}
if (S[i + 1] == 0) {
ans2 += 1;
S[i - 1] += (S[i] > 0) ? -1 : 1;
}
}
cout << min(ans, ans2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
namespace tools {
template <typename T>
class fenwick {
private:
std::vector<T> values;
public:
template <typename InputIter,
typename std::enable_if<
std::is_same<T, typename std::iterator_traits<
InputIter>::value_type>::value &&
std::is_base_of<std::input_iterator_tag,
typename std::iterator_traits<
InputIter>::iterator_category>::value,
std::nullptr_t>::type = nullptr>
fenwick(InputIter begin, InputIter end) : values(begin, end) {
for (std::size_t i = 1; i < this->values.size(); ++i) {
this->values[i + (i & -i) - 1] += this->values[i - 1];
}
}
std::size_t size() const { return this->values.size(); }
void add(std::size_t i, const T value) {
assert(i < this->values.size());
for (i += 1; i <= this->values.size(); i += i & -i) {
this->values[i - 1] += value;
}
}
T sum(const std::size_t begin, const std::size_t end) const {
if (begin == 0) {
assert(end <= this->values.size());
T result = 0;
for (std::size_t i = end; i > 0; i -= i & -i) {
result += this->values[i - 1];
}
return result;
} else {
return begin < end ? this->sum(0, end) - this->sum(0, begin) : 0;
}
}
T operator[](const std::size_t i) const { return this->sum(i, i + 1); }
};
} // namespace tools
std::int_fast64_t solve(const tools::fenwick<std::int_fast64_t>& a_fenwick,
const std::int_fast64_t first_target) {
assert(first_target == 1 || first_target == -1);
tools::fenwick<std::int_fast64_t> work = a_fenwick;
std::int_fast64_t target = first_target;
std::int_fast64_t result = 0;
for (std::size_t i = 0; i < work.size(); ++i, target *= -1) {
const std::int_fast64_t current_sum = work.sum(0, i + 1);
const std::int_fast64_t diff =
(target > 0 ? current_sum < target : target < current_sum)
? target - current_sum
: 0;
result += std::abs(diff);
work.add(i, diff);
}
return result;
}
int main() {
std::int_fast64_t n;
std::cin >> n;
std::vector<std::int_fast64_t> a(n);
std::copy_n(std::istream_iterator<std::int_fast64_t>(std::cin), n, a.begin());
tools::fenwick<std::int_fast64_t> a_fenwick(a.begin(), a.end());
std::cout << std::min(solve(a_fenwick, 1), solve(a_fenwick, -1)) << std::endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int,input().split()))
#n = 6
#a = list(map(int,"-1 4 3 2 -5 4".split()))
#a = list(map(int,"-1 4 -4 2 -5 5".split()))
f = "+" if a[0] < 0 else "-"
s = a[0]
c = 0
#print((s,f,c))
for i in range(1,n):
if f == "+" and s + a[i] > 0:
f = "-"
s = s + a[i]
# print((s,f,c))
elif f == "+":
c = c + abs(s + a[i])+1
f = "-"
s = 1
# print((s,f,c))
elif f == "-" and s + a[i] < 0:
f = "+"
s = s + a[i]
# print((s,f,c))
else:
c = c + abs(s+a[i])+1
f = "+"
s = -1
# print((s,f,c))
print(c)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
std::vector<int64_t> data(n);
for (int i = 0; i < n; i++) {
cin >> data.at(i);
}
int answer = 0;
int64_t sum_a = data.at(0);
for (int i = 1; i < n; i++) {
sum_a += data.at(i);
if (data.at(0) > 0) {
if (i % 2 != 0 && sum_a >= 0) {
while (sum_a != -1) {
sum_a--;
answer++;
}
}
if (i % 2 == 0 && sum_a <= 0) {
while (sum_a != 1) {
sum_a++;
answer++;
}
}
}
if (data.at(0) < 0) {
if (i % 2 != 0 && sum_a <= 0) {
while (sum_a != 1) {
sum_a++;
answer++;
}
}
if (i % 2 == 0 && sum_a >= 0) {
while (sum_a != -1) {
sum_a--;
answer++;
}
}
}
}
cout << answer << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ld = long double;
using ll = long long;
using ui = unsigned int;
using ull = unsigned long long;
using Pi_i = pair<int, int>;
using Pll_ll = pair<ll, ll>;
using VB = vector<bool>;
using VC = vector<char>;
using VD = vector<double>;
using VI = vector<int>;
using VLL = vector<ll>;
using VS = vector<string>;
using VSH = vector<short>;
using VULL = vector<ull>;
const int MOD = 1000000007;
const int INF = 1000000000;
const int NIL = -1;
const ll LINF = 1000000000000000000;
const double EPS = 1E-10;
template <class T, class S>
bool chmax(T &a, const S &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T, class S>
bool chmin(T &a, const S &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
int main() {
int n;
cin >> n;
VI a(n);
for (int i = (0), i_len = (n); i < i_len; ++i) cin >> a[i];
ll ans(LLONG_MAX);
for (int s = (0), s_len = (2); s < s_len; ++s) {
ll sum(0);
ll cnt(0);
for (int i = (0), i_len = (n); i < i_len; ++i) {
int sgn = (s + i) % 2 * 2 - 1;
sum += a[i];
if (sum / sgn <= 0) {
cnt += abs(sum - sgn);
sum = sgn;
}
}
chmin(ans, cnt);
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
a = list(map(int, input().split()))
ans = [0, 0]
if a[0] == 0:
sum = 1
ans[0] = 1
else:
sum = a[0]
for i in range(1, N):
if sum < 0:
if abs(sum) < a[i]:
sum += a[i]
else:
ans[0] += abs(sum)-a[i]+1
sum += 1
elif sum > 0:
if sum + a[i] < 0:
sum += a[i]
else:
ans[0] += abs(-1-sum-a[i])
sum = -1
if a[0] == 0:
sum = -1
ans[1] = 1
else:
if a[0] > 0:
ans[1] += a[0] + 1
sum = -1
else:
ans[1] += abs(a[0]) + 1
sum = 1
for i in range(1, N):
if sum < 0:
if abs(sum) < a[i]:
sum += a[i]
else:
ans[1] += abs(sum)-a[i]+1
sum += 1
elif sum > 0:
if sum + a[i] < 0:
sum += a[i]
else:
ans[1] += abs(-1-sum-a[i])
sum = -1
print(min(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 | package sample.code;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
long[] a = null;
try(Scanner sc = new Scanner(System.in)){
// N入力 aスペース区切り入力
a = new long[sc.nextInt()];
for(int i = 0; i < a.length; i ++) {
a[i] = sc.nextLong();
}
}
long start = a[0];
boolean isNaturalNum = true;
if(start < 0) {
isNaturalNum = false;
}
long ret = 0L;
for(int i = 1; i < a.length; i++) {
long temp2 = start + a[i];
if(isNaturalNum) {
if(temp2 > 0) {
ret += Math.abs(start) + 1 + Math.abs(a[i]);
start += (1 + Math.abs(a[i]) )* -1;
} else {
//OK
start = temp2;
}
isNaturalNum = false;
} else {
if(temp2 < 0) {
ret += Math.abs(start) + 1 - Math.abs(a[i]);
start += Math.abs(start) + 1;
} else {
start = temp2;
}
isNaturalNum = true;
}
}
System.out.println(ret);
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long N;
cin >> N;
vector<long long> a(N), S(N + 7);
for (long long i = 0; i < N; i++) {
cin >> a[i];
}
long long ans = 0;
S[0] = a[0];
if (S[0] == 0) {
for (long long i = 0; i < N; i++) {
if (a[i] > 0) {
if (i % 2 == 0) {
S[0] = 1;
ans++;
break;
} else {
S[0] = -1;
ans++;
break;
}
} else if (a[i] < 0) {
if (i % 2 == 0) {
S[0] = -1;
ans++;
break;
} else {
S[0] = 1;
ans++;
break;
}
} else if (i == N - 1 && a[i] == 0) {
ans = (2 * N) - 1;
cout << ans << endl;
return 0;
}
}
}
for (int i = 1; i < N; i++) {
S[i] = S[i - 1] + a[i];
}
for (long long i = 1; i < N; i++) {
if (S[i - 1] > 0 && S[i] >= 0) {
ans += abs(S[i]) + 1;
S[i] = -1;
} else if (S[i - 1] < 0 && S[i] <= 0) {
ans += abs(S[i]) + 1;
S[i] = 1;
}
if (i != N - 1) {
S[i + 1] = S[i] + a[i + 1];
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
#define forx(i,a,b) for(int i=(a);i<(b);i++)
#define rep(j,n) for(int j=0;j<(n);j++)
typedef long long ll;
int main()
{
int n,ansa=0,ansb=0,suma=0;sumb=0;
cin>>n;
bool plus=true;
rep(i,n){
int a,b;
cin>>b;
a=b;
while(plus&&suma+a<=0){
a++;
ansa++;
}
while(!plus&&suma+a>=0){
a--;
ansa++;
}
while(plus&&sumb+b>=0){
b++;
ansb++;
}
while(!plus&&sumb+b<=0){
b--;
ansb++;
}
suma+=a;
sumb+=b;
plus=!plus;
}
cout<<min(ansa,ansb)<<endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | from copy import copy
n = int(input())
a = [int(x) for x in input().split()]
ans1=[(-1)**i for i in range(n)]
b=copy(a)
res_b=0
c=copy(a)
res_c=0
for i in range(n):
if ans1[i]*sum(b[:i+1])>0:
pass
else:
b[i]=ans1[i]-sum(b[:i])
res_b+=abs(b[i]-a[i])
if -1*ans1[i]*sum(c[:i+1])>0:
pass
else:
c[i]=-1*ans1[i]-sum(c[:i])
res_c+=abs(c[i]-a[i])
print(min(res_b,res_c)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int N;
vector<int> a;
int main() {
cin >> N;
a.resize(N);
for (int i = 0; i < (N); i++) {
cin >> a[i];
}
int sum = a[0];
int ans = 0;
if (sum == 0) {
if (a[1] > 0)
sum--;
else
sum++;
ans++;
}
for (int i = (1); i < (N); i++) {
int b;
if (sum > 0) {
b = sum * -1 - 1;
if (b < a[i]) {
ans += a[i] - b;
a[i] = b;
}
} else {
b = sum * -1 + 1;
if (b > a[i]) {
ans += b - a[i];
a[i] = b;
}
}
sum += a[i];
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long calc(bool plus, vector<long long> a) {
long long ret = 0;
int n = a.size();
if (a[0] == 0) {
if (plus) {
a[0] = 1;
} else {
a[0] = -1;
}
ret++;
} else if (a[0] > 0 && !plus) {
ret += 1 - a[0];
a[0] = -1;
} else if (a[0] < 0 && plus) {
ret += a[0] + 1;
a[0] = 1;
}
for (int i = 1; i <= n - 1; i++) {
a[i] += a[i - 1];
if (a[i] == 0) {
if (a[i - 1] < 0) a[i] = 1;
if (a[i - 1] > 0) a[i] = -1;
ret++;
} else if (a[i] > 0 && a[i - 1] > 0) {
ret += a[i] + 1;
a[i] = -1;
} else if (a[i] < 0 && a[i - 1] < 0) {
ret += 1 - a[i];
a[i] = 1;
}
}
return ret;
}
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
cout << min(calc(true, a), calc(false, a)) << 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;
int a[100100];
int ans1 = 0, ans2 = 0;
int sum1 = 0, sum2 = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
sum1 += a[i];
sum2 += a[i];
if (sum1 >= 0) {
ans1 += (sum1 + 1);
sum1 = -1;
}
if (sum2 <= 0) {
ans2 -= (sum2 - 1);
sum2 = 1;
}
swap(sum1, sum2);
swap(ans1, ans2);
}
cout << min(ans1, ans2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long a[n];
for (int i = 0; i < (n); i++) cin >> a[i];
long long sum = 0;
long long cnt1 = 0;
for (int i = 0; i < (n); i++) {
sum += a[i];
if (i % 2 == 0) {
if (sum <= 0) {
cnt1 += (-1) * sum + 1;
sum = 1;
}
} else {
if (sum >= 0) {
cnt1 += sum + 1;
sum = -1;
}
}
}
sum = 0;
long long cnt2 = 0;
for (int i = 0; i < (n); i++) {
sum += a[i];
if (i % 2 == 0) {
if (sum <= 0) {
cnt2 += (-1) * sum + 1;
sum = 1;
}
} else {
if (sum >= 0) {
cnt2 += sum + 1;
sum = -1;
}
}
}
cout << min(cnt1, cnt2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | def sign(x):
if x<0:
return -1
elif x>0:
return 1
else:
return 0
n = int(input())
a = list(map(int,input().split()))
cumulative_sum = a[0]
flag = sign(cumulative_sum)
ans = 0
for i in range(1,n):
cumulative_sum += a[i]
if sign(cumulative_sum) == flag or sign(cumulative_sum) == 0:
ans += abs(-flag-cumulative_sum)
cumulative_sum = -flag
flag = sign(cumulative_sum)
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
bool ch = false;
long long N, i;
long long ans = 0, count = 0;
cin >> N;
long long a[N];
cin >> a[0];
ans += a[0];
if (ans > 0)
ch = true;
else
ch = false;
for (i = 1; i < N; i++) {
cin >> a[i];
if (ch) {
if (ans >= -a[i]) {
count += ans + a[i] + 1;
ans = -1;
} else
ans += a[i];
ch = false;
} else {
if (ans <= -a[i]) {
count += -ans - a[i] + 1;
ans = 1;
} else
ans += a[i];
ch = true;
}
}
long long con = 0;
if (a[0] > 0) {
ans = -1;
ch = false;
} else {
ans = 1;
ch = true;
}
con = a[0] + 1;
for (i = 1; i < N; i++) {
if (ch) {
if (ans >= -a[i]) {
con += ans + a[i] + 1;
ans = -1;
} else
ans += a[i];
ch = false;
} else {
if (ans <= -a[i]) {
con += -ans - a[i] + 1;
ans = 1;
} else
ans += a[i];
ch = true;
}
}
cout << min(count, con) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
int main() {
long long n;
std::cin >> n;
long long a;
long long sum_plus[n], sum_minus[n];
for (long long i = 0; i < n; ++i) {
std::cin >> a;
if (i == 0)
sum_plus[i] = a;
else
sum_plus[i] = sum_plus[i - 1] + a;
sum_minus[i] = sum_plus[i];
}
long long count_plus = 0;
long long count_minus = 0;
bool plus = true;
for (long long i = 0; i < n; ++i) {
if (plus) {
if (sum_plus[i] <= 0) {
long long add = -sum_plus[i] + 1;
count_plus += add;
for (long long j = i; j < n; ++j) sum_plus[j] += add;
}
if (sum_minus[i] >= 0) {
long long add = sum_minus[i] + 1;
count_minus += add;
for (long long j = i; j < n; ++j) sum_minus[j] -= add;
}
} else {
if (sum_plus[i] >= 0) {
long long add = sum_plus[i] + 1;
count_plus += add;
for (long long j = i; j < n; ++j) sum_plus[j] -= add;
}
if (sum_minus[i] <= 0) {
long long add = -sum_minus[i] + 1;
count_minus += add;
for (long long j = i; j < n; ++j) sum_minus[j] += add;
}
}
plus = !plus;
}
long long ans = std::min(count_plus, count_minus);
std::cout << ans << 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 | java | import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
@SuppressWarnings("resource")
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();
}
int count = 0,sabun = 0;
int[] imos = new int[a.length];
Arrays.fill(imos, 0);
for (int i = 0; i < a.length; i++) {
imos[i] = a[i];
}
for (int i = 0; i < a.length-1; i++) {
imos[i+1] += imos[i];
}
// System.out.println("init");
// for (int i = 0; i < imos.length; i++) {
// System.out.println(imos[i]);
// }
// System.out.println("init:finish");
//
for (int i = 1; i < a.length; i++) {
if (imos[i-1]*imos[i] > 0) {
if (imos[i-1] < 0) {
sabun = -imos[i-1]-1;
} else {
sabun = -imos[i-1]-1;
}
count += Math.abs(sabun);
for (int j = i; j < imos.length; j++) {
imos[j] += sabun;
}
} if (imos[i-1]*imos[i] == 0) {
count += 1;
if (imos[i-1] < 0) {
sabun = 1;
} else {
sabun = -1;
}
for (int j = i; j < imos.length; j++) {
imos[j] += sabun ;
}
}
}
for (int i = 0; i < a.length; i++) {
// System.out.println(imos[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 | python3 | import sys
input = sys.stdin.readline
n = int(input())
s = list(map(int, input().split()))
l = [0] * (n + 1)
l[0] = 0
for i in range(1, n + 1):
l[i] = l[i - 1] + s[i - 1]
count = 0
sabun = 0
for i in range(1, n):
subl = l[i:i + 2]
subl = [subl[0] + sabun, subl[1] + sabun]
if 0 in subl:
if subl[0] > 0:
count += 1
l[i + 1] = -1
else:
count += 1
l[i + 1] = 1
if subl[0] * subl[1] > 0:
t = subl[1]
m = True
if t > 0:
t = 1 + t
else:
t = 1 - t
m = False
if m:
l[i + 1] -= t
sabun -= t
else:
l[i + 1] += t
sabun += t
count += t
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 solve(int *a, int n) {
int count = 0;
int calc = 0;
int state, pstate;
cout << endl;
if (a[0] < 0) state = -1;
if (a[0] > 0) state = 1;
for (int i = 1; i < n; i++) {
pstate = state;
int tmp = a[i] + calc;
if (tmp < 0) state = -1;
if (tmp == 0) state = 0;
if (tmp > 0) state = 1;
if (pstate == state) {
if (state == -1) {
count += 1 - tmp;
calc += 1 - tmp;
state = 1;
} else if (state == 1) {
count += tmp + 1;
calc += -1 - tmp;
state = -1;
}
}
if (state == 0) {
if (pstate == -1) {
count += 1;
calc += 1;
state = 1;
} else if (pstate == 1) {
count += 1;
calc += -1;
state = -1;
}
}
}
return count;
}
int main() {
int n;
int ans;
int *a;
cin >> n;
a = new int[n];
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 1; i < n; i++) a[i] = a[i - 1] + a[i];
if (a[0] == 0) {
int bs, cs;
int *b = new int[n];
int *c = new int[n];
for (int i = 0; i < n; i++) b[i] = a[i] + 1;
for (int i = 0; i < n; i++) c[i] = a[i] - 1;
bs = solve(b, n);
cs = solve(c, n);
ans = bs < cs ? bs : cs;
} else
ans = solve(a, n);
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;
const int inf = 999999999;
const double pi = acos(-1);
long long a[100005] = {};
int main() {
unsigned long long ans = 0;
long long wa = 0;
int n;
cin >> n;
for (int i = (0); i < (int)(n); i++) cin >> a[i];
wa = a[0];
for (int i = (1); i < (int)(n); i++) {
if (wa >= 0) {
long long tes = wa + a[i];
if (tes < 0) {
wa = tes;
} else {
ans += (unsigned long long)(-(-1 - tes));
wa = -1;
}
} else {
long long tes = wa + a[i];
if (tes > 0) {
wa = tes;
} else {
ans += (unsigned long long)(1 - tes);
wa = 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 | python3 |
def read_input():
n = int(input())
alist = list(map(int, input().split()))
return n, alist
def get_sign(x):
if x > 0:
return 1
elif x < 0:
return -1
return 0
def submit():
n, alist = read_input()
s = alist[0]
sign = get_sign(s)
edit = 0
for a in alist[1:]:
temp = s + a
temp_sign = get_sign(temp)
while temp_sign == sign or temp == 0:
edit += 1
temp -= sign
temp_sign = get_sign(temp)
s = temp
sign = temp_sign
print(edit)
if __name__ == '__main__':
submit()
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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 = 0;
cin >> N;
vector<long long int> A(N), a(N), b(N);
for (int i = 0; i < N; i++) cin >> A[i];
for (int i = 0; i < N; i++) a[i] = A[i];
for (int i = 0; i < N; i++) b[i] = A[i];
long long int M = 0;
long long int cnt1 = 0, cnt2 = 0;
for (int i = 1; i < N; i++) {
M += a[i - 1];
if (i % 2 == 0) {
cnt1 += abs(1 - (M + a[i]));
a[i] = 1 - (M + a[i]);
} else {
cnt1 += abs(-1 - (M + a[i]));
a[i] = -1 - (M + a[i]);
}
}
for (int i = 1; i < N; i++) {
M += b[i - 1];
if (i % 2 == 1) {
cnt2 += abs(1 - (M + b[i]));
a[i] = 1 - (M + b[i]);
} else {
cnt2 += abs(-1 - (M + b[i]));
a[i] = -1 - (M + b[i]);
}
}
long long int ans = min(cnt1, cnt2);
cout << ans;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long int n;
cin >> n;
long long int A[n];
for (int i = 0; i < n; i++) cin >> A[i];
long long int sum = A[0], count = 0, temp;
if (sum == 0) {
count++;
sum = 1;
}
for (int i = 1; i < n; i++) {
temp = sum + A[i];
if ((temp < 0 && sum < 0) || (temp > 0 && sum > 0) || temp == 0) {
if (sum > 0) {
count += abs(temp + 1);
sum = -1;
} else if (sum < 0) {
count += abs(temp - 1);
sum = 1;
} else if (temp == 0) {
if (sum > 0) {
count++;
sum = -1;
} else {
count++;
sum = 1;
}
}
} else
sum = temp;
}
cout << count;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.