Search is not available for this dataset
name
stringlengths 2
88
| description
stringlengths 31
8.62k
| public_tests
dict | private_tests
dict | solution_type
stringclasses 2
values | programming_language
stringclasses 5
values | solution
stringlengths 1
983k
|
---|---|---|---|---|---|---|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
L = [0 for _ in range(n)]
L[0] = a[0]
for i in range(1, n):
L[i] = L[i-1] + a[i]
delay = 0
all_over = 0
if a[0] != 0:
sign = (a[0] > 0) - (a[0] < 0)
for i in range(1, n):
L[i] += delay
if L[i] <= 0 and sign == -1:
delay += 1 - L[i]
all_over += 1 - L[i]
L[i] = 1
elif L[i] >= 0 and sign == 1:
delay -= L[i] + 1
all_over += L[i] + 1
L[i] = -1
sign *= -1
print(all_over) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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 = 100000000;
int dx[] = {0, 1, -1, 0, 1, -1, 1, -1};
int dy[] = {1, 0, 0, -1, 1, -1, -1, 1};
int main() {
int n;
cin >> n;
vector<int> v(n + 1, 0), w(n + 1, 0);
for (int i = 1; i <= n; i++) cin >> v[i];
int ans = 0;
for (int i = 1; i <= n; i++) {
w[i] = w[i - 1] + v[i];
if (w[i - 1] > 0 && w[i] > 0) {
int tmp = w[i] + 1;
ans += tmp;
v[i] -= tmp;
w[i] = -1;
} else if (w[i - 1] < 0 && w[i] < 0) {
int tmp = -w[i] + 1;
ans += tmp;
v[i] += tmp;
w[i] = 1;
} else if (w[i] == 0 && w[i - 1] < 0) {
ans++;
v[i]++;
w[i]++;
} else if (w[i] == 0 && w[i - 1] > 0) {
ans++;
v[i]--;
w[i]--;
}
}
int sum = accumulate(v.begin(), v.end(), 0);
if (sum == 0) ans++;
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
template <class T>
bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
using namespace std;
int main(void) {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < (int)(n); i++) cin >> a[i];
for (int i = 0; i < (int)(n - 1); i++) a[i + 1] += a[i];
int make_pair = 0, pm = 0;
int diff = 0;
for (int i = 0; i < (int)(n); i++) {
int goal = (i & 1) ? 1 : -1;
if (goal * (a[i] + diff) > 0) continue;
make_pair += abs(goal - (a[i] + diff));
diff += goal - (a[i] + diff);
}
diff = 0;
for (int i = 0; i < (int)(n); i++) {
int goal = (i & 1) ? -1 : 1;
if (goal * (a[i] + diff) > 0) continue;
pm += abs(goal - (a[i] + diff));
diff += goal - (a[i] + diff);
}
cout << min(make_pair, pm) << "\n";
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> an(n);
for (int i = 0; i < n; ++i) {
cin >> an[i];
}
int cnt_min = INT_MAX;
for (int j = 0; j < 2; ++j) {
int sign = j == 0 ? -1 : 1;
int accum = an[0];
int cnt = 0;
if (accum * sign <= 0) {
auto x = sign - accum;
accum += x;
cnt += abs(x);
}
for (int i = 1; i < n; ++i) {
auto new_accum = accum + an[i];
if (new_accum * accum >= 0) {
int x = -sign - new_accum;
new_accum += x;
cnt += abs(x);
}
int new_sign = new_accum > 0 ? 1 : -1;
accum = new_accum;
sign = new_sign;
}
if (cnt < cnt_min) {
cout << endl;
cnt_min = cnt;
}
}
cout << cnt_min << 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()))
def f(presum, x, fugo):
if (presum+x)*fugo<=0:
sa=fugo-presum-x
prosum=fugo
else:
sa=0
prosum=presum+x
return sa, prosum
out1=0
y=a[0]
for i in range(1, n):
x, y=f(y, a[i], (-1)**i)
out1+=abs(x)
out2=0
y=a[0]
for i in range(1, n):
x, y=f(y, a[i], -(-1)**i)
out2+=abs(x)
print(min(out1, out2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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 drop(const T &x) {
cout << x << endl;
exit(0);
}
void solve() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i) cin >> a.at(i);
int ans = 0;
if (!a.at(0)) {
if (a.at(1) > 0) a.at(0)--;
if (a.at(1) < 0) a.at(0)++;
ans++;
}
int sum = a.at(0);
for (int i = 1; i < n; ++i) {
if (sum < 0) {
if (sum + a.at(i) <= 0) {
while (sum + a.at(i) <= 0) {
++a.at(i);
++ans;
}
}
} else {
if (sum + a.at(i) >= 0) {
while (sum + a.at(i) >= 0) {
--a.at(i);
++ans;
}
}
}
sum += a.at(i);
}
cout << ans << '\n';
return;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int T = 1;
while (T--) 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 <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <utility>
#include <cstdio>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <cmath>
#include <set>
#include <map>
using ll = long long;
using itn = int;
using namespace std;
int GCD(int a, int b){
return b ? GCD(b, a%b) : a;
}
int main() {
int n;
cin >> n;
int a[n];
for(int i=0; i<n; i++){
cin >> a[i];
}
int asum[n+1]={};
for(int i=0; i<n; i++){
asum[i+1] = asum[i]+a[i];
}
int cnt=0;
int accSum=0;
for(int i=1; i<n; i++){
asum[i+1]+=accSum;
if(asum[i+1]*asum[i]>0){
int s=abs(asum[i+1])+1;
cnt+=s;
asum[i+1]<0 ? accSum+=s : accSum+=-1*s;
asum[i+1]<0 ? asum[i+1]=1 : asum[i+1]=-1;
}else if(asum[i+1]*asum[i]==0){
cnt+=1;
asum[i]<0 ? asum[i+1]=1,accSum+=1 : asum[i+1]=-1,accSum=-1;
}
}
cout << cnt << endl;
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | n = gets.to_i
as = gets.split.map(&:to_i)
cnt1 = 0
sum = as[0]
n.times.with_index(1) do |_,i|
break if as[i].nil?
prev = sum < 0 ? "n" : "p"
tmp = sum
sum += as[i]
if prev == "n"
if sum == 0
sum += 1
cnt1 += 1
elsif sum < 0
sum = 1
cnt1 += tmp.abs - as[i].abs + 1
end
else
if sum == 0
sum -= 1
cnt1 += 1
elsif sum > 0
sum = -1
cnt1 += as[i].abs + tmp + 1
end
end
# p "a: #{as[i]}"
# p "sum: #{sum}"
# p "cnt: #{cnt1}"
# p "--------"
end
puts cnt1
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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 = 1e9 + 7;
const long long longinf = 1LL << 60;
const int mx = 100010;
const long long mod = 1e9 + 7;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = (int)(0); i < (int)(n); ++i) {
cin >> a[i];
}
long long ansa = 0;
long long sum = a[0];
bool pm = (a[0] > 0 ? true : false);
for (int i = 1; i < n; i++) {
sum += a[i];
if (pm) {
if (sum >= 0) {
ansa += sum + 1;
sum = -1;
}
pm = false;
} else {
if (sum <= 0) {
ansa += abs(sum) + 1;
sum = 1;
}
pm = true;
}
}
long long ansb = abs(a[0]) + 1;
sum = (a[0] > 0 ? -1 : 1);
pm = (a[0] > 0 ? false : true);
for (int i = 1; i < n; i++) {
sum += a[i];
if (pm) {
if (sum >= 0) {
ansb += sum + 1;
sum = -1;
}
pm = false;
} else {
if (sum <= 0) {
ansb += abs(sum) + 1;
sum = 1;
}
pm = true;
}
}
cout << min(ansa, ansb) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using std::cerr;
using std::cin;
using std::cout;
using std::endl;
void OutputError(std::string s) {
cerr << "\033[93m" << s << "\033[m" << endl;
return;
}
int main(void) {
cout << std::fixed << std::setprecision(10);
cin.tie(0);
std::ios::sync_with_stdio(false);
int n;
cin >> n;
std::vector<int64_t> a(n, 0);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int64_t cum = 0;
bool is_plus = false;
int64_t result = 0;
if (a[0] > 0) {
is_plus = false;
} else if (a[0] < 0) {
is_plus = true;
} else {
bool found = false;
for (int i = 1; i < n; i++) {
if (a[i] > 0) {
a[0] = -1;
result++;
is_plus = true;
found = true;
break;
} else if (a[i] < 0) {
a[0] = 1;
result++;
is_plus = false;
found = true;
break;
}
}
if (!found) {
result = 2 * n - 1;
cout << result << endl;
exit(0);
}
}
for (int i = 0; i < n; i++) {
cum += a[i];
if (is_plus) {
if (cum >= 0) {
result += cum + 1;
cum = -1;
is_plus = false;
} else {
is_plus = false;
}
} else {
if (cum > 0) {
is_plus = true;
} else {
result += (-cum) + 1;
cum = 1;
is_plus = true;
}
}
}
cout << result << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | import scala.io.StdIn
import scala.annotation.tailrec
object Main extends App {
val n = StdIn.readInt
val a = StdIn.readLine.split(" ").map(_.toInt)
val init1 = if(a.head < 0) 1 - a.head else 0
val ans1 = a.tail./:(a.head + init1, init1.abs)((acc,i) => {
val (bsum, bcnt) = acc
val sum = bsum + i
val cnt = if(bsum < 0 && sum < 0) 1 - sum
else if(bsum > 0 && sum > 0) -1 - sum
else if(sum == 0) if(bsum < 0) 1 else -1
else 0
// println(sum + " " + cnt)
(sum+cnt, bcnt+cnt.abs)
})._2
val init2 = if(a.head < 0) 0 else -1 - a.head
val ans2 = a.tail./:(a.head + init2, init2.abs)((acc,i) => {
val (bsum, bcnt) = acc
val sum = bsum + i
val cnt = if(bsum < 0 && sum < 0) 1 - sum
else if(bsum > 0 && sum > 0) -1 - sum
else if(sum == 0) if(bsum < 0) 1 else -1
else 0
// println(sum + " " + cnt)
(sum+cnt, bcnt+cnt.abs)
})._2
println(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;
const int MOD = 1000000007;
int sign(long long A) {
if (A > 0)
return 1;
else if (A < 0)
return -1;
else
return 0;
}
int main(void) {
int N;
cin >> N;
long long ans = 0;
long long diff = 0;
long long sss;
vector<long long> arr;
for (int i = 0; i < N; i++) {
cin >> sss;
arr.push_back(sss);
}
vector<long long> s(N + 1, 0);
for (int i = 0; i < N; i++) s[i + 1] = s[i] + arr[i];
if (s[1] == 0) {
diff++;
ans++;
}
long long a = 0, b = 0;
for (int i = 1; i <= N - 1; i++) {
a = s[i] + diff;
b = s[i + 1] + diff;
if (sign(a) == sign(b)) {
if (sign(a) == 1) {
diff += (-1 - b);
ans += (1 + b);
} else if (sign(a) == -1) {
diff += (1 - b);
ans += (1 - b);
}
}
if (sign(b) == 0) {
if (sign(a) == 1)
diff--;
else if (sign(a) == -1)
diff++;
ans++;
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | require 'prime'
include Math
def max(a,b); a > b ? a : b end
def min(a,b); a < b ? a : b end
def swap(a,b); a, b = b, a end
def gif; gets.to_i end
def gff; gets.to_f end
def gsf; gets.chomp end
def gi; gets.split.map(&:to_i) end
def gf; gets.split.map(&:to_f) end
def gs; gets.chomp.split.map(&:to_s) end
def gc; gets.chomp.split('') end
def pr(num); num.prime_division end
def digit(num); num.to_s.length end
def array(s,ini=nil); Array.new(s){ini} end
def darray(s1,s2,ini=nil); Array.new(s1){Array.new(s2){ini}} end
def rep(num); num.times{|i|yield(i)} end
def repl(st,en,n=1); st.step(en,n){|i|yield(i)} end
n = gif
a = gi
sum = []
count = 0
sum << 0
repl 1,a.size do |i|
sum << a[i-1]+sum[i-1]
if sum[i-1] > 0
if sum[i] >= 0
count += sum[i]+1
sum[i] = -1
end
elsif sum[i-1] < 0
if sum[i] <= 0
count += 1-sum[i]
sum[i] = 1
end
end
end
puts count |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <bits/stdc++.h>
int main(void) {
int n, a;
int i, sum = 0, check = 0;
long long int count = 0;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &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>
template <class T, class S>
void cmin(T &a, const S &b) {
if (a > b) a = b;
}
template <class T, class S>
void cmax(T &a, const S &b) {
if (a < b) a = b;
}
using namespace std;
signed main() {
long long int n;
cin >> n;
vector<long long int> v(n);
bool flag = false;
for (long long int i = 0; i < n; i++) cin >> v[i];
vector<long long int> sum(n);
long long int ans = 0;
sum[0] = v[0];
for (long long int i = 0; i < n; i++) sum[i] = v[i];
for (long long int i = 0; i < n; i++) {
if (!i) {
if (sum[0] > 0)
flag = true;
else
flag = false;
continue;
}
sum[i] += sum[i - 1];
if (flag) {
if (sum[i] <= 0)
flag = false;
else {
ans += (abs(sum[i]) + 1);
sum[i] = -1;
flag = false;
}
} else {
if (sum[i] >= 0)
flag = true;
else {
ans += (abs(sum[i]) + 1);
sum[i] = 1;
flag = true;
}
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
c = 0
i = 1
k = 1
sum = a[0]
if a[0] > 0:
while i < n:
if i == 2*k-1:
sum = sum + a[2*k-1]
if sum >= 0:
c = c + sum + 1
sum = -1
else:
pass
i += 1
else:
sum = sum + a[2*k]
if sum <= 0:
c = c - sum + 1
sum = 1
else:
pass
i += 1
k += 1
print(c)
else:
while i < n:
if i == 2*k-1:
sum = sum + a[2*k-1]
if sum <= 0:
c = c - sum + 1
sum = 1
else:
pass
i += 1
else:
sum = sum + a[2*k]
while sum >= 0:
c = c + sum + 1
sum = -1
else:
pass
i += 1
k += 1
print(c) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
int operation_even_pos = 0;
int operation_odd_pos = 0;
long long sum_even_pos = 0;
long long sum_odd_pos = 0;
for (int i = 0; i < n; i++) {
sum_even_pos += a[i];
sum_odd_pos += a[i];
if (!(i % 2) && sum_even_pos <= 0) {
operation_even_pos += 1 - sum_even_pos;
sum_even_pos = 1;
} else if ((i % 2) && sum_even_pos >= 0) {
operation_even_pos += sum_even_pos + 1;
sum_even_pos = -1;
}
if (!(i % 2) && sum_odd_pos >= 0) {
operation_odd_pos += sum_odd_pos + 1;
sum_odd_pos = -1;
} else if ((i % 2) && sum_odd_pos <= 0) {
operation_odd_pos += 1 - sum_odd_pos;
sum_odd_pos = 1;
}
}
cout << min(operation_even_pos, operation_odd_pos) << 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.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
int n;
int[] as;
public static void main(String args[]) {
new Main().run();
}
void run() {
FastReader sc = new FastReader();
n = sc.nextInt();
as = new int[n];
for (int i = 0; i < n; i++) {
as[i] = sc.nextInt();
}
solve();
}
void solve() {
long[] sums = new long[n];
sums[0] = as[0];
for (int i = 1; i < n; i++) {
sums[i] = sums[i - 1] + as[i];
}
long evenCount = 0;
long evenChange = 0;
long[] sumsForEven = sums.clone();
for (int i = 0; i < n; i++) {
sumsForEven[i] += evenChange;
if (i % 2 == 0 && sumsForEven[i] <= 0) {
evenCount += -sumsForEven[i] + 1;
evenChange += -sumsForEven[i] + 1;
sumsForEven[i] = 1;
} else if (i % 2 == 1 && sumsForEven[i] > 0) {
evenCount += sumsForEven[i] + 1;
evenChange -= sumsForEven[i] + 1;
sumsForEven[i] = -1;
}
}
long oddCount = 0;
long oddChange = 0;
for (int i = 0; i < n; i++) {
sums[i] += oddChange;
if (i % 2 == 1 && sums[i] <= 0) {
oddCount += -sums[i] + 1;
oddChange += -sums[i] + 1;
sums[i] = 1;
} else if (i % 2 == 0 && sums[i] > 0) {
oddCount += sums[i] + 1;
oddChange -= sums[i] + 1;
sums[i] = -1;
}
}
System.out.println(Math.min(evenCount, oddCount));
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
using System.Linq;
using System.Collections.Generic;
using static System.Console;
using System.Text;
using System.IO;
using static System.Math;
using System.Numerics;
namespace AtCoder
{
public class Program
{
public static void Main(string[] args)
{
//SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false });
Solve();
//Out.Flush();
Read();
}
static void Solve()
{
var n = Sarray()[0];
var ai = Sarray();
var sum = new long[2] { ai[0], ai[0] };
var cnt = new long[2] { 0, 0 };
if (ai[0] == 0)
{
sum[0] = 1;
sum[1] = -1;
cnt[0]=cnt[1] = 1;
}
for (var c = 0; c < 2; ++c)
{
for (var i = 1; i < n; ++i)
{
var preSum = sum[c];
sum[c] += ai[i];
if (sum[c] * preSum < 0) continue;
cnt[c] += Abs(preSum + ai[i]) + 1;
sum[c] = (0 < preSum) ? -1 : 1;
}
}
WriteLine(Min(cnt[0], cnt[1]));
}
static long Mod = (long)1e9 + 7;
static public long[] Sarray() { return ReadLine().Split().Select(long.Parse).ToArray(); }
static public List<long> Slist() { return ReadLine().Split().Select(long.Parse).ToList(); }
static public (T1 a, T2 b) Slice<T1, T2>()
{
var t = ReadLine().Split();
return (
(T1)Convert.ChangeType(t[0], typeof(T1)),
(T2)Convert.ChangeType(t[1], typeof(T2)));
}
static public (T a, T b) Slice2<T>()
{
var t = ReadLine().Split();
return (
(T)Convert.ChangeType(t[0], typeof(T)),
(T)Convert.ChangeType(t[1], typeof(T)));
}
static public (T a, T b, T c) Slice3<T>()
{
var t = ReadLine().Split();
return (
(T)Convert.ChangeType(t[0], typeof(T)),
(T)Convert.ChangeType(t[1], typeof(T)),
(T)Convert.ChangeType(t[2], typeof(T)));
}
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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> a;
int main() {
int n;
int ans = 0;
int prevSum = 0;
cin >> n;
for (int i = 0; i < n; i++) {
int buf;
cin >> buf;
if (prevSum > 0 && (prevSum + buf) >= 0) {
ans += prevSum + buf + 1;
prevSum = -1;
} else if (prevSum < 0 && (prevSum + buf) <= 0) {
ans += -(prevSum + buf) + 1;
prevSum = 1;
} else {
prevSum += buf;
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | /* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Main
{
static int result1 = Integer.MAX_VALUE;
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.next());
int[] input = new int[n];
int[] result = new int[n];
for(int i = 0; i < n; i++) {
input[i] = Integer.parseInt(sc.next());
}
counting(input, result, 0, 0, true);
counting(input, result, 0, 0, false);
System.out.println(result1);
}
public static void counting(int[] input, int[] result, int count, int index, boolean sign) {
if(index > 0) {
result[index] = result[index-1] + input[index];
} else {
result[index] = input[index];
}
if(sign) {
if(result[index] <= 0) {
count += Math.abs(result[index]) + 1;
result[index] = result[index] + Math.abs(result[index]) + 1;
}
sign = false;
} else {
if(result[index] >= 0) {
count += Math.abs(result[index]) + 1;
result[index] = result[index] - Math.abs(result[index]) - 1;
}
sign = true;
}
if(index < result.length-1) {
counting(input, result, count, index+1, sign);
} else {
if(result1 > count) {
result1 = count;
}
}
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | open Batteries
let () =
let n,t = Scanf.scanf "%d %d " (fun a b -> a,b) in
let t_lst = Array.to_list @@ Array.init n (fun _ -> Scanf.scanf "%d " (fun a -> a)) in
let rec aux prev l =
match l with
| [] -> []
| hd :: tl -> (hd - prev) :: (aux hd tl)
in
let l = aux 0 t_lst in
let res = t + List.fold_left (fun x y -> x + (if y < t then y else t)) 0 l in
Printf.printf "%d\n" res
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import itertools
def sign(num):
if num < 0:
return -1
elif num > 0:
return 1
else:
return 0
N = input()
a_i = list(map(int, input().split()))
a_sum = [a_i[0]]
for i, a in enumerate(a_i[1:]):
i += 1
a_sum.append(a_sum[-1]+a)
signs = [1, -1]
for i, sum_i in enumerate(a_sum):
if sum_i != 0:
signs[i%2] = sign(sum_i)
signs[i%2+1] = -sign(sum_i)
break
a_sum = 0
changes = 0
for i, a in enumerate(a_i):
a_sum += a
if sign(a_sum) != signs[i%2]:
changes += abs(a_sum) + 1
a_sum = signs[i%2]
print(changes)
#
# for i, sum_i in enumerate(a_sum):
# if i == 0:
# signs = [sign(sum_i), -sign(sum_i)]
# elif sign(sum_i) != signs[i%2]:
# a_sum[i:] = [num + (abs(sum_i) + 1) * signs[i%2] for num in a_sum[i:]]
# changes += abs(sum_i) + 1
# # print(a_sum)
# print(changes)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long ans = 0;
long long now;
cin >> now;
if (now == 0) {
now++;
ans++;
}
for (long long(i) = (0); (i) < (n - 1); ++i) {
long long tmp;
cin >> tmp;
if (now > 0) {
now += tmp;
if (now >= 0) {
ans += now + 1;
now = -1;
}
} else {
now += tmp;
if (now <= 0) {
ans -= (now - 1);
now = 1;
}
}
cout << now << ' ' << ans << endl;
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long int a[n], sum[n];
cin >> a[0];
sum[0] = a[0];
long long int ans = 0;
for (int i = 1; i < n; i++) cin >> a[i];
if (sum[0] > 0) {
for (int i = 1; i < n; i++) {
sum[i] = sum[i - 1] + a[i];
if (i & 1) {
if (sum[i] >= 0) {
ans += sum[i] + 1;
sum[i] = -1;
}
} else {
if (sum[i] <= 0) {
ans += 1 - sum[i];
sum[i] = 1;
}
}
}
} else if (sum[0] < 0) {
for (int i = 1; i < n; i++) {
sum[i] = sum[i - 1] + a[i];
if (i & 1) {
if (sum[i] <= 0) {
ans += 1 - sum[i];
sum[i] = 1;
}
} else {
if (sum[i] >= 0) {
ans += sum[i] + 1;
sum[i] = -1;
}
}
}
} else {
long long int ans2 = 1, ans3 = 1;
sum[0] = 1;
for (int i = 1; i < n; i++) {
sum[i] = sum[i - 1] + a[i];
if (i & 1) {
if (sum[i] >= 0) {
ans2 += sum[i] + 1;
sum[i] = -1;
}
} else {
if (sum[i] <= 0) {
ans2 += 1 - sum[i];
sum[i] = 1;
}
}
}
sum[0] = -1;
for (int i = 1; i < n; i++) {
sum[i] = sum[i - 1] + a[i];
if (i & 1) {
if (sum[i] <= 0) {
ans3 += 1 - sum[i];
sum[i] = 1;
}
} else {
if (sum[i] >= 0) {
ans3 += sum[i] + 1;
sum[i] = -1;
}
}
}
ans = min(ans2, ans3);
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | #!/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 | cpp | #include <bits/stdc++.h>
using namespace std;
constexpr int MOD = 1000000007;
using long long = long long;
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;
}
void print(const std::vector<int> &v) {
std::for_each(v.begin(), v.end(), [](int x) { std::cout << x << " "; });
std::cout << std::endl;
}
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (long long i = 0; i < (long long)n; i++) {
cin >> a[i];
}
long long res = (1ll << 60);
long long ans = 0LL;
long long s = a[0];
for (int i = 1; i < n; i++) {
s += a[i];
if (i % 2 == 1) {
if (s >= 0) {
ans += s + 1;
s = -1;
}
} else {
if (s <= 0) {
ans += -s + 1;
s = 1;
}
}
}
res = min(res, ans);
ans = 0;
s = a[0];
for (int i = 1; i < n; i++) {
s += a[i];
if (i % 2 == 0) {
if (s >= 0) {
ans += s + 1;
s = -1;
}
} else {
if (s <= 0) {
ans += -s + 1;
s = 1;
}
}
}
res = min(res, ans);
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 | java | import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
long count1 = 0;
long sum1 = 0;
// 最初の和が正
for (int i = 0; i < n; i++) {
sum1 += a[i];
if (i % 2 == 0) {
while (sum1 <= 0) {
sum1++;
count1++;
}
} else {
while (sum1 >= 0) {
sum1--;
count1++;
}
}
}
long count2 = 0;
long sum2 = 0;
// 最初の和が負
for (int i = 0; i < n; i++) {
sum2 += a[i];
if (i % 2 == 0) {
while (sum2 >= 0) {
sum2--;
count2++;
}
} else {
while (sum2 <= 0) {
sum2++;
count2++;
}
}
}
long ans = Long.min(count1, count2);
System.out.println(ans);
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | from sys import stdin
import sys
import math
n = int(input())
a = list(map(int, stdin.readline().rstrip().split()))
#print(odd)
#print(pair)
## odd
count_odd = 0
current_sum = 0
for i in range(len(a)):
if i % 2 == 1:
if current_sum + a[i] < 1:
diff = 1 - (current_sum + a[i])
a[i] += diff
count_odd += diff
elif i % 2 == 0:
if current_sum + a[i] > -1:
diff = -1 - (current_sum + a[i])
a[i] += diff
count_odd += -1 * diff
current_sum += a[i]
## pair
count_pair = 0
current_sum = 0
for i in range(len(a)):
if i % 2 == 1:
if current_sum + a[i] > -1:
diff = -1 - (current_sum + a[i])
a[i] += diff
count_pair += -1 * diff
elif i % 2 == 0:
if current_sum + a[i] < 1:
diff = 1 - (current_sum + a[i])
a[i] += diff
count_pair += diff
else:
print("error")
current_sum += a[i]
print(min(count_odd, count_pair))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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 N = 3e5 + 10;
long long gcd(long long n, long long m) {
if (n == 0)
return m;
else
return gcd(m % n, n);
}
long long a[N];
int32_t main() {
long long n;
cin >> n;
for (long long i = 1; i <= n; i++) {
cin >> a[i];
}
long long sum = 0;
long long ans = 0;
for (long long i = 1; i <= n; i++) {
sum += a[i];
if (i % 2 == 1) {
if (sum < 0) {
} else {
ans += sum + 1;
sum = -1;
}
} else {
if (sum > 0) {
} else {
ans += abs(sum) + 1;
sum = 1;
}
}
}
long long ans1 = ans;
ans = 0;
sum = 0;
for (long long i = 1; i <= n; i++) {
sum += a[i];
if (i % 2 == 1) {
if (sum > 0)
continue;
else {
sum = 1;
ans += abs(sum) + 1;
}
} else {
if (sum < 0) {
continue;
} else {
ans += sum + 1;
sum = -1;
}
}
}
cout << min(ans, ans1) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long n;
scanf("%ld", &n);
long a[n];
for (long i = 0; i < n; i++) scanf(" %ld", &a[i]);
long S = a[0];
long j = 0;
for (long 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("%ld\n", j);
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
int ans = 0;
if (a[0] >= 0) {
int sum = a[0];
for (int i = 1; i < n; ++i) {
if (i % 2 == 1) {
if (sum + a[i] < 0) {
sum += a[i];
} else {
ans += sum + a[i] + 1;
sum = -1;
}
} else {
if (sum + a[i] > 0) {
sum += a[i];
} else {
ans += abs(sum + a[i] - 1);
sum = 1;
}
}
}
} else {
int sum = a[0];
for (int i = 1; i < n; ++i) {
if (i % 2 == 1) {
if (sum + a[i] > 0) {
sum += a[i];
} else {
ans += abs(sum + a[i] - 1);
sum = 1;
}
} else {
if (sum + a[i] < 0) {
sum += a[i];
} else {
ans += sum + a[i] + 1;
sum = -1;
}
}
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | 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 ans = 0, sum = 0;
if (a[0] != 0)
sum = a[0];
else if (a[1] > 0)
sum = -1, ans++;
else
sum = 1, ans++;
for (int i = 1; i <= (N - 1); ++i) {
if ((sum + a[i]) * sum >= 0) {
int k = a[i];
a[i] = (abs(sum) + 1) * (sum / abs(sum)) * (-1);
ans += abs(k - a[i]);
}
sum += a[i];
}
cout << ans << '\n';
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using long long = long long;
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 INF = 1e9;
long long n;
vector<long long> a;
int main() {
cin >> n;
a.resize(n);
for (long long i = 0; i < (n); ++i) cin >> a[i];
long long sum;
long long ans = 0;
sum = -1;
ans += abs(a[0] - sum);
for (long long i = 0; i < (n - 1); ++i) {
if (sum < 0) {
if (sum + a[i + 1] > 0) {
sum += a[i + 1];
continue;
} else {
ans += 1 - sum - a[i + 1];
sum = 1;
}
} else {
if (sum + a[i + 1] < 0) {
sum += a[i + 1];
continue;
} else {
ans += a[i + 1] + sum + 1;
sum = -1;
}
}
}
long long ans2 = 0;
sum = 1;
ans2 += abs(a[0] - sum);
for (long long i = 0; i < (n - 1); ++i) {
if (sum < 0) {
if (sum + a[i + 1] > 0) {
sum += a[i + 1];
continue;
} else {
ans2 += 1 - sum - a[i + 1];
sum = 1;
}
} else {
if (sum + a[i + 1] < 0) {
sum += a[i + 1];
continue;
} else {
ans2 += a[i + 1] + sum + 1;
sum = -1;
}
}
}
chmin(ans, ans2);
long long ans3 = 0;
sum = a[0];
for (long long i = 0; i < (n - 1); ++i) {
if (sum == 0) continue;
if (sum < 0) {
if (sum + a[i + 1] > 0) {
sum += a[i + 1];
continue;
} else {
ans3 += 1 - sum - a[i + 1];
sum = 1;
}
} else {
if (sum + a[i + 1] < 0) {
sum += a[i + 1];
continue;
} else {
ans3 += a[i + 1] + sum + 1;
sum = -1;
}
}
}
chmin(ans, ans3);
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | def sequence(N: int, A: list) -> int:
op1, op2 = 0, 0
s1, s2 = 0, 0
for i, a in enumerate(A[1:]):
s1, s2 = s1 + a, s2 + a
if i % 2: # odd
if s1 <= 0:
op1 += abs(s1) + 1
s1 += abs(s1) + 1
if s2 >= 0:
op2 += abs(s2) + 1
s2 -= abs(s2) + 1
else: # even
if s1 >= 0:
op1 += abs(s1) + 1
s1 -= abs(s1) + 1
if s2 >= 0:
op2 += abs(s2) + 1
s2 += abs(s2) + 1
return min(s1, s2)
if __name__ == "__main__":
N = int(input())
A = [int(s) for s in input().split()]
ans = sequence(N, A)
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int d[n];
for (int i = 0; i < n; i++) {
cin >> d[i];
}
int count = 0;
int sum = d[0];
int f = 0;
if (d[0] > 0) {
f = -1;
}
if (d[0] < 0) {
f = 1;
}
for (int i = 1; i < n; i++) {
sum += d[i];
if (sum == 0) {
if (f == 1) {
count++;
f = -1;
sum = 1;
continue;
}
if (f == -1) {
count++;
f = 1;
sum = -1;
continue;
}
}
if (sum > 0) {
if (f == 1) {
f = -1;
continue;
}
if (f == -1) {
count += sum + 1;
sum = -1;
f = 1;
continue;
}
}
if (sum < 0) {
if (f == -1) {
f = 1;
continue;
}
if (f == 1) {
count += 1 - sum;
sum = 1;
f = -1;
continue;
}
}
}
int ccount = 0;
int ssum;
int ff = 0;
if (d[0] > 0) {
ff = 1;
ccount = 1 + d[0];
ssum = -1;
}
if (d[0] < 0) {
ff = -1;
ccount = 1 - d[0];
ssum = 1;
}
for (int i = 1; i < n; i++) {
sum += d[i];
if (ssum == 0) {
if (ff == 1) {
ccount++;
ff = -1;
ssum = 1;
continue;
}
if (ff == -1) {
ccount++;
ff = 1;
ssum = -1;
continue;
}
}
if (ssum > 0) {
if (f == 1) {
ff = -1;
continue;
}
if (ff == -1) {
ccount += sum + 1;
ssum = -1;
ff = 1;
continue;
}
}
if (ssum < 0) {
if (ff == -1) {
ff = 1;
continue;
}
if (ff == 1) {
ccount += 1 - sum;
ssum = 1;
ff = -1;
continue;
}
}
}
cout << min(count, ccount) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ABC59C
{
class Program
{
static void Main(string[] args)
{
int n = int.Parse(Console.ReadLine());
string[] str = Console.ReadLine().Split(' ');
int[] a = new int[n];
for(int i = 0; i < n; i++)
{
a[i] = int.Parse(str[i]);
}
int x = 0;
int y = 0;
int f = 0;
int sum = 0;
for(int i = 0; i < n; i++)
{
sum += a[i];
if (f == 1 && sum >= 0)
{
x += (sum + 1);
f = 0;
sum = -1;
}else if (f == 0 && sum <= 0)
{
x += (1 - sum);
f = 1;
sum = 1;
}else
{
f = 1 - f;
}
}
f = 1;
for (int i = 0; i < n; i++)
{
sum += a[i];
if (f == 1 && sum >= 0)
{
y += (sum + 1);
f = 0;
sum = -1;
}
else if (f == 0 && sum <= 0)
{
y += (1 - sum);
f = 1;
sum = 1;
}
else
{
f = 1 - f;
}
}
Console.WriteLine(Math.Min(x,y));
}
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a_list = [int(i) for i in input().split(' ')]
total = [a_list[0]]
counter = 0
for a in a_list[1:]:
total.append(total[-1] + a)
if total[0]==0:
total = list(map(lambda n: n-int(total[1]/abs(total[1])), total))
for i in range(1,n):
if total[i-1]<0 and total[i]<=0:
counter += abs(total[i])+1
total[i:] = list(map(lambda n: n-(total[i])+1, total[i:]))
elif total[i-1]>0 and total[i]>=0:
counter += abs(total[i])+1
total[i:] = list(map(lambda n: n-(total[i])-1, total[i:]))
else:
continue
print(counter) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long int INF = (long long int)1e18;
int main() {
int n;
cin >> n;
long long int ans = 0;
long long int t;
cin >> t;
for (int i = (0); i < (n - 1); ++i) {
int a;
cin >> a;
if ((t > 0 && t + a >= 0) || (t < 0 && t + a <= 0)) {
ans += abs(t + a) + 1;
if (t > 0) {
t = -1;
} else {
t = 1;
}
} else {
t += 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;
int32_t main() {
uint64_t N;
cin >> N;
int64_t total;
cin >> total;
int64_t sign = total / abs(total);
uint64_t count = 0;
for (uint64_t i = 1; i < N; i++) {
int64_t val;
cin >> val;
total += val;
sign *= -1;
if ((total == 0) || (sign * total < 0)) {
count += abs(sign - total);
total = sign;
}
}
cout << count << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
cnt = 0
x = []
x.append(a[0])
for i in range(n-1):
if x[i] > 0:
while x[i]+a[i+1] >= 0:
cnt += 1
a[i+1] -= 1
x += [x[i] + a[i+1]]
else:
while x[i]+a[i+1] <= 0:
cnt += 1
a[i+1] += 1
x += [x[i] + a[i+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 |
use std::io::*;
use std::str::FromStr;
pub fn read<T: FromStr>() -> T {
let stdin = stdin();
let stdin = stdin.lock();
let token: String = stdin
.bytes()
.map(|c| c.expect("failed to read char") as char)
.skip_while(|c| c.is_whitespace())
.take_while(|c| !c.is_whitespace())
.collect();
token.parse().ok().expect("failed to parse token")
}
use std::cmp::{max, min};
use std::collections::BTreeMap;
fn main() {
let n = read::<i64>();
let mut vec_a = vec![];
for i in 0..n {
vec_a.push(read::<i64>());
}
let mut prev_sum = 0 as i64;
let mut ans_plus = 0;
let mut prev_sum2 = 0 as i64;
let mut ans_minus = 0;
// 最初を正にする
if vec_a[0] <= 0 {
prev_sum = 1;
ans_plus = (1 - prev_sum).abs();
} else {
prev_sum = vec_a[0];
}
// 2こ目以降は流れで
for i in 1..vec_a.len() {
let b = vec_a[i as usize];
if 0 < prev_sum {
if 0 <= prev_sum + b {
ans_plus += (1 + prev_sum).abs() + b;
prev_sum = -1;
} else {
prev_sum += b;
}
} else if prev_sum < 0 {
if prev_sum + b <= 0 {
ans_plus += (1 - prev_sum).abs() - b;
prev_sum = 1;
} else {
prev_sum += b;
}
}
}
// 最初を負にする
if 0 <= vec_a[0] {
prev_sum2 = -1;
ans_minus = (1 + prev_sum2).abs();
} else {
prev_sum2 = vec_a[0];
}
// 2こ目以降は流れで
for i in 1..vec_a.len() {
let b = vec_a[i as usize];
if 0 < prev_sum2 {
if 0 <= prev_sum2 + b {
ans_minus += (1 + prev_sum2).abs() + b;
prev_sum2 = -1;
} else {
prev_sum2 += b;
}
} else if prev_sum2 < 0 {
if prev_sum2 + b <= 0 {
ans_minus += (1 - prev_sum2).abs() - b;
prev_sum2 = 1;
} else {
prev_sum2 += b;
}
}
}
// println!("plus: {}, minus: {}", ans_minus, ans_plus);
println!("{}", min(ans_minus, ans_plus));
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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 T1, class T2>
using dict = std::unordered_map<T1, T2>;
int main() {
int n;
cin >> n;
int64_t a[n];
for (int i = 0; i < (int)(n); i++) cin >> a[i];
int64_t s = 0;
int64_t count1 = 0;
int64_t count2 = 0;
for (int i = 0; i < (int)(n); i++) {
s += a[i];
if (i % 2 == 1 && s <= 0) {
count1 += -s + 1;
s = 1;
} else if (i % 2 == 0 && s >= 0) {
count1 += s + 1;
s = -1;
}
}
for (int i = 0; i < (int)(n); i++) {
s += a[i];
if (i % 2 == 1 && s >= 0) {
count2 += s + 1;
s = -1;
} else if (i % 2 == 0 && s <= 0) {
count2 += -s + 1;
s = 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;
const long MOD = 1e9 + 7;
const long LINF = 1e13;
const long LLINF = 1e18;
template <class T>
void Swap(T& r, T& l) {
T tmp = r;
r = l;
l = tmp;
}
int main() {
long n;
cin >> n;
vector<long> a(n);
vector<long> accum(n, 0);
for (int i = 0; i < n; ++i) {
cin >> a[i];
accum[i] = a[i];
if (i > 0) accum[i] += accum[i - 1];
}
vector<long> accumtmp(n, 0);
copy(accum.begin(), accum.end(), accumtmp.begin());
for (int i = 0; i < n; ++i) {
cerr << accum[i] << endl;
}
long ans = 0;
long count = 0;
long tmpcount = 0;
for (int i = 1; i < n; ++i) {
if (i % 2 == 1) {
if (accumtmp[i] + tmpcount >= 0) {
long tmpc = -(-1 - (accumtmp[i] + tmpcount));
count += tmpc;
tmpcount -= tmpc;
accumtmp[i] = -1;
}
} else {
if (accumtmp[i] + tmpcount <= 0) {
long tmpc = 1 - (accumtmp[i] + tmpcount);
count += tmpc;
tmpcount += tmpc;
}
}
}
for (int i = 0; i < n; ++i) {
cerr << accumtmp[i] << endl;
}
ans = count;
cerr << "count:" << count << endl;
count = 0;
cerr << endl;
for (int i = 0; i < n; ++i) {
cerr << accum[i] << endl;
}
copy(accum.begin(), accum.end(), accumtmp.begin());
tmpcount = 0;
for (int i = 1; i < n; ++i) {
long accump = accumtmp[i] + tmpcount;
if (i % 2 == 0) {
if (accump >= 0) {
cerr << "a[i] " << a[i] << endl;
long tmpc = -(-1 - accump);
count += tmpc;
accumtmp[i] = -1;
tmpcount -= tmpc;
}
} else {
if (accump <= 0) {
cerr << "a[i] " << a[i] << endl;
long tmpc = 1 - accump;
count += tmpc;
tmpcount += tmpc;
}
}
}
for (int i = 0; i < n; ++i) {
cerr << accumtmp[i] << endl;
}
cerr << "count:" << count << endl;
ans = min(ans, count);
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 | def main():
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
def Chk(a, pos):
cnt = 0
tmp = 0
for a in A:
tmp += a
if pos and tmp < 1:
cnt += 1 - tmp
tmp = 1
elif not pos and tmp > -1:
cnt += 1 + tmp
tmp = -1
pos = not pos
return cnt
print(min(Chk(A, True), Chk(A, False)))
if __name__ == '__main__':
main() |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #!/usr/bin/env ruby
#
n = STDIN.gets.to_i
a = STDIN.gets.split(' ').map{|x| x.to_i}
s = Array.new(n,0)
output = 0
s[0] = a[0]
1.upto(n-1) do |i|
s[i] = s[i-1] + a[i]
if s[i] == 0
change = 1
s[i] = (s[i-1] > 0) ? -1 : 1
else
if (s[i-1] > 0) ^ (s[i] > 0)
change = 0
else
change = s[i].abs+1
s[i] = (s[i-1] > 0) ? -1: 1
end
end
output += change
end
puts output
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long MOD7 = 1000000007;
const long long MOD9 = 1000000009;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
long long N;
cin >> N;
vector<long long> vec(N);
for (long long i = 0; i < N; i++) cin >> vec[i];
long long res, partial, distance_0;
vector<long long> res_vec;
bool flag_before;
for (long long n = 0; n < 2; ++n) {
res = 0;
if (vec[0] == 0) {
partial = (n == 0) ? +1 : -1;
} else {
partial = vec[0];
if (n == 1) break;
}
flag_before = partial > 0;
for (long long i = 1; i < N; ++i) {
partial += vec[i];
distance_0 = abs(partial) + 1;
if (flag_before) {
if (partial >= 0) {
res += distance_0;
partial -= distance_0;
}
} else {
if (partial <= 0) {
res += distance_0;
partial += distance_0;
}
}
flag_before = !flag_before;
}
res_vec.push_back(res);
}
cout << *min_element(((res_vec)).begin(), ((res_vec)).end()) << "\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 | def 解():
iN = int(input())
aA = [int(_) for _ in input().split()]
iL = len(aA)
iStart = 0
if sum(aA[0::2]) < sum(aA[2::2]):
iStart = 1
iC = 0
aD = [0]*iL
if 0 % 2 == iStart :
if aA[0] < 0:
aA[0] = 1
iC += -1 * aA[0] + 1
else:
if 0 < aA[0] :
aA[0] = -1
iC += aA[0] + 1
aD[0] = aA[0]
for i in range(1,iL):
aD[i] = aD[i-1]+aA[i]
if i % 2 == iStart:
if aD[i] <= 0:
iC += -1*aD[i] +1
aD[i] = 1
else:
if aD[i] >= 0:
iC += aD[i] +1
aD[i] = -1
print(iC)
解()
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | import qualified Data.ByteString.Char8 as BC
import Data.Maybe (fromJust)
main = do
n <- readLn :: IO Int
(a:as) <- getIntListBC
let ans1 = solve a as
x = (+1) $ abs a
ans2 = if a > 0 then x + solve (a-x) as
else x + solve (a+x) as
print $ min ans1 ans2
bsToInt :: BC.ByteString -> Int
bsToInt = fst . fromJust . BC.readInt
getIntListBC :: IO [Int]
getIntListBC = map bsToInt . BC.words <$> BC.getLine
solve :: Int -> [Int] -> Int
solve _ [] = 0
solve s (a:as)
| s > 0 = let n = negate $ s + 1
in if n > a then solve (s + a) as
else (abs $ a - n) + solve (s + n) as
| otherwise = let n = negate $ s - 1
in if n < a then solve (s + a) as
else (abs $ n - a) + solve (s + n) as
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | 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];
for (int i = 1; i < N; i++) {
su += A[i];
if (i % 2 == 1) {
if (su < 1) {
count1 += -1 * su + 1;
su = 1;
}
} else {
if (su > -1) {
count1 += su + 1;
su = -1;
}
}
}
su = A[0];
for (int i = 1; i < N; i++) {
su += A[i];
if (i % 2 == 0) {
if (su < 1) {
count2 += -1 * su + 1;
su = 1;
}
} else {
if (su > -1) {
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;
using ll = long long;
const ll INF = 1 << 29;
int main() {
int n;
cin >> n;
vector<ll> a(n), b(n);
for (int i = 0; i < int(n); i++) {
ll aa;
cin >> aa;
a[i] = aa;
b[i] = aa;
}
ll countmin = INF;
ll count = 0LL;
ll temp;
if (a[0] <= 0LL) {
temp = llabs(a[0]) + 1LL;
a[0] += temp;
count += temp;
}
for (int i = (1); i < (n); i++) {
a[i] += a[i - 1];
if (i % 2 == 1) {
if (a[i] >= 0LL) {
temp = llabs(a[i]) + 1LL;
a[i] -= temp;
count += temp;
}
}
if (i % 2 == 0) {
if (a[i] <= 0LL) {
temp = llabs(a[i]) + 1LL;
a[i] += temp;
count += temp;
}
}
}
countmin = min(countmin, count);
count = 0LL;
if (b[0] >= 0) {
temp = llabs(b[0]) + 1LL;
b[0] -= temp;
count += temp;
}
for (int i = (1); i < (n); i++) {
b[i] += b[i - 1];
if (i % 2 == 1) {
if (b[i] <= 0LL) {
temp = llabs(b[i]) + 1LL;
b[i] += temp;
count += temp;
}
}
if (i % 2 == 0) {
if (b[i] >= 0LL) {
temp = llabs(b[i]) + 1LL;
b[i] -= temp;
count += temp;
}
}
}
countmin = min(countmin, count);
cout << countmin << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < (n); ++i) cin >> a[i];
long long total = a[0];
long long total2 = a[0];
long long ans = 0;
for (int i = (1); i < (n); ++i) {
total += a[i];
if (total * total2 >= 0) {
if (total2 > 0) {
ans += total + 1;
total = -1;
} else {
ans += -total + 1;
total = 1;
}
}
total2 = total;
}
total = a[0];
total2 = a[0];
long long ans2 = 0;
if (total2 > 0) {
ans2 += total2 + 1;
total2 = -1;
} else {
ans2 += -total2 + 1;
total2 = 1;
}
total = total2;
for (int i = (1); i < (n); ++i) {
total += a[i];
if (total * total2 >= 0) {
if (total2 > 0) {
ans2 += total + 1;
total = -1;
} else {
ans2 += -total + 1;
total = 1;
}
}
total2 = total;
}
cout << min(ans, ans2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a;
cin >> a;
long long sum = a;
long long cnt = 0;
for (int i = 1; i < n; ++i) {
cin >> a;
int next = sum + a;
int c, diff;
c = diff = 0;
if (sum > 0) {
if (next >= 0) {
diff = (-1 - sum);
a = diff - a;
c = diff;
}
} else {
if (next <= 0) {
diff = (1 - sum) - a;
a += diff;
c = diff;
}
}
sum += a;
cnt += abs(c);
}
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;
vector<int> sum;
int calc_cost(bool ps) {
int res, dis;
res = dis = 0;
vector<int> temp = sum;
for (int i = 0; i < temp.size(); ++i) {
temp[i] += dis;
if (i % 2 == 0) {
if (temp[i] == 0) {
++res;
if (ps) {
++dis;
} else {
--dis;
}
} else if (ps && temp[i] < 0) {
res += -temp[i] + 1;
dis += -temp[i] + 1;
} else if (!ps && temp[i] > 0) {
res += temp[i] + 1;
dis -= temp[i] + 1;
}
} else {
if (temp[i] == 0) {
++res;
if (ps) {
--dis;
} else {
++dis;
}
} else if (ps && temp[i] > 0) {
res += temp[i] + 1;
dis -= temp[i] + 1;
} else if (!ps && temp[i] < 0) {
res += -temp[i] + 1;
dis += -temp[i] + 1;
}
}
}
return res;
}
int main(void) {
int n;
cin >> n;
sum = vector<int>(n, 0);
for (int i = 0; i < n; ++i) {
int a;
cin >> a;
if (i == 0) {
sum[i] = a;
} else {
sum[i] += sum[i - 1] + a;
}
}
cout << min(calc_cost(true), calc_cost(false)) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
const int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
using namespace std;
int main() {
int n;
cin >> n;
long long a[n];
long long sums[n];
cin >> a[0];
sums[0] = a[0];
for (int i = 1; i < n; ++i) {
cin >> a[i];
sums[i] = sums[i - 1] + a[i];
}
long long cnt = 0;
long long v = 0;
long long diff;
if (sums[0] * sums[1] >= 0) {
if (sums[0] >= 0 && sums[0] < sums[1]) {
diff = -1 - sums[0];
sums[0] += diff;
v += diff;
cnt += abs(diff);
} else if (sums[0] < 0 && sums[0] > sums[1]) {
diff = 1 - sums[0];
sums[0] += diff;
v += diff;
cnt += abs(diff);
}
}
for (int i = 1; i < n; i++) {
sums[i] += v;
if (sums[i - 1] * sums[i] >= 0) {
if (sums[i - 1] < 0) {
diff = 1 - sums[i];
} else {
diff = -1 - sums[i];
}
sums[i] += diff;
v += diff;
cnt += abs(diff);
}
}
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 | python2 | # -*- coding:utf-8 -*-
n = int(raw_input())
numlist = (raw_input()).split(' ')
count = 0
if (int(numlist[0]) == 0):
if (int(numlist[1]) < 0):
numlist[0] = -1
else:
numlist[0] = 1
sumlist = [int(numlist[0])]
for i in range(1, n):
sumlist.append(sumlist[i-1] + int(numlist[i]))
while (True):
if (sumlist[i-1] > 0 and sumlist[i] > 0): #i-1,i番目までのsumがともに正
numlist[i] = int(numlist[i]) - (sumlist[i] + 1)
count += sumlist[i] + 1
sumlist[i] = -1
elif (sumlist[i-1] < 0 and sumlist[i] < 0): #i-1,i番目までのsumがともに負
numlist[i] = int(numlist[i]) + ((-1)*sumlist[i] + 1)
count += (-1)*sumlist[i] + 1
sumlist[i] = 1
elif (sumlist[i] == 0): #i番目までのsum=0
if (sumlist[i-1] > 0):
numlist[i] = int(numlist[i]) - 1
sumlist[i] -= 1
if (sumlist[i-1] < 0):
numlist[i] = int(numlist[i]) + 1
sumlist[i] += 1
count += 1
else:
break
#print numlist
#print sumlist
print count
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long ans = 0, sum = 0, tmp;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
bool sign;
sum = a[0];
if (sum == 0) {
sum++;
ans++;
}
sum > 0 ? sign = true : sign = false;
for (int i = 1; i < n; i++) {
tmp = sum + a[i];
if (sign && tmp >= 0) {
sum = -1;
sign = false;
ans += abs(tmp) + 1;
} else if (!sign && tmp <= 0) {
sum = 1;
sign = true;
ans += abs(tmp) + 1;
} else {
sum += a[i];
sign = !sign;
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const long long LLINF = 1e18;
int main() {
int n;
cin >> n;
long long ruisekiwa;
cin >> ruisekiwa;
long long ans = 0;
if (ruisekiwa != 0) {
for (int i = (1); i < (n); i++) {
long long a;
cin >> a;
if (ruisekiwa * (ruisekiwa + a) < 0) {
ruisekiwa += a;
} else if (ruisekiwa > 0) {
ans += (ruisekiwa + a) + 1;
ruisekiwa = -1;
} else {
ans += 1 - (ruisekiwa + a);
ruisekiwa = 1;
}
}
} else {
long long ans1 = 1;
ruisekiwa = 1;
for (int i = (1); i < (n); i++) {
long long a;
cin >> a;
if (ruisekiwa * (ruisekiwa + a) < 0) {
ruisekiwa += a;
} else if (ruisekiwa > 0) {
ans1 += (ruisekiwa + a) + 1;
ruisekiwa = -1;
} else {
ans1 += 1 - (ruisekiwa + a);
ruisekiwa = 1;
}
}
long long ans2 = 1;
ruisekiwa = -1;
for (int i = (1); i < (n); i++) {
long long a;
cin >> a;
if (ruisekiwa * (ruisekiwa + a) < 0) {
ruisekiwa += a;
} else if (ruisekiwa > 0) {
ans2 += (ruisekiwa + a) + 1;
ruisekiwa = -1;
} else {
ans2 += 1 - (ruisekiwa + a);
ruisekiwa = 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 | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long> a(n), sum1(n), sum2(n);
cin >> a[0];
sum1[0] = sum2[0] = a[0];
long ans1 = 0, ans2 = 0;
for (int i = 1; i < n; i++) {
cin >> a[i];
sum1[i] = sum1[i - 1] + a[i];
sum2[i] = sum2[i - 1] + a[i];
if (i % 2 == 0) {
if (sum1[i] <= 0) {
ans1 += abs(1 - sum1[i]);
sum1[i] = 1;
}
if (sum2[i] >= 0) {
ans2 += abs(-1 - sum2[i]);
sum2[i] = -1;
}
}
if (i % 2 == 1) {
if (sum1[i] >= 0) {
ans1 += abs(-1 - sum1[i]);
sum1[i] = -1;
}
if (sum2[i] <= 0) {
ans2 += abs(1 - sum2[i]);
sum2[i] = 1;
}
}
}
cout << min(ans1, ans2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n=int(input())
count=0
A=[int(i) for i in input().split()]
if A[0]==0:
A[0]+=1
count+=1
elif A[0]<0:
for i in range(len(A)):
A[i]*=(-1)
for i in range(1,len(A)):
s=sum(A[:i+1])
if i%2==0:
if s<=0:
A[i]+=1-s
count+=1-s
else:
if s>=0:
A[i]-=1+s
count+=1+s
print(count) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int N;
vector<int> A;
int sum;
long long trn;
void calcu(int p, char s) {
if (p >= N) return;
int x = A[p];
if (s == '+') {
if (sum + x <= 0) x = 1 - sum;
sum += x;
trn += abs(A[p] - x);
calcu(++p, '-');
} else {
if (sum + x > 0) x = -1 - sum;
sum += x;
trn += abs(A[p] - x);
calcu(++p, '+');
}
}
int main(void) {
cin >> N;
A.resize(N);
for (int i = 0; i < N; ++i) cin >> A[i];
sum = (A[0] >= 0) ? A[0] : 1;
trn = (A[0] >= 0) ? 0 : 1 - A[0];
calcu(1, '-');
long long a = trn;
sum = (A[0] < 0) ? A[0] : -1;
trn = (A[0] < 0) ? 0 : 1 + A[0];
calcu(1, '+');
long long b = trn;
long long ans = (a < b) ? a : b;
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 | UNKNOWN | #include <bits/stdc++.h>
int main(void) {
long long int i, a, n;
long long int sum = 0, bsum = 0, ans = 0;
scanf("%lld", &n);
for (i = 0; i < n; i++) {
scanf("%lld", &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 <bits/stdc++.h>
using namespace std;
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
static const int NIL = -1;
int n;
void printArray(int array[], int n) {
for (int i = (0); i < (n); ++i) {
if (i) cout << " ";
cout << array[i];
}
cout << endl;
}
int sequence(int* a, bool sign) {
int sum = 0, cnt = 0;
for (int i = (0); i < (n); ++i) {
sum += a[i];
if (sign) {
if (sum > -1) {
int rem = abs(-1 - sum);
cnt += rem;
sum = -1;
}
sign = false;
} else {
if (sum < 1) {
int rem = abs(1 - sum);
cnt += rem;
sum = 1;
}
sign = true;
}
}
return cnt;
}
int main(int argc, char const* argv[]) {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> n;
int a[n];
for (int i = (0); i < (n); ++i) cin >> a[i];
int pos = sequence(a, true);
int neg = sequence(a, false);
cout << min(pos, neg) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
int N[100000];
cin >> n;
for (int i = 0; i < n; i++) {
cin >> N[i];
}
int ans = 0;
if (N[0] == 0) {
if (N[1] >= 0) {
N[0] = -1;
}
if (N[1] < 0) {
N[0] = 1;
}
}
int sum = N[0];
if (N[0] > 0) {
for (int i = 1; i < n; i++) {
sum = sum + N[i];
if (i % 2 == 0 && sum <= 0) {
N[i] = N[i] - sum + 1;
ans = ans - sum + 1;
sum = 1;
}
if (i % 2 == 1 && sum >= 0) {
N[i] = N[i] - sum - 1;
ans = ans + sum + 1;
sum = -1;
}
}
}
if (N[0] < 0) {
for (int i = 1; i < n; i++) {
sum = sum + N[i];
if (i % 2 == 0 && sum >= 0) {
N[i] = N[i] - sum - 1;
ans = ans + sum + 1;
sum = -1;
}
if (i % 2 == 1 && sum <= 0) {
N[i] = N[i] - sum + 1;
ans = ans - sum + 1;
sum = 1;
}
}
}
cout << ans;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n;
long long a[100004];
int main() {
scanf("%d", &n);
for (int i = (1); i <= (int)(n); ++i) scanf("%lld", &a[i]);
long long ans = 0;
printf("1000000000000000\n");
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n;
vector<long long> a;
enum State { Plus, Minus, Zero };
State GetState(long long sum) {
State state;
if (sum > 0)
state = Plus;
else if (sum == 0)
state = Zero;
else
state = Minus;
return state;
}
unsigned long long Count(State initialState) {
unsigned long long count = 0;
vector<long long> b = a;
State state = GetState(b[0]);
if (state == Zero) {
if (initialState != Zero) {
if (initialState == Plus)
b[0] = 1;
else if (initialState == Minus)
b[0] = -1;
count++;
}
}
long long sum = b[0];
for (int i = 1; i < n; i++) {
State nextState = GetState(sum + b[i]);
switch (nextState) {
case Plus:
if (state == Plus) {
long long bf_a = b[i];
b[i] = -1 - sum;
count += abs(b[i] - bf_a);
nextState = Minus;
}
break;
case Minus:
if (state == Minus) {
long long bf_a = b[i];
b[i] = 1 - sum;
count += abs(b[i] - bf_a);
nextState = Plus;
}
break;
case Zero:
if (state == Plus) {
long long bf_a = b[i];
b[i] = -1 - sum;
count += abs(b[i] - bf_a);
nextState = Minus;
} else if (state == Minus) {
long long bf_a = b[i];
b[i] = 1 - sum;
count += abs(b[i] - bf_a);
nextState = Plus;
}
default:
break;
}
sum += b[i];
state = nextState;
}
if (sum == 0) count++;
return count;
}
int main() {
cin >> n;
a.resize(n);
for (int i = 0; i < n; i++) cin >> a[i];
unsigned long long pCount = Count(Plus);
unsigned long long mCount = Count(Minus);
unsigned long long zCount = Count(Zero);
unsigned long long temp = pCount < mCount ? pCount : mCount;
unsigned long long ans = temp < zCount ? temp : zCount;
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
count = 0
if a[0] >= 0:
sym = True
else:
sym = False
for i in range(1, n):
if sym:
while sum(a[0:i+1]) >= 0:
a[i] -= 1
count += 1
sym = False
else:
while sum(a[0:i+1]) <= 0:
a[i] += 1
count += 1
sym = True
print(count) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
long long N;
cin >> N;
long long a[N];
long long sum = 0, cnt = 0;
long long minv = 9223372036854775807;
for (long long i = 0; i < N; i++) cin >> a[i];
for (long long j = 0; j < 2; j++) {
cnt = 0;
sum = 0;
for (long long i = 0; i < N; i++) {
if (j == 0 && i == 0) {
if (a[i] < 0) {
sum = 1;
cnt += abs(a[i]) + 1;
} else
sum += a[i];
continue;
} else if (j == 1 && i == 0) {
if (a[i] > 0) {
sum = -1;
cnt += a[i] + 1;
} else
sum += a[i];
continue;
}
if (sum > 0 && sum + a[i] > 0) {
if (a[i] < 0)
cnt += sum + a[i] + 1;
else
cnt += abs(sum + a[i]) + 1;
sum = -1;
} else if (sum < 0 && sum + a[i] < 0) {
if (a[i] > 0)
cnt += abs(sum) - a[i] + 1;
else
cnt += abs(sum + a[i]) + 1;
sum = 1;
} else if (sum + a[i] == 0) {
if (a[i] >= 0) {
sum++;
cnt++;
} else {
sum--;
cnt++;
}
} else
sum += a[i];
}
minv = min(minv, cnt);
}
cout << minv << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long a[100000];
long n;
long long s[100000];
long long ans = 0;
scanf("%ld", &n);
for (long i = 0; i < n; i++) {
scanf("%lld", &a[i]);
}
s[0] = a[0];
for (long i = 1; i < n; i++) {
s[i] = s[i - 1] + a[i];
if ((s[i - 1] > 0 && s[i] < 0) || (s[i - 1] < 0 && s[i] > 0)) {
continue;
} else {
ans += abs((-s[i - 1] / abs(s[i - 1])) - s[i - 1] - a[i]);
s[i] = -s[i - 1] / abs(s[i - 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 <bits/stdc++.h>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
int main() {
int N;
cin >> N;
vector<int> a(N);
for (int i = 0; i < (N); ++i) {
cin >> a[i];
}
long long count = 0;
if (a[0] == 0) {
a[0]++;
count++;
}
long long sum = 0;
for (int i = 0; i < (N - 1); ++i) {
sum += a[i];
long long next_sum = sum + a[i + 1];
if ((sum < 0 && next_sum > 0) || (sum > 0 && next_sum < 0)) {
} else {
if (sum < 0) {
count += 1 - next_sum;
a[i + 1] += 1 - next_sum;
} else {
count += next_sum + 1;
a[i + 1] -= (next_sum + 1);
}
}
}
cout << count << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N, temp;
vector<int> a;
scanf("%d", &N);
int start = 0;
bool v = false;
for (int i = 0; i < N; i++) {
scanf("%d", &temp);
if (temp == 0) {
if (!v) {
start += 1;
}
} else if (!v)
v = true;
a.push_back(temp);
}
long long int sum = 0, cnt = 0;
if (start != 0) {
cnt = 2 * (start - 1) + 1;
if (a[start] > 0) {
if (a[start] > 1) {
sum = a[start] - 1;
} else {
sum = 1;
cnt += 1;
}
} else {
if (a[start] < -1) {
sum = a[start] + 1;
} else {
sum = -1;
cnt += 1;
}
}
} else {
sum = a[start];
}
start++;
for (size_t i = start; i != a.size(); i++) {
if (sum + a[i] == 0) {
if (sum > 0) {
sum = -1;
} else {
sum = 1;
}
cnt += 1;
continue;
}
if (sum + a[i] > 0 && sum > 0) {
cnt += sum + a[i] + 1;
sum = -1;
} else if (sum + a[i] < 0 && sum < 0) {
cnt += 1 - sum - a[i];
sum = 1;
} else {
sum += a[i];
}
}
if (sum == 0) cnt += 1;
printf("%lld\n", cnt);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | import qualified Data.Vector.Unboxed as VU
import qualified Data.ByteString.Char8 as B
import Data.Char
solve :: VU.Vector Int -> Int -> Int
solve vec n = minimum $ map fst [f, g]
where
t = VU.take 2 vec
d = VU.drop 2 vec
f = VU.foldl' step (fst $ init t) d
g = VU.foldl' step (snd $ init t) d
init :: VU.Vector Int -> ((Int, Int), (Int, Int))
init vec
| a + b == 0 = ((1, 1), (1, negate 1))
| a + b > 0 = ((0, a + b), (1 + a + b, negate 1))
| otherwise = ((0, a + b), (abs (1 - (a + b)), 1))
where
a = VU.head vec
b = VU.last vec
step :: (Int, Int) -> Int -> (Int, Int)
step (res, acc) x
| acc + x == 0 = (res + 1, negate (signum acc))
| (signum acc) /= signum (acc + x) = (res, acc + x)
| otherwise =
let
aim = negate $ signum acc
y = aim - (acc + x)
in
(res + abs y, aim)
main = do
n <- readLn :: IO Int
as <- VU.unfoldrN n (B.readInt . B.dropWhile isSpace) <$> B.getLine
print $ solve as 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()))
sum_now=a[0]
sum_before=-a[0]
count=0
for i in range(n):
while sum_now*sum_before>=0:
if sum_before==0:
sum_now=-a[1]/abs(a[1])
count+=1
else:
sum_now=sum_now-sum_before/abs(sum_before)
count+=1
if i!=n-1:
sum_before=sum_now
sum_now=sum_now+a[i+1]
print(count) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
int *a;
int ans = 0;
cin >> n;
a = new int[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int sum = 0;
int opr1 = 0, opr2 = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 1 && sum <= 0) {
int add = abs(sum) + 1;
sum = 1;
opr1 += add;
} else if (i % 2 == 0 && sum >= 0) {
int add = abs(sum) + 1;
sum = -1;
opr1 += add;
}
}
sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 0 && sum <= 0) {
int add = abs(sum) + 1;
sum = 1;
opr2 += add;
} else if (i % 2 == 1 && sum >= 0) {
int add = abs(sum) + 1;
sum = -1;
opr2 += add;
}
}
ans = min(opr1, opr2);
cout << ans << endl;
delete (a);
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>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
int a[N];
int sum = 0, cnt = 0;
for (long long i = 0; i < N; i++) {
cin >> a[i];
if (i == 0) {
sum += a[i];
continue;
}
if (sum * (sum + a[i]) > 0) {
cnt += abs(sum + a[i]) + 1;
if (sum > 0)
sum = -1;
else
sum = 1;
} else if (sum + a[i] == 0) {
cnt++;
if (sum > 0)
sum--;
else
sum++;
} else
sum += a[i];
}
cout << cnt << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
a = list(map(int,input().split()))
S = [0]*N
S[0] = a[0]
for i in range(1,N):
S[i] = S[i-1] + a[i]
count = [0]*N
num = [0]*2
for i in range(2):
value = 0
for j in range(N):
if (S[j]+value)*((-1)**(i+j)) <= 0:
num[i] += abs(S[j] + value - (-1)**(i+j))
value += (-1)**(i+j) - (S[j]-value)
#print(i,num,value)
#print(S)
print(min(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 int MaxN = 2e5 + 5;
int box[MaxN];
int n;
long long solve1() {
long long sum = box[1];
long long res = 0;
if (sum == 0) {
sum = 1;
res = 1;
}
for (int i = 2; i <= n; i++) {
long long temp = box[i] + sum;
if (temp * box[i] >= 0) {
res += abs(temp) + 1;
if (sum > 0)
sum = -1;
else
sum = 1;
} else
sum = temp;
}
return res;
}
long long solve2() {
long long sum = box[1];
long long res = 0;
if (sum == 0) {
sum = -1;
res = 1;
}
for (int i = 2; i <= n; i++) {
long long temp = box[i] + sum;
if (temp * sum >= 0) {
res += abs(temp) + 1;
if (sum > 0)
sum = -1;
else
sum = 1;
} else
sum = temp;
}
return res;
}
int main() {
while (~scanf("%d", &n)) {
for (int i = 1; i <= n; i++) scanf("%d", &box[i]);
long long ans = 1LL << 60;
ans = min(ans, solve1());
ans = min(ans, solve2());
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 | python3 | N = int(input())
A = list(map(int, input().split()))
def count(s):
ans = 0
for i, a in enumerate(A):
if i == 0:
continue
if s < 0:
if 1-s-a >= 0:
ans += 1-s-a
s = 1
else:
s += a
elif s > 0:
if s+a+1 >= 0:
ans += s+a+1
s = -1
else:
s += a
return ans
b = A[0]
ans1 = count(b)
if b > 0:
ans2 = count(-1)+b+1
elif b < 0:
ans2 = count(1)+1-b
print(min(ans1, ans2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
s = list(map(int,input().split()))
temp = s[0]
cand1 = 0
for i in range(1,n):
temp += s[i]
if i%2 == 0:#正になってほしい
if temp <= 0:
cand1 += abs(temp-1)
temp = 1
else:#負になって欲しい
if temp >= 0:
cand1 += abs(temp+1)
temp = -1
temp = s[0]
cand2 = 0
for i in range(1,n):
temp += s[i]
if i%2 == 0:
if temp >= 0:
cand2 += abs(temp+1)
temp = -1
else:
if temp <= 0:
cand2 += abs(temp-1)
temp = 1
print(min(cand2,cand1))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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()))
lis=[]
now=0
for num in a:
now+=num
lis.append(now)
ans=10**10
cnt=0
sm=0
for i in range(len(lis)):
if i%2==0:
if lis[i]+sm >= 0:
add = lis[i]+sm+1
cnt+= add
sm=-add
else:
if lis[i]+sm <= 0:
add = abs(1-lis[i]-sm)
cnt+= add
sm=add
ans=min(ans,cnt)
cnt=0
sm=0
for i in range(len(lis)):
if i%2==1:
if lis[i]+sm >= 0:
add = lis[i]+sm+1
cnt+= add
sm=-add
else:
if lis[i]+sm <= 0:
add = abs(1-lis[i]-sm)
cnt+= add
sm=add
ans=min(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;
const int N = 100010, mod = 1e9 + 7;
int n, a[N];
long long s[N];
int check(int a, int b) {
if (a < 0 && b > 0 || a > 0 && b < 0)
return 0;
else if (b < 0)
return 1;
else
return -1;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", a + i);
s[i] = s[i - 1] + a[i];
}
long long res = 0, t = 0;
for (int i = 2; i <= n; i++) {
s[i] += t;
int v = check(s[i], s[i - 1]);
if (!v) continue;
t = v - s[i];
s[i] = v;
res += abs(t);
}
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 n;
bool hugou;
long long sum;
long long ans;
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
long long a;
cin >> a;
if (i == 0) {
if (a > 0) hugou = true;
sum += a;
} else {
if (hugou && sum + a >= 0) {
ans += sum + a + 1;
sum = -1;
hugou = false;
} else if (!hugou && sum + a <= 0) {
ans += 1 - sum - a;
sum = 1;
hugou = true;
} else if (hugou && sum + a < 0) {
sum += a;
hugou = false;
} else if (!hugou && sum + a > 0) {
sum += a;
hugou = true;
}
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
StringTokenizer str = new StringTokenizer(br.readLine(), " ");
int sum = Integer.parseInt(str.nextToken());
int flg;
int count = 0;
if( sum > 0) flg = 1;
else flg = -1;
for(int i = 0; i < n-1; i++){
int tmp = 0;
sum += Integer.parseInt(str.nextToken());
if(flg == 1){ //次は負
if(sum >= 0){
tmp = (sum + 1);
sum = -1;
}
flg = -1;
}
else{ //次は正
if(sum <= 0){
tmp = 1+ (-sum);
sum = 1;
}
flg = 1;
}
//System.out.print(tmp+" ");
count += tmp;
}
System.out.println(count);
} catch (IOException e) {
System.out.println("error");
}
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 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 <string>
#include <math.h>
#include <bitset>
#include <vector>
#include <queue>
#include <map>
#define i64 int64_t
#define ff(ii,nn,mm) for(int ii=nn;ii<mm;ii++)
#define sort(vvv) sort(vvv.begin(),vvv.end())
#define rvs(vvv) reverse(vvv.begin(),vvv.end())
int inf = 1000000007;
using namespace std;
int main() {
int n;
cin >> n;
vector<int> data(n);
int ans = 0;
ff(i, 0, n) {
cin >> data.at(i);
}
vector<int> data2 = data;
i64 sum = data.at(0);
i64 sump = sum;
ff(i, 1, n) {
sump += data.at(i);
if (sum * sump >= 0) {
int c = sump;
if (c < 0)c *= -1;
c++;
ans += c;
if (sump > 0) {
data.at(i) -= c;
sump -= c;
}
else {
data.at(i) += c;
sump += c;
}
}
sum += data.at(i);
}
i64 ans2 = 0;
i64 sum = data2.at(0)*-1;
i64 sump = sum;
ff(i, 1, n) {
sump += data2.at(i);
if (sum * sump >= 0) {
int c = sump;
if (c < 0)c *= -1;
c++;
ans2 += c;
if (sump > 0) {
data2.at(i) -= c;
sump -= c;
}
else {
data2.at(i) += c;
sump += c;
}
}
sum += data2.at(i);
}
if (ans > ans2) {
cout << ans << endl;
}
else {
cout << 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 | using System;
using System.Collections.Generic;
using System.Linq;
namespace AtCoderBeginnerContest
{
public class Program
{
public static void Main(string[] args)
{
// C
var n = long.Parse(Console.ReadLine());
var a = Console.ReadLine().Split(' ').Select(x => long.Parse(x)).ToArray();
long sum = a[0];
long count = 0;
for (int i = 1; i < n; i++)
{
// 差し引き0になってしまうとき
if (i != n - 1 && sum + a[i] == 0)
{
if (i < n - 1)
{
if (a[i + 1] > 0) sum = -1;
else sum = 1;
count++;
}
}
// sumとsum+a[i]の符号が違うとき
else if ((sum > 0 && sum+a[i] < 0) || (sum < 0 && sum + a[i] > 0))
{
sum = sum + a[i];
}
// sumとsum+a[i]の符号が同じとき
else
{
if (Math.Abs(sum + a[i]) > Math.Abs(sum))
{
count += Math.Abs(sum) + 1;
sum = sum > 0 ? sum = -1 + a[i] : sum = 1 + a[i];
}
else
{
count += Math.Abs(sum+a[i]) + 1;
sum = sum > 0 ? sum = -1 : sum = 1;
}
}
}
Console.WriteLine(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())
nums = list(map(int, input().split()))
sum_n = 0
before = 0
ans = 10*14
for n in [-1, 1]:
cnt = 0
before = n
sum_n = nums[0]
if sum_n * before >= 0:
if before > 0:
sum_n = -1
else:
sum_n = 1
cnt += abs(sum_n - before)
before = sum_n
for num in nums[1:]:
sum_n += num
if before * sum_n >= 0:
if before > 0:
cnt += abs(-1-sum_n)
sum_n = -1
else:
cnt += abs(1-sum_n)
sum_n = 1
before = sum_n
ans = min(ans, cnt)
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import sys
stdin = sys.stdin
def li(): return [int(x) for x in stdin.readline().split()]
def li_(): return [int(x)-1 for x in stdin.readline().split()]
def lf(): return [float(x) for x in stdin.readline().split()]
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(ns())
def nf(): return float(ns())
n = ni()
a = li()
# 正→負→正→...
cur = a[0]
ans_pn = 0
if cur <= 0:
ans_pn = abs(a[0])+1
for i in range(1,n):
cur += a[i]
if i%2 == 0 and cur <= 0:
ans_pn += abs(cur)+1
cur = 1
elif i%2 == 1 and cur >= 0:
ans_pn += abs(cur)+1
cur = -1
# 負→正→負...
cur = a[0]
ans_np = 0
if cur >= 0:
ans_np = abs(a[0])+1
for i in range(1,n):
cur += a[i]
if i%2 == 0 and cur >= 0:
ans_np += abs(cur)+1
cur = -1
elif i%2 == 1 and cur <= 0:
ans_np += abs(cur)+1
cur = 1
print(min(ans_np, ans_pn)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | #
# Written by NoKnowledgeGG @YlePhan
# ('ω')
#
#import math
#mod = 10**9+7
#import itertools
#import fractions
#import numpy as np
#mod = 10**4 + 7
"""def kiri(n,m):
r_ = n / m
if (r_ - (n // m)) > 0:
return (n//m) + 1
else:
return (n//m)"""
""" n! mod m 階乗
mod = 1e9 + 7
N = 10000000
fac = [0] * N
def ini():
fac[0] = 1 % mod
for i in range(1,N):
fac[i] = fac[i-1] * i % mod"""
"""mod = 1e9+7
N = 10000000
pw = [0] * N
def ini(c):
pw[0] = 1 % mod
for i in range(1,N):
pw[i] = pw[i-1] * c % mod"""
"""
def YEILD():
yield 'one'
yield 'two'
yield 'three'
generator = YEILD()
print(next(generator))
print(next(generator))
print(next(generator))
"""
"""def gcd_(a,b):
if b == 0:#結局はc,0の最大公約数はcなのに
return a
return gcd_(a,a % b) # a = p * b + q"""
"""def extgcd(a,b,x,y):
d = a
if b!=0:
d = extgcd(b,a%b,y,x)
y -= (a//b) * x
print(x,y)
else:
x = 1
y = 0
return d"""
def readInts():
return list(map(int,input().split()))
mod = 10**9 + 7
def main():
n = int(input())
A = readInts()
Cost = 0
# 符号 positive?
#po_ = True
# 変わったか変わってないか
ANS = [0] * (n)
if A[0] > 0:
pri_po = True
elif A[0] == 0:
pri_po = True
A[0] += 1
Cost += 1
else:
pri_po = False
ANS[0] = A[0]
for i in range(1,n):
#print(ANS[i-1],pri_po,Cost)
if ANS[i-1] + A[i] > 0 and pri_po == True:
pri_po = False
Cost += abs(-1 - (ANS[i-1] + A[i]))
#print(abs(-1 - (ANS[i-1] + A[i])))
A[i] -= abs(-1 - (ANS[i-1] + A[i]))
#print('Hi^^',A[i])
ANS[i] = ANS[i-1] + A[i]
elif ANS[i-1] + A[i] == 0 and pri_po == False:
pri_po = True
Cost += 1
A[i] += 1
ANS[i] = ANS[i-1] + A[i]
elif ANS[i-1] + A[i] == 0 and pri_po == True:
pri_po = False
Cost += 1
A[i] -= 1
ANS[i] = ANS[i-1] + A[i]
elif ANS[i-1] + A[i] > 0 and pri_po == False:
pri_po = True
ANS[i] = ANS[i-1] + A[i]
elif ANS[i-1] + A[i] < 0 and pri_po == True:
pri_po = False
ANS[i] = ANS[i-1] + A[i]
elif ANS[i-1] + A[i] < 0 and pri_po == False:
pri_po = True
Cost += 1 - (ANS[i-1] + A[i])
A[i] += 1 - (ANS[i-1] + A[i])
ANS[i] = ANS[i-1] + A[i]
else:
pass
print(Cost)
if __name__ == '__main__':
main() |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long MAX = 100005;
int main() {
long long n;
long long tmp;
long long ans1{0};
long long ans2{0};
cin >> n;
long long A[MAX], B[MAX];
long long cum_sum = 0;
for (long long i = 0; i < n; i++) {
cin >> tmp;
cum_sum += tmp;
A[i] = cum_sum;
B[i] = cum_sum;
}
tmp = 0;
if (A[0] <= 0) {
tmp = -A[0] + 1;
ans1 += tmp;
for (long long i = 0; i < n; i++) {
A[i] += tmp;
}
}
for (long long i = 0; i < n - 1; i++) {
if (A[i] > 0) {
if (A[i + 1] >= 0) {
tmp = (A[i + 1] + 1);
ans1 += tmp;
for (long long j = i + 1; j < n; j++) A[j] -= tmp;
}
} else if (A[i] < 0) {
if (A[i + 1] <= 0) {
tmp = (-A[i + 1] + 1);
ans1 += tmp;
for (long long j = i + 1; j < n; j++) A[j] += tmp;
}
}
}
tmp = 0;
if (B[0] >= 0) {
tmp = B[0] + 1;
ans2 += tmp;
for (long long i = 0; i < n; i++) {
A[i] -= tmp;
}
}
for (long long i = 0; i < n - 1; i++) {
if (B[i] > 0) {
if (B[i + 1] >= 0) {
tmp = (B[i + 1] + 1);
ans2 += tmp;
for (long long j = i + 1; j < n; j++) B[j] -= tmp;
}
} else if (B[i] < 0) {
if (B[i + 1] <= 0) {
tmp = (-B[i + 1] + 1);
ans2 += tmp;
for (long long j = i + 1; j < n; j++) B[j] += tmp;
}
}
}
long long ans = (ans1 > ans2 ? ans2 : ans1);
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
public class Hello
{
public static void Main()
{
var n = int.Parse(Console.ReadLine().Trim());
string[] line = Console.ReadLine().Trim().Split(' ');
var a = Array.ConvertAll(line, long.Parse);
Console.WriteLine(Math.Min(culc0m(a), culc0p(a)));
}
public static long culc0m(long[] a)
{
var count = 0L;
var sum = a[0];
if (sum >= 0) { count += -sum + 1; sum = -1; }
for (int i = 1; i < a.Length; i++)
{
if (i % 2 == 1)
{
if (sum + a[i] > 0) sum += a[i];
else { count += -(sum + a[i]) + 1; sum = 1; }
}
else
{
if (sum + a[i] < 0) sum += a[i];
else { count += (sum + a[i]) + 1; sum = -1; }
}
}
return count;
}
public static long culc0p(long[] a)
{
var count = 0L;
var sum = a[0];
if (sum <= 0) { count += -sum + 1; sum = 1; }
for (int i = 1; i < a.Length; i++)
{
if (i % 2 == 1)
{
if (sum + a[i] < 0) sum += a[i];
else { count += (sum + a[i]) + 1; sum = -1; }
}
else
{
if (sum + a[i] > 0) sum += a[i];
else { count += -(sum + a[i]) + 1; sum = 1; }
}
}
return count;
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 |
n = int(input())
a = list(map(int, input().split(" ")))
def solve(s1, s2):
"if start == true, assume the first of the sum is positive."
res = 0
sum = 0
for i in range(n):
sum += a[i]
if sum <= 0 and i % 2 == s1:
res += abs(sum) + 1
sum = 1
elif sum >= 0 and i % 2 == s2:
res += abs(sum) + 1
sum = -1
return res
print(min(solve(0, 1), solve(1, 2)))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | package main
import (
"bufio"
"fmt"
"math"
"os"
"strconv"
)
const pi = math.Pi
var mod int = pow(10, 9) + 7
var Umod uint64 = 1000000007
var ans int64
func main() {
reader.Split(bufio.ScanWords)
n, _ := strconv.Atoi(read())
a := make([]int, n)
for i := 0; i < n; i++ {
a[i], _ = strconv.Atoi(read())
}
sum := make([]int64, n)
sum[0] = int64(a[0])
for i := 1; i < n; i++ {
sum[i] += int64(a[i]) + sum[i-1]
if (0 <= sum[i-1] && 0 <= sum[i]) || (sum[i-1] <= 0 && sum[i] <= 0) {
// NGパターン
if sum[i] < 0 {
ans += 1 - sum[i]
sum[i] = 1
} else {
ans += sum[i] + 1
sum[i] = -1
}
}
}
fmt.Println(ans)
}
/* ---------------------------------------- */
var reader = bufio.NewScanner(os.Stdin)
func read() string {
reader.Scan()
return reader.Text()
}
func lcm(x, y int) int {
return (x / gcd(x, y)) * y
}
func gcd(x, y int) int {
if x%y == 0 {
return y
} else {
r := x % y
return gcd(y, r)
}
}
var fac [1000000]int
var finv [1000000]int
var inv [1000000]int
func combination_init() {
fac[0], fac[1] = 1, 1
finv[0], finv[1] = 1, 1
inv[1] = 1
// invは a^(-1) mod p
// pをaで割ることを考える
// p/a*(a) + p%a = p
// p/a*(a) + p%a = 0 (mod p)
// -p%a = p/a*(a) (mod p)
// -p%a *a^(-1)= p/a (mod p)
// a^(-1)= p/a * (-p%a)^(-1) (mod p)
// a^(-1) =
for i := 2; i < 1000000; i++ {
fac[i] = fac[i-1] * i % mod
inv[i] = mod - inv[mod%i]*(mod/i)%mod
finv[i] = finv[i-1] * inv[i] % mod
}
}
func combination(x, y int) int {
if x < y {
return 0
}
if fac[0] != 1 {
combination_init()
}
return fac[x] * (finv[y] * finv[x-y] % mod) % mod
//return fac[x] / (fac[y] * fac[x-y])
}
func permutation(x, y int) int {
if x < y {
return 0
}
if fac[0] != 1 {
combination_init()
}
return fac[x] * (finv[x-y] % mod) % mod
//return fac[x] / fac[x-y]
}
func max(x ...int) int {
var res int = x[0]
for i := 1; i < len(x); i++ {
res = int(math.Max(float64(x[i]), float64(res)))
}
return res
}
func min(x ...int) int {
var res int = x[0]
for i := 1; i < len(x); i++ {
res = int(math.Min(float64(x[i]), float64(res)))
}
return res
}
func pow(x, y int) int { return int(math.Pow(float64(x), float64(y))) }
func abs(x int) int { return int(math.Abs(float64(x))) }
func floor(x int) int { return int(math.Floor(float64(x))) }
func ceil(x int) int { return int(math.Ceil(float64(x))) }
type SortBy [][]int
func (a SortBy) Len() int { return len(a) }
func (a SortBy) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a SortBy) Less(i, j int) bool { return a[i][0] < a[j][0] }
type PriorityQueue []int
func (h PriorityQueue) Len() int { return len(h) }
func (h PriorityQueue) Less(i, j int) bool { return h[i] < h[j] }
func (h PriorityQueue) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *PriorityQueue) Push(x interface{}) { *h = append(*h, x.(int)) }
func (h *PriorityQueue) Pop() interface{} {
old := *h
n := len(old)
x := old[n-1]
*h = old[0 : n-1]
return x
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
long long a[100005];
cin >> n;
for (long long i = 0; i < n; i++) cin >> a[i];
long long nCount = 0, sum = a[0];
bool isPositive;
if (a[0] == 0) {
nCount++;
sum++;
isPositive = true;
} else if (a[0] > 0) {
isPositive = true;
} else {
isPositive = false;
}
for (long long i = 1; i < n; i++) {
if (isPositive) {
if ((a[i] + sum) < 0) {
sum += a[i];
} else {
nCount += abs(a[i] + sum) + 1;
sum = -1;
}
isPositive = !isPositive;
} else {
if ((a[i] + sum) > 0) {
sum += a[i];
} else {
nCount += abs(a[i] + sum) + 1;
sum = 1;
}
isPositive = !isPositive;
}
}
cout << nCount << 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 | #!usr/bin/env python3
from collections import defaultdict
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
l = [None for i in range(n)]
for i in range(n):l[i] = I()
return l
def LIR(n):
l = [None for i in range(n)]
for i in range(n):l[i] = LI()
return l
def SR(n):
l = [None for i in range(n)]
for i in range(n):l[i] = S()
return l
def LSR(n):
l = [None for i in range(n)]
for i in range(n):l[i] = SR()
return l
mod = 1000000007
#A
#B
#C
n = I()
a = LI()
ans = 0
k = a[0]
for i in range(1,n):
if k*(k+a[i]) >= 0:
if k < 0:
ans += abs(1-k-a[i])
a[i] = 1-k
k = 1
else:
ans += abs(-1-k-a[i])
a[i] = -1-k
k = -1
else:
k += a[i]
print(ans)
#D
#E
#F
#G
#H
#I
#J
#K
#L
#M
#N
#O
#P
#Q
#R
#S
#T
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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()))
res_plus = np.cumsum(A)
res_minus = np.cumsum(A)
ans_plus = 0
ans_minus = 0
tmp_tmp = 0
# 奇数項を正とする
for i in range(N):
res_plus[i] += tmp_tmp
if i % 2 == 0: # 奇数項
if res_plus[i] > 0:
pass
else:
tmp = 1 - res_plus[i]
ans_plus += tmp
tmp_tmp += tmp
else: # 偶数項
if res_plus[i] < 0:
pass
else:
tmp = res_plus[i] + 1
ans_plus += tmp
tmp_tmp -= tmp
tmp_tmp = 0
# 奇数項を負とする
for i in range(N):
res_minus[i] += tmp_tmp
if i % 2 == 0: # 奇数項
if res_minus[i] < 0:
pass
else:
tmp = res_minus[i] + 1
ans_minus += tmp
tmp_tmp -= tmp
else: # 偶数項
if res_minus[i] > 0:
pass
else:
tmp = 1 - res_minus[i]
ans_minus += tmp
tmp_tmp += tmp
##print(ans_minus, ans_plus)
print(min(ans_plus, ans_minus)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> vec;
for (int i = 0; i < n; i++) {
int aux;
cin >> aux;
vec.push_back(aux);
}
int sumA = 0;
int sumB = 0;
int auxA = vec[0];
int auxB;
if (auxA > 0) {
auxB = -1;
sumB += auxA + 1;
} else if (auxA < 0) {
auxB = 1;
sumB += abs(auxA) + 1;
} else {
auxA = 0;
auxB = 0;
sumA++;
sumB++;
}
int sumAuxA = auxA;
int sumAuxB = auxB;
for (int i = 1; i < n; i++) {
if (sumAuxA > 0) {
sumAuxA += vec[i];
sumAuxB += vec[i];
if (sumAuxA >= 0) {
sumA += sumAuxA + 1;
sumAuxA = -1;
}
if (sumAuxB <= 0) {
sumB += abs(sumAuxB) + 1;
sumAuxB = 1;
}
} else {
sumAuxA += vec[i];
sumAuxB += vec[i];
if (sumAuxA <= 0) {
sumA += abs(sumAuxA) + 1;
sumAuxA = 1;
}
if (sumAuxB >= 0) {
sumB += sumAuxB + 1;
sumAuxB = -1;
}
}
}
cout << min(sumA, sumB);
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, c = 0;
cin >> n;
long long int sum[n];
long long int a[n];
for (long long int i = 0; i < n; i++) cin >> a[i];
if (a[0] == 0) {
a[0]++;
c++;
}
sum[0] = a[0];
long long int e = a[0] / abs(a[0]);
for (long long int i = 1; i < n; i++) {
sum[i] = sum[i - 1] + a[i];
if (sum[i - 1] * sum[i] >= 0) {
c += abs(sum[i] - pow(-1, i) * e);
sum[i] = pow(-1, i) * e;
}
}
cout << c << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> a(N);
for (int i=0; i<N; i++) {
cin >> a.at(i);
}
int ans1 = 0, ans2 = 0, sum1 = 0, sum2 = 0;
for (int i=0; i<N; i++) {
sum1 += a.at(i);
if (i%2 == 0 && sum <= 0) {
ans1 += 1-sum; //sumは負
sum1 = 1;
}
else if (i%2 != 0 && sum >= 0) {
ans1 += sum+1;
sum1 = -1;
}
}
for (int i=0; i<N; i++) {
sum2 += a.at(i);
if (i%2 == 0 && sum >= 0) {
ans2 += sum+1;
sum2 = -1;
}
else if (i%2 != 0 && sum <= 0) {
ans2 += 1-sum;
sum2 = 1;
}
}
cout << min(ans1, ans2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
constexpr int MOD = 1000000007;
using long long = long long;
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;
}
void print(const std::vector<int> &v) {
std::for_each(v.begin(), v.end(), [](int x) { std::cout << x << " "; });
std::cout << std::endl;
}
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (long long i = 0; i < (long long)n; i++) {
cin >> a[i];
}
int ans = 0;
if (a[0] > 0) {
int s = a[0];
for (int i = 1; i < n; i++) {
s += a[i];
if (i % 2 == 1) {
if (s > 0) {
ans += s + 1;
s = -1;
}
} else {
if (s < 0) {
ans += -s + 1;
s = 1;
}
}
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const long long LINF = 1e18;
long long n;
vector<long long> a;
long long count(long long sum) {
long long cnt = 0;
for (long long i = 1; i < n; ++i) {
if (sum > 0) {
sum += a[i];
if (sum >= 0) {
cnt += abs(sum) + 1;
sum = -1;
}
} else {
sum += a[i];
if (sum <= 0) {
cnt += abs(sum) + 1;
sum = 1;
}
}
}
return cnt;
}
int main() {
cin >> n;
long long ans = LINF;
a.resize(n);
for (long long i = 0; i < n; ++i) {
cin >> a[i];
}
if (a[0] == 0) {
ans = min(ans, count((long long)1) + 1);
ans = min(ans, count((long long)-1) + 1);
} else {
ans = min(ans, count(a[0]));
}
cout << ans << endl;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.