Search is not available for this dataset
name
stringlengths 2
88
| description
stringlengths 31
8.62k
| public_tests
dict | private_tests
dict | solution_type
stringclasses 2
values | programming_language
stringclasses 5
values | solution
stringlengths 1
983k
|
---|---|---|---|---|---|---|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long sum = 0;
long long cost = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 0 && sum < 0) {
cost += 1 - sum;
sum += 1 - sum;
}
if (i % 2 != 0 && sum > 0) {
cost += 1 + sum;
sum -= 1 + sum;
}
}
if (sum == 0) {
cost++;
}
const long long even = cost;
sum = 0;
cost = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 0 && sum > 0) {
cost += 1 + sum;
sum -= 1 + sum;
}
if (i % 2 != 0 && sum < 0) {
cost += 1 - sum;
sum += 1 - sum;
}
}
if (sum == 0) {
cost++;
}
const long long odd = cost;
cout << min(even, odd);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()) {
v.assign(a, vector<T>(b, t));
}
template <class F, class T>
void convert(const F &f, T &t) {
stringstream ss;
ss << f;
ss >> t;
}
int main() {
long long n;
cin >> n;
long long a[n];
for (int i = 0; i < int(n); ++i) {
cin >> a[i];
}
long long sum = 0;
long long ans = 0;
for (int i = 0; i < int(n); ++i) {
long long nextSum = sum + a[i];
if ((i == 0) && (a[i] == 0)) {
if (n >= 2) {
a[i] = (a[1] > 0) ? -1 : 1;
} else {
a[i] = 1;
}
++ans;
}
if ((i > 0) && (sum * nextSum >= 0)) {
a[i] += (nextSum > 0 ? -1 : 1) * (abs(nextSum) + 1);
ans += abs(nextSum) + 1;
}
sum += a[i];
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #includ<bits/stdc++.h>
using namespace std;
int main()
{ int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
int sum=arr[0];
int sum1,sum2;
int ans1=0,ans2=0;
// case 1
if(sum>0)
{ sum1=sum;
sum2=-1;
ans1=0;
ans2=sum+1;
}
else if(sum<0)
{ sum2=sum;
sum1=1;
ans2=0;
ans1=abs(sum)+1;
}
else
{ sum1=1;
sum2=1;
ans2=1;
ans1=1;
}
for(int i=1;i<n;i++)
{ if(sum1>0)
{ sum1=sum1+arr[i];
if(sum1<0)
continue;
else
{ ans1+=sum1+1;
sum1=-1;
}
}
else
{ sum1=sum1+arr[i];
if(sum1>0)
continue;
else
{ ans1+=abs(sum1)+1;
sum1=1;
}
}
}
// case 2;
for(int i=1;i<n;i++)
{ if(sum2>0)
{ sum2=sum2+arr[i];
if(sum2<0)
continue;
else
{ ans2+=sum2+1;
sum2=-1;
}
}
else
{ sum2=sum2+arr[i];
if(sum2>0)
continue;
else
{ ans2+=abs(sum2)+1;
sum2=1;
}
}
}
if(ans1<ans2)
cout<<ans1;
else
cout<<ans2;
cout<<endl;
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long sum = a[0], kaisu = 0;
if (sum == 0) {
kaisu++;
if (a[1] > 0) {
sum = -1;
} else {
sum = 1;
}
}
for (int i = 1; i < n; i++) {
long long presum = sum;
sum += a[i];
if (presum > 0) {
if (sum >= 0) {
kaisu += sum + 1;
a[i] = a[i] - sum - 1;
sum = -1;
}
}
if (presum < 0) {
if (sum <= 0) {
kaisu += 1 - sum;
sum = 1;
}
}
}
cout << kaisu << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
cnt=0
for i in range(1,n):
#print(a)
now_tmp = sum(a[:i])
next_tmp = now_tmp + a[i]
#print(i, now_tmp, next_tmp)
# 符号が逆転していればOK かつ 現在までの総和が0でない
# 異なる符号を掛けるとマイナスになる
if now_tmp * next_tmp <0 and now_tmp !=0:
continue
else:
# 現在の合計がマイナスの場合
if now_tmp < 0:
a[i] += -next_tmp+1
cnt +=abs(next_tmp+1)
# 現在の合計がプラスの場合
elif now_tmp > 0 :
a[i] += -next_tmp-1
cnt +=abs(next_tmp+1)
# 現在の合計が0の場合
elif now_tmp == 0 :
# 1個前がプラスの場合、
if sum(a[:i-1]) > 0:
a[i] += -next_tmp+1
cnt +=abs(next_tmp+1)
# 1個前がマイナスの場合
else:
a[i] += -next_tmp+1
cnt +=abs(next_tmp+1)
print(cnt) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | gets
seq = gets.split.map(&:to_i)
def foo(seq)
cnt = 0
sum = seq.shift
seq.each{|a|
if sum < 0
if sum + a > 0
sum += a
else
cnt += 1 - (sum + a)
sum = 1
end
else
if sum + a < 0
sum += a
else
cnt += 1 + (sum + a)
sum = -1
end
end
# p [a, sum, cnt]
}
return cnt
end
if seq[0] != 0
p foo(seq)
else
seq.shift
p [foo([1] + seq), foo([-1] + seq)].min + 1
end
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
template <class T, class U>
inline void chmin(T &t, U f) {
if (t > f) t = f;
}
template <class T, class U>
inline void chmax(T &t, U f) {
if (t < f) t = f;
}
int n;
void solve() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> n;
vector<long long> v(n);
long long ans1 = 0, ans2 = 0;
long long sum = 0;
for (int i = (int)(0); i < (int)(n); i++) {
cin >> v[i];
}
for (int i = (int)(0); i < (int)(n); i++) {
sum += v[i];
if (i % 2 == 0) {
if (sum < 0) {
ans1 += abs(1 - sum);
sum = 1;
}
} else {
if (sum > 0) {
ans1 += abs(sum + 1);
sum = -1;
}
}
}
for (int i = (int)(0); i < (int)(n); i++) {
sum += v[i];
if (i % 2 == 1) {
if (sum < 0) {
ans2 += abs(1 - sum);
sum = 1;
}
} else {
if (sum > 0) {
ans2 += abs(sum + 1);
sum = -1;
}
}
}
if (ans1 > ans2) {
cout << ans2 << endl;
} else {
cout << ans1 << endl;
}
}
int main() {
solve();
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
s = [ sum(a[0:i+1]) for i in range(n)]
s2 = [ s[i]*(-1) for i in range(n)]
c1 = 0
for i in range(n):
if s[i]*(-1)**i <= 0:
c1 = c1 + s[i]*(-1)**(i+1) + 1
s = [s[j] if j <= i-1 else s[j] - (s[i])*(-1)**i + (-1)**i for j in range(n)]
c2 = 0
for i in range(n):
if s2[i]*(-1)**i <= 0:
c2 = c2 + s2[i]*(-1)**(i+1) + 1
s2 = [s2[j] if j <= i-1 else s2[j] - (s2[i])*(-1)**i + (-1)**i for j in range(n)]
print(min(c1,c2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int64_t min(int64_t a, int64_t b) {
if (a > b) {
return b;
} else {
return a;
}
}
int64_t solve(vector<int> a) {
bool nextposi = (a.at(0) < 0);
int ans = 0;
int sum = a.at(0);
for (int i = 1; i < a.size(); i++) {
sum += a.at(i);
if (nextposi != (sum > 0)) {
if (nextposi == 1) {
ans += abs(sum - 1);
sum = 1;
} else {
ans += abs(sum + 1);
sum = -1;
}
}
nextposi = !nextposi;
}
return ans;
}
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a.at(i);
}
int64_t ans = 0;
if (a.at(0) == 0) {
a.at(0) = 1;
ans = solve(a);
a.at(0) = -1;
ans = min(ans, solve(a)) + 1;
} else {
ans = solve(a);
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main(void) {
ll n;
cin >> n;
vector<ll> a(n);
for (auto& it : a) cin >> it;
ll total = 0;
ll ans = 0;
for (ll i = 0; i < n; i++) {
if (total > 0) {
if (total + a[i] >= 0) {
ans += abs(-total - 1 - a[i]);
a[i] = -total - 1;
}
} else {
if (total + a[i] <= 0) {
ans += abs(-total + 1 - a[i]);
a[i] = -total + 1;
}
}
total += 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 | UNKNOWN | fun main(args: Array<String>) {
val n= readLine()!!.toInt()
var a = readLine()!!.split(" ").map{it.toInt()}
//先頭がプラス
var move1=0
var sum1=0
for(i in 0..n-1){
sum1+=a[i]
if(i%2==0 && sum1<1){
move1 += 1-sum1
sum1 = 1
}
if(i%2==1 && sum1>-1){
move1 += sum1-(-1)
sum1 = -1
}
}
//先頭がマイナス
var move2=0
var sum2=0
for(i in 0..n-1){
sum2+=a[i]
if(i%2==1 && sum2<1){
move2 += 1-sum2
sum2 = 1
}
if(i%2==0 && sum2>-1){
move2 += sum2-(-1)
sum2 = -1
}
}
println(Math.min(move1,move2))
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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>
void chmax(T &a, T b) {
if (a < b) a = b;
}
template <class T>
void chmin(T &a, T b) {
if (a > b) a = b;
}
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < (n); i++) cin >> a[i];
long long sum = a[0];
long long change1 = 0LL;
long long change2 = 0LL;
for (int i = 1; i < n; i++) {
if (i % 2 == 1 && sum + a[i] >= 0) {
sum += a[i];
change1 += abs(-1LL - sum);
sum = -1LL;
} else if (i % 2 == 0 && sum + a[i] <= 0) {
sum += a[i];
change1 += abs(1LL - sum);
sum = 1LL;
} else {
sum += a[i];
}
}
for (int i = 1; i < n; i++) {
if (i % 2 == 0 && sum + a[i] >= 0) {
sum += a[i];
change2 += abs(-1LL - sum);
sum = -1LL;
} else if (i % 2 == 1 && sum + a[i] <= 0) {
sum += a[i];
change2 += abs(1LL - sum);
sum = 1LL;
} else {
sum += a[i];
}
}
cout << min(change1, change2);
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>
int32_t sgn(int32_t val) { return (val > 0) - (val < 0); }
int main() {
uint32_t n = 0;
std::cin >> n;
uint64_t op = 0;
int32_t sum = 0;
std::cin >> sum;
for (size_t i = 1; i < n; i++) {
int32_t a_i = 0;
std::cin >> a_i;
if (sgn(sum) * sgn(sum + a_i) != -1) {
int32_t diff = -sgn(sum) - (sum + a_i);
a_i += diff;
op += std::abs(diff);
}
sum += a_i;
}
std::cout << op << 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.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int []a = new int [n];
for(int i = 0; i < n ;i++) {
a[i] = sc.nextInt();
}
sc.close();
long []sum = new long [n];
int []temp = new int [n];
long count = 0;
sum[0] = a[0];
if(a[0] > 0) {
temp[0] = 1;
}
if(a[0] < 0) {
temp[0] = -1;
}
if(a[0] == 0) {
if(a[1] >0) {
count++;
temp[0] = -1;
sum[0] = -1;
}
else {
count++;
temp[0] = 1;
sum[0] = 1;
}
}
for(int i = 1 ; i < n ; i++) {
sum[i] +=(long) a[i] + sum[i-1];
if(sum[i] >0) {
temp[i] = 1;
}
if(sum[i] < 0){
temp[i] = -1;
}
if(temp[i-1] * temp [i] > 0) {
count += Math.abs(sum[i]) + 1;
sum[i] = temp[i] * -1;
temp[i] = temp[i] * -1;
}
if(temp[i] == 0) {
count++;
sum[i] = temp[i-1] * -1;
temp[i] = (int)sum[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 | n = int(input())
a = map(int, input().split(" "))
s = 0
num = 0
for i, x in enumerate(a):
if i == 0:
if x == 0:
num += 1
for j, y in enumerate(a):
if j == n - 1:
s = 1
break
elif y > 0:
s = -1
break
elif y < 0:
s = 1
break
else:
s = x
else:
if s > 0:
if s + x < 0:
s += x
else:
num += abs(-s-x-1)
s = -1
else:
if s + x > 0:
s += x
else:
num += abs(-s-x+1)
s = 1
print(num) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int keta(int num) {
int ans = 0;
int rem;
for (int i = 4; i >= 0; i--) {
rem = pow(10, i);
ans += (num / rem);
num = num % rem;
}
return ans;
}
int main() {
int n;
cin >> n;
vector<int64_t> ar(n);
for (int i = 0; i < n; i++) {
cin >> ar[i];
}
int ans1, ans2 = 0;
int cum = 0;
for (int i = 0; i < n; i++) {
int next = cum + ar[i];
if (i % 2 == 0) {
if (next <= 0) {
ans1 += abs(next - 1);
cum = 1;
} else {
cum = next;
}
} else {
if (next >= 0) {
ans1 += abs(next + 1);
cum = -1;
} else {
cum = next;
}
}
}
cum = 0;
for (int i = 0; i < n; i++) {
int next = cum + ar[i];
if (i % 2 == 0) {
if (next >= 0) {
ans2 += abs(next + 1);
cum = -1;
} else {
cum = next;
}
} else {
if (next <= 0) {
ans2 += abs(next - 1);
cum = 1;
} else {
cum = next;
}
}
}
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 num = 0;
int N;
int M = 0;
int A[100000];
cin >> N;
for (int i = 0; i < N; ++i) {
cin >> A[i];
}
for (int i = 0; i < N - 1; ++i) {
M = 0;
for (int j = 0; j <= i; ++j) {
M += A[j];
}
if ((M + A[i + 1]) * M >= 0) {
break;
}
if (i == N - 2) {
cout << num << endl;
return 0;
}
}
{
int m = 0;
for (int i = 0; i < N; ++i) {
if (m * (m + A[i]) >= 0) {
int k = -1 * (m + 1);
if (m < 0) {
k = -1 * (m - 1);
}
if (i == 0 && A[i] > 0) {
k = 1;
}
num += abs(k - A[i]);
A[i] = k;
}
m += A[i];
}
}
cout << num << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define INF 1e12
#define PB push_back
#define PF push_front
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vpi vector<pii>
#define vll vector<ll>
#define vpl vector<pll>
#define vvi vector<vector<int>>
#define vvl vector<vector<ll>>
#define MX(x) *max_element(all(x))
#define MN(x) *min_element(all(x))
#define ios ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define pr_d(x) cout << fixed << setprecision(15) << x << endl
#define ud(c, x) distance(c.begin(), upper_bound(all(c), x))
#define ld(c, x) distance(c.begin(), lower_bound(all(c), x))
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, a, b) for (int i = (a); i < (b); ++i)
#define rep3(i, n) for (int i = (n - 1); i >= 0; --i)
#define rep4(i, a, b) for (int i = (a); i > (b); --i)
#define pb push_back
#define out(x) cout << x << "\n"
bool odd(int i) { return i % 2; }
#define all(v) v.begin(), v.end()
#define size(x) int(x.size())
int gcd(int a, int b) { return __gcd(a, b); }
int lcm(int a, int b) { return a * (b / gcd(a, b)); }
void Yes_No(bool f) {
if (f)
printf("Yes\n");
else
printf("No\n");
}
void YES_NO(bool f) {
if (f)
printf("YES\n");
else
printf("NO\n");
}
template <typename T>
void deb1(T x) {
cout << x << "\n";
}
template <typename T>
bool chmax(T& a, const T& b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T>
bool chmin(T& a, const T& b) {
if (a > b) {
a = b;
return true;
}
return false;
}
//-------------------ここから回答する-----------------------
void solve(void) {
int n;
cin >> n;
vll v(n);
rep(i, n) cin >> v[i];
int ans = 0;
if (v[0] == 0) v[0]++, ans++;
ll cta = v[0];
rep2(i, 1, n) {
ll x = v[i];
if (cta * (cta + v[i]) < 0) {
cta += x;
continue;
}
if (cta > 0)
ans += abs(-cta - 1 - x), v[i] = -cta - 1;
else
ans += abs(-cta + 1 - x), v[i] = -cta + 1;
cta += v[i];
}
out(ans);
}
int main(void) { solve(); } |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int sum;
cin >> sum;
long answer = 0;
for (int i = 1; i < n; i++) {
int a;
cin >> a;
long nextSum = sum + a;
if ((sum > 0 && nextSum < 0) || (sum < 0 && nextSum > 0)) {
sum = nextSum;
} else if (nextSum == 0) {
sum = sum > 0 ? -1 : 1;
answer += 1;
} else if (nextSum > 0) {
sum = -1;
answer += nextSum + 1;
} else if (nextSum < 0) {
sum = 1;
answer += nextSum * -1 + 1;
}
}
cout << answer << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void) {
int num, i = 1, a;
long long sum, ans = 0;
bool sig = true;
cin >> num >> sum;
if (sum < 0) sig = false;
for (; i < num; i++) {
scanf("%d", &a);
if (sum == 0) {
if (a >= 0) {
sum--;
sig = false;
} else
sum++;
ans++;
}
sum += a;
if (sig == true && sum >= 0) {
ans += sum + 1;
sum = -1;
} else if (sig == false && sum <= 0) {
ans += (-1) * sum + 1;
sum = 1;
}
sig = !sig;
}
cout << ans << "\n";
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
a = [int(x) for x in input().split()]
asum = [a[0]]
ansp,ansm,tempp,tempm = 0,0,0,0
for i in range(1,N):
A = asum[i-1] + a[i]
asum += [A]
A1 = A + tempp
if A1 == 0:
ansp += 1
tempp += ((i%2)*2-1)
elif i%2==0 and A1 < 0:
ansp += -A1 + 1
tempp += -A1 + 1
elif i%2!=0 and A1 > 0:
ansp += A1 + 1
tempp += -A1 - 1
A2 = A + tempm
if A2 == 0:
ansm += 1
tempm += -((i%2)*2-1)
elif i%2!=0 and A2 < 0:
ansm += -A2 + 1
tempm += -A2 + 1
elif i%2==0 and A2 > 0:
ansm += A2 + 1
tempm += -A2 - 1
print(min(ansp,ansm)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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 -*-
trial_num = int(input())
number_lists = list(map(int, input().split()))
def main():
sum_num = 0
change_num = 0
if(number_lists[0] < 0):
for i in range(trial_num):
number_lists[i] = -1 * number_lists[i]
elif(number_lists[0] == 0):
number_lists[0] = 1
change_num += 1
for i in range(trial_num):
if(i == 0):
sum_num = number_lists[0]
else:
if(i%2 == 1):
if(sum_num + number_lists[i] < 0):
sum_num += number_lists[i]
else:
change_num += abs(sum_num + number_lists[i]) + 1
sum_num = -1
else:
if (sum_num + number_lists[i] > 0):
sum_num += number_lists[i]
else:
change_num += abs(sum_num + number_lists[i]) + 1
sum_num = 1
print(change_num)
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;
using ll = int64_t;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int DX[] = {1, 1, 0, -1, -1, -1, 0, 1};
int DY[] = {0, -1, -1, -1, 0, 1, 1, 1};
int n;
ll hoge(ll a[]) {
ll ans = 0;
int temp = 0;
for (int(i) = 0; (i) < (n); (i)++) {
if (temp > 0 && temp + a[i] > 0) {
ans += abs(-1 - temp - a[i]);
temp = -1;
} else if (temp < 0 && temp + a[i] < 0) {
ans += abs(1 - temp - a[i]);
temp = 1;
} else if (temp + a[i] == 0) {
if (temp > 0) {
temp = -1;
} else {
temp = 1;
}
ans += 1;
} else {
temp += a[i];
}
}
return ans;
}
void solve() {
cin >> n;
ll a[n];
for (int(i) = 0; (i) < (n); (i)++) cin >> a[i];
ll ans1 = hoge(a);
int temp = 0;
if (a[0] > 0) {
temp += (a[0] * (-1) - 1);
a[0] = -1;
} else {
temp = (a[0] * (-1) + 1);
a[0] = 1;
}
ll ans2 = hoge(a) + temp;
cout << min(ans1, ans2) << endl;
}
int main() {
solve();
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | 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 == 0 && l1[i] == 0) x++, s++;
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;
long long int ans1, ans2, sum1, sum2;
int n, i;
long long int a[100005];
int main() {
cin >> n;
for (i = 1; i <= n; i++) {
cin >> a[i];
}
ans1 = 0;
ans2 = 0;
sum1 = 0;
sum2 = 0;
if (a[1] >= 0) {
sum1 += a[1];
ans2 += a[1] + 1;
sum2 += -1;
} else {
sum1 = 1;
ans1 += abs(a[1]) + 1;
sum2 += a[1];
}
for (i = 2; i <= n; i++) {
if (sum1 > 0) {
if (a[i] + sum1 >= 0) {
ans1 += a[i] + sum1 + 1;
sum1 = -1;
} else {
sum1 += a[i];
}
} else {
if (a[i] + sum1 <= 0) {
ans1 += abs(sum1 + a[i]) + 1;
sum1 = 1;
} else
sum1 += a[i];
}
}
for (i = 2; i <= n; i++) {
if (sum2 > 0) {
if (a[i] + sum2 >= 0) {
ans2 += a[i] + sum2 + 1;
sum2 = -1;
} else {
sum2 += a[i];
}
} else {
if (a[i] + sum2 <= 0) {
ans2 += abs(sum2 + a[i]) + 1;
sum2 = 1;
} else
sum2 += a[i];
}
}
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 ll = long long;
using namespace std;
struct aaa {
aaa() {
cin.tie(0);
ios::sync_with_stdio(0);
cout << fixed << setprecision(20);
};
} aaaaaaa;
int MOD = 1e9 + 7;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return (a * b) / gcd(a, b); }
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int N;
int main() {
cin >> N;
vector<int> a(N);
for (int i = 0; i < (N); ++i) cin >> a[i];
long ans1 = 0, ans2 = 0, sum = 0;
if (a[0] > 0) {
sum = a[0];
} else if (a[0] == 0) {
sum = 1, ans1++;
} else {
sum = 1, ans1 += -a[0] + 1;
}
for (int i = 1; i <= (N - 1); ++i) {
if ((sum + a[i]) * sum >= 0) {
int k;
if (sum > 0)
k = -sum - 1;
else
k = -sum + 1;
sum += k;
ans1 += abs(k - a[i]);
}
}
sum = 0;
if (a[0] < 0) {
sum = a[0];
} else if (a[0] == 0) {
sum--, ans2++;
} else {
sum = -1, ans2 += a[0] + 1;
}
for (int i = 1; i <= (N - 1); ++i) {
if ((sum + a[i]) * sum >= 0) {
int k;
if (sum > 0)
k = -sum - 1;
else
k = -sum + 1;
sum += k;
ans2 += abs(k - a[i]);
}
}
cout << min(ans1, ans2) << '\n';
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
ans = 0
while a[0] == 0:
if a[1] > 0:
a[0] = -1
elif a[1] < 0:
a[0] = 1
a.pop(0)
ans += 1
if a[0] < 0:
a = list(map(lambda x: x * (-1), a))
product = 0
for i in range(len(a)):
product += a[i]
if i % 2 == 0:
if product <= 0:
ans += abs(product) + 1
product = 1
elif i % 2 == 1:
if product >= 0:
ans += abs(product) + 1
product = -1
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import numpy as np
n = input()
a_s = [int(x) for x in input().split()]
sum_i = 0
manipulate_num = 0
for a in a_s:
new_sum_i = sum_i + a
# print(f"a:{a}, sum_i:{sum_i}, new_sum_i:{new_sum_i}")
if new_sum_i == 0:
manipulate_num += 1
if new_sum_i * sum_i > 0:
manipulate_num += abs(a)+1
new_sum_i = np.sign(new_sum_i)*(-1)
sum_i = new_sum_i
# print(f"a:{a}, sum_i:{sum_i}")
# print()
print(manipulate_num) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
const long long LLINF = 1LL << 60;
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
long long int i, n;
long long int sum = 0, ans = 0, minans;
cin >> n;
vector<int> v(n, 0), w(n, 0);
for (i = 0; i < n; i++) {
cin >> v[i];
}
sum = v[0];
w[0] = v[0];
for (i = 1; i < n; i++) {
if ((sum < 0 && sum + v[i] > 0) || (sum > 0 && sum + v[i] < 0)) {
w[i] = v[i];
sum += w[i];
continue;
}
if (sum < 0) {
ans += abs(-1 * sum + 1 - v[i]);
w[i] = -1 * sum + 1;
sum += w[i];
if (sum == 0) {
w[i]++;
sum++;
}
} else {
ans += abs(-1 * sum - 1 - v[i]);
w[i] = -1 * sum - 1;
sum += w[i];
if (sum == 0) {
w[i]--;
sum--;
}
}
}
minans = ans;
ans = 0;
w.clear();
sum = -1 * v[0];
w[0] = -1 * v[0];
for (i = 1; i < n; i++) {
if ((sum < 0 && sum + v[i] > 0) || (sum > 0 && sum + v[i] < 0)) {
w[i] = v[i];
sum += w[i];
continue;
}
if (sum < 0) {
ans += abs(-1 * sum + 1 - v[i]);
w[i] = -1 * sum + 1;
sum += w[i];
if (sum == 0) {
w[i]++;
sum++;
}
} else {
ans += abs(-1 * sum - 1 - v[i]);
w[i] = -1 * sum - 1;
sum += w[i];
if (sum == 0) {
w[i]--;
sum--;
}
}
}
minans = min(ans, minans);
cout << minans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
long long sum = 0, cnt1 = 0, cnt2 = 0;
for (int i = 0, s = 1; i < n; i++, s *= -1) {
sum += a[i];
if (sum * s <= 0) cnt1 += abs(sum) + 1, sum = s;
}
for (int i = 0, s = -1; i < n; i++, s *= -1) {
sum += a[i];
if (sum * s <= 0) cnt2 += abs(sum) + 1, sum = s;
}
cout << min(cnt1, cnt2) << 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()))
# 0 1 2 3 evn odd
# kevn + - + - + -
# kodd - + - + - +
def kf(a,flag):
a0=a[0]*flag
if a0<=0:
kevn=1-a0
sevn=1
elif a0>0:
kevn=0
sevn=a0
for i in range(1,n):
ai=a[i]*flag
if i%2==0:
if sevn+ai<=0:
kevn=kevn+1-(ai+sevn)
sevn=1
elif sevn+ai>0:
kevn=kevn
sevn=sevn+ai
elif i%2==1:
if sevn+ai<0:
kevn=kevn
sevn=sevn+ai
elif sevn+ai>=0:
kevn=kevn+1+(ai+sevn)
sevn=-1
return kevn
kevn1=kf(a,1,n)
kevn=kf(a,-1,n)
kevn=min(kevn,kevn1)
print(kevn) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
string divide[4] = {"dream", "dreamer", "erase", "eraser"};
int main() {
int N, C, K;
cin >> N;
vector<int> T(N);
for (int i = 0; i < N; i++) {
cin >> T.at(i);
}
int sum = 0;
int cnt1 = 0;
for (int i = 0; i < N; i++) {
sum += T.at(i);
if (i % 2 == 0) {
if (sum <= 0) {
cnt1 += -sum + 1;
sum = 1;
}
} else {
if (sum >= 0) {
cnt1 += sum + 1;
sum = -1;
}
}
}
int cnt2 = 0;
sum = 0;
for (int i = 0; i < N; i++) {
sum += T.at(i);
if (i % 2 == 1) {
if (sum <= 0) {
cnt2 += -sum + 1;
sum = 1;
}
} else {
if (sum >= 0) {
cnt2 += sum + 1;
sum = -1;
}
}
}
cout << min(cnt1, cnt2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<long long> A(N);
vector<long long> B(N);
long long count1 = 0;
long long count2 = 0;
for (int i = 0; i < N; ++i) {
cin >> A[i];
}
if (A[0] < 0) {
B[0] = 1;
count1 = abs(A[0]) + 1;
} else {
B[0] = A[0];
}
for (int i = 1; i < N; ++i) {
B[i] = A[i] + B[i - 1];
if (i % 2 == 1 && 0 <= B[i]) {
count1 += abs(B[i]) + 1;
B[i] = -1;
} else if (i % 2 == 0 && B[i] <= 0) {
count1 += abs(B[i]) + 1;
B[i] = 1;
}
}
if (0 < A[0]) {
B[0] = -1;
count2 = abs(A[0]) + 1;
} else {
B[0] = A[0];
}
for (int i = 1; i < N; ++i) {
B[i] = A[i] + B[i - 1];
if (i % 2 == 0 && 0 <= B[i]) {
count2 += abs(B[i]) + 1;
B[i] = -1;
} else if (i % 2 == 1 && B[i] <= 0) {
count2 += abs(B[i]) + 1;
B[i] = 1;
}
}
cout << min(count1, count2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
cin.sync_with_stdio(false);
int n;
cin >> n;
vector<ll> a(n);
ll sum = 0;
int flag = true;
for (int i = 0; i < n; i++) {
cin >> a[i];
if (sum * (sum + a[i]) > 0) {
flag = false;
}
sum += a[i];
if (sum == 0) {
flag = false;
}
}
if (flag) {
cout << 0 << endl;
return 0;
}
ll count1 = 0;
ll count2 = 0;
sum = a[0] + a[1];
if (sum == 0) {
count1 += 1;
} else if (sum < 0) {
count1 += -1 - sum;
} else {
count1 += sum + 1;
}
sum = -1;
for (int i = 2; i < n; i++) {
if (sum < 0 && sum + a[i] < 0) {
count1 += abs(sum) - a[i] + 1;
sum = 1;
} else if (sum > 0 && sum + a[i] > 0) {
count1 += abs(sum + a[i] + 1);
sum = -1;
} else {
sum += a[i];
}
if (sum == 0) {
count1 += 1;
if (i % 2) {
sum = -1;
} else {
sum = 1;
}
}
}
sum = a[0] + a[1];
if (sum == 0) {
count2 += 1;
} else if (sum < 0) {
count2 += 1 - sum;
} else {
count2 += sum - 1;
}
sum = 1;
for (int i = 2; i < n; i++) {
if (sum < 0 && sum + a[i] < 0) {
count2 += abs(sum) - a[i] + 1;
sum = 1;
} else if (sum > 0 && sum + a[i] > 0) {
count2 += abs(sum + a[i] + 1);
sum = -1;
} else {
sum += a[i];
}
if (sum == 0) {
count2 += 1;
if (i % 2) {
sum = 1;
} else {
sum = -1;
}
}
}
cout << min(count1, count2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
using System.Text;
using System.Linq;
using System.Collections.Generic;
class Program {
const long mod = 1000000007;
//public static int[] max;
static void Main(string[] args) {
char[] cs = new char[] { ' ', ',' };
int val = int.Parse(Console.ReadLine());
int[] vals = parseAry(Console.ReadLine().Split(cs));
//string str = Console.ReadLine();
//string[] strs = Console.ReadLine().Split(cs);
//int[] vals = parseAry(Console.ReadLine().Split(cs));
//int n = vals[0];
//string res="";
//int y = vals[0];
//int x = vals[1];
int res = 0;
int sum = vals[0];
for (int i = 1; i < vals.Length; i++) {
int pre = sum;
int cnt;
int f = 1;
if (sum < 0) { f = -1; }
sum += vals[i];
if (( sum * f ) < 0) {
//ok
} else {
if (f == -1) {
cnt = Math.Abs(pre + vals[i]);
} else {
cnt = (pre + vals[i]+1)*-1;
}
sum += cnt;
res += Math.Abs(cnt);
if (sum == 0) {
sum += f*-1;
res++;
}
}
}
Console.WriteLine(res);
return;
}
// String[] -----> int[]
static int[] parseAry(string[] str) {
int[] nums = new int[str.Length];
for (int i = 0; i < str.Length; i++) { nums[i] = int.Parse(str[i]); }
return nums;
}
// String[] -----> long[]
static long[] parseAryl(string[] str) {
long[] nums = new long[str.Length];
for (int i = 0; i < str.Length; i++) { nums[i] = int.Parse(str[i]); }
return nums;
}
//文字列を1文字ずつString[]に入れる
static string[] strAry(String str) {
string[] re = new string[str.Length];
for (int i = 0; i < str.Length; i++) { re[i] = str[i].ToString(); }
return re;
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 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 <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <cmath>
#include <cctype>
typedef long long ll;
using namespace std;
int main() {
int N;
ll res, ans;
int count = 0;
cin >> N;
int a[N];
for (int i = 0; i < N; i++) {
cin >> a[i];
}
ll sum = 0;
for (int i = 1, s = 1; i <= N; i++, s *= -1) {
sum += a[i];
if (sum * s <= 0) {
res += abs(sum - s);
sum = s;
}
}
ll sum = 0;
for (int i = 1, s = -1; i <= N; i++, s *= -1) {
sum += a[i];
if (sum * s <= 0) {
ans = abs(sum - s);
sum = s;
}
}
cout << min(res, ans) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[]) {
long long int n, ans = 0, sum = 0;
cin >> n;
vector<long long int> a(n);
for (size_t i = 0; i < n; i++) {
cin >> a[i];
if (sum * (sum + a[i]) > 0) {
if (sum < 0) {
ans += abs(1 - (sum + a[i]));
a[i] += abs(1 - (sum + a[i]));
} else if (sum > 0) {
ans += abs(-1 - (sum + a[i]));
a[i] += -1 - (sum + a[i]);
}
}
if (sum + a[i] == 0) {
if (sum > 0)
a[i]--;
else
a[i]++;
ans++;
}
sum += a[i];
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
template <typename T>
void showvector(vector<T> v) {
for (T x : v) cout << x << " ";
cout << "\n";
}
template <typename T>
void showvector1(vector<T> v) {
long long int n = v.size();
for (long long int i = 1; i <= n - 1; i++) cout << v[i] << "\n";
}
template <typename T>
void showset(set<T> s) {
for (T x : s) cout << x << " ";
cout << "\n";
}
template <class T>
void showvectorpair(vector<T> v) {
for (auto it = v.begin(); it != v.end(); it++)
cout << it->first << " " << it->second << "\n";
cout << "\n";
}
template <typename T, typename P>
void showmap(map<T, P> m) {
for (auto it = m.begin(); it != m.end(); it++)
cout << it->first << " " << it->second << "\n";
cout << "\n";
}
template <typename T>
bool comp(T a, T b) {
return (a > b);
}
template <class T>
bool comppair(T a, T b) {
if (a.first == b.first) return (a.second > b.second);
return (a.first > b.first);
}
bool sameparity(long long int a, long long int b) { return (a % 2 == b % 2); }
bool difparity(long long int a, long long int b) { return !(a % 2 == b % 2); }
bool isprime(long long int x) {
if (x <= 1) return false;
for (long long int i = 2; i <= sqrt(x); i++) {
if (x % i == 0) return false;
}
return true;
}
bool iseven(long long int x) { return !(x % 2); }
bool isodd(long long int x) { return (x % 2); }
void vfun() {
long long int n, k;
cin >> n;
vector<long long int> v(n);
for (long long int i = 0; i < n; i++) cin >> v[i];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long int test = 1;
while (test--) {
long long int n;
cin >> n;
vector<long long int> v(n);
for (long long int i = 0; i < n; i++) cin >> v[i];
long long int sum = v[0], psum = v[0], cnt = 0;
if (v[0] == 0) {
cnt = 1;
if (v[1] > 0)
sum = psum = -1;
else
sum = psum = 1;
}
for (long long int i = 1; i <= n - 1; i++) {
sum += v[i];
if (psum > 0) {
if (sum >= 0) {
cnt += (sum + 1);
sum = -1;
}
} else {
if (sum <= 0) {
cnt += (abs(sum) + 1);
sum = 1;
}
}
psum = sum;
}
long long int dcnt = abs(v[0]) + 1;
if (v[0] > 0)
sum = psum = -1;
else
sum = psum = 1;
for (long long int i = 1; i <= n - 1; i++) {
sum += v[i];
if (psum > 0) {
if (sum >= 0) {
dcnt += (sum + 1);
sum = -1;
}
} else {
if (sum <= 0) {
dcnt += (abs(sum) + 1);
sum = 1;
}
}
psum = sum;
}
cout << min(dcnt, cnt) << "\n";
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <bits/stdc++.h>
int main() {
int n;
scanf("%d", &n);
int a[n];
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
long long mans = 0, pans = 0;
int msub[n], psub[n];
if (a[0] == 0) {
pans = 1;
mans = 1;
psub[0] = 1;
msub[0] = -1;
}
if (a[0] > 0) {
psub[0] = a[0];
msub[0] = -1;
mans = a[0] + 1;
} else {
psub[0] = 1;
msub[0] = a[0];
pans = 1;
}
for (int i = 1; i < n; i++) {
if (i % 2 == 1 && psub[i - 1] + a[i] >= 0) {
pans += psub[i - 1] + a[i] + 1;
psub[i] = -1;
} else if (i % 2 == 0 && psub[i - 1] + a[i] <= 0) {
pans += 1 - (psub[i - 1] + a[i]);
psub[i] = 1;
} else {
psub[i] = psub[i - 1] + a[i];
}
if (i % 2 == 1 && msub[i - 1] + a[i] <= 0) {
mans += 1 - (msub[i - 1] + a[i]);
msub[i] = 1;
} else if (i % 2 == 0 && msub[i - 1] + a[i] >= 0) {
mans += msub[i - 1] + a[i] + 1;
msub[i] = -1;
} else {
msub[i] = msub[i - 1] + a[i];
}
}
printf("%lld", mans < pans ? mans : pans);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
long long sum;
cin >> n >> sum;
long long ans = 0;
if (sum == 0) {
sum = 1;
ans = 1;
}
for (int i = 0; i < n - 1; ++i) {
long long a;
cin >> a;
if ((a + sum) * sum >= 0) {
if (sum > 0) {
ans += a + sum + 1;
sum = -1;
} else {
ans += -(a + sum) + 1;
sum = 1;
}
} else {
sum += a;
}
}
cout << ans << "\n";
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
long long count1 = 0, count2 = 0;
cin >> N;
vector<long long> A(N);
for (int i = 0; i < N; i++) cin >> A[i];
long long su = A[0];
bool plus = A[0] > 0;
for (int i = 1; i < N; i++) {
plus = !plus;
su += A[i];
if (plus) {
if (su <= 0) {
count1 += -1 * su + 1;
su = 1;
}
} else {
if (su >= 0) {
count1 += su + 1;
su = -1;
}
}
}
su = A[0];
plus = A[0] < 0;
for (int i = 1; i < N; i++) {
plus = !plus;
su += A[i];
if (plus) {
if (su <= 0) {
count2 += -1 * su + 1;
su = 1;
}
} else {
if (su >= 0) {
count2 += su + 1;
su = -1;
}
}
}
cout << min(count1, count2) << 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);
vector<int> a1(n);
vector<int> a2(n);
long long sum1[100010] = {0};
long long sum2[100010] = {0};
for (int i = 0; i < n; i++) {
cin >> a[i];
a1[i] = a[i];
a2[i] = a[i];
}
int ans1 = 0;
int ans2 = 0;
for (int i = 0; i < n; i++) {
int j = i;
while (j >= 0) {
sum1[i] += a1[j];
j--;
}
if (i % 2 == 0 && sum1[i] <= 0) {
a1[i] += (1 - sum1[i]);
ans1 += (1 - sum1[i]);
sum1[i] += (1 - sum1[i]);
} else if (i % 2 == 1 && sum1[i] >= 0) {
a1[i] -= (sum1[i] + 1);
ans1 += (sum1[i] + 1);
sum1[i] -= (1 + sum1[i]);
}
}
if (sum1[n - 1] == 0) ans1++;
for (int i = 0; i < n; i++) {
int j = i;
while (j >= 0) {
sum2[i] += a2[j];
j--;
}
if (i % 2 == 0 && sum2[i] >= 0) {
a2[i] -= (1 + sum2[i]);
ans2 += (sum2[i] + 1);
sum2[i] -= (1 + sum2[i]);
} else if (i % 2 == 1 && sum2[i] <= 0) {
a2[i] += (1 - sum2[i]);
ans2 += (1 - sum2[i]);
sum2[i] += (1 - sum2[i]);
}
}
if (sum2[n - 1] == 0) ans2++;
if (ans1 >= ans2)
cout << ans2 << endl;
else
cout << ans1 << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
b = a
count0 = 0
memo = 0
for i in range(n):
if i % 2 == 0:
if a[i] <= 0:
count0 += 1 - a[i]
memo = 1 - a[i]
else:
if a[i] >= 0:
count0 += a[i] + 1
memo = -(a[i] + 1)
if i + 1 < n:
a[i+1] = a[i+1] + a[i] + memo
count1 = 0
memo = 0
a = b
for i in range(n):
if i % 2 == 0:
if a[i] >= 0:
count1 += a[i] + 1
memo = -(a[i] + 1)
else:
if a[i] <= 0:
count1 += 1 - a[i]
memo = 1 - a[i]
if i + 1 < n:
a[i+1] = a[i+1] + a[i] + memo
print(min(count0, count1))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL << 60;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<int> A(n);
vector<int> B(n + 1);
vector<int> B2(n + 1);
B[0] = 0;
B2[0] = 0;
for (long long i = 0; i < n; i++) {
cin >> A[i];
B[i + 1] = A[i] + B[i];
B2[i + 1] = B[i + 1];
}
int sum_p = 0;
int pm = 0;
for (long long i = 1; i < n + 1; i++) {
int del = 0;
if (i % 2 && B[i] + pm <= 0) del = abs(B[i] + pm) + 1;
if (i % 2 == 0 && B[i] + pm >= 0) del = -(B[i] + pm + 1);
pm += del;
sum_p += abs(del);
}
int sum_m = 0;
pm = 0;
for (long long i = 1; i < n + 1; i++) {
int del = 0;
if (i % 2 == 0 && B2[i] + pm <= 0) del = abs(B2[i] + pm) + 1;
if (i % 2 && B2[i] + pm >= 0) del = -(B2[i] + pm + 1);
pm += del;
sum_m += abs(del);
}
cout << min(sum_p, sum_m) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<long long> a(N);
for (int i = 0; i < N; i++) cin >> a[i];
for (int i = 1; i < N; i++) a[i] += a[i - 1];
vector<long long> b(a);
int ansp = 0;
int n = 0;
for (int i = 0; i < N; i++) {
if (i % 2) {
if (a[i] <= 0) {
int c = 1 - a[i];
n += c;
ansp += c;
}
} else {
if (a[i] >= 0) {
int c = 1 + a[i];
n -= c;
ansp += c;
}
}
if (i < N - 1) a[i + 1] += n;
}
int ansm = 0;
n = 0;
for (int i = 0; i < N; i++) {
if (i % 2 == 0) {
if (b[i] <= 0) {
int c = 1 - b[i];
n += c;
ansm += c;
}
} else {
if (b[i] >= 0) {
int c = 1 + b[i];
n -= c;
ansm += c;
}
}
if (i < N - 1) b[i + 1] += n;
}
cout << min(ansp, ansm) << "\n";
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int t;
int answer = 0;
int sumi;
bool flag;
cin >> t;
vector<int> A(t);
cin >> A[0];
sumi = A[0];
if (sumi > 0) {
flag = true;
} else if (sumi < 0) {
flag = false;
} else {
answer += 1;
A[0] += 1;
sumi += 1;
flag = true;
}
for (int i = 1; i < t; i++) {
cin >> A[i];
sumi += A[i];
if (sumi == 0) {
answer += 1;
if (flag) {
sumi = -1;
} else {
sumi = 1;
}
} else if (sumi > 0 == flag) {
answer += sumi + 1;
if (sumi > 0) {
sumi = -1;
} else {
sumi = 1;
}
}
flag = !flag;
}
cout << answer << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text.RegularExpressions;
using System.Diagnostics;
//var input = Console.ReadLine().Split().Select(int.Parse).ToArray();
namespace AtCoderSolve
{
class Solve
{
const int mod = 1000000007;
static void Main(string[] args)
{
//var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
//Console.SetOut(sw);
int N = int.Parse(Console.ReadLine());
var a = Console.ReadLine().Split().Select(long.Parse).ToArray();
long[] sum = new long[N];
long[] ans = new long[2];
long c = 1;
for (var j = 0; j < 2; j++)
{
for (var i = 0; i < N; i++)
{
if (i == 0)
{
sum[i] = a[i];
}
else
{
sum[i] = sum[i - 1] + a[i];
}
if (sum[i] * c <= 0)
{
ans[j] += 1 + Math.Abs(sum[i]);
sum[i] = c;
}
c *= -1;
}
c *= -1;
}
Console.WriteLine(ans.Min());
//Console.Out.Flush();
}
}
public class Calculation
{
}
public class Graph
{
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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 int MOD = 1000000007;
const long long L_INF = 1LL << 60;
const int INF = 2147483647;
const double PI = acos(-1);
template <class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T>
void debug(T v) {
for (int i = 0; i < v.size(); ++i) cout << v[i] << " ";
cout << endl;
}
const long long int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const long long int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long int n;
cin >> n;
vector<long long int> a(n), s(n + 1);
for (int i = 0; i < n; ++i) cin >> a[i];
long long int res = 0;
if (a[0] == 0) {
if (a[1] > 0)
s[1] = -1;
else
s[1] = 1;
res++;
} else
s[1] = a[0];
for (int i = 1; i < n; ++i) {
if (s[i] < 0) {
if (a[i] < 0) {
res += abs(a[i]) + abs(s[i]) + 1;
s[i + 1] = 1;
} else {
s[i + 1] = s[i] + a[i];
if (s[i + 1] <= 0) {
res += abs(s[i + 1]) + 1;
s[i + 1] = 1;
}
}
} else {
if (a[i] <= 0) {
s[i + 1] = s[i] + a[i];
if (s[i + 1] >= 0) {
res += s[i + 1] + 1;
s[i + 1] = -1;
}
} else {
res += abs(a[i]) + abs(s[i]) + 1;
s[i + 1] = -1;
}
}
}
cout << res << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | def 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))]
a = L[0]
c0,c1=0,0
SL = []
s = 0
for i,l in enumerate(L):
s += L[i]
SL.append(s)
if (a>0):
c0 = resolve(SL)
c1 = (a+1) + resolve(list(map(lambda x:x-(a+1), SL)))
elif (a<0):
c0 = resolve(SL)
c1 = (-a+1) + resolve(list(map(lambda x:x+(-a+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() {
int n;
cin >> n;
vector<int> a(n), b(n);
for (long long(i) = (0); (i) < (long long)(n); ++(i))
cin >> a[i], b[i] = a[i];
long long sum = a[0];
long long ans = 0;
if (sum == 0) sum++, ans++;
for (int i = 1; i < n; ++i) {
if ((sum > 0 and sum + a[i] < 0) or (sum < 0 and sum + a[i] > 0)) {
sum += a[i];
} else {
if (sum > 0) {
sum += a[i];
for (; sum >= 0; --sum) {
++ans;
}
} else {
sum += a[i];
for (; sum <= 0; ++sum) {
++ans;
}
}
}
}
long long ans2 = 0;
sum = a[0];
if (sum > 0) {
for (; sum >= 0; --sum) {
++ans2;
}
} else {
for (; sum <= 0; ++sum) {
++ans2;
}
}
if (sum == 0) sum--, ans2++;
for (int i = 1; i < n; ++i) {
if ((sum > 0 and sum + a[i] < 0) or (sum < 0 and sum + a[i] > 0)) {
sum += a[i];
} else {
if (sum > 0) {
sum += a[i];
for (; sum >= 0; --sum) {
++ans2;
}
} else {
sum += a[i];
for (; sum <= 0; ++sum) {
++ans2;
}
}
}
}
cout << min(ans, ans2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int s[maxn];
int ans[maxn];
int anp[maxn];
int main() {
int n, j;
cin >> n;
long long sum = 0;
for (int i = 1; i <= n; i++) {
cin >> s[i];
}
for (int i = 1; i < n; i++) {
ans[i] = ans[i - 1] + s[i];
if (ans[i] > 0) {
if (s[i + 1] >= 0) {
sum += (s[i + 1] + ans[i] + 1);
s[i + 1] = -(ans[i] + 1);
} else {
if (abs(s[i + 1]) > ans[i]) {
} else {
sum += (s[i + 1] + ans[i] + 1);
s[i + 1] = -(ans[i] + 1);
}
}
} else if (ans[i] == 0) {
sum++;
} else if (ans[i] < 0) {
if (s[i + 1] > 0) {
if (abs(ans[i]) < s[i + 1]) {
} else {
sum += (1 - ans[i] - s[i + 1]);
s[i + 1] = -ans[i] + 1;
}
} else {
sum += (1 - ans[i] - s[i + 1]);
s[i + 1] = -ans[i] + 1;
}
}
}
cout << sum << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | "use strict";
const main = arg => {
arg = arg.trim().split("\n");
const N = parseInt(arg[0]);
const A = arg[1].split(" ").map(n=>parseInt(n));
let totalSum = A[0];
let answer = 0;
for(let i=1; i<N; i++) {
if(totalSum >= 0 && totalSum + A[i] >= 0) {
let origin = A[i];
A[i] -= totalSum + A[i] + 1;
answer += origin + 1;
}
if(totalSum <= 0 && totalSum + A[i] <= 0) {
A[i] += (totalSum + A[i]) + 2;
answer += (totalSum + A[i]) + 2;
}
totalSum += A[i];
}
console.log(answer);
}
main(require('fs').readFileSync('/dev/stdin', 'utf8')); |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import numpy as np
n = int(input())
a = list(map(int,input().split()))
cnt=0
sum_a=0
for i in range(n-1):
sum_a += a[i]
if abs(sum_a) >= abs(a[i+1]) or sum_a*a[i+1]>=0:
cnt += abs(-sum_a-np.sign(sum_a) -a[i+1])
a[i+1]=-sum_a-np.sign(sum_a)
print(cnt) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | 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);
}
vector<int> b(n);
b[0] = a[0];
int ansa = 0, ansb = 0, sum = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
if (b[i] >= 0) {
ansa += 1 + b[i];
b[i] = -1;
b[i + 1] = b[i] + a[i + 1];
} else
b[i + 1] = b[i] + a[i + 1];
} else {
if (b[i] <= 0) {
ansa += 1 - b[i];
b[i] = 1;
b[i + 1] = b[i] + a[i + 1];
} else
b[i + 1] = b[i] + a[i + 1];
}
}
b[0] = a[0];
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
if (b[i] <= 0) {
ansb += 1 - b[i];
b[i] = 1;
b[i + 1] = b[i] + a[i + 1];
} else
b[i + 1] = b[i] + a[i + 1];
} else {
if (b[i] >= 0) {
ansb += 1 + b[i];
b[i] = -1;
b[i + 1] = b[i] + a[i + 1];
} else
b[i + 1] = b[i] + a[i + 1];
}
}
int ans = min(ansa, ansb);
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int check(long long int a) {
if (a > 0) return 1;
if (a < 0) return 2;
return 0;
}
int main() {
long long int sum, n, i, cnt = 0;
cin >> n;
long long int x, a[n];
for (i = 0; i < n; i++) cin >> a[i];
sum = a[0];
for (i = 1; i < n; i++) {
if (check(sum + a[i]) == check(sum)) {
if (sum < 0) {
x = 1 - sum;
cnt += (x - a[i]);
sum = 1;
} else {
x = -1 - sum;
cnt += (a[i] - x);
sum = -1;
}
} else
sum += a[i];
if (sum == 0) cnt++;
}
cout << cnt;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
long[] a_ = new long[n];
for(int i = 0; i < n; i++){
a_[i] = scan.nextLong();
}
long[] sum_ = new long[n];
sum_[0] = a_[0];
long count1 = 0;
long count2 = 0;
//sum_[0] >= 1
if(sum_[0] < 1){
count1 = 1-sum_[0];
}else{
}
for(int i = 1; i < n; i++){
sum_[i] = sum_[i-1]+a_[i];
if(i % 2 != 0){
if(sum_[i] <= -1){
//OK
}else{
count1 += sum_[i] + 1;
sum_[i] = -1;
}
}else{
if(sum_[i] >= 1){
//OK
}else{
count1 += 1 - sum_[i];
sum_[i] = 1;
}
}
}
//sum_[0] <= -1
if(sum_[0] > -1){
count2 = sum_[0]+1;
}else{
}
for(int i = 1; i < n; i++){
sum_[i] = sum_[i-1]+a_[i];
if(i % 2 != 0){
if(sum_[i] >= 1){
//OK
}else{
count2 += 1-sum_[i];
sum_[i] = 1;
}
}else{
if(sum_[i] <= -1){
//OK
}else{
count2 += sum_[i]+1;
sum_[i] = -1;
}
}
}
System.out.println(Math.min(count1, count2));
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long total = a[0], ans = 0;
bool flg1 = false, flg2 = false;
if (total != 0)
for (int i = 1; i < n; i++) {
if (total < 0) {
flg1 = false;
} else {
flg1 = true;
}
if (flg1) {
total += a[i];
if (total >= 0) {
ans += total + 1;
total = -1;
}
} else {
total += a[i];
if (total <= 0) {
ans += abs(total - 1);
total = 1;
}
}
}
else {
total = -1;
long long ans1 = 1, ans2 = 1;
for (int i = 1; i < n; i++) {
if (total < 0) {
flg1 = false;
} else {
flg1 = true;
}
if (flg1) {
total += a[i];
if (total >= 0) {
ans1 += total + 1;
total = -1;
}
} else {
total += a[i];
if (total <= 0) {
ans1 += abs(total - 1);
total = 1;
}
}
}
total = 1;
for (int i = 1; i < n; i++) {
if (total < 0) {
flg1 = false;
} else {
flg1 = true;
}
if (flg1) {
total += a[i];
if (total >= 0) {
ans2 += total + 1;
total = -1;
}
} else {
total += a[i];
if (total <= 0) {
ans2 += abs(total - 1);
total = 1;
}
}
}
ans = min(ans1, ans2);
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AtCoder
{
class Program
{
static void Main(string[] args)
{
int n = int.Parse(Console.ReadLine());
int[] a = new int[n];
int[] sum = new int[n];
string[] lines = Console.ReadLine().Split(' ');
for (int i = 0; i < n; i++)
{
a[i] = int.Parse(lines[i]);
}
int ans1 = 0;
int ans2 = 0;
int sign = 1;
sum[0] = a[0];
for (int i = 1; i < n; i++)
{
sum[i] = sum[i - 1] + a[i];
if (sign > 0)
{
if (sum[i] >= 0)
{
ans1 += 1 + sum[i];
sum[i] = -1;
}
}
else
{
if (sum[i] <= 0)
{
ans1 += 1 - sum[i];
sum[i] = 1;
}
}
sign = -sign;
}
sign = -1;
sum[0] = a[0];
for (int i = 1; i < n; i++)
{
sum[i] = sum[i - 1] + a[i];
if (sign > 0)
{
if (sum[i] >= 0)
{
ans2 += 1 + sum[i];
sum[i] = -1;
}
}
else
{
if (sum[i] <= 0)
{
ans2 += 1 - sum[i];
sum[i] = 1;
}
}
sign = -sign;
}
Console.WriteLine(Math.Min(ans1, ans2));
}
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, i;
cin >> n;
long long a[100010];
long long ans = 0;
long long cnt = 0;
int flag = 1;
for (i = 0; i < n; i++) cin >> a[i];
if (a[0] > 0)
flag = 1;
else if (a[0] < 0)
flag = -1;
for (i = 0; i < n; i++)
if (a[i] != 0) break;
if (i != 0) {
if (a[i] > 0) {
if (i % 2)
flag = -1;
else
flag = 1;
} else {
if (i % 2) flag = 1;
flag = -1;
}
}
cnt = a[0];
for (i = 1; i < n; i++) {
cnt += a[i];
if (cnt * flag >= 0) {
ans += abs(cnt) + 1;
if (flag == -1) {
cnt = 1;
} else {
cnt = -1;
}
}
if (flag == -1) {
flag = 1;
} else {
flag = -1;
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long int;
const ll INF = (1LL << 32);
const ll MOD = (ll)1e9 + 7;
const double EPS = 1e-9;
ll dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
ll dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
signed main() {
ios::sync_with_stdio(false);
ll n;
cin >> n;
vector<ll> a;
for (ll i = 0; i < n; i++) {
ll x;
cin >> x;
a.push_back(x);
}
ll sum = a[0];
ll ans = 0;
for (ll i = (1); i < (n); i++) {
if (sum > 0 and (sum + a[i]) >= 0) {
while (sum + a[i] != -1) {
a[i]--;
ans++;
}
} else if (sum < 0 and (sum + a[i]) <= 0) {
while (sum + a[i] != 1) {
a[i]++;
ans++;
}
}
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 | UNKNOWN | #include <bits/stdc++.h>
int main(void) {
int i, a, n;
long long int sum = 0, bsum = 0, ans = 0;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &a);
bsum = sum;
sum += a;
if (bsum > 0) {
if (sum > 0) {
do {
sum--;
ans++;
} while (sum >= 0);
sum = -1;
}
if (sum == 0) {
ans++;
sum = -1;
}
}
if (bsum < 0) {
if (sum < 0) {
do {
sum++;
ans++;
} while (sum <= 0);
sum = 1;
}
if (sum == 0) {
ans++;
sum = 1;
}
}
}
printf("%lld\n", ans);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int n; cin >> n;
vector<int> a(n + 1); for (int i = 1; i <= n; i++) cin >> a[i];
vector<int> a1; a1 = a;
long long int sum1 = 0, ans1 = ;;
for (int i = 1; i <= n; i++) {
sum1 += a1[i];
if (i % 2 == 1 && sum1 <= 0) {
int plus = 1 - sum1;
sum1 = 1;
ans1 += plus;
continue;
} if (i % 2 == 0 && sum1 >= 0) {
int minus = 1 + sum1;
sum1 = -1;
ans1 += minus;
}
}
vector<int> a2; a2 = a;
long long int sum2 = 0, ans2 = 0;
for (int i = 1; i <= n; i++) {
sum2 += a2[i];
if (i % 2 == 1 && sum2 >= 0) {
int minus = 1 + sum2;
sum2 = -1;
ans2 += minus;
continue;
} else if (i % 2 == 0 && sum2 <= 0) {
int plus = 1 - sum2;
sum2 = 1;
ans2 += plus;
}
}
cout << min(ans1, ans2) << endl;
return 0;
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | n = gets.to_i
ary = gets.split(' ').map(&:to_i)
sum = ary[0]
cnt = 0
if ary[0] == 0
if ary[1] > 0
sum = -1
else
sum = 1
end
cnt = 1
end
(1...n).each{ |i|
if sum < 0
sum += ary[i]
if sum <= 0
diff = -sum+1
cnt += diff
sum += diff
end
elsif sum > 0
sum += ary[i]
if sum >= 0
diff = sum+1
cnt += diff
sum -= diff
end
end
}
puts 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;
vector<int> v(1e5 + 7);
long long solve_pos(int n) {
long long sum = v[0];
long long ans = 0;
if (sum < 0) sum = 1, ans = abs(v[0]) + 1;
for (int i = 1; i < n; i++) {
if (sum < 0 && sum + v[i] > 0)
sum += v[i];
else if (sum > 0 && sum + v[i] < 0)
sum += v[i];
else if (sum < 0 && sum + v[i] <= 0)
ans += abs(sum + v[i]) + 1, sum = 1;
else if (sum > 0 && sum + v[i] >= 0)
ans += abs(sum + v[i]) + 1, sum = -1;
}
return ans;
}
long long solve_neg(int n) {
long long sum = v[0];
long long ans = 0;
if (sum > 0) sum = -1, ans = abs(v[0]) + 1;
for (int i = 1; i < n; i++) {
if (sum < 0 && sum + v[i] > 0)
sum += v[i];
else if (sum > 0 && sum + v[i] < 0)
sum += v[i];
else if (sum < 0 && sum + v[i] <= 0)
ans += abs(sum + v[i]) + 1, sum = 1;
else if (sum > 0 && sum + v[i] >= 0)
ans += abs(sum + v[i]) + 1, sum = -1;
}
return ans;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> v[i];
long long ans = solve_pos(n);
ans = min(ans, solve_neg(n));
cout << ans;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | a = int(input())
b = list(map(int, input().split()))
num = 0
k = b[0] + b[1]
if k == 0:
if b[0] >= 0:
k -= 1
else:
k += 1
num += 1
for i in range(a-2):
if k > 0:
k += b[i+2]
while k > -1:
k -= 1
num += 1
else:
k += b[i+2]
while k < 1:
k += 1
num += 1
print(num) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const long long mod2 = 998244353;
const long long INF = 1e18;
const long double EPS = 1e-10;
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < (n); ++i) cin >> a[i];
long long ans = 0;
if (a[0] == 0) {
a[0] = 1;
for (int i = 0; i < (n); ++i) {
if (a[i] != 0) {
if (a[i] > 0 && i % 2 == 1) a[i] = -1;
if (a[i] < 0 && i % 2 == 0) a[i] = -1;
break;
}
}
ans++;
}
for (int i = 0; i < (n - 1); ++i) {
a[i + 1] += a[i];
if (a[i] * a[i + 1] >= 0) {
ans += abs(a[i + 1]) + 1;
if (a[i + 1] > 0)
a[i + 1] = -1;
else
a[i + 1] = 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 | n = int(input())
a = list(map(int, input().split()))
ans = 0
cumsum = a[0]
p = a[0] > 0
for i in range(1, n):
if p:
if cumsum+a[i] >= 0:
ans += cumsum+a[i]+1
cumsum = -1
else:
cumsum += a[i]
else:
if cumsum+a[i] <= 0:
ans += 1-(cumsum+a[i])
cumsum = 1
else:
cumsum += a[i]
if p: p = False
else: p = True
# print(cumsum)
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
sum = 0
A = [0] * n
for i, num in enumerate(input().split()):
sum += int(num)
A[i] = sum
print(A)
counter_plus = 0
is_plus = True
temp = 0
for i in range(n):
if is_plus == True:
temp += -min(0, A[i] - 1 + temp)
print(is_plus, temp)
counter_plus += temp
else:
temp = -max(0, A[i] + 1 + temp)
print(is_plus, temp)
counter_plus += -temp
is_plus = not(is_plus)
print()
counter_minus = 0
is_plus = False
temp = 0
for i in range(n):
if is_plus == True:
temp += -min(0, A[i] - 1 + temp)
print(is_plus, temp)
counter_minus += temp
else:
temp = -max(0, A[i] + 1 + temp)
print(is_plus, temp)
counter_minus += -temp
is_plus = not(is_plus)
print(min(counter_plus, counter_minus)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using mii = map<int, int>;
using pqls = priority_queue<long long>;
using pqlg = priority_queue<long long, vector<long long>, greater<long long>>;
using mll = map<long long, long long>;
using pll = pair<long long, long long>;
using sll = set<long long>;
long long divup(long long a, long long b);
long long kaijou(long long i);
long long P(long long n, long long k);
long long C(long long n, long long k);
long long GCD(long long a, long long b);
long long LCM(long long a, long long b);
bool prime(long long N);
double distance(vector<long long> p, vector<long long> q, long long n);
void press(vector<long long> &v);
void ranking(vector<long long> &v);
void erase(vector<long long> &v, long long i);
void unique(vector<long long> &v);
void printv(vector<long long> v);
vector<ll> keta(ll x);
long long modpow(long long a, long long n, long long mod);
long long modinv(long long a, long long mod);
vector<long long> inputv(long long n);
vector<long long> yakusuu(int n);
map<long long, long long> soinsuu(long long n);
vector<vector<long long>> maze(long long i, long long j, vector<string> &s);
vector<long long> eratos(long long n);
set<long long> eraset(long long n);
long long divup(long long a, long long b) {
long long x = abs(a);
long long y = abs(b);
long long z = (x + y - 1) / y;
if ((a < 0 && b > 0) || (a > 0 && b < 0))
return -z;
else if (a == 0)
return 0;
else
return z;
}
long long kaijou(long long i) {
if (i == 0) return 1;
long long j = 1;
for (long long k = 1; k <= i; k++) {
j *= k;
}
return j;
}
long long P(long long n, long long k) {
if (n < k) return 0;
long long y = 1;
for (long long i = 0; i < k; i++) {
y *= (n - i);
}
return y;
}
long long C(long long n, long long k) {
if (n < k) return 0;
return P(n, k) / kaijou(k);
}
long long GCD(long long a, long long b) {
if (a < b) swap(a, b);
long long d = a % b;
if (d == 0) {
return b;
}
return GCD(b, d);
}
long long LCM(long long a, long long b) { return (a / GCD(a, b)) * b; }
bool prime(long long N) {
if (N == 1) {
return false;
}
if (N < 0) return false;
long long p = sqrt(N);
for (long long i = 2; i <= p; i++) {
if (N % i == 0) {
return false;
}
}
return true;
}
double distance(vector<long long> p, vector<long long> q, long long n) {
double x = 0;
for (long long i = 0; i < n; i++) {
x += pow((p.at(i) - q.at(i)), 2);
}
return sqrt(x);
}
void press(vector<long long> &v) {
long long n = v.size();
vector<long long> w(n);
map<long long, long long> m;
for (auto &p : v) {
m[p] = 0;
}
long long i = 0;
for (auto &p : m) {
p.second = i;
i++;
}
for (long long i = 0; i < n; i++) {
w.at(i) = m[v.at(i)];
}
v = w;
return;
}
void ranking(vector<long long> &v) {
long long n = v.size();
map<long long, long long> m;
long long i;
for (i = 0; i < n; i++) {
m[v.at(i)] = i;
}
vector<long long> w(n);
i = 0;
for (auto &p : m) {
v.at(i) = p.second;
i++;
}
return;
}
void erase(vector<long long> &v, long long i) {
long long n = v.size();
if (i > n - 1) return;
for (long long j = i; j < n - 1; j++) {
v.at(j) = v.at(j + 1);
}
v.pop_back();
return;
}
void unique(vector<long long> &v) {
long long n = v.size();
set<long long> s;
long long i = 0;
while (i < n) {
if (s.count(v.at(i))) {
erase(v, i);
n--;
} else {
s.insert(v.at(i));
i++;
}
}
return;
}
void printv(vector<long long> v) {
cout << "{ ";
for (auto &p : v) {
cout << p << ",";
}
cout << "}" << endl;
}
vector<ll> keta(ll x) {
if (x == 0) return {0};
ll n = log10(x) + 1;
vll w(n, 0);
for (ll i = 0; i < n; i++) {
ll p;
p = x % 10;
x = x / 10;
w[n - 1 - i] = p;
}
return w;
}
long long modpow(long long a, long long n, long long mod) {
long long res = 1;
while (n > 0) {
if (n & 1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
long long modinv(long long a, long long mod) { return modpow(a, mod - 2, mod); }
vector<long long> inputv(long long n) {
vector<long long> v(n);
for (long long i = 0; i < n; i++) {
cin >> v[i];
}
return v;
}
vector<long long> yakusuu(long long n) {
vector<long long> ret;
for (long long i = 1; i <= sqrt(n); ++i) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n) {
ret.push_back(n / i);
}
}
}
sort(ret.begin(), ret.end());
return ret;
}
map<long long, long long> soinsuu(long long n) {
map<long long, long long> m;
long long p = sqrt(n);
while (n % 2 == 0) {
n /= 2;
if (m.count(2)) {
m[2]++;
} else {
m[2] = 1;
}
}
for (long long i = 3; i * i <= n; i += 2) {
while (n % i == 0) {
n /= i;
if (m.count(i)) {
m[i]++;
} else {
m[i] = 1;
}
}
}
if (n != 1) m[n] = 1;
return m;
}
vector<vector<long long>> maze(ll i, ll j, vector<string> &s) {
ll h = s.size();
ll w = s[0].size();
queue<vector<long long>> q;
vector<vector<long long>> dis(h, vll(w, -1));
q.push({i, j});
dis[i][j] = 0;
while (!q.empty()) {
auto v = q.front();
q.pop();
if (v[0] > 0 && s[v[0] - 1][v[1]] == '.' && dis[v[0] - 1][v[1]] == -1) {
dis[v[0] - 1][v[1]] = dis[v[0]][v[1]] + 1;
q.push({v[0] - 1, v[1]});
}
if (v[1] > 0 && s[v[0]][v[1] - 1] == '.' && dis[v[0]][v[1] - 1] == -1) {
dis[v[0]][v[1] - 1] = dis[v[0]][v[1]] + 1;
q.push({v[0], v[1] - 1});
}
if (v[0] < h - 1 && s[v[0] + 1][v[1]] == '.' && dis[v[0] + 1][v[1]] == -1) {
dis[v[0] + 1][v[1]] = dis[v[0]][v[1]] + 1;
q.push({v[0] + 1, v[1]});
}
if (v[1] < w - 1 && s[v[0]][v[1] + 1] == '.' && dis[v[0]][v[1] + 1] == -1) {
dis[v[0]][v[1] + 1] = dis[v[0]][v[1]] + 1;
q.push({v[0], v[1] + 1});
}
}
return dis;
}
long long modC(long long n, long long k, long long mod) {
if (n < k) return 0;
long long p = 1, q = 1;
for (long long i = 0; i < k; i++) {
p = p * (n - i) % mod;
q = q * (i + 1) % mod;
}
return p * modinv(q, mod) % mod;
}
long long POW(long long a, long long n) {
long long res = 1;
while (n > 0) {
if (n & 1) res = res * a;
a = a * a;
n >>= 1;
}
return res;
}
vector<long long> eratos(long long n) {
if (n < 2) return {};
vll v(n - 1);
for (long long i = 0; i < n - 1; i++) {
v[i] = i + 2;
}
ll i = 0;
while (i < n - 1) {
ll p = v[i];
for (ll j = i + 1; j < n - 1; j++) {
if (v[j] % p == 0) {
v.erase(v.begin() + j);
n--;
}
}
i++;
}
v.resize(n - 1);
return v;
}
set<long long> eraset(long long n) {
set<long long> s;
vll v = eratos(n);
for (auto &t : v) {
s.insert(t);
}
return s;
}
vll line(ll x1, ll y1, ll x2, ll y2) {
vector<ll> v(3);
v[0] = y1 - y2;
v[1] = x2 - x1;
v[2] = -x1 * (y1 - y2) + y1 * (x1 - x2);
return v;
}
double dis(vll v, ll x, ll y) {
double s = sqrt(v[0] * v[0] + v[1] * v[1]);
return (double)abs(v[0] * x + v[1] * y + v[2]) / s;
}
ll const mod = 1e9 + 7;
int main() {
ll n;
cin >> n;
auto a = inputv(n);
ll l = 0;
ll res = 0;
for (long long i = 0; i < n; i++) {
if (l == 0) {
if (a[0] == 0) {
for (long long j = 0; j < n; j++) {
if (a[j] != 0) {
a[0] = a[j] / abs(a[j]);
if (j & 1 && j != 0) a[0] *= (-1);
break;
}
}
res++;
}
if (!a[0]) a[0] = 1;
l += a[0];
} else if (l < 0) {
if (a[i] < -l + 1) {
res += -l + 1 - a[i];
a[i] = -l + 1;
l = 1;
} else {
l += a[i];
}
} else if (l > 0) {
if (a[i] > -l - 1) {
res += abs(a[i] - (-l - 1));
a[i] = -l - 1;
l = -1;
} else {
l += a[i];
}
}
}
cout << res << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int posi(long long x) {
if (x > 0) return 1;
if (x < 0) return -1;
return 0;
}
int main() {
int N;
cin >> N;
vector<long long> a(N);
for (auto &i : a) cin >> i;
long long ans = 0, tmp = 0;
long long sum = a[0];
for (int i = 1; i < N; i++) {
if (posi(sum + a[i]) * posi(sum) != -1) {
tmp += abs(sum + a[i]) + 1LL;
sum = (sum > 0LL) ? -1LL : 1LL;
} else
sum += a[i];
}
ans = tmp;
tmp = abs(a[0]) + 1LL;
sum = (a[0] > 0) ? -1LL : 1LL;
for (int i = 1; i < N; i++) {
if (posi(sum + a[i]) * posi(sum) != -1) {
tmp += abs(sum + a[i]) + 1LL;
sum = (sum > 0LL) ? -1LL : 1LL;
} else
sum += a[i];
}
ans = min(ans, tmp);
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
namespace mp = boost::multiprecision;
long int MOD = (int)1e9 + 7;
template<typename T> istream& operator>>(istream &s, vector<T> &v) {
for (T &t : v) s >> t;
return s;
}
template<typename T> ostream& operator<<(ostream &s, const vector<T> &v) {
for (const T &t : v) s << t << endl;
return s;
}
template<typename T> T min(const vector<T>& v) {return *min_element(v.begin(), v.end());}
template<typename T> T max(const vector<T>& v) {return *max_element(v.begin(), v.end());}
template<typename T> int min_element(vector<T>& v) {return min_element(v.begin(), v.end()) - v.begin();}
template<typename T> int max_element(vector<T>& v) {return max_element(v.begin(), v.end()) - v.begin();}
template<typename T> void sort(vector<T>& v) {sort(v.begin(), v.end());}
template<typename T> void greatersort(vector<T>& v) {sort(v.begin(), v.end(), greater<>());}
template<typename T, typename Function> void sort(vector<T>& v, Function func) {sort(v.begin(), v.end(), func);}
template<typename T> void rsort(vector<T>& v) {sort(v.rbegin(), v.rend());}
template<typename T> void reverse(vector<T>& v) {reverse(v.begin(), v.end());}
template<typename T> void unique(vector<T>& v) {v.erase(unique(v.begin(), v.end()), v.end());}
template<typename T> void nth_element(vector<T>& v, int n) {nth_element(v.begin(), v.begin() + n, v.end());}
template<typename T> bool next_permutation(vector<T>& v) {return next_permutation(v.begin(), v.end());}
template<typename T> int find(vector<T>& v, T t) {return find(v.begin(), v.end(), t) - v.begin();}
template<typename T> int in(vector<T> v, T t) {return find(v, t) != int(v.size());}
template<typename T> int lower_bound(vector<T>& v, T t) {return lower_bound(v.begin(), v.end(), t) - v.begin();}
template<typename T> int upper_bound(vector<T>& v, T t) {return upper_bound(v.begin(), v.end(), t) - v.begin();}
template<typename T> T accumulate(const vector<T>& v, function<T(T, T)> func = plus<T>()) {return accumulate(v.begin(), v.end(), T(), func);}
template<typename T> void adjacent_difference(vector<T>& v) {adjacent_difference(v.begin(), v.end(), v.begin());}
template<typename T> void adjacent_difference(vector<T>& v, vector<T>& u) {adjacent_difference(v.begin(), v.end(), u.begin());}
template<typename T> vector<T> partial_sum(const vector<T>& v) {
vector<T> u(v.size());
partial_sum(v.begin(), v.end(), u.begin());
return u;
}\
template<typename T> T inner_product(vector<T>& v, vector<T>& u) {return inner_product(v.begin(), v.end(), u.begin(), T(0));}
template<typename T> int count(const vector<T>& v, T t) {return count(v.begin(), v.end(), t);}
template<typename T, typename Function> int count_if(const vector<T>& v, Function func) {return count_if(v.begin(), v.end(), func);}
template<typename T, typename Function> void remove_if(vector<T>& v, Function func) {v.erase(remove_if(v.begin(), v.end(), func), v.end());}
template<typename T, typename Function> bool all_of(vector<T> v, Function func) {return all_of(v.begin(), v.end(), func);}
template<typename T, typename Function> bool any_of(vector<T> v, Function func) {return any_of(v.begin(), v.end(), func);}
template<typename T, typename Function> bool none_of(vector<T> v, Function func) {return none_of(v.begin(), v.end(), func);}
template<typename T> vector<T> subvector(vector<T>& v, int a, int b) {return vector<T>(v.begin() + a, v.begin() + b);}
template<typename T> int kinds(const vector<T>& v) {return set<T>(v.begin(), v.end()).size();}
template<typename T> map<T, int> count_kinds(const vector<T>& v) {
map<T, int> res;
for (const auto& i : v) ++res[i];
return res;
}
template<typename T> void iota(vector<T>& v, T t = 0) {iota(v.begin(), v.end(), t);}
template<typename T> bool is_sorted(const vector<T>& v) {return is_sorted(v.begin(), v.end());}
int plusVector(vector<int>& v, int n) {int r = 0; for (int i = 0; i < n; ++i) {r += v[i];} return r;}
void yesno(bool b) {if (b) {cout << "yes" << endl;} else {cout << "no" << endl;}}
void YesNo(bool b) {if (b) {cout << "Yes" << endl;} else {cout << "No" << endl;}}
void YESNO(bool b) {if (b) {cout << "YES" << endl;} else {cout << "NO" << endl;}}
long addMOD(long a, long b) {return (a + b) % MOD;}
long subMOD(long a, long b) {return (a + MOD - b) % MOD;}
long mulMOD(long a, long b) {return ((a % MOD) * (b % MOD)) % MOD;}
long long invMOD(long long a, long long m) {long long b = m, u = 1, v = 0;while (b) {long long t = a / b;a -= t * b; swap(a, b);u -= t * v; swap(u, v);}u %= m;if (u < 0) u += m;return u;}
double PI = 3.1415926535897932384626433832795;
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int N;
cin >> N;
vector<int> a(N);
cin >> a;
long answer = 0;
int temp = a[0];
for (int i = 1; i < N; ++i) {
if (temp < 0) {
if (temp + a[i] <= 0) {
answer += (1 - (temp + a[i]));
temp = 1;
} else {
temp += a[i];
}
} else {
if (temp + a[i] >= 0) {
answer += ((temp + a[i]) + 1);
temp = -1;
} else {
temp += a[i];
}
}
}
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 | python3 | def solve():
n = int(input())
a = list(map(int, input().split()))
b = [a[i] for i in range(n)]
# print(a)
# print(b)
i = 0
sum = 0
ans = 0
for i in range(n-1):
sum += a[i]
if sum > 0 and sum+a[i+1] > 0:
tmp = -1 - sum
ans += abs(tmp - a[i+1])
a[i+1] = tmp
elif sum < 0 and sum+a[i+1] < 0:
tmp = 1 - sum
ans += abs(tmp - a[i+1])
a[i+1] = tmp
# print(ans)
# print(a)
tmp_ans_1 = ans
ans = 0
sum = 0
i = 0
if b[0] > 0:
ans += abs(-1 - b[0])
b[0] = -1
elif b[0] < 0:
ans += abs(1 - b[0])
b[0] = 1
for i in range(n-1):
sum += b[i]
if sum > 0 and sum+b[i+1] > 0:
tmp = -1 - sum
ans += abs(tmp - b[i+1])
b[i+1] = tmp
elif sum < 0 and sum+b[i+1] < 0:
tmp = 1 - sum
ans += abs(tmp - b[i+1])
b[i+1] = tmp
# print(ans)
# print(b)
tmp_ans_2 = ans
if tmp_ans_1 <= tmp_ans_2:
ans = tmp_ans_1
elif tmp_ans_2 < tnp_ans_1:
ans = tmp_ans_2
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 | cpp | #include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long INF = 1e18;
const double pi = acos(-1.0);
int main(void) {
long long n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < (n); ++i) cin >> a[i];
long long ans, sum = 0, res1 = 0, res2 = 0;
for (int sign = 0; sign < (2); ++sign) {
for (int i = 0; i < (n); ++i) {
sum += a[i];
if ((i % 2 ^ sign) && sum >= 0) {
res1 += sum + 1;
sum = -1;
}
if (!(i % 2 ^ sign) && sum <= 0) {
res2 -= sum - 1;
sum = 1;
}
}
}
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 | cpp | #include <bits/stdc++.h>
using namespace std;
long long solve(long long *a, int n) {
long long count = 0;
long long calc = 0;
int state, pstate;
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;
long long ans;
long long *a;
cin >> n;
a = new long long[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) {
long long bs, cs;
long long *b = new long long[n];
long long *c = new long long[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) + 1;
cs = solve(c, n) + 1;
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;
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < (int)(n); i++) cin >> a[i];
long long prevArraySum = a[0];
long long currentArraySum = a[0];
int res = 0;
if (a[0] == 0) {
res = 1;
prevArraySum = 1;
currentArraySum = 1;
for (int i = (1); i < (n); ++i) {
if (prevArraySum > 0) {
currentArraySum = prevArraySum + a[i];
if (currentArraySum >= 0) {
res += abs(-1 - currentArraySum);
prevArraySum = -1;
} else {
prevArraySum = currentArraySum;
}
} else {
currentArraySum = prevArraySum + a[i];
if (currentArraySum <= 0) {
res += abs(1 - currentArraySum);
prevArraySum = 1;
} else {
prevArraySum = currentArraySum;
}
}
}
int res1 = res;
res = 1;
for (int i = (1); i < (n); ++i) {
if (prevArraySum > 0) {
currentArraySum = prevArraySum + a[i];
if (currentArraySum >= 0) {
res += abs(-1 - currentArraySum);
prevArraySum = -1;
}
} else {
currentArraySum = prevArraySum + a[i];
if (currentArraySum <= 0) {
res += abs(1 - currentArraySum);
prevArraySum = 1;
} else {
prevArraySum = currentArraySum;
}
}
}
res = min(res, res1);
} else {
for (int i = (1); i < (n); ++i) {
if (prevArraySum > 0) {
currentArraySum = prevArraySum + a[i];
if (currentArraySum >= 0) {
res += abs(-1 - currentArraySum);
prevArraySum = -1;
} else {
prevArraySum = currentArraySum;
}
} else {
currentArraySum = prevArraySum + a[i];
if (currentArraySum <= 0) {
res += abs(1 - currentArraySum);
prevArraySum = 1;
} else {
prevArraySum = currentArraySum;
}
}
}
}
cout << res << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
scanf("%lld", &n);
long long a[n];
for (int i = 0; i < n; i++) scanf(" %lld", &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 | #!/usr/bin/env python3
from itertools import accumulate
def main():
n = int(input())
a = list(map(int, input().split()))
a = list(accumulate(a))
ans = 10**18
diff = [None, None]# a[0]<0, a[0]>0それぞれの初期コスト
for i in range(2):
if a[0] * [-1,1][i] < 0:
diff[i] = 0
else:
diff[i] = [-1,1][i] * (abs(a[0])+1)
for j in range(2):
ans2 = abs(diff[j])
for i in range(1,n):
p = a[i] + diff[j]
q = a[i-1] + diff[j]
if p * q >= 0:
tmp = -q//abs(q) - p
ans2 += abs(tmp)
diff[j] += tmp
ans = min(ans, ans2)
print(ans)
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 | python3 | N = int(input())
As = list(map(int, input().split()))
"""
i番目を+1すると、
i+k番目は+(k+1)される
回数を保持しておいて、後で足す?
"""
from itertools import accumulate
acc = list(accumulate(As))
# print(acc)
prev = acc[0]
ans = 0
cnt = 0 #累積カウント
for a in acc[1:]:
a += cnt
# print(a,"a")
if prev * a < 0:
prev = a
elif prev > 0 and a == 0:
cnt -= 1
ans += 1
prev = -1
elif prev < 0 and a == 0:
cnt += 1
ans += 1
prev = 1
elif prev > 0 and a > 0:
# print("aa")
cnt_prev = prev - 1
cnt -= cnt_prev
ans += cnt_prev
a -= cnt_prev
if a < 0:
prev = a
else:
cnt -= (a+1) #上で引いたぶんで消さなくて良い可能性はある
ans += (a+1)
prev = -1
else: #prev < 0 and a <0
# print("bb")
cnt_prev = -1 - prev
cnt += cnt_prev
ans += cnt_prev
a += cnt_prev
if a > 0:
prev = a
else:
cnt += (a+1)
ans += (a+1)
prev = 1
# print(prev,ans,cnt)
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 n;
long long a[100001];
int main() {
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
long long sum = 0;
long long change_time = 0;
for (int i = 0; i < n; ++i) {
bool stop = true;
while (stop) {
long long next_sum = sum + a[i];
if (next_sum <= 0 && sum < 0) {
a[i] += 1;
change_time++;
} else if (next_sum >= 0 && sum > 0) {
a[i] -= 1;
change_time++;
} else {
stop = false;
break;
}
}
sum += a[i];
}
cout << change_time << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
long long int ans1, ans2, sum1, sum2;
int n, i;
long long int a[100005];
cin >> n;
for (i = 1; i <= n; i++) {
cin >> a[i];
}
if (a[1] > 0) {
sum1 = a[1];
ans2 = a[1] + 1;
sum2 = -1;
} else if (a[1] == 0) {
sum1 = 1;
ans1 = ans2 = 1;
sum2 = -1;
} else {
sum1 = 1;
ans1 = abs(a[1]) + 1;
sum2 = a[1];
}
for (i = 2; i <= n; i++) {
if (sum1 > 0) {
if (a[i] + sum1 >= 0) {
ans1 += a[i] + sum1 + 1;
sum1 = -1;
} else {
sum1 += a[i];
}
} else {
if (a[i] + sum1 <= 0) {
ans1 += abs(sum1 + a[i]) + 1;
sum1 = 1;
} else {
sum1 += a[i];
}
}
}
if (sum1 == 0) {
ans1++;
}
for (i = 2; i <= n; i++) {
if (sum2 > 0) {
if (a[i] + sum2 >= 0) {
ans2 += a[i] + sum2 + 1;
sum2 = -1;
} else {
sum2 += a[i];
}
} else {
if (a[i] + sum2 <= 0) {
ans2 += abs(sum2 + a[i]) + 1;
sum2 = 1;
} else
sum2 += a[i];
}
}
if (sum2 == 0) {
ans2++;
}
cout << min(ans1, ans2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
long long n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < (n); ++i) cin >> a[i];
long long prev = a[0];
long long curr;
long long res = 0;
for (int i = 1; i < (n); ++i) {
curr = prev + a[i];
if (curr == 0) {
if (prev < 0)
curr = 1;
else
curr = -1;
++res;
}
if (prev < 0 && curr < 0) {
res += 1 - curr;
curr = 1;
}
if (prev > 0 && curr > 0) {
res += curr + 1;
curr = -1;
}
prev = curr;
}
cout << res << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
bool debug = false;
int main() {
int n;
long long a[100005];
long long cnt = 0;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
long long sum = a[0] + a[1];
bool plus;
if (sum >= 0)
plus = true;
else
plus = false;
for (int i = 2; i < n; i++) {
sum += a[i];
if (debug) cout << "sum:" << sum << endl;
if (plus) {
if (sum >= 0) {
cnt += sum + 1;
sum = -1;
}
plus = false;
} else {
if (sum <= 0) {
cnt += abs(sum) + 1;
sum = 1;
}
plus = true;
}
}
cout << cnt << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long INF = (1LL << 62);
long long N;
vector<long long> A, W;
long long S[100002] = {INF * (-1)};
int dp[100002] = {0};
void calcDP(int n) {
if (n == 1) {
if (W[1] != 0) {
dp[1] = 0;
} else {
dp[1] = 1;
if (W[2] <= 0) {
W[1] = 1;
} else {
W[1] = -1;
}
S[1] = W[1];
}
return;
} else {
S[n] = S[n - 1] + W[n];
if (S[n - 1] * S[n] < 0) {
dp[n] = dp[n - 1];
} else {
dp[n] = dp[n - 1] + abs(0 - S[n - 1] - W[n]) + 1;
W[n] = 0 - S[n - 1] - (abs(S[n - 1]) / S[n - 1]);
S[n] = S[n - 1] + W[n];
}
return;
}
}
int main(int argc, char* argv[]) {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> N;
W.push_back(0);
S[0] = 0;
for (int i = 1; i <= N; i++) {
long long a;
cin >> a;
A.push_back(a);
W.push_back(a);
if (i == 1) {
S[1] = a;
} else {
S[i] = S[i - 1] + a;
}
}
for (int i = 1; i <= N; i++) {
calcDP(i);
}
printf("%d\n", dp[N]);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <bits/stdc++.h>
int main(void) {
int n, i, check = 0;
long long int a, count = 0, sum = 0;
scanf("%d", &n);
scanf("%lld", &a);
if (a == 0) {
sum = 1;
count = 1;
check = 1;
}
for (i = 1; i < n; i++) {
scanf("%lld", &a);
sum += a;
if (check == 1 && sum >= 0) {
count += (1 + sum);
sum = -1;
} else if (check == -1 && sum <= 0) {
count += (1 - sum);
sum = 1;
}
if (sum >= 0) {
check = 1;
} else {
check = -1;
}
}
printf("%lld", count);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long a[1000000];
int min(long long a, long long b) {
int t = a;
if (b <= t) t = b;
return t;
}
int main() {
int n;
cin >> n;
for (int t = 0; t < n; t++) cin >> a[t];
long long sum = 0LL;
long long x = 0LL;
for (int t = 0; t < n; t++) {
sum += a[t];
if (t % 2 == 1 && sum >= 0) {
long long s = sum + 1;
sum = -1;
x += s;
} else if (t % 2 == 0 && sum <= 0) {
long long s = 1 - sum;
sum = 1;
x += s;
}
}
long long positive_x = x;
x = 0LL;
sum = 0LL;
for (int t = 0; t < n; t++) {
sum += a[t];
if (t % 2 == 0 && sum >= 0) {
long long s = sum + 1;
sum = -1;
x += s;
} else if (t % 2 == 1 && sum <= 0) {
long long s = 1 - sum;
sum = 1;
x += s;
}
}
long long negative_x = x;
long long result = min(positive_x, negative_x);
cout << result << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
long long sum = a[0];
long long cnt = 0;
if (sum == 0) {
int ind = 1;
for (int i = 0; i < n; i++) {
if (a[i] != 0) ind = i;
break;
}
sum = (a[ind] > 0 ? -1 : 1);
cnt++;
}
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;
}
if (nsum == 0) {
sum = (sum > 0 ? -1 : 1);
cnt++;
} else {
if (sum > 0 && nsum > 0)
sum = -1;
else
sum = 1;
cnt += abs(nsum) + 1;
}
}
cout << cnt << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int num = 0;
int N;
int M = 0;
int A[100000];
cin >> N;
for (int i = 0; i < N; ++i) {
cin >> A[i];
}
for (int i = 0; i < N - 1; ++i) {
M = 0;
for (int j = 0; j <= i; ++j) {
M += A[j];
}
if ((M + A[i + 1]) * M >= 0) {
break;
}
if (i == N - 2) {
cout << num << endl;
return 0;
}
}
{
int m = 0;
for (int i = 0; i < N; ++i) {
int k = -1 * (m + 1);
if (m < 0) {
k = -1 * (m - 1);
}
if (i == 0 && A[i] > 0) {
k = 1;
}
if (m * (m + A[i]) >= 0) {
num += abs(k - A[i]);
A[i] = k;
}
m += A[i];
}
}
{
for (int i = 0; i < N; ++i) {
cout << A[i];
}
cout << endl;
}
cout << num << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
a = [int(i) for i in input().split()]
sam = a[0]
old = sam
num = 0
for i in range(1, len(a)):
sam += a[i]
if sam >= 0 and old > 0:
while sam >= 0:
sam -= 1
num += 1
elif sam <= 0 and old < 0:
while sam <= 0:
sam += 1
num += 1
old = sam
print(num)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
const int INF = 1e9;
using namespace std;
int main() {
long long n;
cin >> n;
long long a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long sum = 0, ans1 = 0, ans2 = 0;
for (long long i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 0 && sum <= 0) {
ans1 += (1 - sum);
sum = 1;
} else if (i % 2 == 1 && sum >= 0) {
ans1 += sum + 1;
sum = -1;
}
}
for (long long i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 1 && sum <= 0) {
ans2 += (1 - sum);
sum = 1;
} else if (i % 2 == 0 && sum >= 0) {
ans2 += sum + 1;
sum = -1;
}
}
cout << min(ans1, ans2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
long long a[100010];
int n;
long long solve() {
long long sum = 0;
long long oo = a[0], flag;
if (a[0] > 0)
flag = 1;
else if (a[0] < 0)
flag = -1;
for (int i = 1; i < n; i++) {
oo += a[i];
if (flag == 1) {
if (oo >= 0) {
sum += oo + 1;
oo = -1;
}
}
if (flag == -1) {
if (oo <= 0) {
sum += 0 - oo + 1;
oo = 1;
}
}
flag = -flag;
}
return sum;
}
int main() {
while (scanf("%d", &n) != EOF) {
long long sum;
for (int i = 0; i < n; i++) {
scanf("%lld", &a[i]);
}
if (a[0] == 0) {
a[0] = 1;
long long sum1 = solve();
a[0] = -1;
long long sum2 = solve();
sum = min(sum1, sum2) + 1;
} else {
long long sum1 = solve();
sum = sum1;
}
printf("%lld\n", sum);
}
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
A = list(map(int, input().split()))
a = A[0]
ans = 0
for i in range(1,n):
b = A[i]
if a == 0:
if b < 0:
a = -b-1
ans = a
else:
a = -b+1
ans = -a
if a> 0:
if a + b >= 0:
ans += abs(-1-a-b)
a = -1
else:
a += b
elif a < 0:
if a+b <= 0:
ans += abs(1-a-b)
a = 1
else:
a += b
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 int INF = 1000000007;
int main() {
int N;
cin >> N;
vector<int> A(N);
for (int i = 0; i < (int)(N); i++) cin >> A[i];
long long sum = A[0], ans = 0;
int start_plus = 0;
if (sum == 0)
ans++;
else if (sum < 0)
start_plus = 1;
for (int i = 1; i < N; i++) {
sum += A[i];
if (i % 2 == start_plus) {
if (sum > 0)
continue;
else {
ans += 1 - sum;
sum = 1;
}
} else if (i % 2 != start_plus) {
if (sum < 0)
continue;
else {
ans += sum + 1;
sum = -1;
}
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java |
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Comparator;
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
public class Main implements Runnable { // クラス名はMain1
PrintWriter out = new PrintWriter(System.out);
InputReader sc = new InputReader(System.in);
static int mod = (int) (1e9 + 7); //10^9+7
int[] dx = { 1, 0, -1, 0 }, dy = { 0, 1, 0, -1 }; //4 directions
public static void main(String[] args) {
Thread.setDefaultUncaughtExceptionHandler((t, e) -> System.exit(1));
new Thread(null, new Main(), "", 1024 * 1024 * 1024).start(); // 16MBスタックを確保して実行
}
public void run() {
try {
//String S = sc.next().trim();
int N = sc.nextInt();
long[] A = new long[N];
for (int i = 0; i < N; i++) {
//A[i] = sc.nextInt();
A[i] = sc.nextLong();
}
long[] wa = new long[N + 1];
for (int i = 1; i <= N; i++) {
wa[i] += wa[i - 1] + A[i - 1];
}
//out.println(Arrays.toString(wa));
boolean plus = wa[1] > 0 ? true : false;
long cntp = 0;
long cntm = 0;
for (int i = 2; i <= N; i++) {
wa[i] += cntp - cntm;
if (plus && wa[i] >= 0) {
cntm += wa[i] + 1;
} else if (!plus && wa[i] <= 0) {
cntp += Math.abs(wa[i]) + 1;
}
plus = !plus;
}
//out.println(Arrays.toString(wa));
out.println(cntp + cntm);
} catch (ArithmeticException ae) {
//ae.printStackTrace();
throw new RuntimeException();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
} finally {
out.flush();
out.close();
}
}
class InfoLong implements Comparable<InfoLong> {
public long a;
public long b;
public InfoLong(long a, long b) {
this.a = a;
this.b = b;
}
@Override
public int compareTo(InfoLong o) {
//return this.a - o.a;//昇順
// return o.a - this.a;//降順
//複数項目で並び替え。aの降順、aが同じならbの降順
if (this.a == o.a) {
return Long.compare(o.b, this.b);
} else {
return Long.compare(o.a, this.a);
}
}
}
class InfoInt implements Comparable<InfoInt> {
public int a;
public int b;
public InfoInt(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public int compareTo(InfoInt o) {
//return this.a - o.a;//昇順
// return o.a - this.a;//降順
//複数項目で並び替え。aの降順、aが同じならbの降順
if (this.a == o.a) {
return Integer.compare(o.b, this.b);
} else {
return Integer.compare(o.a, this.a);
}
}
}
//Arrays.sort(Sample,new Comp());
class Comp implements Comparator<InfoLong> {
public int compare(InfoLong be, InfoLong af) {
return Long.compare(af.a, be.a);
}
}
/**
*
* @param n
* @param m
* @return Combinationの数を返す(mod無しなので、大きい値には使用できない桁溢れする)
*/
long calcCombination(int n, int m) {
if (n < m || m < 0) {
throw new IllegalArgumentException("引数の値が不正です ( n : " + n + ", m : " + m + ")");
}
long c = 1;
m = (n - m < m ? n - m : m);
for (int ns = n - m + 1, ms = 1; ms <= m; ns++, ms++) {
c *= ns;
c /= ms;
}
return c;
}
/*
* 使用するときはModに注意。Global変数を参照
*/
public static long comb(long a, long b) {
if (a < b)
return 0;
long res = 1;
long inv = pow(fact(Math.min(b, a - b)), mod - 2);
for (long i = a; i > a - Math.min(b, a - b); i--) {
res *= i;
res %= mod;
}
res *= inv;
res %= mod;
return res;
}
public static long pow(long x, long n) {
long res = 1;
while (n > 0) {
if ((n & 1) == 1) {
res *= x;
res %= mod;
}
x *= x;
x %= mod;
n >>= 1;
}
return res;
}
public static long fact(long n) {
long res = 1;
while (n > 0) {
res *= n;
res %= mod;
n--;
}
return res;
}
// 高速なScanner
static class InputReader {
private InputStream in;
private byte[] buffer = new byte[1024];
private int curbuf;
private int lenbuf;
public InputReader(InputStream in) {
this.in = in;
this.curbuf = this.lenbuf = 0;
}
public boolean hasNextByte() {
if (curbuf >= lenbuf) {
curbuf = 0;
try {
lenbuf = in.read(buffer);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0)
return false;
}
return true;
}
private int readByte() {
if (hasNextByte())
return buffer[curbuf++];
else
return -1;
}
private boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
private void skip() {
while (hasNextByte() && isSpaceChar(buffer[curbuf]))
curbuf++;
}
public boolean hasNext() {
skip();
return hasNextByte();
}
public String next() {
if (!hasNext())
throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while (!isSpaceChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public int nextInt() {
if (!hasNext())
throw new NoSuchElementException();
int c = readByte();
while (isSpaceChar(c))
c = readByte();
boolean minus = false;
if (c == '-') {
minus = true;
c = readByte();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res = res * 10 + c - '0';
c = readByte();
} while (!isSpaceChar(c));
return (minus) ? -res : res;
}
public long nextLong() {
if (!hasNext())
throw new NoSuchElementException();
int c = readByte();
while (isSpaceChar(c))
c = readByte();
boolean minus = false;
if (c == '-') {
minus = true;
c = readByte();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res = res * 10 + c - '0';
c = readByte();
} while (!isSpaceChar(c));
return (minus) ? -res : res;
}
public double nextDouble() {
return Double.parseDouble(next());
}
public int[] nextIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public long[] nextLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public char[][] nextCharMap(int n, int m) {
char[][] map = new char[n][m];
for (int i = 0; i < n; i++)
map[i] = next().toCharArray();
return map;
}
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <bits/stdc++.h>
int main() {
int n;
scanf("%d", &n);
long long a[n];
for (int i = 0; i < n; i++) {
scanf("%lld", &a[i]);
}
long long mans = 0, pans = 0;
long long msub[n], psub[n];
if (a[0] == 0) {
pans = 1;
mans = 1;
psub[0] = 1;
msub[0] = -1;
}
if (a[0] > 0) {
psub[0] = a[0];
msub[0] = -1;
mans = a[0] + 1;
} else if (a[0] < 0) {
psub[0] = 1;
msub[0] = a[0];
pans = a[0] + 1;
}
int i;
for (i = 1; i < n; i++) {
if (i % 2 == 1 && psub[i - 1] + a[i] >= 0) {
pans += (psub[i - 1] + a[i] + 1);
psub[i] = -1;
} else if (i % 2 == 0 && psub[i - 1] + a[i] <= 0) {
pans += (1 - (psub[i - 1] + a[i]));
psub[i] = 1;
} else {
psub[i] = (psub[i - 1] + a[i]);
}
if (i % 2 == 1 && msub[i - 1] + a[i] <= 0) {
mans += (1 - (msub[i - 1] + a[i]));
msub[i] = 1;
} else if (i % 2 == 0 && msub[i - 1] + a[i] >= 0) {
mans += (msub[i - 1] + a[i] + 1);
msub[i] = -1;
} else {
msub[i] = (msub[i - 1] + a[i]);
}
}
printf("%lld", mans < pans ? mans : pans);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import copy
import sys
write = sys.stdout.write
n = int(input())
A = list(map(int,input().split())) # +, -, +, ...
B = copy.deepcopy(A) #-, +, -, ...
sumA = []
sumB = []
cntA = 0
cntB = 0
if A[0] == 0:
A[0] += 1
B[0] -= 1
elif A[0] > 0:
cntB += (B[0]+1)
B[0] = -1
else:
cntA += abs(A[0])+1
A[0] = 1
sumA.append(A[0])
sumB.append(B[0])
#write("cntA : " + str(cntA) + " cntB : " + str(cntB) + "\n")
for i in range(1, n):
tempA = sumA[i-1] + A[i]
tempB = sumB[i-1] + B[i]
if i%2 == 1: #Aは-, Bは+
if tempA == 0:
#A[i] -= 1
cntA += 1
sumA.append(-1)
elif tempA > 0:
#A[i] -= abs(tempA) + 1
cntA += abs(tempA) + 1
sumA.append(-1)
else:
sumA.append(tempA)
if tempB == 0:
#B[i] += 1
cntB += 1
sumB.append(1)
elif tempB < 0:
#B[i] += abs(tempB) + 1
cntB += abs(tempB) + 1
sumB.append(1)
else:
sumB.append(tempB)
else: #Aは+, Bは-
if tempA == 0:
cntA += 1
sumA.append(1)
elif tempA < 0:
cntA += abs(tempA) + 1
sumA.append(1)
else:
sumA.append(tempA)
if tempB == 0:
#B[i] -= 1
cntB += 1
sumB.append(-1)
elif tempB > 0:
#B[i] -= abs(tempB) + 1
cntB += abs(tempB) + 1
sumB.append(-1)
else:
sumB.append(tempB)
#write("cntA : " + str(cntA) + " cntB : " + str(cntB) + "\n")
print(str(min(cntA, cntB))) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
const ll linf = 1LL << 62;
const int inf = 999999;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
ll gcd(ll a, ll b) {
if (a % b == 0)
return b;
else
gcd(b, a % b);
}
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
int main() {
int n;
bool jud;
ll ans = 0;
vector<ll> v;
cin >> n;
for (int i = 0; i < n; i++) {
ll a;
cin >> a;
v.push_back(a);
}
ll a = v[0];
if (a > 0)
jud = true;
else
jud = false;
for (int i = 1; i < n; i++) {
if (jud) {
a += v[i];
if (a < 0)
jud = false;
else {
ans += abs(a) + 1;
a -= abs(a) + 1;
jud = false;
}
} else {
a += v[i];
if (a > 0)
jud = true;
else {
ans += abs(a) + 1;
a += abs(a) + 1;
jud = true;
}
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace kyoupuro {
class MainClass {
public static void Main() {
var N = Input.NextLong();
var list = Input.LongList();
// 最初を非ゼロにする
if (list[0] == 0) {
list[0] = list.SkipWhile(x => x == 0).DefaultIfEmpty(1).First();
}
long sum = list[0];
long count = 0;
for (int i = 1; i < N; i++) {
var nextSum = sum + list[i];
if (sum > 0 && nextSum >= 0) {
count += nextSum + 1;
sum = -1;
} else if (sum < 0 && nextSum <= 0) {
count += -nextSum + 1;
sum = 1;
} else {
sum = nextSum;
}
}
Console.WriteLine(count);
}
}
class Input {
static IEnumerator<string> enumerator = new string[] { }.AsEnumerable().GetEnumerator();
public static string Line() {
return Console.ReadLine();
}
public static int NextInt() {
while (!enumerator.MoveNext()) {
enumerator = StrArr().AsEnumerable().GetEnumerator();
}
return int.Parse(enumerator.Current);
}
public static long NextLong() {
while (!enumerator.MoveNext()) {
enumerator = StrArr().AsEnumerable().GetEnumerator();
}
return long.Parse(enumerator.Current);
}
public static string[] StrArr() {
return Line().Split(' ');
}
public static List<int> IntList() {
return StrArr().Select(int.Parse).ToList();
}
public static List<long> LongList() {
return StrArr().Select(long.Parse).ToList();
}
public static void Skip(int line = 1) {
enumerator.Reset();
for (int i = 0; i < line; i++) Console.ReadLine();
}
public static void Reset() {
enumerator.Reset();
}
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
int64_t n;
cin >> n;
vector<int64_t> a(n);
for (int64_t i = 0; i < n; i++) cin >> a[i];
int64_t sum1 = 0, cost1 = 0;
for (int64_t i = 0; i < n; i++) {
sum1 += a[i];
if (i % 2 == 0 && sum1 < 0) sum1 += abs(sum1) + 1, cost1 += abs(sum1) + 1;
if (i % 2 == 1 && sum1 > 0) sum1 -= abs(sum1) - 1, cost1 += abs(sum1) + 1;
if (sum1 == 0) {
if (i % 2 == 0) sum1--, cost1++;
if (i % 2 == 1) sum1++, cost1++;
}
}
int64_t sum2 = 0, cost2 = 0;
for (int64_t i = 0; i < n; i++) {
sum2 += a[i];
if (i % 2 == 0 && sum1 > 0) sum2 -= abs(sum2) - 1, cost2 += abs(sum2) + 1;
if (i % 2 == 1 && sum1 < 0) sum2 += abs(sum2) + 1, cost1 += abs(sum2) + 1;
if (sum2 == 0) {
if (i % 2 == 0) sum2++, cost2++;
if (i % 2 == 1) sum2--, cost2++;
}
}
cout << min(cost1, cost2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | def examC():
N = I()
a = LI()
ansC = []
"""
if a[0]<0:
for i in range(N):
a[i] = -a[i]
"""
for l in range(2):
ans = 0
if l==0:
if a[0]<0:
sumA = 1
ans += (-a[0]+1)
else:
sumA = a[0]
elif l == 1:
if a[0]>0:
sumA = -1
ans += (a[0]+1)
else:
sumA = a[0]
for i in range(1,N):
cur = sumA + a[i]
if cur * sumA > 0:
if sumA < 0:
ans += (-cur + 1)
sumA = 1
else:
ans += cur + 1
sumA = -1
elif cur == 0:
if sumA < 0:
ans += 1
sumA = 1
else:
ans += 1
sumA = -1
else:
sumA = cur
ansC.append(ans)
print(min(ansC))
import sys
import copy
import bisect
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')
examC()
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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()))
sums = 0
count = 0
count2 = 0
for i in range(len(a)):
if i % 2 == 0:
pp = a[i]
if sums + pp > 0:
None
else:
sums = 1
count = count + 1 - pp - sums
else:
pp = a[i]
if sums + pp < 0:
None
else:
sums = -1
count = count -1 -pp - sums
for i in range(len(a)):
if i % 2 == 1:
pp = a[i]
if sums + pp > 0:
None
else:
sums = 1
count2 = count2 + 1 - pp - sums
else:
pp = a[i]
if sums + pp < 0:
None
else:
sums = -1
count2 = count2 -1 -pp - sums
print(min(count,count2))
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.