Search is not available for this dataset
name
stringlengths 2
88
| description
stringlengths 31
8.62k
| public_tests
dict | private_tests
dict | solution_type
stringclasses 2
values | programming_language
stringclasses 5
values | solution
stringlengths 1
983k
|
---|---|---|---|---|---|---|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n;
long long a[100004], temp[100004];
long long solve() {
long long ans = 0;
for (int i = (2); i <= (int)(n); ++i) {
if (a[i - 1] > 0) {
if (a[i] + a[i - 1] < 0) {
a[i] += a[i - 1];
continue;
}
ans += abs(a[i] + 1 + a[i - 1]);
a[i] = -1;
} else {
if (a[i] + a[i - 1] > 0) {
a[i] += a[i - 1];
continue;
}
ans += abs(a[i] - 1 + a[i - 1]);
a[i] = 1;
}
}
return ans;
}
int main() {
scanf("%d", &n);
for (int i = (1); i <= (int)(n); ++i) scanf("%lld", &a[i]);
long long ans = 0;
if (!a[1]) {
a[1] = 1;
memcpy(temp, a, sizeof(a));
ans = solve() + 1;
memcpy(a, temp, sizeof(temp));
a[1] = -1;
ans = min(ans, solve() + 1);
} else
ans = solve();
printf("%lld\n", ans);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
int a[n];
long long int s = 0;
long long int ans = INT_MAX;
int i;
for (i = 0; i < n; i++) cin >> a[i];
s = a[0];
long long int p = 0;
if (s > 0) {
for (i = 1; i < n; i++) {
if (i % 2) {
if (s + a[i] < 0) {
s += a[i];
} else {
p += 1 + s + a[i];
s = -1;
}
} else {
if (s + a[i] > 0)
s += a[i];
else {
p += 1 - s - a[i];
s = 1;
}
}
}
s = -1;
ans = min(ans, p);
p = a[0] + 1;
for (i = 1; i < n; i++) {
if (i % 2 == 0) {
if (s + a[i] < 0) {
s += a[i];
} else {
p += 1 + s + a[i];
s = -1;
}
} else {
if (s + a[i] > 0)
s += a[i];
else {
p += 1 - s - a[i];
s = 1;
}
}
}
ans = min(ans, p);
cout << ans << endl;
} else if (s < 0) {
for (i = 1; i < n; i++) {
if (i % 2 == 0) {
if (s + a[i] < 0) {
s += a[i];
} else {
p += 1 + s + a[i];
s = -1;
}
} else {
if (s + a[i] > 0)
s += a[i];
else {
p += 1 - s - a[i];
s = 1;
}
}
}
s = 1;
ans = min(ans, p);
p = (-1) * a[0] + 1;
for (i = 1; i < n; i++) {
if (i % 2 == 1) {
if (s + a[i] < 0) {
s += a[i];
} else {
p += 1 + s + a[i];
s = -1;
}
} else {
if (s + a[i] > 0)
s += a[i];
else {
p += 1 - s - a[i];
s = 1;
}
}
}
ans = min(ans, p);
cout << ans << endl;
} else {
p = 1;
s = 1;
for (i = 1; i < n; i++) {
if (i % 2) {
if (s + a[i] < 0) {
s += a[i];
} else {
p += 1 + s + a[i];
s = -1;
}
} else {
if (s + a[i] > 0)
s += a[i];
else {
p += 1 - s - a[i];
s = 1;
}
}
}
s = -1;
ans = min(ans, p);
p = 1;
for (i = 1; i < n; i++) {
if (i % 2 == 0) {
if (s + a[i] < 0) {
s += a[i];
} else {
p += 1 + s + a[i];
s = -1;
}
} else {
if (s + a[i] > 0)
s += a[i];
else {
p += 1 - s - a[i];
s = 1;
}
}
}
ans = min(ans, p);
cout << ans << endl;
}
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> a(N);
for (int i = 0; i < N; i++) {
cin >> a[i];
}
long long sum = 0;
long long ans = 0;
for (int i = 0; i < N; i++) {
sum += a[i];
if (i % 2 == 0) {
if (sum < 0) {
ans += -(sum) + 1;
sum = 1;
} else if (sum == 0) {
ans++;
sum = 1;
}
} else {
if (sum > 0) {
ans += sum + 1;
sum = -1;
} else if (sum == 0) {
ans++;
sum = -1;
}
}
}
sum = 0;
long long ans2 = 0;
for (int i = 0; i < N; i++) {
sum += a[i];
if (i % 2 == 0) {
if (sum < 0) {
ans2 += -(sum) + 1;
sum = 1;
} else if (sum == 0) {
ans2++;
sum = 1;
}
} else {
if (sum > 0) {
ans2 += sum + 1;
sum = -1;
} else if (sum == 0) {
ans2++;
sum = -1;
}
}
}
cout << min(ans, ans2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
clock_t CLOCK;
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using mll = map<ll, ll>;
using qll = queue<ll>;
using P = pair<ll, ll>;
constexpr ll INF = 0x3f3f3f3f3f3f3f3f;
constexpr ld PI = 3.141592653589793238462643383279;
ll get_digit(ll x) { return to_string(x).size(); }
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
vector<P> factorize(ll n) {
vector<P> result;
for (ll i = 2; i * i <= n; ++i) {
if (n % i == 0) {
result.push_back({i, 0});
while (n % i == 0) {
n /= i;
result.back().second++;
}
}
}
if (n != 1) {
result.push_back({n, 1});
}
return result;
}
vll divisor(ll n) {
vll ret;
for (ll i = 1; i * i <= n; i++) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n) ret.push_back(n / i);
}
}
sort(ret.begin(), ret.end());
return (ret);
}
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll N;
cin >> N;
vll A(N);
for (ll i = 0; i < (ll)(N); ++i) cin >> A[i];
ll ans = 0;
ll current_num;
ll before_sign = true;
bool start_flag = false;
for (ll i = 0; i < (ll)(N); ++i) {
if (!start_flag) {
if (A[i] == 0) {
ans++;
} else {
before_sign = A[i] > 0;
current_num = A[i];
start_flag = true;
}
continue;
}
current_num += A[i];
if (before_sign) {
if (current_num >= 0) {
ans += current_num + 1;
current_num = -1;
}
} else {
if (current_num <= 0) {
ans += abs(current_num) + 1;
current_num = 1;
}
}
before_sign = !before_sign;
}
cout << ans << "\n";
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int ans;
int main() {
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) cin >> arr[i];
int prefix[n];
prefix[0] = arr[0];
for (int i = 1; i < n; i++) prefix[i] = prefix[i - 1] + arr[i];
int res = 0, cntr = 0;
for (int i = 0; i < n; i++) {
if (i % 2) {
if (prefix[i] + cntr <= 0) {
res += 1 - prefix[i] - cntr;
cntr += 1 - prefix[i] - cntr;
}
} else {
if (prefix[i] + cntr >= 0) {
res += prefix[i] + cntr + 1;
cntr -= prefix[i] + cntr + 1;
}
}
}
ans = res;
res = 0;
cntr = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
if (prefix[i] + cntr <= 0) {
res += 1 - prefix[i] - cntr;
cntr += 1 - prefix[i] - cntr;
}
} else {
if (prefix[i] + cntr >= 0) {
res += prefix[i] + cntr + 1;
cntr -= prefix[i] + cntr + 1;
}
}
}
ans = min(ans, res);
cout << ans;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long a[n];
for (int i = 0; i < (n); i++) cin >> a[i];
long long cnt = 0;
long long s = 0;
for (int i = 1; i < n; i++) {
s += a[i - 1];
long long t = 0, u;
if (s > 0) {
u = (-1) * s - 1;
if (u < a[i]) {
t = a[i] - u;
a[i] = u;
}
} else {
u = (-1) * s + 1;
if (u > a[i]) {
t = u - a[i];
a[i] = u;
}
}
cnt += t;
}
cout << cnt << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
long long l1[n + 1];
long long x = 0, s = 0;
for (int i = 1; i <= n; i++) {
cin >> l1[i];
x += l1[i];
if (i == 1 && l1[i] == 0 && l1[i + 1] <= 0)
x++, s++, l1[i] = 1;
else if (i == 1 && l1[i] == 0 && l1[i + 1] > 0)
x--, s++, l1[i] = -1;
if (i >= 2) {
if (x - l1[i] <= 0 && x <= 0) {
s += abs(-(x - l1[i]) + 1 - l1[i]);
cout << abs(-(x - l1[i]) + 1 - l1[i]) << endl;
x = 1;
} else if (x - l1[i] >= 0 && x >= 0) {
s += abs(-(x - l1[i]) - 1 - l1[i]);
cout << abs(-(x - l1[i]) - 1 - l1[i]) << endl;
x = -1;
}
}
}
cout << s << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import copy
n = int(input())
a = [int(i) for i in input().split()]
b=a.copy()
s0p = a[0]
s0n = b[0]
countp = 0
countn = 0
if a.count(0)==n:
print(2*n+1)
exit()
if s0p<=0:
s0p+=(abs(s0p)+1)
countp+=1
if s0n>=0:
s0n-=(abs(s0n)+1)
countn+=1
for i in range(1,n):
s1 = s0p+a[i]
if s0p*s1>=0:
if s1>0:
a[i]-=(abs(s1)+1)
countp+=(abs(s1)+1)
elif s1<0:
a[i]+=(abs(s1)+1)
countp+=(abs(s1)+1)
elif s1==0:
if s0p>0:
a[i]-=1
countp+=1
elif s0p<0:
a[i]+=1
countp+=1
s0p += a[i]
for i in range(1,n):
s1 = s0n+b[i]
if s0n*s1>=0:
if s1>0:
b[i]-=(abs(s1)+1)
countn+=(abs(s1)+1)
elif s1<0:
b[i]+=(abs(s1)+1)
countn+=(abs(s1)+1)
elif s1==0:
if s0n>0:
b[i]-=1
countn+=1
elif s0n<0:
b[i]+=1
countn+=1
s0n += b[i]
print(countp if countp<=countn else(countn))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 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 = 1 << 30;
long long a[111111];
int total[2], cnt[2];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
for (int i = 0; i < 2; i++) {
for (int j = 1; j <= n; j++) {
total[i] += a[j];
if ((i + j) % 2 == 0) {
if (total[i] >= 0) {
cnt[i] += total[i] + 1;
total[i] = -1;
}
} else {
if (total[i] <= 0) {
cnt[i] += (1 - total[i]);
total[i] = 1;
}
}
}
}
cout << min(cnt[0], cnt[1]) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
A = list(map(int, input().split()))
ans = 0
prev_sm = A[0] # total to i - 1
for i in range(1, N):
# if prev_sum is plus and a is more minus than prev_sum.
if prev_sm > 0 and prev_sm + A[i] < 0:
pass
# if prev_sum is plus and a is larger than or equal to prev_sum.
elif prev_sm > 0 and prev_sm + A[i] >= 0:
ans += prev_sm + A[i] + 1
A[i] -= prev_sm + A[i] + 1
# if prev_sum is minus and a is more plus than prev_sum.
elif prev_sm < 0 and prev_sm + A[i] > 0:
pass
# if prev_sum is minus and a is more smaller than or equal to prev_sum.
elif prev_sm < 0 and prev_sm + A[i] <= 0:
ans += -(prev_sm + A[i] - 1)
A[i] += -(prev_sm + A[i] + 1)
prev_sm += A[i]
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | public class Main {
private static java.util.Scanner scanner = new java.util.Scanner(System.in);
public static void main(String[] args) {
int n = scanner.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = scanner.nextInt();
int m1 = 0, m2 = 0, s1 = 0, s2 = 0;
for (int i = 0; i < n; i++) {
s1 += a[i];
s2 += a[i];
if ((i & 1) == 0) {
if (s1 <= 0) {
m1 += Math.abs(s1) + 1;
s1 = 1;
} if (s2 >= 0) {
m2 += Math.abs(s2) + 1;
s2 = -1;
}
} else {
if (s1 >= 0) {
m1 += Math.abs(s1) + 1;
s1 = 1;
} if (s2 <= 0) {
m2 += Math.abs(s2) + 1;
s2 = -1;
}
}
}
System.out.println(Math.min(m1, m2));
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll LINF = 1LL << 60;
const int INF = 1e9 + 7;
vector<vector<ll>> g(100010);
vector<ll> dist(100010);
int main() {
ll n;
cin >> n;
ll a[n];
for (ll i = 0; i < n; ++i) cin >> a[i];
ll ans = 0;
ll sum;
if (a[0] >= 0) {
sum = -1;
ans += a[0] + 1;
} else {
sum = a[0];
}
for (ll i = 1; i < n; ++i) {
if ((i & 1 && sum + a[i] <= 0) || (!(i & 1) && sum + a[i] >= 0)) {
ans += abs(sum + a[i]) + 1;
sum = -1 * (sum / abs(sum));
} else {
sum += a[i];
}
}
ll res = 0;
if (a[0] <= 0) {
sum = 1;
res += a[0] + 1;
} else {
sum = a[0];
}
for (ll i = 1; i < n; ++i) {
if ((!(i & 1) && sum + a[i] <= 0) || (i & 1 && sum + a[i] >= 0)) {
res += abs(sum + a[i]) + 1;
sum = -1 * (sum / abs(sum));
} else {
sum += a[i];
}
}
ans = min(ans, res);
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
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;
}
total = a[0];
total2 = a[0];
long long ans3 = 0;
if (total2 > 0) {
ans3 += total2 - 1;
total2 = 1;
} else {
ans3 += -total2 - 1;
total2 = -1;
}
total = total2;
for (int i = (1); i < (n); ++i) {
total += a[i];
if (total * total2 >= 0) {
if (total2 > 0) {
ans3 += total + 1;
total = -1;
} else {
ans3 += -total + 1;
total = 1;
}
}
total2 = total;
}
cout << min(min(ans, ans2), ans3) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import copy
n = int(input())
a = [int(i) for i in input().split()]
b=a.copy()
s0p = a[0]
s0n = b[0]
countp = 0
countn = 0
if a.count(0)==n:
print(2*n+1)
exit()
if s0p<=0:
while s0p<=0:
s0p+=1
countp+=1
if s0n>=0:
while s0n>=0:
s0n-=1
countn+=1
"""
for i in range(1,n):
s1 = s0p+a[i]
if s0p*s1>=0:
if s1>0:
a[i]-=(abs(s1)+1)
countp+=(abs(s1)+1)
elif s1<0:
a[i]+=(abs(s1)+1)
countp+=(abs(s1)+1)
elif s1==0:
if s0p>0:
a[i]-=1
countp+=1
elif s0p<0:
a[i]+=1
countp+=1
s0p += a[i]
"""
for i in range(1,n):
s1 = s0n+b[i]
if s0n*s1>=0:
if s1>0:
b[i]-=(abs(s1)+1)
countn+=(abs(s1)+1)
elif s1<0:
b[i]+=(abs(s1)+1)
countn+=(abs(s1)+1)
elif s1==0:
if s0n>0:
b[i]-=1
countn+=1
elif s0n<0:
b[i]+=1
countn+=1
s0n += b[i]
print(countp if countp<=countn else(countn))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using VI = vector<int>;
using VVI = vector<VI>;
using VB = vector<bool>;
using VVB = vector<VB>;
using VS = vector<string>;
using PII = pair<int, int>;
using VPII = vector<PII>;
using VL = vector<long long>;
using VVL = vector<VL>;
int n;
VI A;
long long numoperations() {
long long ret = (A[0] == 0) ? 1 : 0;
long long sum = (A[0] == 0) ? 1 : A[0];
assert(A[0] != 0);
for (int i = 1; i < (int)n; ++i) {
long long prevsum = sum;
sum += A[i];
if (prevsum > 0 && sum >= 0) {
ret += abs(-1 - sum);
sum = -1;
} else if (prevsum < 0 && sum <= 0) {
ret += 1 - sum;
sum = 1;
}
}
return ret;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
A = VI(n);
for (int i = 0; i < (int)n; ++i) cin >> A[i];
cout << numoperations() << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long ans(int n, int *, int change);
int main() {
int n;
int a[110000];
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
long long pAns, nAns;
pAns = ans(n, a, 1);
nAns = ans(n, a, -1);
printf("%d\n", min(pAns, nAns));
}
long long ans(int n, int *a, int change) {
long long Ans = 0;
long long sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
switch (change) {
case -1:
if (sum > -1) Ans += 1 + sum, sum = -1;
change *= -1;
break;
case 1:
if (sum < 1) Ans += 1 - sum, sum = 1;
change *= -1;
break;
}
}
return Ans;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | package main
import (
"bufio"
"fmt"
"os"
"strconv"
)
func abs(x int64) int64 {
if x < 0 {
return -x
}
return x
}
func min(a, b int64) int64 {
if a < b {
return a
}
return b
}
func main() {
var n int
fmt.Scan(&n)
sc := bufio.NewScanner(os.Stdin)
sc.Split(bufio.ScanWords)
a := make([]int64, n)
for i := 0; i < n; i++ {
sc.Scan()
x, _ := strconv.Atoi(sc.Text())
a[i] = int64(x)
}
ans := int64(0)
if a[0] == 0 {
a[0] = -1
ans = solve(a, 0)
a[0] = 1
ans = min(ans, solve(a, 1))
} else {
ans = min(solve(a, 0), solve(a, 1))
}
fmt.Println(ans)
}
func solve(a []int64, ra int) int64 {
s := int64(0) // S_i = \sum_i a[i]
ans := int64(0)
for i, e := range a {
if i%2 == ra {
if s+e >= 0 {
ans += abs(-1 - s - e)
s = -1
continue
}
} else {
if s+e <= 0 {
ans += abs(1 - s - e)
s = 1
continue
}
}
s += int64(e)
}
return ans
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
count = 0
sum_ = 0
for i in range(n):
if sum_ * (sum_+a[i]) >=0 and i!=0:
if sum_ > 0:
count += sum_+a[i]+1
a[i] = -sum_-1
if sum_ < 0:
count += abs(sum_+a[i])+1
a[i] = -sum_+1
sum_ += a[i]
print(count) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
int sum = 0;
for (long long i = 0; i < n; i++) {
int x;
cin >> x;
sum += x;
a[i] = sum;
}
int f = (a[0] > 0 ? 1 : -1);
long long int ans = 0;
long long int fix = 0;
for (long long i = 0; i < n; i++) {
if (f == 1) {
if (a[i] + fix <= 0) {
ans += 1 - (fix + a[i]);
fix += 1 - (fix + a[i]);
}
f = -1;
} else {
if (a[i] + fix >= 0) {
ans += (fix + a[i]) + 1;
fix -= ((fix + a[i]) + 1);
}
f = 1;
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import numpy as np
N = int(input())
a_s = input().split()
for i in range(N):
a_s[i] = int(a_s[i])
a_s = np.array(a_s)
def get_sign(x):
if x>0:
return +1
elif x<0:
return -1
else:
return 0
ans = 0
for i,a in enumerate(a_s):
if i==0:
if a!=0:
S = a
else:
ans += 1
if np.all(a)==0:
S = +1
else:
for j in range(1,N):
if a_s[j]!=0:
S = get_sign(a_s[j])* ((-1)**j)
break
else:
S_tmp = S0 + a
if (get_sign(S_tmp)!=get_sign(S0))&(S_tmp!=0):
S = S_tmp
else: #S should be sign(S0)*-1
S = get_sign(S0) * (-1)
ans += abs(S - S_tmp)
S0 = S
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int,input().split()))
cnt = 0
sum = [0]*n
sum[0] = a[0]
for i in range(1,n):
sum[i] = sum[i-1]+a[i]
if sum[i]*sum[i-1]<0:
continue
elif sum[i-1]*a[i]<0:
cnt += abs(sum[i-1])-abs(a[i])+1
sum[i] = -1*(sum[i-1]//abs(sum[i-1]))
else:
cnt += abs(sum[i-1])+abs(a[i])+1
sum[i] = -1*(sum[i-1]//abs(sum[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 | python3 |
import sys
input = sys.stdin.readline
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
# A = [ int(input()) for _ in range(N) ]
##############################
N = int(input())
A = list(map(int, input().split()))
count = 0
summary = A[0]
for i in range(1, N):
# print(summary)
# 次はマイナス
if summary > 0:
# 条件を満たしてる?
if (summary + A[i]) < 0:
summary += A[i]
else:
# プラスになっちゃってるので修正
summary += A[i]
count += abs(-1-summary)
summary = -1
# 次はプラス
else:
if (summary + A[i]) > 0:
summary += A[i]
else:
# マイナスになっちゃってるので修正
summary += A[i]
count += abs(1-summary)
summary = 1
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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define MOD 1000000007
# define INF (1 < <29)
#define MODSET(d) if ((d) >= MOD) d %= MOD;
#define MODNEGSET(d) if ((d) < 0) d = ((d % MOD) + MOD) % MOD;
#define MODADDSET(d) if ((d) >= MOD) d -= MOD;
#define MODADDWHILESET(d) while ((d) >= MOD) d -= MOD;
//defines
#define FILE_IO freopen("in.txt","r",stdin); freopen("out.txt","w",stdout);
#define sc1(a,type) type a; cin>>a;
#define sc2(a,b,type) type a,b; cin>>a>>b;
#define sc3(a, b, c,type) type a,b,c; cin>>a>>b>>c;
#define sc4(a, b, c, d,type) type a ,b,c,d; cin>>a>>b>>c>>d;
#define nl cout<<"\n";
#define foreach(v, c) for(__typeof( (c).begin()) v = (c).begin(); v != (c).end(); ++v)
#define revforeach(v, c) for(__typeof( (c).rbegin()) v = (c).rbegin(); v != (c).rend(); ++v)
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);
#define re(i,b) for(int i=0;i<int(b);i++)
#define re1(i,b) for(int i=1;i<=int(b);i++)
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(),c.rend()
#define mpresent(container, element) (container.find(element) != container.end()) //for map,set..etc (returns true/false value)
#define vpresent(container, element) (find(all(container),element) != container.end()) //for vectors,strings,list,deque (returns true/false value)
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define ins insert
#define F first
#define S second
#define clr clear()
#define sz(x) ((int)x.size())
#define dt distance
#define test(t) int t; cin>>t; while(t--)
#define csb(i) __builtin_popcount(i)
#define csbll(i) __builtin_popcountll(i)
#define clz(x) __builtin_clz(x)
#define clzl(x) __builtin_clzl(x)
#define cp(x) __builtin_parity(x)
#define adv(v,num) advance(v,num)//used for lists and other structures that use iterators,when you can't access elements randomly ( iterator moves num positions)
#define mod 1000000007
#define MAX_ARR 1000000
#define v2d(rowsize,colsize,type,name) vector<vector<type>> name(rowsize,vector<type>(colsize));
#define digits_in(i) (ll)log10(i)+1 // gives no of digits in a number
#define sqr(x) (x)*(x)
//does not apply for i==0 , add an excetion contition for n==0 ( cust return count 1 for that inseted of using this function)
//typedef
typedef string str;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<str> vs;
typedef vector<char> vc;
typedef pair<int,int> pii;
typedef pair<str,int> psi;
typedef pair<int,str> pis;
typedef vector<pii> vii;
typedef map<int,int> mii;
typedef map<ll,ll> mll;
typedef map<str,int> msi;
typedef map<char,int> mci;
typedef map<int,str> mis;
typedef unordered_map<int,int> umii;
typedef unordered_map<str,int> umsi;
typedef unordered_map<int,str> umis;
typedef unordered_map<str,str> umss;
typedef unordered_map<char,int> umci;
typedef set<str> ss;
typedef set<int> si;
typedef unordered_set<str> uss;
typedef unordered_set<int> usi;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
// #ifndef ONLINE_JUDGE
// #include "debug.h"
// #else
// #define debug(args...)
// #endif
int main(){fastio
// #ifndef ONLINE_JUDGE
// FILE_IO
// #endif
vll v;
test(t){
int temp;cin>>temp;
v.pb(temp);
}
vll v1(all(v));
ll ct=0;
re(i,sz(v)-1){
// debug(v[i] ,v[i]+v[i+1]);
if( (v[i]<0 && v[i]+v[i+1]<0) || (v[i]>0 && v[i]+v[i+1]>0 || v[i]+v[i+1]==0) ){
if( v[i]>0 && v[i]+v[i+1]>0){
ct+=v[i]+v[i+1]+1;
}
else if(v[i]<0 && v[i]+v[i+1]<0 ){
ct+=abs(v[i]+v[i+1])+1;
}
else{
ct+=1;
}
v[i+1]= v[i]>0?-1:1;
}
else{
v[i+1]+=v[i];
}
// debug(ct);
}
ll ct1=0;
re(i,sz(v)-1)v1[i]*=-1;
re(i,sz(v)-1){
// debug(v[i] ,v[i]+v[i+1]);
if( (v1[i]<0 && v1[i]+v1[i+1]<0) || (v1[i]>0 && v1[i]+v1[i+1]>0 || v1[i]+v1[i+1]==0) ){
if( v1[i]>0 && v1[i]+v1[i+1]>0){
ct1+=v1[i]+v1[i+1]+1;
}
else if(v1[i]<0 && v1[i]+v1[i+1]<0 ){
ct1+=abs(v1[i]+v1[i+1])+1;
}
else{
ct1+=1;
}
v1[i+1]= v1[i]>0?-1:1;
}
else{
v1[i+1]+=v1[i];
}
// debug(ct);
}
cout<<min(ct,ct1);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
for (int i = (0); i < ((n)); i++) cin >> a[i];
int sum = 0;
bool plus = true;
int64_t ans1 = 0;
for (int i = (0); i < ((n)); i++) {
sum += a[i];
if (plus) {
if (sum < 0) {
ans1 += abs(sum) + 1;
sum = 1;
}
} else {
if (sum > 0) {
ans1 += abs(sum) + 1;
sum = -1;
}
}
plus ^= true;
}
sum = 0;
plus = false;
int64_t ans2 = 0;
for (int i = (0); i < ((n)); i++) {
sum += a[i];
if (plus) {
if (sum < 0) {
ans2 += abs(sum) + 1;
sum = 1;
}
} else {
if (sum > 0) {
ans2 += abs(sum) + 1;
sum = -1;
}
}
plus ^= true;
}
cout << min(ans1, ans2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
lst1 = list(map(int,input().split()))
odd = sum(lst1[::2])
even = sum(lst1[1::2])
if odd < even:
need = "-"
else:
need = "+"
ans = 0
now = 0#現在のi迄の和
for i in range(n):
if lst1[i] < 0:
if need == "-":
if abs(now) >= abs(lst1[i]):
ans += now+lst1[i]+1
now = -1
else:
now += lst1[i]
need = "+"
else: #need == "+"
ans += abs(now)-lst1[i] + 1
now = 1
need = "-"
else:
if need == "+":
if abs(now) >= abs(lst1[i]):
ans += abs(now)-lst1[i]+1
now = 1
else:
now += lst1[i]
need = "-"
else: #need == "-"
ans += abs(now)+lst1[i]+1
now = -1
need = "+"
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;
vector<int> a(N), S(N + 7);
for (int i = 0; i < N; i++) {
cin >> a[i];
}
int ans = 0;
S[0] = a[0];
if (S[0] == 0) {
for (int i = 0; i < N; i++) {
if (a[i] > 0) {
if (i % 2 == 0) {
S[0] = 1;
ans++;
break;
} else {
S[0] = -1;
ans++;
break;
}
} else if (a[i] < 0) {
if (i % 2 == 0) {
S[0] = -1;
ans++;
break;
} else {
S[0] = 1;
ans++;
break;
}
} else if (i == N - 1 && a[i] == 0) {
ans = (2 * N) - 1;
cout << ans << endl;
return 0;
}
}
}
for (int i = 1; i < N; i++) {
S[i] = S[i - 1] + a[i];
}
for (int i = 1; i < N; i++) {
if (S[i - 1] > 0 && S[i] >= 0) {
ans += abs(S[i]) + 1;
S[i] = -1;
if (i != N - 1) {
S[i + 1] = S[i] + a[i + 1];
}
} else if (S[i - 1] < 0 && S[i] <= 0) {
ans += abs(S[i]) + 1;
S[i] = 1;
if (i != N - 1) {
S[i + 1] = S[i] + a[i + 1];
}
}
if (i != N - 1) {
S[i + 1] = S[i] + a[i + 1];
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | 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]
#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;
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long ans = 0;
long long sum = 0;
long long tmp = 0;
for (int j = 0; j < 2; j++) {
for (int i = 0; i < n; i++) {
if (j == 0 && i == 0) {
sum = a[i];
continue;
} else if (j == 1 && i == 0) {
tmp = ans;
ans = 0;
sum = a[i] > 0 ? -1 : 1;
ans += a[i] > 0 ? a[i] + 1 : -(a[i] - 1);
continue;
}
if (sum > 0) {
if (sum + a[i] >= 0) {
ans += sum + a[i] + 1;
sum += a[i] - (sum + a[i] + 1);
continue;
}
} else {
if (sum + a[i] <= 0) {
ans -= sum + a[i] - 1;
sum += a[i] - (sum + a[i] - 1);
continue;
}
}
sum += a[i];
}
}
cout << (ans < tmp ? ans : tmp) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};
int ny, nx;
using namespace std;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long m, long long n) {
if ((0 == m) || (0 == n)) return 0;
return ((m / gcd(m, n)) * n);
}
long long llpow(long long x, long long y) {
long long ans = 1;
for (int i = 0, i_len = (y); i < i_len; ++i) ans *= x;
return ans;
}
int ctoi(char c) {
if (c >= '0' && c <= '9') {
return c - '0';
}
return 0;
}
class UnionFind {
public:
vector<long long> par;
vector<long long> siz;
UnionFind(long long sz_) : par(sz_), siz(sz_, 1LL) {
for (long long i = 0; i < sz_; ++i) par[i] = i;
}
void init(long long sz_) {
siz.assign(sz_, 1LL);
par.resize(sz_);
for (long long i = 0; i < sz_; ++i) par[i] = i;
}
long long root(long long x) {
while (par[x] != x) {
x = par[x] = par[par[x]];
}
return x;
}
bool merge(long long x, long long y) {
x = root(x);
y = root(y);
if (x == y) return false;
if (siz[x] < siz[y]) swap(x, y);
siz[x] += siz[y];
par[y] = x;
return true;
}
bool issame(long long x, long long y) { return root(x) == root(y); }
long long size(long long x) { return siz[root(x)]; }
};
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;
}
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0, i_len = (n); i < i_len; ++i) cin >> a[i];
int odd = 0, even = 0;
bool flag = false;
long long total = 0, res = 0;
for (int i = 0, i_len = (n); i < i_len; ++i) {
total += a[i];
if (i % 2 != 0) {
if (total == 0 or total > 0) {
even += abs(total + 1);
total = -1;
}
} else {
if (total == 0 or total < 0) {
even += abs(total - 1);
total = 1;
}
}
}
for (int i = 0, i_len = (n); i < i_len; ++i) {
res += a[i];
if (i % 2 == 0) {
if (res == 0 or res > 0) {
odd += abs(res + 1);
res = -1;
}
} else {
if (res == 0 or res < 0) {
odd += abs(res - 1);
res = 1;
}
}
}
cout << min(odd, even) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import numpy as np
n=int(input())
a=list(map(int,input().split()))
r=[0]
for i in range(n):
r.append(r[i]+a[i])
r.pop(0)
q=[r[i] for i in range(n)]
pm=[1-2*(i%2) for i in range(n)]
mp=[1-2*((i+1)%2) for i in range(n)]
sum1,sum2=0,0
sousa1,sousa2=0,0
for i in range(n):
if np.sign(r[i]) != pm[i]:
sum1+=abs(pm[i]-r[i])
sousa1=pm[i]-r[i]
for j in range(n-i-1):
r[i+j+1]=r[i+j+1]+sousa1
for i in range(n):
if np.sign(q[i]) != mp[i]:
sum2+=abs(mp[i]-q[i])
sousa2=mp[i]-q[i]
for j in range(n-i-1):
q[i+j+1]=q[i+j+1]+sousa2
print(min(sum1,sum2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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
n=int(input())
a=[int(i) for i in input().split()]
ans=0
sum=0
for j in a:
if numpy.sign(sum)==numpy.sign(sum+j) or numpy.sign(sum+j)==0:
ans+=abs(sum+j)+1
sum=-numpy.sign(sum)
else:
sum+=j
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | n = gets.to_i
a = gets.split.map(&:to_i)
s = a[0]
c = 0
if s == 0
s = a[1] > 0 ? -1 : 1
c += 1
end
a[1..-1].each do |i|
sign = s > 0
s += i
if s == 0
c += 1
s = sign ? -1 : 1
next
end
if sign
if s > 0
c += s + 1
s = -1
end
else
if s < 0
c += -s + 1
s = 1
end
end
end
p 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 count(vector<int> a, bool plus) {
plus = !plus;
long sum = 0;
long count = 0;
int n = a.size();
for (int i = 0; i < n; i++) {
plus = !plus;
sum += a.at(i);
if (plus) {
if (sum > 0) {
continue;
} else {
count += 1 - sum;
sum = 1;
}
} else {
if (sum < 0) {
continue;
} else {
count += 1 + sum;
sum = -1;
}
}
}
return count;
}
int main() {
long n;
cin >> n;
vector<int> a(n);
for (int &x : a) {
cin >> x;
}
cout << min(count(a, true), count(a, false)) << 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 = 999999999;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int64_t sum1 = 0;
int count1 = 0;
int64_t sum2 = 0;
int count2 = 0;
for (int i = 0; i < n; i++) {
sum1 += a[i];
if (i % 2 == 0) {
if (sum1 <= 0) {
count1 += abs(sum1) + 1;
sum1 = 1;
}
} else {
if (sum1 >= 0) {
count1 += abs(sum1) + 1;
sum1 = -1;
}
}
}
for (int i = 0; i < n; i++) {
sum2 += a[i];
if (i % 2 == 0) {
if (sum2 >= 0) {
count2 += abs(sum2) + 1;
sum2 = -1;
}
} else {
if (sum2 <= 0) {
count2 += abs(sum2) + 1;
sum2 = 1;
}
}
}
cout << min(count1, count2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | 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()
cur = 0
ans1 = 0
for i in range(n):
cur += A[i]
if i % 2:
if cur <= 0:
ans1 += -cur+1
cur = 1
else:
if cur >= 0:
ans1 += cur + 1
cur = -1
cur = 0
ans2 = 0
for i in range(n):
cur += A[i]
if i % 2:
if cur >= 0:
ans2 += cur + 1
cur = -1
else:
if cur <= 0:
ans2 += -cur + 1
cur = 1
print(min(ans1,ans2))
if __name__ == '__main__':
main() |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
sum = a[0]
count = 0
for i in range(1, n) :
if sum == 0 :
count += 1
if a[i] > 0 :
sum = -1
elif a[i] > 0 :
sum = 1
temp = sum
sum += a[i]
if temp > 0 and sum > 0 :
count += sum + 1
sum = -1
elif temp < 0 and sum < 0 :
count += -sum + 1
sum = 1
if sum == 0 :
count += 1
print(count)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | def solve(N, As):
positive = True if As[0] < 0 else False
for i, a in enumerate(As):
if a != 0:
if i % 2 == 0:
positive = True if a < 0 else False
else:
positive = False if a < 0 else True
break
ans = 0
i = As[0]
if i == 0:
ans = 1
if positive:
i = -1
else:
i = 1
for a in As[1:]:
if positive:
if i + a <= 0:
ans += abs(1 - (i + a))
a += 1 - (i + a)
else:
if i + a >= 0:
ans += abs(-1 - (i + a))
a += -1 - (i + a)
i += a
positive = not positive
return ans
if __name__ == "__main__":
n = int(input())
As = list(map(int, input().split(" ")))
print(solve(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() {
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < (int)(n); i++) cin >> a[i];
vector<long long> cusum(n);
cusum[0] = a[0];
for (int i = 1; i < n; i++) {
cusum[i] = cusum[i - 1] + a[i];
}
int tc = 2;
long long ans = 1e12;
while (tc--) {
long long sum = 0;
long long tmp = 0;
for (int i = 0; i < n; i++) {
long long x = cusum[i] + sum;
if (x > 0) {
if ((tc && i % 2 == 0) || (tc == 0 && i % 2 == 1)) {
continue;
} else {
tmp += x + 1;
sum -= (x + 1);
}
} else {
if ((tc && i % 2 == 0) || (tc == 0 && i % 2 == 1)) {
tmp += ((-1) * x + 1);
sum += ((-1) * x + 1);
} else {
continue;
}
}
}
ans = min(ans, tmp);
}
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;
const int INF = 1 << 29;
inline int two(int n) { return 1 << n; }
inline int test(int n, int b) { return (n >> b) & 1; }
inline void set_bit(int &n, int b) { n |= two(b); }
inline void unset_bit(int &n, int b) { n &= ~two(b); }
const long long mod = 1e9 + 7;
const int N = 1e6 + 9;
long long a[N], n;
vector<long long> v[N];
long long modexp(long long a, long long n) {
long long r = 1;
while (n) {
if (n & 1) r = (r * a) % mod;
a = (a * a) % mod;
n >>= 1;
}
return r;
}
bool cmp(const pair<double, long long> &a, const pair<double, int> &b) {
if (a.first == b.first) {
return a.second < b.second;
} else
return a.first > b.first;
}
long long solve(long long sum, long long k, long long ans) {
for (int i = 1; i < n; i++) {
sum += a[i];
if (k == 1) {
if (sum >= 0) {
ans += sum + 1;
sum = -1;
}
} else {
if (sum <= 0) {
ans += sum + 1;
sum = 1;
}
}
if (k == 0)
k = 1;
else
k = 0;
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long ans1 = 0, ans2 = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long sum1 = a[0];
if (sum1 <= 0) {
ans1 += sum1 + 1;
sum1 = 1;
}
long long p = solve(sum1, 1, ans1);
long long sum2 = a[0];
if (sum2 >= 0) {
ans2 += sum2 + 1;
sum2 = -1;
}
long long pp = solve(sum2, 0, ans2);
cout << min(p, pp) << 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()))
total = a[0]
ans = 0
hugo = 0
if total == 0:
if a[1] >= 0:
total = -1
ans = 1
hugo = -1
else:
total = 1
ans = 1
hugo = 1
elif total > 0:
ans = 0
hugo = 1
elif total < 0:
ans = 0
hugo = -1
for i in range(1,n):
total += a[i]
if hugo == -1 and total <= 0:
ans = ans + (1-total)
total = 1
hugo = 1
elif hugo == -1 and total > 0:
hugo = 1
elif hugo == 1 and total >= 0:
ans = ans + (1+total)
total = -1
hugo = -1
elif hugo == 1 and total < 0:
hugo = -1
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | def sequence(N: int, A: list) -> int:
def count_op(s: int) -> int:
s = (s // abs(s)) * A[0]
op = 0
for a in A[1:]:
if s < 0:
if s + a > 0:
# OK
s = s + a
continue
else:
op += 1 - (s + a)
s = 1
else: # s > 0
if s + a < 0:
# OK
s = s + a
continue
else:
op += (s + a) - (-1)
s = -1
return op
return min(count_op(1), count_op(-1))
if __name__ == "__main__":
N = int(input())
A = [int(s) for s in input().split()]
ans = sequence(N, A)
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
long long sum = a[0];
long long cnt = 0;
if (sum == 0) sum = (a[1] > 0 ? -1 : 1);
for (int i = 1; i < n; i++) {
long long nsum = sum + a[i];
if (sum > 0 && nsum < 0 || sum < 0 && nsum > 0) {
sum = nsum;
continue;
}
if (nsum == 0) {
sum = (sum > 0 ? -1 : 1);
cnt += 1;
} else {
if (sum > 0 && nsum > 0)
sum = -1;
else
sum = 1;
cnt += abs(nsum) + 1;
}
}
cout << cnt << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n;
int a[100010];
int solve(int sign) {
int ans = 0;
int sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (sign == 1) {
if (sum <= 0) {
ans = ans + 1 - sum;
sum = 1;
}
} else {
if (sum >= 0) {
ans = ans + 1 + sum;
sum = -1;
}
}
sign *= -1;
}
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long t = 1;
while (t--) {
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
cout << min(solve(1), solve(-1));
}
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
int calc(bool firstPositive, int a[], int n) {
bool positive = firstPositive;
int cost = 0;
long sum = 0;
for (int i = 0; i < n; ++i) {
bool sumpos = (sum + a[i]) >= 0;
if (sumpos != positive) {
while (((sum + a[i]) >= 0) == sumpos) {
a[i] += sumpos ? -1 : 1;
++cost;
}
}
if ((sum + a[i]) == 0) {
a[i] += sumpos ? -1 : 1;
++cost;
}
sum += a[i];
positive = !positive;
}
return cost;
}
int main(int argc, char *argv[]) {
int n;
std::cin >> n;
int a[1 << 11], b[1 << 11];
for (int i = 0; i < n; ++i) {
std::cin >> a[i];
b[i] = a[i];
}
std::cout << std::min(calc(true, a, n), calc(false, b, n)) << std::endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long int answer(vector<long long int> a, int s) {
int n = a.size();
long long int ans = 0, sum = 0;
if (s == 1) {
if (a[0] < 0) a[0] += 1 - a[0];
for (size_t i = 0; i < n; i++) {
if (sum * (sum + a[i]) > 0) {
if (sum < 0) {
ans += abs(1 - (sum + a[i]));
a[i] += abs(1 - (sum + a[i]));
} else if (sum > 0) {
ans += abs(-1 - (sum + a[i]));
a[i] += -1 - (sum + a[i]);
}
}
if (sum + a[i] == 0) {
if (sum > 0)
a[i]--;
else
a[i]++;
ans++;
}
sum += a[i];
}
} else {
if (a[0] > 0) a[0] -= a[0] + 1;
for (size_t i = 0; i < n; i++) {
if (sum * (sum + a[i]) > 0) {
if (sum < 0) {
ans += abs(1 - (sum + a[i]));
a[i] += abs(1 - (sum + a[i]));
} else if (sum > 0) {
ans += abs(-1 - (sum + a[i]));
a[i] += -1 - (sum + a[i]);
}
}
if (sum + a[i] == 0) {
if (sum > 0)
a[i]--;
else
a[i]++;
ans++;
}
sum += a[i];
}
}
return ans;
}
int main(int argc, char const *argv[]) {
long long int n, ans = 0, sum = 0;
cin >> n;
vector<long long int> a(n);
for (size_t i = 0; i < n; i++) cin >> a[i];
cout << min(answer(a, 1), answer(a, -1)) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9;
const long long MOD = 1e9 + 7;
int main() {
long long n;
cin >> n;
vector<long long> a(n);
for (long long i = 0; i < n; ++i) {
cin >> a[i];
}
vector<long long> sum(n);
sum[0] = a[0];
for (long long i = 1; i < n; i++) {
sum[i] += sum[i - 1] + a[i];
}
long long even_sum = 0;
long long odd_sum = 0;
for (long long i = 0; i < n - 1; ++i) {
if (i % 2 == 0 && sum[i] >= 0) {
even_sum += 1 + sum[i];
}
if (i % 2 == 1 && sum[i] <= 0) {
even_sum += 1 - sum[i];
}
}
for (long long i = 0; i < n - 1; ++i) {
if (i % 2 == 0 && sum[i] <= 0) {
odd_sum += 1 - sum[i];
}
if (i % 2 == 1 && sum[i] >= 0) {
odd_sum += 1 + sum[i];
}
}
cout << min(odd_sum, even_sum) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long sign(long long A) { return (A > 0) - (A < 0); }
int main(void) {
long long n;
vector<long long> a;
long long count = 0;
cin >> n;
for (int i = 0; i < n; i++) {
long long temp;
cin >> temp;
a.push_back(temp);
}
long long diff = a[0];
for (int i = 1; i < n; i++) {
if (diff + a[i] == 0 || diff * (diff + a[i]) > 0) {
count += abs(-1 * sign(diff) * (abs(diff) + 1) - a[i]);
diff = -1 * sign(diff);
} else
diff += a[i];
}
cout << count << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
A = tuple(map(int, input().split(' ')))
cs = A[0]
ans1 = 0
for na in A[1:]:
if cs >= 0:
cs += na
if cs < 0:
continue
ans1 += cs + 1
cs = -1
else:
cs += na
if cs > 0:
continue
ans1 += -cs + 1
cs = 1
cs = A[-1]
ans2 = 0
for na in reversed(A[:-1]):
if cs >= 0:
cs += na
if cs < 0:
continue
ans2 += cs + 1
cs = -1
else:
cs += na
if cs > 0:
continue
ans2 += -cs + 1
cs = 1
print(min(ans1, ans2))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
int n;
int a[100000];
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
int total = 0, ans1 = 0, ans2 = 0;
for (int i = 0; i < n; i++) {
total += a[i];
if (i % 2 == 0 && total <= 0) {
ans1 += 1 - total;
total = 1;
} else if (i % 2 != 0 && total >= 0) {
ans1 += total - (-1);
total = -1;
}
}
total = 0;
for (int i = 0; i < n; i++) {
total += a[i];
if (i % 2 != 0 && total <= 0) {
ans2 += 1 - total;
total = 1;
} else if (i % 2 == 0 && total >= 0) {
ans2 += total - (-1);
total = -1;
}
}
cout << min(ans1, ans2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
string flag;
vector<int> a;
int str;
long long str_sum = 0, str_nsum = 0;
int num;
long long co = 0;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> num;
for (int i = 0; i < num; i++) {
cin >> str;
a.push_back(str);
}
if (a[0] > 0) {
flag = "up";
} else if (a[0] < 0) {
flag = "down";
} else {
if (a[1] >= 0) {
flag = "down";
co++;
a[0] = -1;
} else {
flag = "up";
co++;
a[0] = 1;
}
}
str_sum = a[0];
for (int i = 1; i < num; i++) {
str_sum += a[i];
if (flag == "up" && str_sum >= 0) {
co += str_sum + 1;
str_sum -= str_sum + 1;
} else if (flag == "down" && str_sum <= 0) {
co += 0 - str_sum + 1;
str_sum += 0 - str_sum + 1;
}
if (str_sum > 0) {
flag = "up";
} else if (str_sum < 0) {
flag = "down";
}
}
cout << co << "\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>
int main(void) {
long long sum, ans = 0, b;
int n;
scanf("%d %lld", &n, &sum);
for (int i = 0; i < n - 1; i++) {
scanf("%lld", &b);
if (sum >= 0 && sum + b >= 0) {
while (sum >= 0 && sum + b >= 0) {
sum--;
ans++;
}
} else if (sum <= 0 && sum + b <= 0) {
while (sum <= 0 && sum + b <= 0) {
sum++;
ans++;
}
}
sum += b;
}
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 long long MX = 1e5 + 5, INF = 5 << 60, MOD = 1e9 + 7;
long long N;
vector<long long> A;
void input() {
cin >> N;
A.resize(N);
for (long long i = (long long)(0); i <= (long long)(N - 1); ++i) {
cin >> A[i];
}
}
void solve() {
long long ans = INF;
long long fugo;
for (long long fg = (long long)(0); fg <= (long long)(0); ++fg) {
if (fg == 1) {
fugo = 1;
} else
fugo = 0;
long long prev = 0;
long long s = 0;
long long ans1 = 0;
for (long long i = (long long)(0); i <= (long long)(N - 1); ++i) {
s += A[i];
if (fugo) {
if (s > 0) {
ans1 += 0;
} else if (s == 0) {
ans1 += 1;
s = 1;
} else {
ans1 += abs(s) + 1;
s = 1;
}
} else {
if (s > 0) {
ans1 += (abs(s) + 1);
s = -1;
} else if (s == 0) {
ans1 += 1;
s = -1;
} else {
ans1 += 0;
}
}
prev = s;
fugo ^= 1;
}
ans = min(ans1, ans);
}
cout << ans << endl;
}
signed main() {
input();
solve();
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long INF = 1e18;
const double pi = acos(-1.0);
int main(void) {
long long n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < (n); ++i) cin >> a[i];
long long ans, sum = 0, res1 = 0, res2 = 0;
for (int sign = 0; sign < (2); ++sign) {
for (int i = 0; i < (n); ++i) {
sum += a[i];
if ((i % 2 ^ sign) && sum >= 0) {
res1 += sum + 1;
sum = -1;
} else if (!(i % 2 ^ sign) && sum <= 0) {
res2 += abs(sum - 1);
sum = 1;
}
}
}
ans = min(res1, res2);
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <iostream>
#include <algorithm>
#include <cmath>
#include <limits>
#include <vector>
#include <cstdio>
#include <bits/stdc++.h>
#include <set>
#include <map>
#include <stdio.h>
#include <stack>
#include <queue>
#include <deque>
#include <numeric>
using namespace std;
using ll = long long;
map <int ,int> mpa,mpb;
typedef pair<ll, ll> P;
priority_queue<P, vector<P>, greater<P>> pque;
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
int N;
cin >> N;
int a[N+2];
for(int i=1;i<=N;i++){
cin >> a[i];
}
int cnt1=0,cnt2=0;
sum=0;
for(int i=1,s=1;i<=N;i++,s*=-1){
sum+=a[i];
if(sum*s<=0) cnt1+=abs(sum-s),sum=s;
}
sum=0;
for(int i=1,s=-1;i<=N;i++,s*=-1){
sum+=a[i];
if(sum*s<=0) cnt2+=abs(sum-s),sum=s;
}
cout << min(cnt1,cnt2) << endl;
return 0;
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n, 0);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int cnt1 = 0;
int cnt2 = 0;
for (int i = 0, sum1 = 0, sum2 = 0; i < n; i++) {
sum1 += a[i];
if (i % 2 == 0 && sum1 <= 0) {
cnt1 += 1 - sum1;
sum1 = 1;
} else if (i % 2 == 1 && sum1 >= 0) {
cnt1 += sum1 + 1;
sum1 = -1;
}
sum2 += a[i];
if (i % 2 == 0 && sum2 >= 0) {
cnt2 += sum2 + 1;
sum2 = -1;
} else if (i % 2 == 1 && sum2 <= 0) {
cnt2 += 1 - sum2;
sum2 = 1;
}
}
cout << min(cnt1, cnt2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long n;
long long f(vector<long long>& sum, vector<long long>& pm) {
long long tmp;
tmp = 0;
for (int i = 0; i < (n - 1); i++) {
sum[i + 1] += pm[i];
if (sum[i] * sum[i + 1] >= 0) {
if (sum[i + 1] == 0) {
if (sum[i] < 0)
sum[i + 1] = pm[i + 1] = 1;
else
sum[i + 1] = pm[i + 1] = -1;
} else if (sum[i + 1] < 0) {
pm[i + 1] = 1 - sum[i + 1];
sum[i + 1] = 1;
} else if (sum[i + 1] > 0) {
pm[i + 1] = -1 - sum[i + 1];
sum[i + 1] = -1;
}
tmp += abs(pm[i + 1]);
}
}
return tmp;
}
signed main(void) {
cin >> n;
vector<long long> s(n), t, pm(n, 0);
long long ans, tmp;
for (int i = 0; i < (n); i++) {
int a;
cin >> a;
s[i] = a;
}
for (int i = 0; i < (n - 1); i++) s[i + 1] += s[i];
ans = 1e18;
copy(s.begin(), s.end(), back_inserter(t));
tmp = 0;
if (t[0] <= 0) {
pm[0] = 1 - t[0];
t[0] = 1;
tmp += abs(pm[0]);
}
tmp += f(t, pm);
ans = min(ans, tmp);
for (int i = 0; i < (n); i++) pm[i] = 0;
tmp = 0;
if (s[0] >= 0) {
pm[0] = -1 - s[0];
s[0] = -1;
tmp += abs(pm[0]);
}
tmp += f(s, pm);
ans = min(ans, tmp);
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int fplus(int n, int arr[]) {
int ans = 0, sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
if (i % 2) {
if (sum <= 0) {
ans += -sum + 1;
sum = 1;
}
} else {
if (sum >= 0) {
ans += sum + 1;
sum = -1;
}
}
}
return ans;
}
int fminus(int n, int arr[]) {
int ans = 0, sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
if (i % 2) {
if (sum >= 0) {
ans += sum + 1;
sum = -1;
}
} else {
if (sum <= 0) {
ans += -sum + 1;
sum = 1;
}
}
}
return ans;
}
int main() {
int n, a[100000];
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
int ans = min(fplus(n, a), fminus(n, a));
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 atcoder() {
long long n;
cin >> n;
long long ans = 0;
long long a[110000] = {};
for (int i = 0; i < n; ++i) cin >> a[i];
long long tmpsum = a[0];
for (int i = 1; i < n; ++i) {
if (tmpsum * (a[i] + tmpsum) >= 0) {
if (a[i] + tmpsum > 0) {
ans += a[i] + tmpsum + 1;
tmpsum = -1;
} else if (a[i] + tmpsum < 0) {
ans += -(a[i] + tmpsum) + 1;
tmpsum = 1;
} else {
ans++;
if (tmpsum < 0)
tmpsum = 1;
else if (tmpsum > 0)
tmpsum = -1;
}
} else
tmpsum += a[i];
}
cout << ans << "\n";
return 0;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
atcoder();
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T>
inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
template <class T>
inline T sqr(T x) {
return x * x;
}
const double EPS = 1e-10;
const double PI = acos(-1.0);
const long long INF = 1000000007;
template <class T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
int main(void) {
int N;
cin >> N;
vector<long long> a(N);
for (int i = (0); i < (N); ++i) cin >> a[i];
vector<long long> s(N);
s[0] = a[0];
for (int i = (0); i < (N - 1); ++i) s[i + 1] = s[i] + a[i + 1];
long long offset = 0, ans = 0;
for (int i = (0); i < (N - 1); ++i) {
if ((s[i] + offset) * (s[i + 1] + offset) >= 0) {
ans += abs(s[i + 1] + offset) + 1;
if (s[i + 1] + offset > 0)
offset -= s[i + 1] + offset + 1;
else
offset += -(s[i + 1] + offset - 1);
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(int argc, char *argv[]) {
int n;
cin >> n;
long sum;
long ans = 0;
cin >> sum;
for (int i = 1; i < n; i++) {
long ai;
cin >> ai;
if (sum * (sum + ai) < 0) {
sum += ai;
} else {
ans += abs(sum + ai) + 1;
if (sum > 0)
sum = -1;
else
sum = 1;
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] a = new int[n];
for(int i = 0 ; i < n ; i++) a[i] = sc.nextInt();
int sum = 0, cnt = 0;
for(int i = 0 ; i < n ; i++) {
sum += a[i];
if(i % 2 == 0 && sum <= 0) {
// 正に変える
cnt += Math.abs(sum) + 1;
sum = 1;
} else if(i % 2 == 1 && sum >= 0) {
// 負に変える
cnt += sum + 1;
sum = -1;
}
}
int sum1 = 0;
int cnt1 = 0;
for(int i = 0 ; i < n ; i++) {
sum1 += a[i];
if(i % 2 == 0 && sum1 >= 0) {
// 負に変える
cnt1 += sum1 + 1;
sum1 = -1;
} else if(i % 2 == 1 && sum1 <= 0) {
// 正に変える
cnt1 += Math.abs(sum1) + 1;
sum1 = 1;
}
}
System.out.println(Math.min(cnt, 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 MAX = 100005;
int main() {
int n, tmp;
int ans1{0};
int ans2{0};
cin >> n;
int A[MAX], B[MAX];
int cum_sum = 0;
for (int 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 (int i = 0; i < n; i++) {
A[i] += tmp;
}
}
for (int i = 0; i < n - 1; i++) {
if (A[i] > 0) {
if (A[i + 1] >= 0) {
tmp = (A[i + 1] + 1);
ans1 += tmp;
for (int 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 (int j = i + 1; j < n; j++) A[j] += tmp;
}
}
}
tmp = 0;
if (B[0] >= 0) {
tmp = B[0] + 1;
ans2 += tmp;
for (int i = 0; i < n; i++) {
A[i] -= tmp;
}
}
for (int i = 0; i < n - 1; i++) {
if (B[i] > 0) {
if (B[i + 1] >= 0) {
tmp = (B[i + 1] + 1);
ans2 += tmp;
for (int 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 (int j = i + 1; j < n; j++) B[j] += tmp;
}
}
}
int 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 | cpp | #include <iostream>
#include <cmath>
using namespace std;
int main(){
int n;cin>>n;
int a[n];
for(int i = 0; n > i; i++)cin>>a[i];
long long sh = 0;
long long nw = 0;
for(int i = 0; n > i; i++){
(long long)nw += a[i];
if(i % 2 == 0)if(nw<=0)(long long)sh += 1-nw,nw=1;
if(i % 2 == 1)if(nw>=0)(long long)sh += nw+1,nw=-1;
}
long long hs = 0;
nw = 0;
for(int i = 0; n > i; i++){
(long long)nw += a[i];
if(i % 2 == 1)if(nw<=0)(long long)hs += 1-nw,nw=1;
if(i % 2 == 0)if(nw>=0)(long long)hs += nw+1,nw=-1;
}
cout << min(hs,sh) << 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>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
using vl = vector<long long>;
using vvl = vector<vector<long long>>;
using vs = vector<string>;
const int mod = 1000000007;
class mint {
public:
long long x;
mint(long long x = 0) : x((x % mod + mod) % mod) {}
mint operator-() const { return mint(-x); }
mint& operator+=(const mint& a) {
if ((x += a.x) >= mod) x -= mod;
return *this;
}
mint& operator-=(const mint& a) {
if ((x += mod - a.x) >= mod) x -= mod;
return *this;
}
mint& operator*=(const mint& a) {
(x *= a.x) %= mod;
return *this;
}
mint operator+(const mint& a) const {
mint res(*this);
return res += a;
}
mint operator-(const mint& a) const {
mint res(*this);
return res -= a;
}
mint operator*(const mint& a) const {
mint res(*this);
return res *= a;
}
mint pow(long long t) const {
if (!t) return 1;
mint a = pow(t >> 1);
a *= a;
if (t & 1) a *= *this;
return a;
}
mint inv() const { return pow(mod - 2); }
mint& operator/=(const mint& a) { return (*this) *= a.inv(); }
mint operator/(const mint& a) const {
mint res(*this);
return res /= a;
}
friend ostream& operator<<(ostream& os, const mint& m) {
os << m.x;
return os;
}
};
long long modpow(long long x, long long n, long long p = 1000000007) {
if (n == 0) return 1 % p;
if (n % 2 == 0)
return modpow(x * x % p, n / 2, p);
else
return x * modpow(x, n - 1, p) % p;
}
void Main() {
long long N;
cin >> N;
vl v(N);
for (long long i = 0; i < N; i++) cin >> v[i];
long long ans = 0;
;
if (v[0]) {
long long flg = (v[0] > 0);
long long acc = v[0];
for (long long i = 1; i < N; i++) {
acc += v[i];
if (flg && acc >= 0) {
ans += acc + 1;
acc = -1;
} else if (!flg && acc <= 0) {
ans += -acc + 1;
acc = 1;
}
flg ^= 1;
}
} else {
v[0] = -1;
long long flg = (v[0] > 0);
long long ans1 = 1;
long long acc = v[0];
for (long long i = 1; i < N; i++) {
acc += v[i];
if (flg && acc >= 0) {
ans1 += acc + 1;
acc = -1;
} else if (!flg && acc <= 0) {
ans1 += -acc + 1;
acc = 1;
}
flg ^= 1;
}
v[0] = 1;
flg = (v[0] > 0);
long long ans2 = 1;
acc = v[0];
for (long long i = 1; i < N; i++) {
acc += v[i];
if (flg && acc >= 0) {
ans2 += acc + 1;
acc = -1;
} else if (!flg && acc <= 0) {
ans2 += -acc + 1;
acc = 1;
}
flg ^= 1;
}
ans = min(ans1, ans2);
}
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
long long t = 1;
for (long long i = 0; i < t; i++) Main();
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
using System.Linq;
using System.Collections.Generic;
using static System.Math;
using static System.Console;
using System.Text;
class Program
{
static void Main(string[] args)
{
solve();
}
static Scanner cin;
static int n;
static int[] a;
static void input()
{
cin = new Scanner();
n = cin.nextInt();
a = cin.ArrayInt(n);
}
static void solve()
{
input();
int sum = 0;
int ans = 0;
for(int i = 0; i < n; i++)
{
sum += a[i];
if(sum * (sum - a[i]) > 0)
{
if(sum > 0)
{
int x = -1 - sum;
ans += Abs(x);
a[i] += x;
sum = -1;
}
else
{
int x = 1 - sum;
ans += Abs(x);
a[i] += x;
sum = 1;
}
}
else if(sum == 0)
{
if(sum - a[i] > 0)
{
sum = -1;
}
else
{
sum = 1;
}
ans++;
}
}
WriteLine(ans);
}
class Scanner
{
string[] s;
int i;
char[] cs = new char[] { ' ' };
public Scanner()
{
s = new string[0];
i = 0;
}
public string next()
{
if (i < s.Length) return s[i++];
string st = Console.ReadLine();
while (st == "") st = Console.ReadLine();
s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
if (s.Length == 0) return next();
i = 0;
return s[i++];
}
public int nextInt()
{
return int.Parse(next());
}
public int[] ArrayInt(int N, int add = 0)
{
int[] Array = new int[N];
for(int i = 0; i < N; i++)
{
Array[i] = nextInt() + add;
}
return Array;
}
public long nextLong()
{
return long.Parse(next());
}
public long[] ArrayLong(int N, long add = 0)
{
long[] Array = new long[N];
for(int i = 0; i < N; i++)
{
Array[i] = nextLong() + add;
}
return Array;
}
public double nextDouble()
{
return double.Parse(next());
}
public double[] ArrayDounble(int N, int add = 0)
{
double[] Array = new double[N];
for (int i = 0; i < N; i++)
{
Array[i] = nextDouble() + add;
}
return Array;
}
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
List<Long> alist = new ArrayList<>();
for (int i = 0; i < n; i++) {
alist.add(sc.nextLong());
}
int cntOdd = 0;
int cntEvn = 0;
long sum = 0;
for (int i = 0; i < alist.size(); i++) {
sum += alist.get(i);
//iが偶数のとき正
if(i%2 == 0) {
if(sum > 0) {
continue;
} else {
long diff = 1-sum;
cntEvn += 1-sum;
sum += diff;
}
} else {
if(sum < 0) {
continue;
} else {
long diff = 1+sum;
cntEvn += 1+sum;
sum -= diff;
}
}
}
sum =0;
for (int i = 0; i < alist.size(); i++) {
sum += alist.get(i);
//iが偶数のとき負
if (i%2 == 0) {
if(sum < 0) {
continue;
} else {
long diff = 1+sum;
cntOdd += 1+sum;
sum -= diff;
}
} else {
if(sum > 0) {
continue;
} else {
long diff = 1-sum;
cntOdd += 1-sum;
sum += diff;
}
}
}
if(cntOdd <= cntEvn) {
System.out.println(cntOdd);
} else {
System.out.println(cntEvn);
}
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
al = list(map(int, input().split()))
import itertools
alsum = list(itertools.accumulate(al))
temp1 = 0
temp2 = 0
for i in range(n):
if i % 2 ==0:
if alsum[i] >0:
pass
else:
y = abs(alsum[i])+1
temp1 += y
alsum = list(map(lambda x:x +y ,alsum))
else:
if alsum[i] <0:
pass
else:
y = alsum[i]+1
temp1 += y
alsum = list(map(lambda x:x-y,alsum))
import itertools
alsum = list(itertools.accumulate(al))
for j in range(n):
if j % 2 ==0:
if alsum[j] <0:
pass
else:
y = abs(alsum[j])+1
temp2 += y
alsum = list(map(lambda x:x-y,alsum))
else:
if alsum[j] >0:
pass
else:
y = abs(alsum[j])+1
temp2 += y
alsum = list(map(lambda x:x+y,alsum))
print(min(temp1,temp2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 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 top = a[0], cnt = 0;
bool sign = (top >= 0 ? true : false);
for (int i = 1; i < n; i++) {
if (sign && top + a[i] < 0) {
top += a[i];
sign = false;
} else if (!sign && top + a[i] > 0) {
top += a[i];
sign = true;
} else if (top + a[i] == 0) {
cnt++;
if (sign) {
top = -1;
sign = false;
} else {
top = 1;
sign = false;
}
} else {
if (sign) {
cnt += (top + a[i]) + 1;
top = -1;
sign = false;
} else {
cnt += 1 - (top + a[i]);
top = 1;
sign = true;
}
}
}
cout << cnt << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
const long long MOD = 1000000007;
using namespace std;
map<long long, int> mp;
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < (n); i++) cin >> a[i];
int ans1 = 0;
int ans2 = 0;
int sum = 0;
for (int i = 0; i < (n); i++) {
sum += a[i];
if (i % 2 == 0) {
if (sum <= 0) {
ans1 += 1 - sum;
sum = 1;
}
} else {
if (sum >= 0) {
ans1 += sum + 1;
sum = -1;
}
}
}
sum = 0;
for (int i = 0; i < (n); i++) {
sum += a[i];
if (i % 2 != 0) {
if (sum <= 0) {
ans2 += 1 - sum;
sum = 1;
}
} else {
if (sum >= 0) {
ans2 += sum + 1;
sum = -1;
}
}
}
cout << min(ans1, ans2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <bits/stdc++.h>
int main(void) {
int n;
long sum1 = 0;
long sum2 = 0;
long tmp;
long lcount = 0;
long rcount = 0;
long a[150000];
char input[1500000];
int i = 0, j = 0;
int cp = 0, tcp = 0;
char tp[12];
tp[12] = '\0';
fgets(input, 1500000, stdin);
n = atoi(input);
fgets(input, 1500000, stdin);
for (i = 0; i < n; i++) {
while (input[cp] != ' ' && input[cp] != '\n') {
tp[tcp] = input[cp];
tcp++;
cp++;
}
tp[tcp] = '\0';
tcp = 0;
cp++;
a[i] = atoi(tp);
}
tmp = a[0];
for (i = 1; i < n; i++) {
if (i % 2 == 0) {
tmp += a[i];
if (tmp > -1) {
lcount += tmp + 1;
tmp = -1;
}
} else {
tmp += a[i];
if (tmp < 1) {
lcount += 1 - tmp;
tmp = 1;
}
}
}
tmp = a[0];
for (i = 1; i < n; i++) {
if (i % 2 == 1) {
tmp += a[i];
if (tmp > -1) {
rcount += tmp + 1;
tmp = -1;
}
} else {
tmp += a[i];
if (tmp < 1) {
rcount += 1 - tmp;
tmp = 1;
}
}
}
printf("%ld\n", lcount > rcount ? rcount : lcount);
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, M;
cin >> N >> M;
if (N == M) {
cout << "EQUAL";
} else if (N < M) {
cout << "GREATER";
} else {
cout << "LESS";
}
cout << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
A = list(map(int,input().split()))
totals = [0] * N
totals[0] = A[0]
con = 0
if totals[0] == 0:
for i in range(1,N):
if A[i] != 0:
f = A[i]
if f > 0:
totals[0] = -1
else:
totals[0] = 1
break
else:
totals[0] = 1
con += 1
for i in range(1,N):
totals[i] = totals[i - 1] + A[i]
if totals[i - 1] * totals[i] >= 0:
if totals[i - 1] < 0:
con += abs(1 - totals[i])
totals[i] += abs(1 - totals[i])
elif totals[i - 1] > 0:
con += abs(-1 - totals[i])
totals[i] -= abs(-1 - totals[i])
print(con)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
double a[10002] = {};
double b[10002] = {};
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
b[i] = a[i];
}
int eve = 0, sum = 0;
for (int j = 0; j < n; j++) {
if (j % 2 == 0 && sum + a[j] <= 0) {
eve += abs(a[j] + sum) + 1;
a[j] = abs(sum) + 1;
}
if (j % 2 == 1 && sum + a[j] >= 0) {
eve += a[j] + sum + 1;
a[j] = -abs(sum) - 1;
}
sum += a[j];
}
sum = 0;
int odd = 0;
for (int k = 0; k < n; k++) {
if (k % 2 == 0 && sum + b[k] >= 0) {
odd += abs(b[k] + sum) + 1;
b[k] = -abs(sum) - 1;
}
if (k % 2 == 1 && sum + b[k] <= 0) {
odd += abs(sum + b[k]) + 1;
b[k] = abs(sum) + 1;
}
sum += b[k];
}
cout << min(odd, eve) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java |
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
long[] a = new long[n];
boolean flag = true;
long sum = 0, ans = 0;
int i = 1;
for (int j = 0; j < a.length; j++) {
a[j] = in.nextLong();
}
in.close();
if (a[0] != 0) {
sum += a[0];
flag = a[0] > 0;
} else {
++ans;
for (; i < a.length; i++) {
if (a[i] != 0) {
flag = a[i] > 0;
if (flag) {
sum = a[i] - 1;
} else {
sum = a[i] + 1;
}
i++;
break;
}
ans += 2;
}
}
for (; i < a.length; i++) {
if (flag && sum + a[i] >= 0) {
ans += sum + a[i] + 1;
sum = -1;
flag = false;
} else if (!flag && sum + a[i] <= 0) {
ans += 1 - (sum + a[i]);
sum = 1;
flag = true;
} else {
sum += a[i];
flag = sum > 0;
}
}
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 | cpp | #include <bits/stdc++.h>
using namespace std;
long long a[100001];
int main() {
int N;
cin >> N;
for (int i = 0; i < N; i++) {
cin >> a[i];
}
for (int i = 1; i < N; i++) {
a[i] += a[i - 1];
}
long long add = 0, res1 = 0, res2 = 0;
for (int i = 0; i < N; i++) {
if (i % 2 == 0) {
if (a[i] + add > 0) {
res1 += abs(a[i] + add + 1);
add -= (a[i] + add + 1);
} else if (a[i] + add == 0) {
add--;
res1++;
}
} else {
if (a[i] + add < 0) {
res1 += abs(-a[i] + add + 1);
add += (-a[i] + add + 1);
} else if (a[i] + add == 0) {
add++;
res1++;
}
}
}
add = 0;
for (int i = 0; i < N; i++) {
if (i % 2 == 1) {
if (a[i] + add > 0) {
res2 += abs(a[i] + add + 1);
add -= (a[i] + add + 1);
} else if (a[i] == 0) {
add--;
res2++;
}
} else {
if (a[i] + add < 0) {
res2 += abs(-a[i] + add + 1);
add += (-a[i] + add + 1);
} else if (a[i] == 0) {
add++;
res2++;
}
}
}
cout << min(res1, res2) << 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 sum = 0, num = 0;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
if (sum > 0 && sum + a >= 0) {
num += sum + a + 1;
a -= sum + a + 1;
} else if (sum < 0 && sum + a <= 0) {
num += abs(sum + a) + 1;
a += sum + a + 1;
}
sum += a;
}
cout << num << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long calc(long long a[], int n) {
long long sum = a[0];
long long res = 0;
for (int i = 1; i < n; i++) {
if (sum < 0 && sum + a[i] <= 0) {
res += 1 - sum - a[i];
a[i] += 1 - sum - a[i];
} else if (sum > 0 && sum + a[i] >= 0) {
res += abs(a[i] - (-1 - sum));
a[i] = -1 - sum;
}
sum += a[i];
}
return res;
}
int main() {
int n;
long long a[100010];
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%lld", &a[i]);
long long res = 0;
if (a[0] == 0) {
res++;
a[0] = 1;
res += calc(a, n);
a[0] = -1;
res = min(calc(a, n) + 1, res);
} else {
res = calc(a, n);
}
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 | python3 | n=int(input())
a=map(int,input().split())
ans1,ans2=0,0
m1,m2=0,0
k=1
for i in a:
m1+=i
m2+=i
if k:
k=0
if m1>=0:
ans1+=m1+1
m1=-1
if m2<=0:
ans2+=1-m2
m2=1
else:
k=1
if m1<=0:
ans1+=1-m1
m1=1
if m2>=0:
ans2+=m2+1
m2=-1
print(ans1,ans2)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
A = list(map(int, input().split()))
ans1 = 0
sum = A[0]
for a in A[1:]:
if (sum + a) * sum < 0:
sum += a
else:
fugo = sum // abs(sum)
nextsum = - fugo
#print('fugo={}, nextsum={}'.format(fugo, nextsum))
a_should_be = nextsum - sum
dif = abs(a_should_be - a)
#print('a_shoule_be={}, dif={}'.format(a_should_be, dif))
sum = nextsum
ans1 += dif
#print(ans1)
#A[0]を反転したほうがいいパターンの処理
ans2 = abs(A[0]) + 1
sum = - (A[0] // abs(A[0]))
for a in A[1:]:
if (sum + a) * sum < 0:
sum += a
else:
fugo = sum // abs(sum)
nextsum = - fugo
a_should_be = nextsum - sum
dif = abs(a_should_be - a)
sum = nextsum
ans2 += dif
#print(ans2)
print(min(ans1, ans2))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
bool plus = false;
long long ans = 0;
long long sum = a[0];
if (sum > 0) {
plus = false;
}
else if (sum < 0) {
plus = true;
}
for (int i = 1; i < n; ++i) {
sum += a[i];
if (plus == false) {
if (sum >= 0) {
ans += (abs(sum)+1);
sum = -1;
}
plus = true;
}
else {
if (sum <= 0) {
ans += (abs(sum)+1);
sum = 1;
}
plus = false;
}
}
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 | python3 | n=int(input())
l=list(map(int,input().split()))
ans=0
if l[0]==0:
for i in range(n):
if l[i]!=0:
s=-1**(i%2)
ans=1
k1=l[0]
for i in range(1,n):
if k1>0:
k1+=l[i]
if k1>=0:
ans+=k1+1
k1=-1
continue
if k1<0:
k1+=l[i]
if k1<=0:
ans+=abs(k1)+1
k1=1
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n, a[100000];
int even() {
int res = 0;
int sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 0) {
if (sum <= 0) {
res += -sum + 1;
sum = 1;
}
} else {
if (sum >= 0) {
res += sum + 1;
sum = -1;
}
}
}
}
int odd() {
int res = 0;
int sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 0) {
if (sum >= 0) {
res += sum + 1;
sum = -1;
}
} else {
if (sum <= 0) {
res += -sum + 1;
sum = 1;
}
}
}
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
cout << min(even(), odd()) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
long long a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long sum = 0;
long long res1 = 0;
long long res2 = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
if (sum + a[i] > 0) {
sum += a[i];
} else {
res1 = res1 + 1 - (sum + a[i]);
sum = 1;
}
} else {
if (sum + a[i] < 0) {
sum += a[i];
} else {
res1 = res1 + (sum + a[i]) + 1;
sum = -1;
}
}
}
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
if (sum + a[i] < 0) {
sum += a[i];
} else {
res2 = res2 + 1 + sum;
a[i];
sum = -1;
}
} else {
if (sum + a[i] > 0) {
sum += a[i];
} else {
res2 = res2 + 1 - sum - a[i];
sum = 1;
}
}
}
cout << min(res1, res2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
void answer1() {
cin.tie(0);
ios_base::sync_with_stdio(false);
int n;
cin >> n;
vector<int> a(n);
for (int& a_i : a) {
cin >> a_i;
}
long count = 0;
long sum = 0;
long count2 = 0;
long sum2 = 0;
bool is_positive = a.at(0) > 0;
for (int i = 0; i < a.size(); i++) {
sum += a.at(i);
if (is_positive) {
if (sum <= 0) {
long diff = 1 - sum;
count += diff;
sum += diff;
}
if (sum2 >= 0) {
long diff = 1 + sum2;
count2 += diff;
sum2 -= diff;
}
} else {
if (sum >= 0) {
long diff = 1 + sum;
count += diff;
sum -= diff;
}
if (sum2 <= 0) {
long diff = 1 - sum2;
count2 += diff;
sum2 += diff;
}
}
is_positive = !is_positive;
}
cout << min(count, count2) << endl;
}
int main() { answer1(); }
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
long long sum;
cin >> sum;
long long delta = 0;
for (int i = 1; i < N; ++i) {
int temp;
cin >> temp;
if (sum > 0 && sum + temp >= 0) {
delta += sum + temp + 1;
sum = -1;
} else if (sum < 0 && sum + temp <= 0) {
delta += 1 - (sum + temp);
sum = 1;
} else {
sum += temp;
}
}
cout << delta;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <bits/stdc++.h>
int main(void) {
int n, ans = 0, tmp = 0;
scanf("%d", &n);
int a[n], sum[n];
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for (int i = 0; i < n; i++) {
sum[i] = sum[i - 1] + a[i];
sum[0] = a[0];
if (i % 2 == 0) {
if (sum[i] <= 0) {
tmp += -1 * sum[i] + 1;
sum[i] = 1;
}
} else {
if (sum[i] >= 0) {
tmp += sum[i] + 1;
sum[i] = -1;
}
}
}
for (long long i = 0; i < n; i++) {
sum[i] = sum[i - 1] + a[i];
sum[0] = a[0];
if (i % 2 == 0) {
if (sum[i] >= 0) {
ans += sum[i] + 1;
sum[i] = -1;
}
} else {
if (sum[i] <= 0) {
ans += -1 * sum[i] + 1;
sum[i] = 1;
}
}
}
if (tmp < ans) {
ans = tmp;
}
printf("%d\n", ans);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
A = list(map(int, input().split()))
def sol(S):
ret = 0
B = [S]
for a in A[1:]:
b = a
if S * (S + b) > 0:
b = (abs(S) + 1) * (1 if S < 0 else -1)
if S + b == 0:
b = b - 1 if b < 0 else b + 1
ret += abs(b - a)
S += b
B.append(b)
return ret
ans = min(
sol(A[0]),
sol(-A[0] // abs(A[0])) + abs(A[0]) + 1
)
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
std::vector<int> seq, sum;
int main() {
int n;
std::cin >> n;
seq.resize(n);
sum.resize(n);
std::cin >> seq[0];
sum[0] = seq[0];
for (int i = 1; i < n; i++) {
std::cin >> seq[i];
sum[i] = sum[i - 1] + seq[i];
}
bool is_plus = (sum[0] > 0);
long ans = 0;
int dif = 0;
for (int i = 1; i < sum.size(); i++) {
if (is_plus && sum[i] + dif >= 0) {
int tmp = -(sum[i] + dif) - 1;
dif += tmp;
ans += (tmp < 0 ? -tmp : tmp);
} else if (!is_plus && sum[i] + dif <= 0) {
int tmp = 1 - (sum[i] + dif);
dif += tmp;
ans += (tmp < 0 ? -tmp : tmp);
}
is_plus = !is_plus;
}
std::cout << ans << std::endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> A(n);
for (int i = 0; i < n; i++) {
cin >> A[i];
}
long ans = 0;
if (A[0] == 0) {
if (0 < A[1])
A[0]--;
else
A[0]++;
ans++;
}
long acc = 0;
int psign = 0;
for (int i = 0; i < n; i++) {
acc += A[i];
int sign = (acc < 0) ? -1 : 1;
if (acc == 0 || psign == sign) {
int tmp = abs(acc) + 1;
ans += tmp;
acc -= psign * tmp;
psign = -sign;
} else {
psign = 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;
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T>
inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
template <class T>
inline T sqr(T x) {
return x * x;
}
const double EPS = 1e-10;
const double PI = acos(-1.0);
pair<long long, long long> maxP(vector<long long> a, long long size) {
pair<long long, long long> p;
long long Max = a[0];
long long place = 0;
for (int i = (0); i < (size); ++i) {
if (a[i] > Max) {
Max = a[i];
place = i;
}
}
p.first = Max;
p.second = place;
return p;
}
pair<long long, long long> minP(vector<long long> a, long long size) {
pair<long long, long long> p;
long long min = a[0];
long long place = 0;
for (int i = (0); i < (size); ++i) {
if (a[i] < min) {
min = a[i];
place = i;
}
}
p.first = min;
p.second = place;
return p;
}
long long sumL(vector<long long> a, long long size) {
long long sum = 0;
for (int i = (0); i < (size); ++i) {
sum += a[i];
}
return sum;
}
long long counT(vector<long long> a, long long t) {
sort(a.begin(), a.end());
return upper_bound(a.begin(), a.end(), t) -
lower_bound(a.begin(), a.end(), t);
}
long long DIV[1000 + 1][1000 + 1];
void divide(long long n, long long m) {
DIV[0][0] = 1;
for (int i = (1); i < (n + 1); ++i) {
DIV[i][0] = 0;
}
for (int i = (0); i < (n + 1); ++i) {
DIV[i][1] = 1;
}
for (int i = (1); i < (m + 1); ++i) {
for (int t = (0); t < (n + 1); ++t) {
if (DIV[t][i] > 0) continue;
if (t >= i) {
DIV[t][i] = DIV[t - i][i] + DIV[t][i - 1];
} else {
DIV[t][i] = DIV[t][i - 1];
}
}
}
}
bool IsPrime(int num) {
if (num < 2)
return false;
else if (num == 2)
return true;
else if (num % 2 == 0)
return false;
double sqrtNum = sqrt(num);
for (int i = 3; i <= sqrtNum; i += 2) {
if (num % i == 0) {
return false;
}
}
return true;
}
class UnionFind {
public:
vector<long long> par;
vector<long long> rank;
UnionFind(long long N) : par(N), rank(N) {
for (int i = (0); i < (N); ++i) par[i] = i;
for (int i = (0); i < (N); ++i) rank[i] = 0;
}
~UnionFind() {}
long long root(long long x) {
if (par[x] == x)
return x;
else {
par[x] = root(par[x]);
return par[x];
}
}
void unite(long long x, long long y) {
long long rx = root(x);
long long ry = root(y);
if (rx == ry) return;
if (rank[rx] < rank[ry]) {
par[rx] = ry;
} else {
par[ry] = rx;
if (rank[rx] == rank[ry]) {
rank[rx]++;
}
}
}
bool same(long long x, long long y) {
long long rx = root(x);
long long ry = root(y);
return rx == ry;
}
};
class BFS_shortestDistance {
public:
BFS_shortestDistance(vector<vector<char> > p_, long long h_, long long w_) {
p = p_;
h = h_;
w = w_;
initial_number = h * w * 2;
for (int i = (0); i < (h); ++i) {
vector<long long> k(w);
for (int t = (0); t < (w); ++t) k[t] = initial_number;
field.push_back(k);
}
}
vector<vector<char> > p;
long long h;
long long w;
long long initial_number;
vector<vector<long long> > field;
pair<long long, long long> plus(pair<long long, long long> &a,
pair<long long, long long> &b) {
pair<long long, long long> p;
p.first = a.first + b.first;
p.second = a.second + b.second;
return p;
}
bool equal(pair<long long, long long> &a, pair<long long, long long> &b) {
return (a.first == b.first && a.second == b.second);
}
bool is_in_field(int h, int w, const pair<long long, long long> &point) {
const int c = point.second;
const int r = point.first;
return (0 <= c && c < w) && (0 <= r && r < h);
}
void init() {
for (int i = (0); i < (field.size()); ++i) {
for (int t = (0); t < (field[i].size()); ++t) {
field[i][t] = initial_number;
}
}
}
void shortest(long long sy, long long sx) {
init();
pair<long long, long long> c[4];
c[0].first = 0;
c[0].second = 1;
c[1].first = 0;
c[1].second = -1;
c[2].first = 1;
c[2].second = 0;
c[3].first = -1;
c[3].second = 0;
queue<pair<long long, long long> > Q;
pair<long long, long long> s;
s.first = sy;
s.second = sx;
field[sy][sx] = 0;
Q.push(s);
while (Q.empty() == false) {
pair<long long, long long> now = Q.front();
Q.pop();
for (int u = 0; u < 4; u++) {
pair<long long, long long> x = c[u];
pair<long long, long long> next = plus(now, x);
if (is_in_field(h, w, next)) {
if (p[next.first][next.second] == '.') {
if (field[next.first][next.second] == initial_number) {
field[next.first][next.second] = field[now.first][now.second] + 1;
Q.push(next);
} else {
}
}
}
}
}
}
};
bool Ischanged(long long a, long long b) {
if (a * b < 0) {
return true;
} else {
return false;
}
}
int main() {
long long n;
cin >> n;
vector<long long> a(n);
for (int i = (0); i < (n); ++i) cin >> a[i];
long long sum = 0;
long long count = 0;
for (int i = (0); i < (n); ++i) {
if (i == 0) {
sum += a[i];
if (sum == 0 && n != 1) {
if (a[1] >= 0) {
sum = -1;
} else {
sum = 1;
}
count++;
} else if (sum == 0 && n == 1) {
count++;
}
} else {
long long was = sum;
sum += a[i];
if (Ischanged(was, sum)) {
continue;
} else {
if (sum < 0) {
count += abs(sum) + 1;
sum = 1;
} else {
count += abs(sum) + 1;
sum = -1;
}
}
}
}
cout << count << 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())
l=list(map(int,input().split()))
if l[0]!=0:
count=0
suml=l[0]
for i in range(1,n):
if suml>0 and suml+l[i]<0:
suml+=l[i]
elif suml<0 and suml+l[i]>0:
suml+=l[i]
elif suml>0 and suml+l[i]>=0:
count+=suml+l[i]+1
suml=-1
else:
count+=-suml-l[i]+1
suml=1
a1=count
count=abs(l[0])+1
suml=-l[0]
for i in range(1,n):
if suml>0 and suml+l[i]<0:
suml+=l[i]
elif suml<0 and suml+l[i]>0:
suml+=l[i]
elif suml>0 and suml+l[i]>=0:
count+=suml+l[i]+1
suml=-1
else:
count+=-suml-l[i]+1
suml=1
a2=count
print(min(a1,a2))
else:
count=1
suml1=1
for i in range(1,n):
if suml1>0 and suml1+l[i]<0:
suml1+=l[i]
elif suml1<0 and suml1+l[i]>0:
suml1+=l[i]
elif suml1>0 and suml1+l[i]>=0:
count+=suml1+l[i]+1
suml1=-1
else:
count+=-suml1-l[i]+1
suml1=1
k1=count
count=1
suml2=-1
for i in range(1,n):
if suml2>0 and suml2+l[i]<0:
suml2+=l[i]
elif suml2<0 and suml2+l[i]>0:
suml2+=l[i]
elif suml2>0 and suml2+l[i]>=0:
count+=suml2+l[i]+1
suml2=-1
else:
count+=-suml2-l[i]+1
suml2=1
k2=count
print(min(k1,k2))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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 = Integer.parseInt(sc.nextLine());
long now = sc.nextInt();
long ans = now == 0 ? 1 : 0;
for(int i = 1; i < N; i++){
int A = sc.nextInt();
if(now > 0){
if(now + A >= 0){
ans += now+A+1;
now = -1;
}else{
now = now+A;
}
}else if(now < 0){
if(now + A <= 0){
ans += Math.abs(now+A)+1;
now = 1;
}else{
now = now+A;
}
}else{
if(A < 0){
ans++;
now++;
}else if(A > 0){
ans++;
now--;
}else{
ans+=2;
}
}
}
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 | cpp | #include <bits/stdc++.h>
using namespace std;
#define forx(i,a,b) for(int i=(a);i<(b);i++)
#define rep(j,n) for(int j=0;j<(n);j++)
typedef long long ll;
int main()
{
int n,ansa=0,ansb=0,sum=0;
cin>>n;
bool plus=true;
rep(i,n){
int a;
cin>>a;
while(plus&&sum+a<=0){
a++;
ansa++;
}
while(!plus&&sum+a>=0){
a--;
ansa++;
}
sum+=a;
plus=!plus
}
plus=false;
sum=0;
rep(i,n){
int a;
cin>>a;
while(plus&&sum+a>=0){
a++;
ansb++;
}
while(!plus&&sum+a<=0){
a--;
ansb++;
}
sum+=a;
plus=!plus
}
cout<<min(ansa,ansb)<<endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | _,*A=map(int,open(0).read().split())
def is_plus(x):
return 0 <= x
total_cnt=1 if A[0]==0 else 0
cur_sum=A[0]+total_cnt
pre_sum=cur_sum
for a in A[1:]:
cur_sum+=a
if is_plus(pre_sum)==is_plus(cur_sum) or cur_sum==0:
total_cnt+=abs(cur_sum)+1
cur_sum=-1 if is_plus(cur_sum) else +1
pre_sum=cur_sum
print(total_cnt) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
for (int i = (0); i < (n); ++i) cin >> a[i];
long long sum1, sum2;
long long ans1 = 0;
long long ans2 = 0;
for (int i = 0; i < n; i++) {
sum1 += a[i];
sum2 += a[i];
if (i % 2) {
ans1 += max((long long)1 - sum1, (long long)0);
sum1 = max((long long)1, sum1);
ans2 += max((long long)1 + sum2, (long long)0);
sum2 = min((long long)-1, sum2);
} else {
ans1 += max((long long)1 + sum1, (long long)0);
sum1 = min((long long)-1, sum1);
ans2 += max((long long)1 - sum2, (long long)0);
sum2 = max((long long)1, sum2);
}
}
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 | java |
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Main {
public static void main(final String[] args) {
final Scanner scanner = new Scanner(System.in);
final int n = scanner.nextInt();
final List<Long> list = IntStream.range(0, n)
.mapToObj(i -> scanner.nextLong())
.collect(Collectors.toList());
final long answer;
if (list.get(0) != 0) {
answer = sum(list.get(0), 0, list);
} else {
answer = Math.min(sum(1, 1, list), sum(-1, 1, list));
}
System.out.println(answer);
}
private static long sum(final long firstValue, final long firstCost, final List<Long> list) {
long sum1 = firstValue, sum2 = firstValue;
long cost1 = firstCost, cost2 = firstCost;
long sign1 = 1, sign2 = -1;
for (int i = 1; i < list.size(); i++) {
final long a = list.get(i);
sum1 += a;
sum2 += a;
if (sign1 == 1 && sum1 < 1) {
cost1 += 1 - sum1;
sum1 = 1;
} else if (sign1 == -1 && sum1 > -1) {
cost1 += sum1 + 1;
sum1 = -1;
}
if (sign2 == 1 && sum2 < 1) {
cost2 += 1 - sum2;
sum2 = 1;
} else if (sign2 == -1 && sum2 > -1) {
cost2 += sum2 + 1;
sum2 = -1;
}
sign1 *= -1;
sign2 *= -1;
}
return Math.min(cost1, cost2);
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 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];
}
for (int i = 1; i < n; i++) {
N[i] = N[i] + N[i - 1];
}
int ans = 0;
if (N[0] == 0) {
if (N[1] <= 0) {
for (int i = 0; i < n; i++) {
N[i]++;
ans = 1;
}
} else {
for (int i = 0; i < n; i++) {
N[i] = N[i] - 1;
}
ans = 1;
}
}
for (int i = 1; i < n; i++) {
if (N[i - 1] < 0) {
if (N[i] <= 0) {
ans = ans - N[i] + 1;
for (int j = i; j < n; j++) {
N[j] = N[j] - N[i] + 1;
}
}
}
if (N[i - 1] > 0) {
if (N[i] >= 0) {
ans = ans + N[i] + 1;
for (int j = i; j < n; j++) {
N[j] = N[j] - N[i] - 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 | python3 | n = int(input())
a = list(map(int, input().split()))
if a[0] >= 0:
flag = 1
else:
flag = -1
ans = 0
dp = [0 for i in range(n)]
dp[0] = a[0]
for i in range(1, n):
dp[i] = dp[i - 1] + a[i]
if flag == 1 and dp[i] >= 0:
ans += abs(-1 - dp[i])
dp[i] = -1
elif flag == -1 and dp[i] <= 0:
ans += (1 - dp[i])
dp[i] = 1
flag *= -1
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python2 | # -*- coding:utf-8 -*-
n = int(raw_input())
numlist = (raw_input()).split(' ')
count = 0
i = 0
while (int(numlist[i]) == 0):
i += 1
if (int(numlist[0]) == 0):
if (int(numlist[i]) > 0):
numlist[0] = (-1) ** i
else:
numlist[0] = (-1) ** (i+1)
count += 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;
bool debug = false;
int main() {
int n;
long long a[100005];
long long cnt = 0;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
long long sum = a[0] + a[1];
bool plus;
if (sum >= 0)
plus = true;
else
plus = false;
for (int i = 2; i < n; i++) {
sum += a[i];
if (debug) cout << "sum:" << sum << endl;
if (plus) {
if (sum >= 0) {
cnt += sum + 1;
sum = -1;
}
plus = false;
} else {
if (sum <= 0) {
cnt += abs(sum) + 1;
sum = 1;
}
plus = true;
}
}
if (sum == 0)
cout << cnt + 1 << endl;
else
cout << cnt << endl;
return 0;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.