Search is not available for this dataset
name
stringlengths 2
88
| description
stringlengths 31
8.62k
| public_tests
dict | private_tests
dict | solution_type
stringclasses 2
values | programming_language
stringclasses 5
values | solution
stringlengths 1
983k
|
---|---|---|---|---|---|---|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
i = 0
sum_before = 0
sum_after = 0
count = 0
while i < n:
sum_after = sum_before + a[i]
if sum_after * sum_before > 0 or sum_after == 0:
if sum_after < 0:
a[i] = a[i] - sum_after + 1
elif sum_after > 0:
a[i] = a[i] - sum_after - 1
elif sum_before < 0:
a[i] += 1
else:
a[i] -= 1
count += abs(sum_after) + 1
sum_after = sum_before + a[i]
i += 1
sum_before = sum_after
#print(a)
print(count)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int a[maxn];
int main() {
int n;
long long sum;
scanf("%d", &n);
scanf("%d", &a[0]);
sum = a[0];
long long cnt = 0;
for (int i = 1; i < n; i++) {
scanf("%d", &a[i]);
if (sum > 0) {
int t = sum + a[i];
if (t < 0)
sum = t;
else {
int b = abs(t + 1);
cnt += b;
sum = -1;
a[i] -= b;
}
} else if (sum < 0) {
int t = sum + a[i];
if (t > 0)
sum = t;
else {
int b = abs(1 - t);
cnt += b;
sum = 1;
a[i] += b;
}
}
}
printf("%lld\n", cnt);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
vector<long long> v(n);
for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n));
i += 1 - 2 * ((0) > (n)))
cin >> v[i];
long long res1 = 0, res2 = 0, res3, res4;
long long som = v[0];
if (som >= 0) {
for (__typeof(n) i = (1) - ((1) > (n)); i != (n) - ((1) > (n));
i += 1 - 2 * ((1) > (n))) {
som = som + v[i];
if (i % 2 == 1)
if (som < 0)
continue;
else {
res1 += som + 1;
som = -1;
}
else if (som > 0)
continue;
else {
res1 += 1 - som;
som = 1;
}
}
res2 = v[0] + 1;
som = -1;
for (__typeof(n) i = (1) - ((1) > (n)); i != (n) - ((1) > (n));
i += 1 - 2 * ((1) > (n))) {
som = som + v[i];
if (i % 2 == 0)
if (som < 0)
continue;
else {
res2 += som + 1;
som = -1;
}
else if (som > 0)
continue;
else {
res2 += 1 - som;
som = 1;
}
}
} else {
for (__typeof(n) i = (1) - ((1) > (n)); i != (n) - ((1) > (n));
i += 1 - 2 * ((1) > (n))) {
som = som + v[i];
if (i % 2 == 0)
if (som < 0)
continue;
else {
res1 += som + 1;
som = -1;
}
else if (som > 0)
continue;
else {
res1 += 1 - som;
som = 1;
}
}
res2 = 1 - v[0];
som = 1;
for (__typeof(n) i = (1) - ((1) > (n)); i != (n) - ((1) > (n));
i += 1 - 2 * ((1) > (n))) {
som = som + v[i];
if (i % 2 == 1)
if (som < 0)
continue;
else {
res2 += som + 1;
som = -1;
}
else if (som > 0)
continue;
else {
res2 += 1 - som;
som = 1;
}
}
}
cout << min(res1, res2);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | package main
import (
"fmt"
"math"
)
func main() {
var n int64
fmt.Scan(&n)
var counter1, counter2 int64 = 0, 0
var total1, total2 int64 = 0, 0
var a int64
for i := 0; i < int(n); i++ {
fmt.Scan(&a)
total1 += a
total2 += a
if i%2 == 0 {
if total1 <= 0 {
counter1 += int64(math.Abs(float64(total1))) + 1
total1 = 1
}
} else {
if total1 >= 0 {
counter1 += int64(math.Abs(float64(total1))) + 1
}
}
if i%2 == 0 {
if total2 >= 0 {
counter2 += int64(math.Abs(float64(total2))) + 1
total2 = -1
}
} else {
if total2 <= 0 {
counter2 += int64(math.Abs(float64(total2))) + 1
total2 = 1
}
}
}
if counter1 < counter2 {
fmt.Println(counter1)
} else {
fmt.Println(counter2)
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 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 c1 = 0, s1 = 0;
long long c2 = 0, s2 = 0;
for (int i = 0; i < n; i++) {
s1 += a[i];
s2 += a[i];
if (i % 2 == 0) {
if (s1 <= 0) {
c1 += 1 - s1;
s1 = 1;
}
if (s2 >= 0) {
c2 += s2 + 1;
s2 = -1;
}
} else {
if (s1 >= 0) {
c1 += s1 + 1;
s1 = -1;
}
if (s2 <= 0) {
c2 += 1 - s2;
s2 = 1;
}
}
}
printf("%d\n", min(c1, c2));
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
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];
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;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n, k = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long sum = a[0], ans = 0;
if (sum >= 0) {
k = 1;
}
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 += (-1 * sum) + 1;
sum = 1;
}
}
if (k == 0)
k = 1;
else
k = 0;
}
long long kk = 1;
long long su = a[0], an = 0;
if (su >= 0) {
kk = 0;
}
for (int i = 1; i < n; i++) {
su += a[i];
if (kk == 1) {
if (su >= 0) {
an += su + 1;
su = -1;
}
} else {
if (su <= 0) {
an += (-1 * su) + 1;
su = 1;
}
}
if (kk == 0)
kk = 1;
else
kk = 0;
}
cout << min(ans, an) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 30;
const long long LINF = 1LL << 50;
const int NIL = -1;
const int MAX = 10000;
const int mod = 1000000007;
const double pi = 3.141592653589;
int f(int x, vector<int> a) {
int sum = 0, cost = 0;
for (int i = 0; i < a.size(); i++) {
sum += a[i];
if (i % 2 == x) {
if (sum <= 0) {
cost += 1 - sum;
sum = 1;
}
} else {
if (sum >= 0) {
cost += 1 + sum;
sum = -1;
}
}
}
return cost;
}
int main() {
int N;
cin >> N;
vector<int> a(N);
for (int i = 0; i < N; i++) cin >> a[i];
cout << min(f(0, a), f(1, a)) << '\n';
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
int sum = 0, prev = 0;
bool flg = true;
for (auto i = 0; i < n; i++) {
cin >> a[i];
}
int ans = INT_MAX, count, tmp;
for (auto i = 0; i < 2; i++) {
bool plus = (i % 2 == 0);
sum = 0;
count = 0;
for (auto j = 0; j < n; j++) {
if ((plus and sum + a[j] > 0) or (not plus and sum + a[j] < 0))
tmp = a[j];
else {
if (plus)
tmp = 1 - sum;
else
tmp = -1 - sum;
count += abs(tmp - a[j]);
}
sum += tmp;
plus = not plus;
}
if (ans > count) ans = count;
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long modpow(long long x, long long y,
long long m = (long long)(1000000007)) {
long long res = 1;
while (y) {
if (y % 2) {
res *= x;
res %= m;
}
x = x * x % (long long)(1000000007);
y /= 2;
}
return res;
}
bool prime(long long x) {
for (long long i = 2; i <= sqrt(x); i++) {
if (!(x % i)) {
return false;
}
}
return true;
}
double kyori(pair<long long, long long> f, pair<long long, long long> s) {
double ans = 0;
double t = fabs(f.first - s.first);
double y = fabs(f.second - s.second);
ans = sqrt(t * t + y * y);
return ans;
}
long long gcd(long long x, long long y) {
if (y == 0) {
return x;
}
return gcd(y, x % y);
}
long long n, m, a[100004], wa, ans;
signed main() {
cin >> n;
for (long long i = 0; i < n; i++) {
cin >> a[i];
}
wa = a[0];
for (long long i = 1; i < n; i++) {
if (wa < 0) {
if (abs(wa) < a[i]) {
} else {
ans += abs(wa) + 1 - a[i];
a[i] = 0 - wa + 1;
}
} else {
if (a[i] < 0) {
if (abs(a[i]) > wa) {
} else {
ans += 0 - wa - 1 - a[i];
a[i] = 0 - wa - 1;
}
} else {
ans += a[i] - (0 - wa - 1);
a[i] = 0 - wa - 1;
}
}
wa += a[i];
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N=int(input())
a=input().split()
ma=list(map(int,a))
A1=ma[0]
if ma[0]>0:
NEXT=-1-ma[0]
elif ma[0]<0:
NEXT=1-ma[0]
ans=0
ans1=0
for k in range(1,N):
if ma[0] > 0:
if k%2 ==0:
if NEXT<=ma[k]:
NEXT=NEXT-ma[k]-2
else:
ans+=abs(ma[k]-NEXT)
NEXT=-2
elif k%2 ==1:
if NEXT>=ma[k]:
NEXT=NEXT-ma[k]+2
else:
ans+=abs(ma[k]-NEXT)
NEXT=2
elif ma[0]<0:
if k%2 ==1:
if NEXT<=ma[k]:
NEXT=NEXT-ma[k]-2
else:
ans+=abs(ma[k]-NEXT)
NEXT=-2
elif k%2 ==0:
if NEXT>=ma[k]:
NEXT=NEXT-ma[k]+2
else:
ans+=abs(ma[k]-NEXT)
NEXT=2
for k in range(1,N):
if ma[0] < 0:
if k%2 ==0:
if NEXT<=ma[k]:
NEXT=NEXT-ma[k]-2
else:
ans1+=abs(ma[k]-NEXT)
NEXT=-2
elif k%2 ==1:
if NEXT>=ma[k]:
NEXT=NEXT-ma[k]+2
else:
ans1+=abs(ma[k]-NEXT)
NEXT=2
elif ma[0]>0:
if k%2 ==1:
if NEXT<=ma[k]:
NEXT=NEXT-ma[k]-2
else:
ans1+=abs(ma[k]-NEXT)
NEXT=-2
elif k%2 ==0:
if NEXT>=ma[k]:
NEXT=NEXT-ma[k]+2
else:
ans1+=abs(ma[k]-NEXT)
NEXT=2
print(min(ans1,ans)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N=int(input())
s=list(map(int,input().split()))
if s[0]<0:
t=1
elif s[0]>0:
t=-1
ss=s[0]
w=0
for i in range(N-1):
if t==1:
if ss+s[i+1]>=t:
ss=ss+s[i+1]
pass
else:
w+=t-ss-s[i+1]
ss=1
t=-1
elif t==-1:
if ss+s[i+1]<=t:
ss=ss+s[i+1]
pass
else:
w+=ss+s[i+1]-t
ss=-1
t=1
print(w)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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
sum_a = a[0]
if sum_a > 0:
for i in range(1,n):
sum_a += a[i]
if i%2 == 1 and sum_a >= 0:
ans += sum_a + 1
sum_a = -1 #いや違くね?
elif i%2 == 0 and sum_a <= 0:
ans += -sum_a + 1
sum_a = 1
else:
for i in range(1,n):
sum_a += a[i]
if i%2 == 0 and sum_a >= 0:
ans += sum_a + 1
sum_a = -1 #いや違くね?
elif i%2 == 1 and sum_a <= 0:
ans += -sum_a + 1
sum_a = 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;
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
static const int NIL = -1;
int n;
void printArray(int array[], int n) {
for (int i = (0); i < (n); ++i) {
if (i) cout << " ";
cout << array[i];
}
cout << endl;
}
int sequence(int* a, bool sign) {
int sum = a[0], cnt = 0;
for (int i = (1); i < (n); ++i) {
if (sign) {
sum += a[i];
if (sum > 0) {
int rem = abs(-1 - sum);
cnt += rem;
sum = -1;
}
sign = false;
} else {
sum += a[i];
if (sum < 0) {
int rem = abs(1 - sum);
cnt += rem;
sum = 1;
}
sign = true;
}
}
if (sum == 0) cnt++;
return cnt;
}
int main(int argc, char const* argv[]) {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> n;
int a[n];
for (int i = (0); i < (n); ++i) cin >> a[i];
int pos = sequence(a, true);
int neg = sequence(a, false);
cout << min(pos, neg) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
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];
cnt = 0;
long long sum = a[0] + a[1];
if (sum <= 0) {
cnt += abs(sum) + 1;
sum = 1;
}
bool plus = true;
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) cnt += 1;
long long tmp = cnt;
cnt = 0;
sum = a[0] + a[1];
if (sum >= 0) {
cnt += sum + 1;
sum = -1;
}
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) cnt += 1;
if (tmp < cnt)
cout << tmp << endl;
else
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 operation(vector<int> a) {
int opeCount = 0;
int preSum = 0;
int sum = 0;
int i = 0;
while (i < a.size()) {
if (i == 0) {
if (a[i] == 0) {
if (a[i + 1] >= 0) {
a[i] = a[i] - 1;
opeCount++;
} else {
a[i] = a[i] + 1;
opeCount++;
}
}
preSum = a[i];
i++;
} else {
sum = preSum + a[i];
if (sum == 0) {
if (preSum > 0) {
a[i] = a[i] - 1;
opeCount++;
} else {
a[i] = a[i] + 1;
opeCount++;
}
} else {
if (sum > 0 && preSum > 0) {
a[i] = a[i] - 1;
opeCount++;
} else if (sum < 0 && preSum < 0) {
a[i] = a[i] + 1;
opeCount++;
} else {
preSum = sum;
i++;
}
}
}
}
return opeCount;
}
int main() {
int n, ai;
cin >> n;
vector<int> a;
for (int i = 0; i < n; ++i) {
cin >> ai;
a.push_back(ai);
}
int result = operation(a);
cout << result << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = (0); i < (n); ++i) cin >> a[i];
ll sum = a[0];
ll ans = 0;
for (int i = (1); i < (n); ++i) {
sum += a[i];
if (sum * (sum - a[i]) < 0)
;
else {
if (sum == 0) {
if (sum - a[i] < 0) {
sum = 1;
ans++;
} else {
ans++;
sum = -1;
}
} else if (sum > 0) {
ans += sum + 1;
sum = -1;
} else {
ans += -sum + 1;
sum = 1;
}
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
scanf("%d", &n);
vector<long long> a;
for (int i = 0; i < n; i++) {
long long an;
scanf("%lld", &an);
a.push_back(an);
}
if (a[0] != 0) {
long long op_count = 0;
long long now_sum = 0;
long long adding = a[0] > 0 ? -1 : 1;
for (int i = 0; i < n; i++) {
now_sum += a[i];
adding *= -1;
if (now_sum == 0) {
a[i] += adding;
now_sum += adding;
op_count++;
continue;
}
if (adding > 0) {
const long long last = 1 - now_sum;
if (last > 1) {
a[i] += last;
now_sum += last;
op_count += abs(last);
}
} else {
const long long last = -1 - now_sum;
if (last < -1) {
a[i] += last;
now_sum += last;
op_count += abs(last);
}
}
}
printf("%lld\n", op_count);
} else {
int n_copy = n;
vector<int> a_copy;
copy(a.begin(), a.end(), a_copy.begin());
long long final_op_count = INT_MAX;
for (int a0 = -1; a0 >= 1; a0 += 2) {
a[0] = a0;
n = n_copy;
copy(a_copy.begin(), a_copy.end(), a.begin());
long long op_count = 0;
long long now_sum = 0;
long long adding = a[0] > 0 ? -1 : 1;
for (int i = 0; i < n; i++) {
now_sum += a[i];
adding *= -1;
if (now_sum == 0) {
a[i] += adding;
now_sum += adding;
op_count++;
continue;
}
if (adding > 0) {
const long long last = 1 - now_sum;
if (last > 1) {
a[i] += last;
now_sum += last;
op_count += abs(last);
}
} else {
const long long last = -1 - now_sum;
if (last < -1) {
a[i] += last;
now_sum += last;
op_count += abs(last);
}
}
}
if (op_count < final_op_count) {
final_op_count = op_count;
}
}
printf("%lld\n", final_op_count);
}
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define INF 1<<50
#define leading zero str.erase(0, min(str.find_first_not_of('0'), str.size()-1));
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
string text="abcdefghijklmnopqrstuvwxyz";
const int maxn=1e6+7;
// .--------------.
// | Try First One|
// '--------------'
// | .--------------.
// | | |
// V V |
// .--------------. |
// | AC. |<---. |
// '--------------' | |
// (True)| |(False) | |
// .--------' | | |
// | V | |
// | .--------------. | |
// | | Try Again |----' |
// | '--------------' |
// | |
// | .--------------. |
// '->| Try Next One |-------'
// '--------------'
ll bin_pow(ll a,ll b,ll m)
{
ll res=1;
a%=m;
while(b>0)
{
if(b&1)
res=res*a%m;
b>>=1;
a=a*a%m;
}
return res;
}
bool miller_rabin(ll d,ll n)
{
ll a=2+rand()%(n-4);
ll x=bin_pow(a,d,n);
if(x==1 || x==n-1)
return true;
while(d!=n-1)
{
x=(x*x)%n;
d*=2;
if(x==1)
return false;
if(x==n-1)
return true;
}
return false;
}
bool prime(ll n,ll k)
{
if(n==1 || n==4)
return false;
if(n<=3)
return true;
ll d=n-1;
while(d%2==0)
d/=2;
for(int i=0; i<k; i++)
{
if(!miller_rabin(d,n))
return false;
}
return true;
}
int n;
string s;
ll ans = 0;
void solve(ll x,ll y){
if(x==n+1){
ans += y;
return;
}
ll cur = 0;
for(ll i=x;i<=n;i++){
cur = (10*cur) + (s[i]-'0');
solve(i+1,y+cur);
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n;
cin>>n;
ll a[n+2];
for(int i=0;i<n;i++)cin>>a[i];
ll sum=0;
ll cnt=0;
ll ans=INF;
for(int i=0;i<n;i++){
sum+=a[i];
if(i%2==0){
if(sum<=0){
cnt+=abs(sum)+1;
sum=1;
}
}
else{
if(sum>=0){
cnt+=abs(sum)+1;
sum=-1;
}
}
}
ans=min(cnt,ans);
sum=0;
cnt=0;
for(int i=0;i<n;i++){
sum+=a[i];
if(i%2==0){
if(sum>=0){
cnt+=abs(sum)+1;
sum=-1;
}
}
else{
if(sum<=0){
cnt+=abs(sum)+1;
sum=1;
}
}
}
cout<<min(ans,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>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using unsi = unsigned;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;
using db = double;
using plex = complex<double>;
using vs = vector<string>;
template <class T>
inline bool amax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
inline bool amin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
struct aaa {
aaa() {
cin.tie(0);
ios::sync_with_stdio(0);
cout << fixed << setprecision(20);
};
} aaaaaaa;
const int INF = 1001001001;
const ll LINF = 1001001001001001001ll;
const int MOD = 1e9 + 7;
const db EPS = 1e-9;
const int dx[] = {1, 1, 0, -1, -1, -1, 0, 1},
dy[] = {0, 1, 1, 1, 0, -1, -1, -1};
signed main() {
long long n;
cin >> n;
long long odd{};
long long ans{};
long long even{};
vector<long long> a(n);
for (auto i = 0; i != n; ++i) {
cin >> a.at(i);
}
if (a[0] < 0) {
for (auto i = 0; odd < n; ++i) {
odd = 2 * i + 1;
while (a[odd] <= 0 && odd < n) {
++a[odd];
++ans;
}
}
for (auto i = 1; even < n; ++i) {
even = 2 * i;
while (a[even] >= 0 && even < n) {
--a[even];
++ans;
}
}
} else if (a[0] >= 0) {
if (a[0] == 0) {
++a[0];
++ans;
}
for (auto i = 0; odd < n; ++i) {
odd = 2 * i + 1;
while (a[odd] >= 0 && odd < n) {
--a[odd];
++ans;
}
}
for (auto i = 1; even < n; ++i) {
even = 2 * i;
while (a[even] <= 0 && even < n) {
++a[even];
++ans;
}
}
}
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;
int sum = 0;
int total = 0;
cin >> n;
vector<int> vect;
bool positive = false;
while (n--) {
int a;
cin >> a;
vect.push_back(a);
}
if (vect[0] + vect[1] == 0) {
if (vect.size() > 2) {
vect[1] += (vect[2] > 0 ? -1 : +1);
total++;
} else {
cout << "1" << endl;
}
}
sum = vect[0] + vect[1];
if (sum < 0)
positive = false;
else
positive = true;
for (int i = 2; i < vect.size(); i++) {
sum += vect[i];
if (positive) {
if (sum >= 0) {
total += abs(sum) + 1;
sum = -1;
}
positive = false;
} else if (!positive) {
if (sum <= 0) {
total += abs(sum) + 1;
sum = 1;
}
positive = true;
}
}
cout << total << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void) {
long long n;
cin >> n;
bool lastPalus = true;
long long a[n];
long long even = 0;
for (long long i = 0; i < n; i++) {
cin >> a[i];
even += i % 2 == 0 ? a[i] : -1 * a[i];
}
long long sum = 0;
long long ans = 0;
for (long long i = 0; i < n; i++) {
if (sum == 0) {
if (a[i] == 0) {
sum += even < 0 ? -1 : 1;
ans++;
} else {
sum += a[i];
}
} else if (sum > 0) {
if (sum + a[i] >= 0) {
ans += abs(sum + a[i]) + 1;
sum = -1;
} else {
sum += a[i];
}
} else {
if (sum + a[i] <= 0) {
ans += abs(sum + a[i]) + 1;
sum = 1;
} else {
sum += a[i];
}
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
int a[100000];
for (long long i = 0; i < n; i++) cin >> a[i];
int ans = 1 << 30;
int sum[100001] = {};
for (long long p = 0; p < 2; p++) {
int cnt = 0;
for (long long i = 0; i < n; i++) {
int border = 1 + (p + i) % 2 * -2;
sum[i + 1] = sum[i] + a[i];
if (border == 1 && sum[i + 1] >= border) continue;
if (border == -1 && sum[i + 1] <= border) continue;
cnt += abs(border - sum[i + 1]);
sum[i + 1] = border;
}
ans = min(ans, cnt);
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
const int MOD = 1e9 + 7;
const int iINF = 2147483647 / 2;
const long long int llINF = 9223372036854775807 / 2;
using namespace std;
using ll = long long int;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vvvl = vector<vector<vector<ll>>>;
bool paircomp(const pair<ll, ll> &a, const pair<ll, ll> &b) {
if (a.first == b.first) return a.second < b.second;
return a.first < b.first;
}
ll POW(ll n, ll m) {
if (m == 0) {
return 1;
} else if (m % 2 == 0) {
ll tmp = POW(n, m / 2);
return (tmp * tmp);
} else {
return (n * POW(n, m - 1));
}
}
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll N;
cin >> N;
vl A(N);
for (ll i = 0; i < (N); i++) cin >> A[i];
ll now = A[0];
ll ans = 0;
for (ll i = (1); i < (N); i++) {
ll next = now + A[i];
if (now * next < 0) {
now = next;
} else if (now > 0 && next > 0) {
ans += next + 1;
next = -1;
now = next;
} else {
ans += 1 - next;
next = 1;
now = next;
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void) {
int n;
cin >> n;
vector<int> A(n), B(n);
for (int i = 0; i < n; i++) {
int a;
cin >> a;
A[i] = a;
B[i] = a;
}
long long ans1 = 0, ans2 = 0;
long long sum = 0;
for (int i = 0; i < n; i++) {
sum += A[i];
if (i % 2 == 0) {
if (sum <= 0) {
A[i] += 1 - sum;
ans1 += 1 - sum;
sum = 1;
}
} else {
if (sum >= 0) {
A[i] += sum + 1;
ans1 += sum + 1;
sum = -1;
}
}
}
sum = 0;
for (int i = 0; i < n; i++) {
sum += B[i];
if (i % 2 == 1) {
if (sum <= 0) {
B[i] += 1 - sum;
ans2 += 1 - sum;
sum = 1;
}
} else {
if (sum > 0) {
B[i] += sum + 1;
ans2 += sum + 1;
sum = -1;
}
}
}
if (ans1 < ans2)
cout << ans1 << endl;
else
cout << ans2 << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | 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++;
}
}
}
cout << res1 << endl;
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 | python3 | N = int(input())
A = list(map(int, input().split()))
cntA, sumA = 0, 0
for i in range(N):
sumA += A[i]
if i % 2 == 0:
if sumA <= 0:
cntA += abs(sumA) + 1
sumA += abs(sumA) + 1
else:
if sumA >= 0:
cntA += abs(sumA) + 1
sumA -= abs(sumA) + 1
cntB, sumB = 0, 0
for i in range(N):
sumB += A[i]
if i % 2 != 0:
if sumB >= 0:
cntB += abs(sumB) + 1
sumB += abs(sumB) + 1
else:
if sumB >= 0:
cntB += abs(sumB) + 1
sumB -= abs(sumB) + 1
print(min(cntA, cntB)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import sys
n = int(input())
a = [int(n) for n in input().split()]
sum = [0]*n
sum[0] = a[0]
ans = 0
for i in range(1,n):
sum[i] = sum[i-1]
while((sum[i]+a[i])*sum[i-1] >= 0):
if(sum[i-1] > 0):
ans+=sum[i-1] + a[i]+1
a[i]-=sum[i-1] + a[i]+1
else:
ans+=1 - sum[i-1] - a[i]
a[i]+=1 - sum[i-1] - a[i]
# print(a)
sum[i] += a[i]
print(ans)
# print(a)
# print(sum)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, a, s = 0, count1 = 0, count2 = 0, i, news;
cin >> n;
vector<long long> as(n);
for (int i = 0; i < n; i++) {
cin >> a;
s += a;
as[i] = s;
}
i = 0;
s = 0;
while (i < n) {
news = as[i] + s;
if (news > -1) {
s -= news + 1;
count1 += news + 1;
}
if (++i >= n) break;
news = as[i] + s;
if (news < 1) {
s += 1 - news;
count1 += 1 - news;
}
i++;
}
i = 0;
s = 0;
while (i < n) {
news = as[i] + s;
if (news < 1) {
s += 1 - news;
count2 += 1 - news;
}
if (++i >= n) break;
news = as[i] + s;
if (news > -1) {
s -= news + 1;
count2 += news + 1;
}
i++;
}
cout << (count1 < count2 ? count1 : count2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
c = 0
i = 0
k = 1
sum = a[0]
while a[i] == 0:
i += 1
if a[i] > 0:
if i % 2 == 1:
a[0] = 1
c = 1
else:
a[0] = -1
c = 1
else:
if i % 2 == 1:
a[0] = -1
c = 1
else:
a[0] = 1
c = 1
i = 1
if a[0] > 0:
while i < n:
if i == 2*k-1:
sum = sum + a[2*k-1]
if sum >= 0:
c = c + sum + 1
sum = -1
else:
pass
i += 1
else:
sum = sum + a[2*k]
if sum <= 0:
c = c - sum + 1
sum = 1
else:
pass
i += 1
k += 1
print(c)
elif a[0] < 0:
while i < n:
if i == 2*k-1:
sum = sum + a[2*k-1]
if sum <= 0:
c = c - sum + 1
sum = 1
else:
pass
i += 1
else:
sum = sum + a[2*k]
while sum >= 0:
c = c + sum + 1
sum = -1
else:
pass
i += 1
k += 1
print(c) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
class ParseError {};
template <typename T>
class Zip {
vector<T> d;
bool flag;
void init() {
sort(d.begin(), d.end());
d.erase(unique(d.begin(), d.end()), d.end());
flag = false;
}
public:
Zip() { flag = false; }
void add(T x) {
d.push_back(x);
flag = true;
}
long long getNum(T x) {
if (flag) init();
return lower_bound(d.begin(), d.end(), x) - d.begin();
}
long long size() {
if (flag) init();
return (long long)d.size();
}
};
long long N, M, K, a, b, c, d, e, H, W, L, T;
long long x, y, z;
long long A[2000004] = {};
long long B[2000004] = {};
long long C[2000004] = {};
long long D[1000006] = {};
long long E[1000006] = {};
bool f;
string S[200000];
string SS;
set<long long> sll;
pair<long long, long long> bufpl;
vector<long long> vl[200005];
vector<long long> vll;
vector<long long> v;
vector<pair<long long, long long>> vpl;
vector<string> vs;
set<long long> llset;
set<string> Sset;
multiset<long long> llmset;
queue<long long> ql;
multiset<pair<long long, long long>> plmset;
typedef struct ST {
long long first;
long long second;
long long cost;
bool operator<(const ST& another) const { return cost < another.cost; };
bool operator>(const ST& another) const { return cost > another.cost; };
} ST;
priority_queue<ST, vector<ST>, greater<ST>> qst;
long long modinv(long long aa, long long mm) {
long long bb = mm, uu = 1, vv = 0;
while (bb) {
long long tt = aa / bb;
aa -= tt * bb;
swap(aa, bb);
uu -= tt * vv;
swap(uu, vv);
}
uu %= mm;
if (uu < 0) uu += mm;
return uu;
}
long long zettai(long long aa) {
if (aa < 0) {
aa *= -1;
}
return aa;
}
float zettai(float aa) {
if (aa < 0) {
aa *= -1;
}
return aa;
}
class UnionFind {
public:
vector<long long> pairent;
vector<long long> depth;
vector<long long> size;
UnionFind(long long Amount)
: pairent(Amount, 1), depth(Amount, 1), size(Amount, 1) {
for (long long i = 0; i < Amount; i++) {
pairent[i] = i;
}
}
long long FindPairent(long long x) {
if (pairent[x] == x)
return x;
else
return pairent[x] = FindPairent(pairent[x]);
}
long long Merge(long long x, long long y) {
x = FindPairent(x);
y = FindPairent(y);
if (x != y) {
if (depth[x] > depth[y]) {
pairent[y] = pairent[x];
return size[x] += size[y];
} else {
pairent[x] = pairent[y];
if (depth[x] == depth[y]) {
depth[y]++;
}
return size[y] += size[x];
}
} else {
return -1;
}
}
bool IsSame(long long x, long long y) {
if (FindPairent(x) == FindPairent(y))
return true;
else
return false;
}
long long GetSize(long long x) {
x = FindPairent(x);
return size[x];
}
};
struct Edge {
long long a, b, cost;
bool operator<(const Edge& other) const { return cost < other.cost; }
};
struct Graph {
long long n;
vector<Edge> es;
};
class Kruskal {
Graph origin_G;
Graph MST;
long long total_cost = 0;
public:
void Solve() {
UnionFind uf = UnionFind(MST.n);
for (long long i = 0; i < origin_G.es.size(); i++) {
long long a = origin_G.es[i].a;
long long b = origin_G.es[i].b;
long long cost = origin_G.es[i].cost;
if (!uf.IsSame(a, b)) {
uf.Merge(a, b);
MST.es.push_back(origin_G.es[i]);
total_cost += cost;
}
}
}
Kruskal(Graph graph) {
origin_G = graph;
MST = graph;
MST.es.clear();
sort(origin_G.es.begin(), origin_G.es.end());
}
long long GetMinCost() { return total_cost; }
};
long long RepeatSquaring(long long N, long long P, long long M) {
if (P == 0) return 1;
if (P % 2 == 0) {
long long t = RepeatSquaring(N, P / 2, M) % M;
return t * t % M;
}
return N * RepeatSquaring(N, P - 1, M) % M;
}
long long GCD(long long a, long long b) {
if (a % b == 0)
return b;
else
return GCD(b, a % b);
}
long long Min(long long a, long long b) {
if (a < b)
return a;
else
return b;
}
long long Max(long long a, long long b) {
if (a > b)
return a;
else
return b;
}
long long Sum(long long a, long long b) { return a + b; }
template <typename T>
class SegmentTree {
long long n;
vector<T> node;
function<T(T, T)> fun, fun2;
bool customChange;
T outValue, initValue;
public:
void init(long long num, function<T(T, T)> resultFunction, T init, T out,
function<T(T, T)> changeFunction = NULL) {
fun = resultFunction;
fun2 = changeFunction;
customChange = changeFunction != NULL;
n = 1;
while (n < num) n *= 2;
node.resize(2 * n - 1);
fill(node.begin(), node.end(), init);
outValue = out;
initValue = init;
}
void valueChange(long long num, T value) {
num += n - 1;
if (customChange)
node[num] = fun2(value, node[num]);
else
node[num] = value;
while (num > 0)
num = (num - 1) / 2,
node[num] = fun(node[num * 2 + 1], node[num * 2 + 2]);
}
T rangeQuery(long long a, long long b, long long l = 0, long long r = -1,
long long k = 0) {
if (r == -1) r = n;
if (a <= l && r <= b) return node[k];
if (b <= l || r <= a) return outValue;
long long mid = (l + r) / 2;
return fun(rangeQuery(a, b, l, mid, 2 * k + 1),
rangeQuery(a, b, mid, r, 2 * k + 2));
}
};
class Combination {
vector<long long> factorial;
vector<long long> factorial_inv;
long long mod;
long long max_n;
public:
void Init(long long init_max_n, long long init_mod) {
max_n = init_max_n;
mod = init_mod;
factorial.resize(max_n + 1);
factorial[0] = 1;
for (long long i = 1; i < factorial.size(); i++) {
factorial[i] = factorial[i - 1] * i;
factorial[i] %= mod;
}
factorial_inv.resize(max_n + 1);
factorial_inv[0] = 1;
for (long long i = 1; i < factorial_inv.size(); i++) {
factorial_inv[i] = factorial_inv[i - 1] * modinv(i, mod);
factorial_inv[i] %= mod;
}
}
long long GetComb(long long n, long long k) {
long long comb = factorial[n];
comb *= factorial_inv[k];
comb %= mod;
comb *= factorial_inv[n - k];
comb %= mod;
return comb;
}
long long GetH(long long n, long long k) {
long long comb = factorial[n + k - 1];
comb *= factorial_inv[n];
comb %= mod;
comb *= factorial_inv[k - 1];
comb %= mod;
return comb;
}
};
class Tree {
long long node_N;
vector<long long> node;
vector<vector<pair<long long, long long>>> pass;
long long diameter = -1;
vector<long long> dist_Diamieter[2];
pair<long long, long long> maxDist_Num;
public:
void Init(long long node_Num) {
node_N = node_Num;
node.resize(node_N + 1);
pass.resize(node_N + 1);
dist_Diamieter[0].resize(node_N + 1);
for (long long i = 0; i < node_N + 1; i++) dist_Diamieter[0][i] = -1;
dist_Diamieter[1].resize(node_N + 1);
for (long long i = 0; i < node_N + 1; i++) dist_Diamieter[1][i] = -1;
}
void AddEdge(long long a, long long b, long long dist) {
bufpl.first = b;
bufpl.second = dist;
pass[a].push_back(bufpl);
bufpl.first = a;
pass[b].push_back(bufpl);
}
void DFS(long long step, long long now, long long dist) {
dist_Diamieter[step][now] = dist;
if (dist_Diamieter[step][now] > maxDist_Num.first) {
maxDist_Num.first = dist_Diamieter[step][now];
maxDist_Num.second = now;
}
for (long long i = 0; i < pass[now].size(); i++) {
long long next_node = pass[now][i].first;
if (dist_Diamieter[step][next_node] == -1) {
DFS(step, next_node, dist + pass[now][i].second);
}
}
}
long long GetDiameter(long long min_node_num) {
if (diameter >= 0)
return diameter;
else {
maxDist_Num.first = -1;
maxDist_Num.second = -1;
DFS(0, min_node_num, 0ll);
long long step2_start = maxDist_Num.second;
maxDist_Num.first = -1;
maxDist_Num.second = -1;
DFS(1, step2_start, 0ll);
diameter = maxDist_Num.first;
return diameter;
}
}
long long GetDistFromMinNode(long long num) { return dist_Diamieter[0][num]; }
};
void Nibu(long long node, long long co) {
C[node] = co % 2;
D[co % 2]++;
for (long long i = 0; i < vl[node].size(); i++) {
long long next = vl[node][i];
if (C[next] == -1) {
Nibu(next, co + 1);
}
}
}
int main() {
cin >> N;
for (long long i = 0; i < N; i++) {
cin >> A[i];
if (i == 0)
B[i] = A[i];
else
B[i] = B[i - 1] + A[i];
}
long long ruiseki = 0;
long long ans = 0;
long long ans2 = 0;
if (B[0] == 0) {
for (long long i = 0; i < N; i++) C[i] = B[i];
B[0] = -1;
C[0] = 1;
for (long long i = 1; i < N; i++) {
if (C[i - 1] + ruiseki < 0) {
if (C[i] + ruiseki <= 0) {
ans2 += 1 - (C[i] + ruiseki);
ruiseki += 1 - (C[i] + ruiseki);
}
} else {
if (C[i] + ruiseki >= 0) {
ans2 += (C[i] + ruiseki) - (-1);
ruiseki -= (C[i] + ruiseki) - (-1);
}
}
}
} else
ans2 = 8223372036854775807ll;
ruiseki = 0;
for (long long i = 1; i < N; i++) {
if (B[i - 1] + ruiseki < 0) {
if (B[i] + ruiseki <= 0) {
ans += 1 - (B[i] + ruiseki);
ruiseki += 1 - (B[i] + ruiseki);
}
} else {
if (B[i] + ruiseki >= 0) {
ans += (B[i] + ruiseki) - (-1);
ruiseki -= (B[i] + ruiseki) - (-1);
}
}
}
cout << min(ans, ans2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int count1 = 0;
int count2 = 0;
vector<long long> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
long long sum = 0;
if (a[0] >= 0) {
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 1) {
while (sum >= 0) {
sum--;
count1++;
}
} else {
while (sum <= 0) {
sum++;
count1++;
}
}
}
} else {
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 0) {
while (sum >= 0) {
sum--;
count1++;
}
} else {
while (sum <= 0) {
sum++;
count1++;
}
}
}
}
if (a[0] >= 0) {
while (a[0] >= 0) {
a[0]--;
count2++;
}
} else {
while (a[0] <= 0) {
a[0]++;
count2++;
}
}
sum = 0;
if (a[0] >= 0) {
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 1) {
while (sum >= 0) {
sum--;
count2++;
}
} else {
while (sum <= 0) {
sum++;
count2++;
}
}
}
} else {
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 0) {
while (sum >= 0) {
sum--;
count2++;
}
} else {
while (sum <= 0) {
sum++;
count2++;
}
}
}
}
if (count1 >= count2)
cout << count2 << endl;
else
cout << count1 << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long int a[n], b[n], s[n], a0;
for (int i = 0; i < n; i++) {
cin >> a[i];
b[i] = a[i];
}
s[0] = a[0];
a0 = a[0];
long long int ans1 = 0, ans2 = 0, tmp;
for (int i = 0; i < n; i++) {
if (i != 0) {
s[i] = s[i - 1] + a[i];
}
tmp = s[i];
if (i % 2 == 0) {
if (tmp <= 0) {
a[i] += abs(tmp) + 1;
s[i] += abs(tmp) + 1;
ans1 += abs(tmp) + 1;
}
} else {
if (tmp >= 0) {
a[i] -= abs(tmp) + 1;
s[i] -= abs(tmp) + 1;
ans1 += abs(tmp) + 1;
}
}
}
s[0] = a0;
for (int i = 0; i < n; i++) {
if (i != 0) {
s[i] = s[i - 1] + b[i];
}
for (int i = 0; i < n; i++) {
cout << s[i] << " ";
}
tmp = s[i];
if (i % 2 != 0) {
if (tmp <= 0) {
b[i] += abs(tmp) + 1;
s[i] += abs(tmp) + 1;
ans2 += abs(tmp) + 1;
}
} else {
if (tmp >= 0) {
b[i] -= abs(tmp) + 1;
s[i] -= abs(tmp) + 1;
ans2 += abs(tmp) + 1;
}
}
}
cout << min(ans1, ans2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list([int(i) for i in input().split()])
sum_a = []
count = 0
sum_a.append(a[0])
if len(a) != 1:
if sum_a[0] == 0:
count += 1
if a[1] > 0:
sum_a[0] = -1
else:
sum_a[0] = 1
else:
if sum_a[0] == 0:
count += 1
for i in range(1,n):
sum_a.append(sum_a[i-1] + a[i])
if sum_a[i] == 0:
if sum_a[i-1] < 0:
sum_a[i] += 1
count += 1
else:
sum_a[i] -= 1
count += 1
elif sum_a[i-1] * sum_a[i] > 0:
if sum_a[i] > 0:
sum_a[i] = -1
count += 1 + abs(sum_a[i-1]+a[i])
else:
sum_a[i] = 1
count += 1 + abs(sum_a[i-1]+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 | java | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] a = new int[n];
int[] s = new int[n];
int sum = 0;
int count1 = 0,count2 = 0;
for(int i=0;i<n;i++){
a[i] = scanner.nextInt();
}
for(int i=0;i<n;i++){
if(i % 2 == 0 && sum + a[i] <= 0){
count1 += Math.abs(sum + a[i] - 1);
sum = 1;
}else if(i % 2 == 1 && sum + a[i] >= 0){
count1 += sum + a[i] + 1;
sum = -1;
}else{
sum += a[i];
}
}
sum = 0;
for(int i=0;i<n;i++){
if(i % 2 == 1 && sum + a[i] <= 0){
count2 += Math.abs(sum + a[i] -1);
sum = 1;
}else if(i % 2 == 0 && sum + a[i] >= 0){
count2 += sum + a[i] + 1;
sum = -1;
}else{
sum += a[i];
}
}
System.out.println(Math.min(count1,count2));
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
int calc(int flag, const int a[], int n) {
int cost = 0;
long sum = 0;
for (int i = 0; i < n; ++i) {
sum += a[i];
if ((sum * flag) <= 0) {
cost += abs(flag - sum);
sum += flag - sum;
}
flag = -flag;
}
return cost;
}
int main(int argc, char *argv[]) {
int n;
std::cin >> n;
int a[1 << 20];
for (int i = 0; i < n; ++i) {
std::cin >> a[i];
}
std::cout << std::min(calc(1, a, n), calc(-1, a, 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 | python3 | n = int(input())
a = list(map(int, input().split()))
count_1 = 0
count_2 = 0
for i in range(len(a)):
if i == 0:
count_1 += abs(1 - a[i])
elif i % 2 == 1:
count_1 += abs(-2 - a[i])
elif i % 2 == 0:
count_1 += abs(2 - a[i])
for i in range(len(a)):
if i == 0:
count_2 += abs(-1 - a[i])
elif i % 2 == 1:
count_2 += abs(2 - a[i])
elif i % 2 == 0:
count_2 += abs(-2 - a[i])
print(count_1 if count_1 < count_2 else count_2) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; i++) {
cin >> a.at(i);
}
long long sum = a.at(0);
long long op_1 = 0;
bool flag = sum > 0 ? 1 : 0;
for (int j = 1; j < n; j++) {
if (flag) {
sum += a.at(j);
if (sum >= 0) {
op_1 += (sum + 1);
sum = -1;
}
flag = 0;
} else {
sum += a.at(j);
if (sum <= 0) {
op_1 += (-1 * sum + 1);
sum = 1;
}
flag = 1;
}
}
sum = a.at(0);
long long op_2 = 0;
if (sum > 0) {
sum = -1;
op_2 += (sum + 1);
} else {
sum = 1;
op_2 += (sum * -1 + 1);
}
for (int j = 1; j < n; j++) {
if (flag) {
sum += a.at(j);
if (sum >= 0) {
op_2 += sum + 1;
sum = -1;
}
flag = 0;
} else {
sum += a.at(j);
if (sum <= 0) {
op_2 += -1 * sum + 1;
sum = 1;
}
flag = 1;
}
}
cout << (op_1 > op_2 ? op_2 : op_1) << 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 TAM = 100003;
long long arr[TAM], acum[TAM], n;
int main() {
scanf("%lld", &n);
for (int i = 0; i < n; i++) {
scanf("%lld", &arr[i]);
acum[i] = arr[i];
if (i) acum[i] += acum[i - 1];
}
long long del = 0, ca = 0, cb = 0;
for (int i = 0; i < n; i++) {
if (i % 2) {
if (acum[i] + del >= 0) {
ca += (acum[i] + del + 1);
del -= (acum[i] + del + 1);
}
} else {
if (acum[i] + del <= 0) {
ca += (-(acum[i] + del) + 1);
del += (-(acum[i] + del) + 1);
}
}
}
del = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
if (acum[i] + del >= 0) {
cb += (acum[i] + del + 1);
del -= (acum[i] + del + 1);
}
} else {
if (acum[i] + del <= 0) {
cb += (-(acum[i] + del) + 1);
del += (-(acum[i] + del) + 1);
}
}
}
ca = min(ca, cb);
printf("%d\n", ca);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = [int(i) for i in input().split()]
s = [0]*n
s[0] = a[0]
count = 0
for i in range(1,n):
s[i] = s[i-1] + a[i]
if s[i] * s[i-1] > 0:
tmp = (-s[i]) + (-s[i]//abs(s[i]))
s[i] += tmp
count += abs(tmp)
if s[i] == 0:
tmp = -s[i-1]//abs(s[i-1])
s[i] += tmp
count += abs(tmp)
print(count)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void) {
int n;
long long ans = 0, sum = 0;
bool flag = true;
scanf("%d", &n);
scanf("%lld", &ans);
if (ans < 0) flag = false;
for (int i = 1; i < n; i++) {
long long x;
scanf("%lld", &x);
ans += x;
if (flag) {
if (ans >= 0) {
sum += ans + 1;
ans = -1;
}
flag = false;
} else {
if (ans <= 0) {
sum += abs(ans) + 1;
ans = 1;
}
flag = true;
}
}
printf("%lld\n", sum);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n;
vector<long long int> v;
int solve(int first_pon) {
int pon = first_pon, sum = 0, result = 0;
for (int i = 0; (i) < (n); i++) {
sum += v[i];
if (pon > 0 && sum >= 0) {
result += sum + 1;
sum = -1;
} else if (pon < 0 && sum <= 0) {
result += (-sum) + 1;
sum = 1;
}
if (sum > 0) {
pon = 1;
} else {
pon = -1;
}
}
return result;
}
int main() {
cin >> n;
v = vector<long long int>(n);
for (int i = 0; (i) < (n); i++) {
cin >> v[i];
}
cout << min(solve(1), solve(-1)) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> List(n);
for (int i = 0; i < n; i++) {
cin >> List.at(i);
}
int cnt = 0;
long long Sign = 0;
for (int i = 0; i < n; i++) {
if (i == 0) {
if (List.at(i) >= 0) {
Sign = List.at(i);
} else if (List.at(i) < 0) {
Sign = List.at(i);
}
continue;
}
if (Sign >= 0) {
if (Sign + List.at(i) >= 0) {
cnt += abs(Sign + List.at(i)) + 1;
Sign = -1;
} else {
Sign += List.at(i);
}
continue;
}
if (Sign < 0) {
if (List.at(i) + Sign <= 0) {
cnt += abs(Sign + List.at(i)) + 1;
Sign = 1;
} else {
Sign += List.at(i);
}
continue;
}
}
cout << cnt << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("4");
WillReturn.Add("1 -3 1 0");
//4
}
else if (InputPattern == "Input2") {
WillReturn.Add("5");
WillReturn.Add("3 -6 4 -5 7");
//0
}
else if (InputPattern == "Input3") {
WillReturn.Add("6");
WillReturn.Add("-1 4 3 2 -5 4");
//8
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static void Main()
{
List<string> InputList = GetInputList();
int[] AArr = InputList[1].Split(' ').Select(X => int.Parse(X)).ToArray();
if (AArr[0] == 0) {
AArr[0] = 1;
long Cost1 = Solve(AArr);
AArr[0] = -1;
long Cost2 = Solve(AArr);
Console.WriteLine(Math.Min(Cost1, Cost2));
}
else {
Console.WriteLine(Solve(AArr));
}
}
static long Solve(int[] pArr)
{
long Cost = 0;
long RunSum = pArr[0];
for (int I = 1; I <= pArr.GetUpperBound(0); I++) {
if (RunSum < 0) {
RunSum += pArr[I];
if (RunSum > 0) continue;
Cost += Math.Abs(RunSum) + 1;
RunSum = 1;
}
else if (RunSum > 0) {
RunSum += pArr[I];
if (RunSum < 0) continue;
Cost += Math.Abs(RunSum) + 1;
RunSum = -1;
}
}
return Cost;
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
const int N = 1e5 + 10;
using namespace std;
int mod = 1e9 + 7;
int num[N], num2[N];
long long sum[N], sum2[N];
int main() {
int n;
while (~scanf("%d", &n)) {
long long ans = 0, ans2 = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", num + i);
num2[i] = num[i];
sum[i] = sum[i - 1] + num[i];
sum2[i] = sum2[i - 1] + num2[i];
}
if (num[1] == 0) {
ans += 1;
num[1] = 1;
sum[1]++;
}
if (num2[1] == 0) {
ans2 += 1;
num2[1] = -1;
sum2[1]--;
}
for (int i = 2; i <= n; i++) {
sum[i] = sum[i - 1] + num[i];
int a = sum[i - 1] < 0;
int b = sum[i] < 0;
if (sum[i] == 0) {
if (sum[i - 1] < 0)
num[i]++;
else
num[i]--;
sum[i] = sum[i - 1] + num[i];
ans++;
} else if (!(a ^ b)) {
if (sum[i] < 0) {
num[i] -= sum[i];
num[i]++;
ans -= sum[i];
sum[i] = sum[i - 1] + num[i];
ans++;
} else if (sum[i] > 0) {
num[i] -= sum[i];
num[i]--;
ans += sum[i];
ans++;
sum[i] = num[i] + sum[i - 1];
}
}
}
for (int i = 2; i <= n; i++) {
sum2[i] = sum2[i - 1] + num2[i];
int a = sum2[i - 1] < 0;
int b = sum2[i] < 0;
if (sum2[i] == 0) {
if (sum2[i - 1] < 0)
num2[i]++;
else
num2[i]--;
sum2[i] = sum2[i - 1] + num2[i];
ans2++;
} else if (!(a ^ b)) {
if (sum2[i] < 0) {
num2[i] -= sum2[i];
num2[i]++;
ans2 -= sum2[i];
sum2[i] = sum2[i - 1] + num2[i];
ans2++;
} else if (sum2[i] > 0) {
num2[i] -= sum2[i];
num2[i]--;
ans2 += sum2[i];
ans2++;
sum2[i] = num2[i] + sum2[i - 1];
}
}
}
printf("%lld\n", min(ans, ans2));
}
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 int N, count = 0;
cin >> N;
vector<long int> A(N);
for (int i = 0; i < N; i++) cin >> A[i];
long int su = A[0];
bool plus = A[0] > 0;
for (int i = 1; i < N; i++) {
plus = !plus;
su += A[i];
if (plus) {
if (su <= 0) {
count += abs(su) + 1;
su = 1;
}
} else {
if (su >= 0) {
count += abs(su) + 1;
su = -1;
}
}
}
cout << count << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
const int N = 1e6 + 5;
const int mod = 1e9 + 7;
using namespace std;
int n, a[N], sum[N], ans1, ans2;
template <typename T>
inline T read() {
T x = 0, w = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') w = -1;
c = getchar();
}
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * w;
}
int mabs(int x) { return x < 0 ? -x : x; }
int main() {
n = read<int>();
for (int i = 1; i <= n; i++) a[i] = read<int>();
for (int sum = 0, i = 1; i <= n; i++) {
if (i & 1) {
if (sum + a[i] > 0) {
sum += a[i];
continue;
}
ans1 += mabs(sum + a[i]) + 1, sum = 1;
} else {
if (sum + a[i] < 0) {
sum += a[i];
continue;
}
ans1 += mabs(sum + a[i]) + 1, sum = -1;
}
}
for (int sum = 0, i = 1; i <= n; i++) {
if (!(i & 1)) {
if (sum + a[i] > 0) {
sum += a[i];
continue;
}
ans2 += mabs(sum + a[i]) + 1, sum = 1;
} else {
if (sum + a[i] < 0) {
sum += a[i];
continue;
}
ans2 += mabs(sum + a[i]) + 1, sum = -1;
}
}
printf("%d\n", min(ans1, ans2));
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 read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - 48;
ch = getchar();
}
return x * f;
}
const int N = 1e5 + 11, mod = 1e9 + 7;
long long ans1, ans2, sum;
int n;
int a[N];
int main() {
n = read();
for (int i = 1; i <= n; i++) a[i] = read();
for (int i = 1; i <= n; i++) {
sum = sum + a[i];
if (i % 2 == 1)
if (sum <= 0) {
ans1 = ans1 + abs(1 - sum);
sum = 1;
} else {
}
else if (sum >= 0) {
ans1 = ans1 + abs(-1 - sum);
sum = -1;
}
}
sum = 0;
for (int i = 1; i <= n; i++) {
sum = sum + a[i];
if (i % 2 == 0)
if (sum <= 0) {
ans2 = ans2 + abs(1 - sum);
sum = 1;
} else {
}
else if (sum >= 0) {
ans2 = ans2 + abs(-1 - sum);
sum = -1;
}
}
printf("%lld\n", 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;
long long chk1, chk2, ans1 = 0, ans2 = 0;
scanf("%d", &n);
vector<int> a(n);
for (auto& e : a) scanf("%d", &e);
chk1 = a[0];
chk2 = a[0];
for (int i = 1; i < n; i++) {
if (i % 2) {
chk1 += a[i];
chk2 += a[i];
if (chk1 >= 0) {
ans1 += chk1 + 1;
chk1 = -1;
}
if (chk2 <= 0) {
ans2 += -1 * chk2 + 1;
chk2 = 1;
}
} else {
chk1 += a[i];
chk2 += a[i];
if (chk1 <= 0) {
ans1 += -1 * chk1 + 1;
chk1 = 1;
}
if (chk2 >= 0) {
ans2 += chk2 + 1;
chk2 = -1;
}
}
}
if (ans1 >= 0 && ans2 >= 0)
printf("%lld\n", min(ans1, ans2));
else if (ans1 >= 0)
printf("%lld\n", ans1);
else if (ans2 >= 0)
printf("%lld\n", ans2);
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 read() {
int k = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
k = k * 10 + ch - '0';
ch = getchar();
}
return k * f;
}
int n, a[100010], s[100010];
int main() {
n = read();
for (int i = 1; i <= n; i++) a[i] = read();
int ans = 1e9, sum = 0;
for (int i = 1; i <= n; i++) {
s[i] = s[i - 1] + a[i];
if (i & 1) {
if (s[i] >= 0) sum += abs(-1 - s[i]), s[i] = -1;
} else {
if (s[i] <= 0) sum += abs(1 - s[i]), s[i] = 1;
}
}
ans = sum;
sum = 0;
for (int i = 1; i <= n; i++) {
s[i] = s[i - 1] + a[i];
if (i & 1) {
if (s[i] <= 0) sum += abs(1 - s[i]), s[i] = 1;
} else {
if (s[i] >= 0) sum += abs(-1 - s[i]), s[i] = -1;
}
}
ans = min(ans, sum);
printf("%d", 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;
bool posi(long long x) { return x > 0; }
int main() {
int N;
cin >> N;
vector<long long> a(N);
for (auto &i : a) cin >> i;
long long ans = 0, tmp = 0;
long long sum = a[0];
for (int i = 1; i < N; i++) {
if (!(posi(sum) ^ posi(sum + a[i]))) {
tmp += abs(sum + a[i]) + 1;
sum = (sum > 0) ? -1 : 1;
} else
sum += a[i];
}
ans = tmp;
tmp = abs(a[0]) + 1;
sum = (a[0] > 0) ? -1 : 1;
for (int i = 1; i < N; i++) {
if (!(posi(sum) ^ posi(sum + a[i]))) {
tmp += abs(sum + a[i]) + 1;
sum = (sum > 0) ? -1 : 1;
} else
sum += a[i];
}
ans = min(ans, tmp);
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n, a[100000];
int main(void) {
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
int sum1 = 0, res1 = 0;
for (int i = 0; i < n; i++) {
sum1 += a[i];
if (i % 2 == 0) {
if (sum1 <= 0) {
res1 += 1 - sum1;
sum1 = 1;
}
} else {
if (sum1 >= 0) {
res1 += 1 + sum1;
sum1 = -1;
}
}
}
int sum2 = 0, res2 = 0;
for (int i = 0; i < n; i++) {
sum2 += a[i];
if (i % 2 == 0) {
if (sum2 >= 0) {
res2 += 1 + sum2;
sum2 = -1;
}
} else {
if (sum2 <= 0) {
res2 += 1 - sum2;
sum2 = 1;
}
}
}
cout << (res1 < res2 ? res1 : res2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
int count = 0;
vector<int> a(100000);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<int> sum(100000);
if (a[0] == 0) {
if (a[1] > 0) {
a[0]--;
count++;
} else {
a[0]++;
count++;
}
}
sum[0] = a[0];
for (int i = 1; i < n; i++) {
sum[i] = sum[i - 1] + a[i];
if (sum[i] == 0) {
if (sum[i - 1] < 0) {
sum[i]++;
count++;
} else {
sum[i]--;
count++;
}
}
if (sum[i] * sum[i - 1] > 0) {
if (sum[i - 1] < 0) {
while (sum[i] * sum[i - 1] >= 0) {
sum[i]++;
count++;
}
} else {
while (sum[i] * sum[i - 1] >= 0) {
sum[i]--;
count++;
}
}
}
}
cout << count;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
vector<int> a1(n);
vector<int> a2(n);
vector<long long> sum1(n);
vector<long long> sum2(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
a1 = a;
a2 = a;
int ans1 = 0;
int ans2 = 0;
for (int i = 0; i < n; i++) {
if (i == 0)
sum1[i] = a1[i];
else
sum1[i] = sum1[i - 1] + a1[i];
if (i % 2 == 0 && sum1[i] <= 0) {
a1[i] += (1 - sum1[i]);
ans1 += (1 - sum1[i]);
sum1[i] += (1 - sum1[i]);
if (sum1[i] == 0) {
a1[i]++;
sum1[i]++;
ans1++;
}
} else if (i % 2 == 1 && sum1[i] >= 0) {
a1[i] -= (sum1[i] + 1);
ans1 += (sum1[i] + 1);
sum1[i] -= (1 + sum1[i]);
if (sum1[i] == 0) {
a1[i]--;
sum1[i]--;
ans1++;
}
}
}
for (int i = 0; i < n; i++) {
if (i == 0)
sum2[i] = a2[i];
else
sum2[i] = sum2[i - 1] + a2[i];
if (i % 2 == 0 && sum2[i] >= 0) {
a2[i] -= (1 + sum2[i]);
ans2 += (sum2[i] + 1);
sum2[i] -= (1 + sum2[i]);
if (sum2[i] == 0) {
a2[i]--;
sum2[i]--;
ans2++;
}
} else if (i % 2 == 1 && sum2[i] <= 0) {
a2[i] += (1 - sum2[i]);
ans2 += (1 - sum2[i]);
sum2[i] += (1 - sum2[i]);
if (sum2[i] == 0) {
a2[i]++;
sum2[i]++;
ans2++;
}
}
}
if (sum2[n - 1] == 0) ans2++;
if (ans1 >= ans2)
cout << ans2 << endl;
else
cout << ans1 << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | solve [] v acc = acc
solve as v acc
| v == 0 =
if head as < 0 -- aiに連続な0はないとした
then solve as 1 (1 + acc)
else solve as (-1) (1 + acc)
| v < 0 =
let w = v + (head as) in
if w <= 0
then solve (tail as) 1 (1 - w + acc)
else solve (tail as) w acc
| v > 0 =
let w = v + (head as) in
if w >= 0
then solve (tail as) (-1) (1 + w + acc)
else solve (tail as) w acc
main = do
n <- read <$> getLine :: IO Int
l <- getLine
let as = fmap read (words l) :: [Int] in
putStrLn (show (solve (tail as) (head as) 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 sum;
cin >> sum;
if (sum == 0) {
long long delta1 = 1;
sum = 1;
long long ar[N];
for (int i = 1; i < N; ++i) {
cin >> ar[i];
long long temp = ar[i];
if (sum > 0 && sum + temp >= 0) {
delta1 += sum + temp + 1;
sum = -1;
} else if (sum < 0 && sum + temp <= 0) {
delta1 += 1 - (sum + temp);
sum = 1;
} else {
sum += temp;
}
}
long long delta2 = 1;
sum = -1;
for (int i = 1; i < N; ++i) {
long long temp = ar[i];
if (sum > 0 && sum + temp >= 0) {
sum = -1;
delta2 += sum + temp + 1;
} else if (sum < 0 && sum + temp <= 0) {
sum = 1;
delta2 += 1 - (sum + temp);
} else {
sum += temp;
}
}
if (delta1 < delta2)
cout << delta1;
else
cout << delta2;
} else {
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 | cpp | #include <bits/stdc++.h>
using namespace std;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
const int INF = 100000000;
const long long LINF = 1000000000000000000;
const int MOD = (int)1e9 + 7;
using pii = std::pair<int, int>;
int main(void) {
cin.tie(0);
ios::sync_with_stdio(false);
int n, sum = 0, a, ans1 = 0, ans2 = 0, sum1 = 0, sum2 = 0;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> a;
sum1 += a;
sum2 += a;
if (i % 2 == 0) {
if (sum1 <= 0) ans1 += 1 - sum1, sum1 = 1;
if (sum2 >= 0) ans2 += 1 + sum2, sum2 = -1;
} else {
if (sum1 >= 0) ans1 += 1 + sum1, sum1 = -1;
if (sum2 <= 0) ans2 += 1 - sum2, sum2 = 1;
}
}
cout << min(ans1, ans2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.*;
public class Main {
private static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int n = sc.nextInt();
long sum = sc.nextLong();
long ret = 0;
long tmp = 0;
for (int i = 1;i < n;i++) {
long a = sc.nextInt();
tmp = sum;
sum += a;
if (tmp*sum<0) continue;
long l = Math.abs(sum)+1;
if (sum>=0) {
sum -= l;
} else {
sum += l;
}
ret += l;
}
System.out.println(ret);
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
static const long long maxLL = (long long)1 << 62;
long long a[100001] = {};
long long s[100001] = {};
int main() {
long long n;
cin >> n;
for (long long i = 1; i <= n; i++) {
cin >> a[i];
}
long long cnt = 0;
for (long long i = 1; i <= n; i++) {
s[i] = s[i - 1] + a[i];
if (i > 1) {
if (s[i] == 0) {
s[i] = s[i - 1] * -1;
cnt++;
}
if (i > 1 && s[i - 1] * s[i] > 0) {
cnt += abs(s[i]) + 1;
if (s[i] > 0)
s[i] -= cnt;
else if (s[i] < 0)
s[i] += cnt;
}
}
}
cout << cnt << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <bits/stdc++.h>
int main() {
int n, sign, ans = 0;
long long a[100000], sum = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%lld", &a[i]);
if (i == 0) {
if (a[i] >= 0) sign = 1;
if (a[i] < 0) sign = -1;
}
sum += a[i];
if ((i % 2 == 0 && sign == 1) || (i % 2 == 1 && sign == -1)) {
if (sum <= 0) {
ans += sum * (-1) + 1;
sum = 1;
}
}
if ((i % 2 == 1 && sign == 1) || (i % 2 == 0 && sign == -1)) {
if (sum >= 0) {
ans += sum + 1;
sum = -1;
}
}
}
printf("%d\n", ans);
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
int main(void) {
double num[10 * 10 * 10 * 10 * 10];
int i, n, ssign;
double sum = 0;
double count = 0;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%lf", &num[i]);
}
if (num[0] == 0) {
num[0]++;
count++;
}
for (i = 1; i < n; i++) {
sum += num[i - 1];
while (1) {
if (fabs(sum) > fabs(num[i])) {
if (sum < 0) {
num[i]++;
count++;
} else if (sum > 0) {
num[i]--;
count++;
}
} else if (fabs(sum) == fabs(num[i])) {
if (sum < 0) {
num[i]++;
count++;
} else {
num[i]--;
count++;
}
} else if (sum > 0 && num[i] > 0 && fabs(sum) < fabs(num[i])) {
num[i]--;
count++;
} else if (sum < 0 && num[i] < 0 && fabs(sum) < fabs(num[i])) {
num[i]++;
count++;
} else
break;
}
}
for (i = 0; i < n; i++) {
sum += num[i];
if (sum == 0.0) {
if ((sum - num[i]) > 0)
num[i]--;
else
num[i]++;
count++;
}
}
printf("%f\n", count);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java |
import java.util.*;
import java.io.*;
import java.math.BigInteger;
public class Main {
private static final int mod =(int)1e9+7;
public static void main(String[] args) throws Exception {
Scanner sc=new Scanner(System.in);
PrintWriter out=new PrintWriter(System.out);
int n=sc.nextInt();
long a[]=new long[n];
for(int i=0;i<n;i++) {
a[i]=sc.nextLong();
}
long sum=a[0];
long operations=0;
if(a.length==1) {
if(a[0]!=0) {
System.out.println(0);
}else {
System.out.println(1);
}
}else {
if(sum==0) {
int f=0;
for(int i=2;i<n;i+=2) {
if(a[i]>0) {
f=1;
break;
}
}
if(f==1)
sum++;
else
sum--;
operations++;
}
for(int i=1;i<n;i++) {
if(sum>0) {
if(sum+a[i]<0) {
sum+=a[i];
}else {
if(sum+a[i]==0) {
sum+=a[i]-1;
operations++;
}else {
long req=(long)-1-1l*sum;
sum=-1;
operations+=(-1l*req+a[i]);
}
}
}else {
if(sum+a[i]>0) {
sum+=a[i];
}else {
if(sum+a[i]==0) {
sum+=a[i]+1;
operations++;
}else {
long req=(long)1+-1l*sum;
sum=1;
operations+=(req-a[i]);
}
}
}
}
System.out.println(operations);
}
}
static boolean vis[]=new boolean[10001];
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
// Function to find gcd of array of
// numbers
static int f(int arr[], int n)
{
int result = n;
int max=-1;
int ans=0;
for (int element: arr){
if(vis[element]==false)
result = gcd(n, element);
if(result>max) {
max=result;
ans=element;
}
}
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()))
c = 0
i = 1
k = 1
sum = a[0]
while a[i] == 0:
i += 1
if a[i] > 0:
if i % 2 == 1:
a[0] = 1
c = 1
else:
a[0] = -1
c = 1
else:
if i % 2 == 1:
a[0] = -1
c = 1
else:
a[0] = 1
c = 1
i = 1
if a[0] > 0:
while i < n:
if i == 2*k-1:
sum = sum + a[2*k-1]
if sum >= 0:
c = c + sum + 1
sum = -1
else:
pass
i += 1
else:
sum = sum + a[2*k]
if sum <= 0:
c = c - sum + 1
sum = 1
else:
pass
i += 1
k += 1
print(c)
elif a[0] < 0:
while i < n:
if i == 2*k-1:
sum = sum + a[2*k-1]
if sum <= 0:
c = c - sum + 1
sum = 1
else:
pass
i += 1
else:
sum = sum + a[2*k]
while sum >= 0:
c = c + sum + 1
sum = -1
else:
pass
i += 1
k += 1
print(c) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int INTMAX = 2147483647;
const int64_t LLMAX = 9223372036854775807;
const int MOD = 1000000007;
template <class T>
inline bool chmax(T& a, const T& b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
inline bool chmin(T& a, const T& b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
inline void swap(int64_t& a, int64_t& b) {
a ^= b;
b ^= a;
a ^= b;
}
inline void swap(int& a, int& b) {
a ^= b;
b ^= a;
a ^= b;
}
vector<int> a;
int main() {
int n;
int64_t ans = 0, tmp, s;
cin >> n;
a.resize(n);
for (int i{0}; i < (int)(n); i++) cin >> a[i];
s = a[0];
tmp = 0;
if (s < 0) {
tmp += (1 - s);
s = 1;
}
for (int i{1}; i < (int)(n); i++) {
if (!(s + a[i]) || (s ^ (s + a[i])) >= 0) {
if (s > 0) {
tmp += 1LL + s + a[i];
s = -1;
} else {
tmp += 1LL - (s + a[i]);
s = 1;
}
} else
s += a[i];
}
ans = tmp;
s = a[0];
tmp = 0;
if (s > 0) {
tmp += 1LL + s;
s = -1;
}
for (int i{1}; i < (int)(n); i++) {
if (!(s + a[i]) || (s ^ (s + a[i])) >= 0) {
if (s > 0) {
tmp += 1LL + s + a[i];
s = -1;
} else {
tmp += 1LL - (s + a[i]);
s = 1;
}
} else
s += a[i];
}
chmin(ans, tmp);
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() {
ios::sync_with_stdio(false);
int n;
cin >> n;
int a[100010];
for (int i = 0; i < n; ++i) cin >> a[i];
int cur1 = 0;
int cur2 = 0;
int ans1 = 0;
int ans2 = 0;
for (int i = 0; i < n; ++i) {
cur1 += a[i];
if (i % 2 == 0) {
if (cur1 <= 0) {
ans1 = ans1 - cur1 + 1;
cur1 = 1;
}
}
if (i % 2 == 1) {
if (cur1 >= 0) {
ans1 = ans1 + cur1 + 1;
cur1 = -1;
}
}
}
for (int i = 0; i < n; ++i) {
cur2 += a[i];
if (i % 2 == 1) {
if (cur2 <= 0) {
ans2 = ans2 - cur2 + 1;
cur2 = 1;
}
}
if (i % 2 == 0) {
if (cur2 >= 0) {
ans2 = ans2 + cur2 + 1;
cur2 = -1;
}
}
}
cout << min(ans1, ans2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int MOD = 1000000007;
const int mod = 1000000007;
const int INF = 1000000000;
const long long LINF = 1e18;
const int MAX = 510000;
bool code(long long int n) {
if (n < 0)
return 1;
else if (n > 0)
return 0;
}
int main() {
int n;
long long int sum = 0;
long long int ans = 0;
long long int ans2 = 0;
cin >> n;
vector<long long int> a(n);
for (int i = 0; i < n; i++) {
cin >> a.at(i);
}
sum = a.at(0);
if (sum != 0) {
for (int i = 1; i < n; i++) {
if (sum + a.at(i) == 0) {
ans++;
if (sum > 0)
sum = -1;
else if (sum < 0)
sum = 1;
} else if (code(sum + a.at(i)) == code(sum)) {
ans += abs(sum + a.at(i)) + 1;
if (sum > 0)
sum = -1;
else if (sum < 0)
sum = 1;
} else {
sum = a.at(i) + sum;
}
}
cout << ans << endl;
return 0;
} else if (sum == 0) {
sum = -1;
ans = 1;
for (int i = 1; i < n; i++) {
if (sum + a.at(i) == 0) {
ans++;
if (sum > 0)
sum = -1;
else if (sum < 0)
sum = 1;
} else if (code(sum + a.at(i)) == code(sum)) {
ans += abs(sum + a.at(i)) + 1;
if (sum > 0)
sum = -1;
else if (sum < 0)
sum = 1;
} else {
sum = a.at(i) + sum;
}
}
sum = 1;
ans2 = 1;
for (int i = 1; i < n; i++) {
if (sum + a.at(i) == 0) {
ans2++;
if (sum > 0)
sum = -1;
else if (sum < 0)
sum = 1;
} else if (code(sum + a.at(i)) == code(sum)) {
ans2 += abs(sum + a.at(i)) + 1;
if (sum > 0)
sum = -1;
else if (sum < 0)
sum = 1;
} else {
sum = a.at(i) + sum;
}
}
cout << min(ans, ans2) << endl;
}
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <bits/stdc++.h>
int main() {
int n;
scanf("%d", &n);
long long a[n];
for (int i = 0; i < n; i++) {
scanf("%lld", &a[i]);
}
long long mans = 0, pans = 0;
long long msub[n], psub[n];
if (a[0] == 0) {
pans = 1;
mans = 1;
psub[0] = 1;
msub[0] = -1;
}
if (a[0] > 0) {
psub[0] = a[0];
msub[0] = -1;
mans = a[0] + 1;
} else {
psub[0] = 1;
msub[0] = a[0];
pans = a[0] + 1;
}
int i;
for (i = 1; i < n; i++) {
if (i % 2 == 1 && psub[i - 1] + a[i] >= 0) {
pans += (psub[i - 1] + a[i] + 1);
psub[i] = -1;
} else if (i % 2 == 0 && psub[i - 1] + a[i] <= 0) {
pans += (1 - (psub[i - 1] + a[i]));
psub[i] = 1;
} else {
psub[i] = (psub[i - 1] + a[i]);
}
if (i % 2 == 1 && msub[i - 1] + a[i] <= 0) {
mans += (1 - (msub[i - 1] + a[i]));
msub[i] = 1;
} else if (i % 2 == 0 && msub[i - 1] + a[i] >= 0) {
mans += (msub[i - 1] + a[i] + 1);
msub[i] = -1;
} else {
msub[i] = (msub[i - 1] + a[i]);
}
}
printf("%lld", mans < pans ? mans : pans);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long calc(vector<int> a, int pattern) {
long long count = 0;
long long sa;
long long bi = 0;
for (int(i) = 0; (i) < (a.size()); (i)++) {
if ((pattern == 1 && a[i] + bi > 0) || (pattern == -1 && a[i] + bi < 0)) {
pattern *= -1;
continue;
}
sa = a[i] + bi - pattern;
bi -= sa;
count += llabs(sa);
pattern *= -1;
}
return count;
}
int main() {
int n;
cin >> n;
vector<int> a(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 mi = min(calc(a, 1), calc(a, -1));
cout << mi << 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 | def sign(i)
return 1 if i.positive? || i == 0
-1
end
n = gets.chomp.to_i
a = gets.chomp.split.map { |item| item.to_i }
b = []
b[0] = a[0]
count = 0
if a[0] == 0
case sign(a[1])
when 1
a[0] = -1
count += 1
when 0
a[0] = 1
count += 1
end
end
(n-1).times do |i|
b[i+1] = b[i] + a[i+1]
if b[i+1] == 0
if b[i] > 0
a[i+1] -= 1
count += 1
else
a[i+1] += 1
count += 1
end
elsif sign(b[i]) == sign(b[i+1])
if b[i].positive?
count += (a[i+1] - (-b[i] - 1)).abs
a[i+1] = -b[i] - 1
b[i+1] = b[i] + a[i+1]
else
count += (a[i+1] - (-b[i] + 1)).abs
a[i+1] = -b[i] + 1
b[i+1] = b[i] + a[i+1]
end
end
end
puts count
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
unsigned int check(int sum, int ans, vector<int> T, int N, bool pre_pm) {
for (int i = 1; i < N; i++) {
if (pre_pm) {
sum += T.at(i);
while (0 <= sum) {
sum--;
ans++;
}
pre_pm = false;
} else {
sum += T.at(i);
while (sum <= 0) {
sum++;
ans++;
}
pre_pm = true;
}
}
return ans;
}
int main() {
int N;
vector<int> T;
cin >> N;
for (int i = 0; i < N; i++) {
int tmp;
cin >> tmp;
T.push_back(tmp);
}
unsigned int ans = 0;
int sum = 0;
bool pre_pm;
sum = T.at(0);
if (0 <= sum) {
pre_pm = true;
unsigned int tmp1 = check(sum, ans, T, N, pre_pm);
pre_pm = false;
unsigned int tmp2 = check(-1, 1 + sum, T, N, pre_pm);
cout << min(tmp1, tmp2) << endl;
} else {
pre_pm = false;
unsigned int tmp1 = check(sum, ans, T, N, pre_pm);
pre_pm = true;
unsigned int tmp2 = check(1, 1 + sum, T, N, pre_pm);
cout << min(tmp1, tmp2) << 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 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>
int main(void) {
long long n, now = 0, ans[2] = {};
bool flag;
scanf("%ld", &n);
long long a[n];
for (int i = 0; i < n; i++) {
scanf("%ld", &a[i]);
}
for (int j = 0; j < 2; j++) {
if (j == 0) {
flag = true;
} else {
flag = false;
}
now = a[0];
for (int i = 1; i < n; i++) {
now += a[i];
if (flag) {
if (now >= 0) {
ans[j] += (-1 - now) * (-1);
now = -1;
}
flag = false;
} else {
if (now <= 0) {
ans[j] += 1 - now;
now = 1;
}
flag = true;
}
}
}
if (ans[0] > ans[1]) {
printf("%ld", ans[1]);
} else {
printf("%ld", ans[0]);
}
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, a[100000];
int i;
int sum = 0, check = 0, count = 0;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for (i = 0; i < n; i++) {
sum += a[i];
switch (check) {
case 1:
if (sum >= 0) {
count += sum + 1;
sum = -1;
}
break;
case -1:
if (sum <= 0) {
count += 1 - sum;
sum = -1;
}
break;
default:
break;
}
if (sum > 0) {
check = 1;
} else {
check = -1;
}
}
printf("%d", count);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long int> a(n);
for (int i = 0; i < (n); ++i) cin >> a[i];
long long int ans = 0;
long long int old_sum = a[0], sum;
for (int i = 1; i < n; ++i) {
sum = old_sum + a[i];
if (sum == 0) {
ans++;
if (old_sum >= 0) {
sum--;
} else {
sum++;
}
}
if ((old_sum >= 0) && (sum >= 0)) {
ans += (sum + 1);
sum = -1;
} else if ((old_sum < 0) && (sum < 0)) {
ans += abs(sum - 1);
sum = 1;
}
old_sum = sum;
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int INF = 1e9, MOD = 1e9 + 7;
const long long LINF = 1e18;
long long int n, cnt = 0, ans = 0, a[10000000], b[10000000], c, cmp, cmpp = 0,
m, h, w, x, y, sum = 0, pos;
int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
string alph("abcdefghijklmnopqrstuvwxyz"), s;
bool fl = true;
int main(void) {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> n;
for (long long int(i) = 0; (i) < (int)(n); (i)++) {
cin >> a[i];
b[i] = a[i];
}
for (long long int(i) = 0; (i) < (int)(n); (i)++) {
sum += b[i];
if (i % 2 == 0) {
if (sum <= 0) {
cmp += sum * -1 + 1;
sum = 1;
}
} else {
if (sum >= 0) {
cmp += sum + 1;
sum = -1;
}
}
}
sum = 0;
for (long long int(i) = 0; (i) < (int)(n); (i)++) {
sum += a[i];
if (i % 2 != 0) {
if (sum <= 0) {
cmpp += sum * -1 + 1;
sum = -1;
}
} else {
if (sum >= 0) {
cmpp += sum + 1;
sum = 1;
}
}
}
ans = min(cmpp, cmp);
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 scan = new Scanner(System.in);
int n = scan.nextInt();
int[] a_ = new int[n];
for(int i = 0; i < n; i++){
a_[i] = scan.nextInt();
}
int count = 0;
int[] sum_ = new int[n];
//i % 2 == 1 : sum_[i] <= -1
if(a_[0] > 0){
sum_[0] = a_[0];
for(int i = 1; i < n;){
sum_[i] = sum_[i-1] + a_[i];
if(i % 2 != 0){
if(sum_[i] < 0){
//OK
i++;
}else{
if(sum_[i-1] > 1){
a_[i-1]--;
i--;
}else{
a_[i]--;
}
count++;
}
}else{
if(sum_[i] > 0){
//OK
i++;
}else{
if(sum_[i-1] < -1){
a_[i-1]++;
i--;
}else{
a_[i]++;
}
count++;
}
}if(count == 10)break;
}
}else{
sum_[0] = a_[0];
for(int i = 1; i < n;){
sum_[i] = sum_[i-1] + a_[i];
if(i % 2 != 0){
if(sum_[i] > 0){
//OK
i++;
}else{
if(sum_[i-1] < -1){
a_[i-1]++;
i--;
}else{
a_[i]++;
}
count++;
}
}else{
if(sum_[i] < 0){
//OK
i++;
}else{
if(sum_[i-1] > 1){
a_[i-1]--;
i--;
}else{
a_[i]--;
}
count++;
}
}
}
}
System.out.println(count);
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long calcSum(int i, long long na[]) {
long long sum = 0;
for (int j = 0; j <= i; j++) {
sum += na[j];
}
return sum;
}
long long check(int n, int s, long long a[]) {
long long cnt = 0;
long long na[n];
for (int i = 0; i < n; i++) {
na[i] = a[i];
long long sum = calcSum(i, na);
if (s == 0) {
if (i % 2 == 0 && sum <= 0) {
long long dx = 1 - sum;
na[i] += dx;
cnt += abs(dx);
} else if (i % 2 != 0 && sum >= 0) {
long long dx = -1 - sum;
na[i] += dx;
cnt += abs(dx);
}
}
if (s == 1) {
if (i % 2 != 0 && sum <= 0) {
long long dx = 1 - sum;
na[i] += dx;
cnt += abs(dx);
} else if (i % 2 == 0 && sum >= 0) {
long long dx = -1 - sum;
na[i] += dx;
cnt += abs(dx);
}
}
}
return cnt;
}
int main() {
int n;
cin >> n;
long long a[100000];
for (int i = 0; i < n; i++) cin >> a[i];
cout << min(check(n, 0, a), check(n, 1, a)) << 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()))
res = []
for start in [1, -1]:
ans = 0
_a = [a[0]]
prev_sign = start
for i in range(1, N):
c = a[i]
if c >= 0 and prev_sign > 0:
ans += abs(c - (-1))
c = -1
elif c <= 0 and prev_sign < 0:
ans += abs(c - 1)
c = 1
if c > 0:
prev_sign = 1
else:
prev_sign = -1
_a.append(c)
__a = [_a[0]]
acm_sum = _a[0]
for i in range(1, N):
c = _a[i]
if abs(acm_sum) >= abs(c):
if c < 0:
c = -1*(abs(acm_sum)+1)
else:
c = abs(acm_sum)+1
acm_sum += c
ans += abs(c - _a[i])
__a.append(c)
res.append(ans)
print(min(res)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | input()
a = list(map(int, input().split()))
s = a[0]
ret = 0
k = 1 if a[0] > 0 else -1
ret2 = k * a[0] + 1
p = -k
for i in a[1:]:
if s * (s+i) < 0:
s = s+i
else:
t = s + i
k = 1 if t > 0 else -1
ret += k * t + 1
s = -k
if p * (p+i) < 0:
p = p+i
else:
t = p + i
k = 1 if t > 0 else -1
ret2 += k * t + 1
p = -k
print(min(ret, ret2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
int A[100000];
cin >> n;
for (int i = 0; i < n; i++) {
cin >> A[i];
}
int sum = 0;
int counter = 0;
for (int i = 0; i < n; i++) {
sum += A[i];
if (i % 2 == 0) {
if (sum <= 0) {
int diff = 1 - sum;
sum += diff;
counter += diff;
}
} else {
if (sum >= 0) {
int diff = sum + 1;
sum -= diff;
counter += diff;
}
}
}
int counterNeg = 0;
sum = 0;
for (int i = 0; i < n; i++) {
sum += A[i];
if (i % 2 == 0) {
if (sum >= 0) {
int diff = sum + 1;
sum -= diff;
counterNeg += diff;
}
} else {
if (sum <= 0) {
int diff = 1 - sum;
sum += diff;
counterNeg += diff;
}
}
}
int ans = counter > counterNeg ? counterNeg : counter;
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
void _IOS() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin.sync_with_stdio(0);
}
int sx, sy, tx, ty;
struct threeElements {
int _1st, _2nd, _3rd;
};
vector<vector<int>> adj(10);
long long v[200009];
int n;
int solve(int x) {
int ans = 0, sum = x;
for (int i = 2; i <= n; i++) {
int u = v[i] + sum;
if (sum < 0) {
if (u <= 0) {
ans += abs(u) + 1;
u = 1;
}
} else {
if (u >= 0) {
ans += u + 1;
u = -1;
}
}
sum = u;
}
return ans;
}
int main() {
_IOS();
cin >> n;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
v[i] = x;
}
if (v[1] == 0) {
cout << min(solve(1), solve(-1)) + 1;
} else {
long long ans2, ans1 = solve(v[1]);
if (v[1] > 0) {
ans2 = solve(-1) + v[1] + 1;
} else {
ans2 = solve(1) + abs(v[1]) + 1;
}
cout << 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, flag = 0;
cin >> n;
vector<long long int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long int ans = 0, sum = a[0];
if (sum > 0) {
flag = 1;
} else if (sum < 0) {
flag = 0;
} else if (sum == 0 && a[1] > 0) {
sum = -1;
ans++;
flag = 0;
} else if (sum == 0 && a[1] < 0) {
sum++;
ans++;
flag = 1;
} else if (sum == 0) {
sum++;
ans++;
flag = 1;
}
for (int i = 1; i < n; i++) {
sum += a[i];
if ((flag == 1 && sum < 0) || (flag == 0 && sum > 0)) {
flag = 1 - flag;
continue;
} else if (sum == 0) {
if (i + 1 == n) {
ans++;
} else if (a[i + 1] > 0) {
sum = -1;
ans++;
flag = 0;
} else if (a[i + 1] < 0) {
sum = 1;
ans++;
flag = 1;
}
} else {
ans += (1 + abs(sum));
if (sum > 0) {
sum = -1;
flag = 0;
} else {
sum = 1;
flag = 1;
}
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
constexpr int MOD = 1000000007;
using long long = long long;
template <class T>
inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T>
inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
void print(const std::vector<int> &v) {
std::for_each(v.begin(), v.end(), [](int x) { std::cout << x << " "; });
std::cout << std::endl;
}
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (long long i = 0; i < (long long)n; i++) {
cin >> a[i];
}
long long res = (1ll << 60);
long long ans = 0LL;
long long s = 0LL;
for (int i = 0; i < n; i++) {
s += a[i];
if (i % 2 == 1) {
if (s >= 0) {
ans += s + 1;
s = -1;
}
} else {
if (s <= 0) {
ans += -s + 1;
s = 1;
}
}
}
res = min(res, ans);
ans = 0;
for (int i = 0; i < n; i++) {
s += a[i];
if (i % 2 == 0) {
if (s >= 0) {
ans += s + 1;
s = -1;
}
} else {
if (s <= 0) {
ans += -s + 1;
s = 1;
}
}
}
res = min(res, ans);
cout << res << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
template <typename T>
ostream& operator<<(ostream& out, const vector<T>& v) {
out << "[";
size_t last = v.size() - 1;
for (size_t i = 0; i < v.size(); ++i) {
out << v[i];
if (i != last) out << ",";
}
out << "]";
return out;
}
const int MAXN = 1e5 + 1;
int n;
long long a[MAXN];
long long s[MAXN], e[MAXN], o[MAXN];
void solve() {
cin >> n;
cin >> a[0];
s[0] = e[0] = o[0] = a[0];
for (int i = 1; i < n; ++i) {
cin >> a[i];
s[i] = s[i - 1] + a[i];
}
long long even = 0, odd = 0;
if (s[0] < 0) {
e[0] = 1;
even += abs(s[0]) + 1;
} else if (s[0] > 0) {
o[0] = -1;
odd += abs(s[0]) + 1;
} else {
e[0] = 1;
o[0] = -1;
even = odd = 1;
}
for (int i = 1; i < n; ++i) {
e[i] = e[i - 1] + a[i];
o[i] = o[i - 1] + a[i];
if (i % 2 == 0 && e[i] <= 0) {
even += abs(e[i]) + 1;
e[i] = 1;
} else if (i % 2 != 0 && e[i] >= 0) {
even += abs(e[i]) + 1;
e[i] = -1;
} else if (i % 2 != 0 && o[i] <= 0) {
odd += abs(o[i]) + 1;
o[i] = 1;
} else if (i % 2 == 0 && o[i] >= 0) {
odd += abs(o[i]) + 1;
o[i] = -1;
}
}
cout << min(even, odd) << endl;
}
int main() {
solve();
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e18;
signed main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
long long n, ans1 = 0, ans2 = 0, sum1 = 0, sum2 = 0;
cin >> n;
vector<long long> a(n);
for (long long i = 0; i < n; i++) {
cin >> a[i];
}
sum1 = a[0];
if (sum1 == 0) {
sum1 = (a[1] > 0 ? -1 : 1);
ans1++;
}
for (long long i = 1; i < n; i++) {
if (sum1 + a[i] == 0) {
ans1++;
} else if (sum1 > 0 && sum1 + a[i] < 0) {
sum1 += a[i];
} else if (sum1 < 0 && sum1 + a[i] > 0) {
sum1 += a[i];
} else if (sum1 > 0 && a[i] + sum1 > 0) {
ans1 += sum1 + a[i] + 1;
sum1 = -1;
} else {
ans1 += -a[i] - sum1 + 1;
sum1 = 1;
}
}
a[0] *= -1;
if (sum2 == 0) {
sum2 = (a[1] > 0 ? -1 : 1);
ans2++;
}
for (long long i = 1; i < n; i++) {
if (sum2 + a[i] == 0) {
ans2++;
} else if (sum2 > 0 && sum2 + a[i] < 0) {
sum2 += a[i];
} else if (sum2 < 0 && sum2 + a[i] > 0) {
sum2 += a[i];
} else if (sum2 > 0 && a[i] + sum2 > 0) {
ans2 += sum2 + a[i] + 1;
sum2 = -1;
} else {
ans2 += -a[i] - sum2 + 1;
sum2 = 1;
}
}
cout << min(ans1, ans2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | #入力部
N = int(input())
a_list = list(map(int,input().split()))
#偶数番目を正とする(0,2,4,…)
#深いコピー
import copy
a_listg = copy.deepcopy(a_list)
a_sumg = 0
xg = 0
for j in range(N):
if j%2 != 0:
if a_sumg + a_listg[j] > -1:
temp = a_listg[j]
a_listg[j] = -1 -a_sumg
xg += abs(temp - a_listg[j])
else:
if a_sumg + a_listg[j] < 1:
temp = a_listg[j]
a_listg[j] = 1 - a_sumg
xg += abs(temp - a_listg[j])
a_sumg += a_listg[j]
#奇数番目を正とする(0,2,4,…)
#深いコピー
import copy
a_listk = copy.deepcopy(a_list)
a_sumk = 0
xk = 0
for j in range(N):
if j%2 == 0:
if a_sumk + a_listk[j] > -1:
temp = a_listk[j]
a_listk[j] = -1 -a_sumg
xk += abs(temp - a_listk[j])
else:
if a_sumk + a_listk[j] < 1:
temp = a_listk[j]
a_listk[j] = 1 - a_sumg
xk += abs(temp - a_listk[j])
a_sumk += a_listk[j]
#2パターンの内小さい方を出力
print(min([xg,xk])) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 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), sum(n);
for (long long i = (0); i < (long long)(n); i++) cin >> A[i];
long long cnt1 = 0;
if (A[0] > 0) {
sum[0] = A[0];
} else {
sum[0] = 1;
cnt1 += abs(1 - A[0]);
}
for (long long i = (1); i < (long long)(n); i++) {
if (sum[i - 1] > 0) {
if (sum[i - 1] + A[i] < 0) {
sum[i] = sum[i - 1] + A[i];
} else {
sum[i] = -1;
cnt1 += abs(sum[i - 1] + A[i] - (-1));
}
} else {
if (sum[i - 1] + A[i] > 0) {
sum[i] = sum[i - 1] + A[i];
} else {
sum[i] = 1;
cnt1 += abs(1 - (sum[i - 1] + A[i]));
}
}
}
long long cnt2 = 0;
if (A[0] < 0) {
sum[0] = A[0];
} else {
sum[0] = 1;
cnt2 += abs(A[0] - (-1));
}
for (long long i = (1); i < (long long)(n); i++) {
if (sum[i - 1] > 0) {
if (sum[i - 1] + A[i] < 0) {
sum[i] = sum[i - 1] + A[i];
} else {
sum[i] = -1;
cnt2 += abs(sum[i - 1] + A[i] - (-1));
}
} else {
if (sum[i - 1] + A[i] > 0) {
sum[i] = sum[i - 1] + A[i];
} else {
sum[i] = 1;
cnt2 += abs(1 - (sum[i - 1] + A[i]));
}
}
}
long long cnt3 = 0;
if (A[n - 1] > 0) {
sum[n - 1] = A[n - 1];
} else {
sum[n - 1] = 1;
cnt3 += abs(1 - A[n - 1]);
}
for (long long i = n - 2; i >= 0; i--) {
if (sum[i + 1] > 0) {
if (sum[i + 1] + A[i] < 0) {
sum[i] = sum[i + 1] + A[i];
} else {
sum[i] = -1;
cnt3 += abs(sum[i + 1] + A[i] - (-1));
}
} else {
if (sum[i + 1] + A[i] > 0) {
sum[i] = sum[i + 1] + A[i];
} else {
sum[i] = 1;
cnt3 += abs(1 - (sum[i + 1] + A[i]));
}
}
}
long long cnt4 = 0;
if (A[n - 1] < 0) {
sum[n - 1] = A[n - 1];
} else {
sum[n - 1] = -1;
cnt4 += abs(A[n - 1] - (-1));
}
for (long long i = n - 2; i >= 0; i--) {
if (sum[i + 1] > 0) {
if (sum[i + 1] + A[i] < 0) {
sum[i] = sum[i + 1] + A[i];
} else {
sum[i] = -1;
cnt4 += abs(sum[i + 1] + A[i] - (-1));
}
} else {
if (sum[i + 1] + A[i] > 0) {
sum[i] = sum[i + 1] + A[i];
} else {
sum[i] = 1;
cnt4 += abs(1 - (sum[i + 1] + A[i]));
}
}
}
cout << min(min(cnt1, cnt2), min(cnt3, cnt4)) << 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 getsign(long long int n) {
if (n > 0) {
return 1;
}
if (n < 0) {
return -1;
}
return -1;
}
int count(int sign0, long long a[], int n) {
long long int sum = 0;
long long int sign = sign0;
long long int count = 0;
for (int i = 0; i < n; ++i) {
sum += a[i];
if (getsign(sum) != sign) {
count += abs(sign - sum);
sum = sign;
}
sign = (sign * -1);
}
return count;
}
int main() {
int n;
cin >> n;
long long a[n];
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
cout << min(count(1, a, n), count(-1, a, n)) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
class Program {
static List<long> rep;
static void Main(string[] args){
//入力を受け取る
var N = long.Parse(Console.ReadLine());
var A = Console.ReadLine().Split().Select(a => long.Parse(a)).ToArray();
var B = new long[N];
Array.Copy(A, B, A.Length);
long ans = 0;
long sum = A[0];
for(int i =1 ;i <N; i++){
if(sum > 0){
if(sum+A[i] >= 0){
var aim = sum*(-1)-1;
ans += (long) Math.Abs(A[i]-aim);
A[i] = aim;
}
}else{
if(sum+A[i] <= 0){
var aim = sum*(-1)+1;
ans += (long) Math.Abs(A[i]-aim);
A[i] = aim;
}
}
sum += A[i];
}
long ans2 = (long)Math.Abs(B[0])+1;
long sum2 = 0;
if(B[0] > 0){
sum2 = -1;
}else{
sum2 = 1;
}
for(int i =1 ;i <N; i++){
if(sum2 > 0){
if(sum2+B[i] >= 0){
var aim = sum2*(-1)-1;
ans2 += (long) Math.Abs(B[i]-aim);
B[i] = aim;
}
}else{
// Console.WriteLine(B[i]);
if(sum2+B[i] <= 0){
var aim = sum2*(-1)+1;
ans2 += (long) Math.Abs(B[i]-aim);
B[i] = aim;
}
}
sum2 += B[i];
}
if(ans2 < ans){
Console.WriteLine(ans2);
}else{
Console.WriteLine(ans);
}
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import sys
def solve():
readline = sys.stdin.buffer.readline
mod = 10 ** 9 + 7
n = int(readline())
a = list(map(int, readline().split()))
t = 0
cnt = 0
lt = -1 if a[0] > 0 else 1
for i in range(n):
lt = lt if i == 0 else t
t += a[i]
if lt * t >= 0:
if t > 0:
cnt += abs(t + 1)
t -= abs(t + 1)
elif t < 0:
cnt += abs(t - 1)
t += abs(t - 1)
else:
if lt < 0:
cnt += abs(t - 1)
t += abs(t - 1)
else:
cnt += abs(t + 1)
t += abs(t + 1)
print(cnt)
if __name__ == '__main__':
solve()
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long INF = 10E9;
const long long MOD = 1000000007;
const long double PI = 3.1415926;
template <class T>
T &chmin(T &a, const T &b) {
return a = min(a, b);
}
template <class T>
T &chmax(T &a, const T &b) {
return a = max(a, b);
}
long long int n, m, k, ans = 0, sum = 0, cnt = 0;
string s;
int main() {
long long int n;
cin >> n;
vector<long long int> acc(n);
long long int x = 0;
for (long long int i = (long long int)(0); i < (long long int)(n); i++) {
cin >> acc[i];
acc[i] += x;
x = acc[i];
}
bool minus = true;
long long int tmp = 0;
for (long long int i = (long long int)(1); i < (long long int)(n); i++) {
if ((minus && acc[i] + tmp >= 0) || (!minus && acc[i] + tmp <= 0)) {
ans += llabs(acc[i] + tmp) + 1;
if (!minus)
tmp += (llabs(acc[i] + tmp) + 1);
else
tmp -= (llabs(acc[i] + tmp) + 1);
}
minus = !minus;
}
long long int ans1 = ans;
ans = 0;
minus = false;
tmp = 0;
for (long long int i = (long long int)(1); i < (long long int)(n); i++) {
if ((minus && acc[i] + tmp >= 0) || (!minus && acc[i] + tmp <= 0)) {
ans += llabs(acc[i] + tmp) + 1;
if (!minus)
tmp += (llabs(acc[i] + tmp) + 1);
else
tmp -= (llabs(acc[i] + tmp) + 1);
}
minus = !minus;
}
cout << min(ans, ans1) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long now;
cin >> now;
long long ans = 0;
for (long long(i) = (0); (i) < (n - 1); ++i) {
long long tmp;
cin >> tmp;
if (now > 0) {
now += tmp;
if (now >= 0) {
ans += now + 1;
now = -1;
}
} else {
now += tmp;
if (now < 0) {
ans -= (now - 1);
now = 1;
}
}
}
cout << 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;
int C[200000];
int count = 0;
cin >> N;
for (int i = 0; i < N; i++) {
cin >> C[i];
}
int sum = C[0];
for (int i = 1; i < N; i++) {
if (sum < 0) {
sum += C[i];
while (sum <= 0) {
sum++;
count++;
}
continue;
}
if (sum > 0) {
sum += C[i];
while (sum >= 0) {
sum--;
count++;
}
continue;
}
}
cout << count << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <iostream>
#include <vector>
using namespace std;
int main()
{
int N;
cin >> N;
vector<int> a(N);
for (int i = 0; i < N; i++) {
cin >> a[i];
}
ll ans = 0;
while (ans < N && a[ans] == 0) ans++;
ll sum;
if (ans == 0) sum = a[0];
else if (ans < N && a[ans + 1] > 0) sum = -1;
else sum = 1;
for (int i = ans + 1; i < N; i++) {
//cout << sum << ' ' << (sum + a[i]) << endl;
if (sum * (sum + a[i]) >= 0) {
if (sum + a[i] >= 0) {
// sum > 0, sum + a[i] >= 0
// sum + a[i] = -1になるように変える
// sum + (-sum - 1) = -1
// a[i] -> -sum - 1
ans += abs(sum + a[i] + 1);
//cout << "cost : " << abs(sum + a[i] + 1) << endl;
a[i] = -sum - 1;
} else {
// sum < 0, sum + a[i] < 0
// sum + a[i] = 1 になるように変える
// sum + (-sum + 1) = 1
// a[i] -> -sum + 1
ans += abs(sum + a[i] - 1);
//cout << "cost : " << abs(sum + a[i] - 1) << endl;
a[i] = -sum + 1;
}
}
sum += a[i];
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.*;
public class Main {
public static void main(String[] args) {
new Main().execute();
}
public void execute() {
Scanner sc = new Scanner(System.in);
final int N = sc.nextInt();
long[] A = new long[N];
for (int i = 0; i < N; i++) {
long ai = sc.nextLong();
A[i] = ai;
}
long cnt = 0;
if (A[0] == 0) {
if (A[1] > 0) {
A[0] = -1;
} else {
A[0] = 1;
}
cnt++;
}
long sum = A[0];
for (int i = 1; i < N; i++) {
if (sum > 0) {
if (sum + A[i] >= 0) {
cnt += sum + A[i] + 1;
A[i] = -sum - 1;
sum = -1;
} else {
sum = sum + A[i];
}
} else {// sum <0
if (sum + A[i] <= 0) {
cnt += (sum + A[i]) * -1 + 1;
A[i] = -sum + 1;
sum = 1;
}else{
sum = sum + A[i];
}
}
}
System.out.println(cnt);
sc.close();
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a.at(i);
}
int sum = a.at(0);
int op = 0;
for (int j = 1; j < n; j++) {
if (sum > 0) {
sum += a.at(j);
while (sum >= 0) {
op++;
sum--;
}
} else {
sum += a.at(j);
while (sum <= 0) {
op++;
sum++;
}
}
}
cout << op << 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 math
N = int(input())
a = [int(i) for i in input().split()]
num = 0
i = 0
if a[0] == 0:
while a[i] == 0:
i += 1
if i % 2 == 0:
a[0] = math.copysign(1, a[i])
else:
a[0] = math.copysign(1, -a[i])
num += 1
old = a[0]
sam = a[0]
for i in range(1, len(a)):
sam += a[i]
sam_sign = int(math.copysign(1, sam))
old_sign = int(math.copysign(1, old))
if sam_sign == old_sign or sam == 0:
num += abs(sam) + 1
a[i] = a[i] + (-old_sign)*(abs(sam)+1)
old += a[i]
sam = old
print(int(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(void) {
long long n;
cin >> n;
long long a[n];
for (long long i = 0; i < n; i++) cin >> a[i];
long long counter = 0;
if (a[0] >= 0) {
for (long long i = 1; i < n; i++) {
long long total = a[0];
total += a[i];
if (i % 2 == 0 && total <= 0) {
counter += abs(total - 1);
a[i] += abs(total - 1);
} else if (i % 2 != 0 && total >= 0) {
counter += abs(total - (-1));
a[i] -= abs(total - (-1));
}
}
} else {
for (long long i = 1; i < n; i++) {
long long total = 0;
for (long long j = 0; j <= i; j++) {
total += a[j];
}
if (i % 2 == 0 && total >= 0) {
counter += abs(total - (-1));
a[i] -= abs(total - (-1));
} else if (i % 2 != 0 && total <= 0) {
counter += abs(total - 1);
a[i] += abs(total - 1);
}
}
}
cout << counter << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
int main() {
ll n, sum, result = 0;
n;
cin >> n;
ll a[n];
for (ll i = 0; i < n; i++) {
a[i];
cin >> a[i];
}
sum = a[0];
if (sum == 0) {
sum = 1;
result = 1;
}
for (ll i = 1; i < n; i++) {
if (sum > 0) {
sum += a[i];
if (sum >= 0) {
result += sum + 1;
sum = -1;
}
} else {
sum += a[i];
if (0 >= sum) {
result += (sum - 1) * (-1);
sum = 1;
}
}
}
result;
cout << result << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #!/usr/bin/env ruby
STDIN.gets.chomp.to_i
array = STDIN.gets.chomp.split(' ').map(&:to_i)
ans = 0
sum = 0
array.each_with_index do |a, i|
if i == 0
if a == 0
sum = 1
ans = 1
else
sum = a
end
next
end
if sum >= 0
if sum + a < 0
sum += a
else
ans += (-1 - (sum + a)).abs
sum = -1
end
else # sumがマイナス
if sum + a > 0
sum += a
else
ans += (1 - (sum + a)).abs
sum = 1
end
end
#puts "#{i}: sum = #{sum}, ans = #{ans}"
end
puts ans
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
std::vector<int> seq, sum;
long compare(bool which) {
bool is_plus = which;
long ans = 0;
long dif = 0;
if (which && sum[0] <= 0) {
dif += 1 - sum[0];
ans += dif;
} else if (!which && sum[0] >= 0) {
dif += -1 - sum[0];
ans += -dif;
}
for (int i = 1; i < sum.size(); i++) {
long num = sum[i] + dif;
if (is_plus && num >= 0) {
long tmp = -1 - num;
dif += tmp;
ans += -tmp;
} else if (!is_plus && num <= 0) {
long tmp = 1 - num;
dif += tmp;
ans += tmp;
}
is_plus = !is_plus;
}
return ans;
}
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];
}
long plus = compare(true);
long minus = compare(false);
std::cout << (plus < minus ? plus : minus) << std::endl;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.