Search is not available for this dataset
name
stringlengths 2
88
| description
stringlengths 31
8.62k
| public_tests
dict | private_tests
dict | solution_type
stringclasses 2
values | programming_language
stringclasses 5
values | solution
stringlengths 1
983k
|
---|---|---|---|---|---|---|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
std::vector<int> a;
int N;
int solve() {
int Num = 0, Sum = 0;
for (int i = 0; i < N; i++) {
if (i % 2 == 0) {
if (Sum + a[i] <= 0) {
Num += 1 - (Sum + a[i]);
Sum = 1;
} else
Sum += a[i];
} else {
if (Sum + a[i] >= 0) {
Num += 1 + Sum + a[i];
Sum = -1;
} else
Sum += a[i];
}
}
return Num;
}
int main() {
cin >> N;
for (int i = 0; i < N; i++) {
int temp;
cin >> temp;
a.push_back(temp);
}
int ans1, ans2;
ans1 = solve();
for (int i = 0; i < N; i++) a[i] *= -1;
ans2 = solve();
printf("%d\n", ans1 < ans2 ? 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;
int main() {
int N, ans = 0;
cin >> N;
int sum = 0;
for (int i = 0; i < N; i++) {
int a;
cin >> a;
int nowsum = a + sum;
if (i == 0)
sum += a;
else {
if ((nowsum < 0) != (sum < 0)) {
if (nowsum == 0) {
if (sum < 0)
nowsum++;
else
nowsum--;
ans++;
}
} else {
if (nowsum < 0) {
for (;;) {
if (nowsum > 0) break;
nowsum++;
ans++;
}
} else {
for (;;) {
if (nowsum < 0) break;
nowsum--;
ans++;
}
}
}
sum = nowsum;
}
}
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 = [int(x) for x in input().split()] # 数列の各項
cnt = 0 # 操作回数
while True:
sum = a[0] # 1列目までの和
# 1~N列目までの和を求める
# print (sum, end = ' ')
for i in range(1, len(a)):
pre = sum
sum += a[i]
# print (sum, end = ' ')
# 条件を満たしてない場合は和を求めるのを中断する
if (pre > 0 and sum > 0) or (pre < 0 and sum < 0) or (sum == 0):
break
# print('')
# print('i = ' + str(i))
# print('pre = ' + str(pre) + ', sum = ', str(sum))
if (pre > 0 and sum < 0) or (pre < 0 and sum > 0):
# 条件を満たした場合の処理
print(str(cnt))
break
else:
# 操作が必要な場合の処理
cnt = cnt + 1 # 操作回数の更新
if pre < 0:
j = 1
elif pre > 0:
j = -1
if pre < -1 or pre > 1:
a[i - 1] += j
else:
a[i] += j
# print (a) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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 cal(int b0, int n, long long* a, long long ans) {
long long b[n];
b[0] = b0;
for (int i = 1; i < n; i++) {
b[i] = b[i - 1] + a[i];
if (b[i] == 0) {
ans++;
b[i] = -1 * b[i - 1] / b[i - 1];
}
if (a[i] * b[i - 1] > 0 || (abs(a[i]) - abs(b[i - 1])) < 0) {
ans += abs(a[i] + b[i - 1]) + 1;
b[i] = -1 * b[i - 1] / b[i - 1];
}
}
return ans;
}
int main() {
int n;
cin >> n;
long long a[n], ans = 0;
for (int i = 0; i < n; i++) cin >> a[i];
if (a[0] != 0) {
cout << cal(a[0], n, a, ans) << endl;
} else {
ans++;
cout << (cal(1, n, a, ans) < cal(-1, n, a, ans) ? cal(1, n, a, ans)
: cal(-1, n, a, ans))
<< endl;
return 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 | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1 << 29;
int main() {
int n;
cin >> n;
vector<ll> a(n), b(n);
for (int i = 0; i < int(n); i++) {
ll aa;
cin >> aa;
a[i] = aa;
b[i] = aa;
}
ll countmin = INF;
ll count = 0LL;
ll temp;
if (a[0] <= 0LL) {
temp = abs(a[0]) + 1LL;
a[0] += temp;
count += temp;
}
for (int i = (1); i < (n); i++) {
a[i] += a[i - 1];
if (i % 2 == 1) {
if (a[i] >= 0LL) {
temp = abs(a[i]) + 1LL;
a[i] -= temp;
count += temp;
}
}
if (i % 2 == 0) {
if (a[i] <= 0LL) {
temp = abs(a[i]) + 1LL;
a[i] += temp;
count += temp;
}
}
}
countmin = min(countmin, count);
count = 0LL;
if (b[0] >= 0) {
temp = abs(b[0]) + 1LL;
b[0] -= temp;
count += temp;
}
for (int i = (1); i < (n); i++) {
b[i] += b[i - 1];
if (i % 2 == 1) {
if (b[i] <= 0LL) {
temp = abs(b[i]) + 1LL;
b[i] += temp;
count += temp;
}
}
if (i % 2 == 0) {
if (b[i] >= 0LL) {
temp = abs(b[i]) + 1LL;
b[i] -= temp;
count += temp;
}
}
}
countmin = min(countmin, count);
cout << countmin << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
prog = list(map(int,input().split()))
def seq(N,prog):
result = [0]*N
result[0] = prog[0]
for i in range(N-1):
result[i+1] = result[i] + prog[i+1]
if result[0] == 0:
if sum(result[0]+result[1]) >= 0:
result[0] -= 1
x = [-1]*(N-1)
result[1:] = [a+b for (a,b) in zip(x,result[1:])]
else:
result[0] += 1
x = [1]*(N-1)
result[1:] = [a+b for (a,b) in zip(x,result[1:])]
answer = 0
for i in range(N-1):
y=0
if result[i] >= 0:
if result[i+1] >0:
y = (-1)*(result[i+1]+1)
result[i+1] = -1
answer += abs(y)
else:
if result[i+1] <= 0:
y = abs(result[i+1])+1
result[i+1] = 1
answer += abs(y)
z = [y]*(N-i-1)
result[(i+1):] = [a+b for (a,b) in zip(z,result[(i+1):])]
print(answer)
seq(N,prog)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const long long INF = mod * mod;
const long double eps = 1e-8;
const long double pi = acos(-1.0);
void solve() {
int n;
cin >> n;
long long a, sum, ans = 0;
cin >> a;
sum = a;
for (int i = 0; i < n - 1; i++) {
cin >> a;
if (sum > 0) {
if (sum + a < 0)
sum += a;
else {
ans += sum + a - (-1);
sum = -1;
}
} else {
if (sum + a > 0)
sum += a;
else {
ans += 1 - (sum + a);
sum = 1;
}
}
}
cout << ans << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
solve();
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
A = [int(i) for i in input().split()]
ans = 0
cnt = 0
if A[0] < 0:
for i in range(n):
if i % 2 == 0:
if cnt + A[i] < 0:
cnt += A[i]
else:
ans += abs(cnt + A[i] - (-1))
cnt = -1
else:
if cnt + A[i] > 0:
cnt += A[i]
else:
ans += abs(cnt + A[i] - 1)
cnt = 1
else:
for i in range(n):
if i % 2 == 1:
if cnt + A[i] < 0:
cnt += A[i]
else:
ans += abs(cnt + A[i] - (-1))
cnt = -1
else:
if cnt + A[i] > 0:
cnt += A[i]
else:
ans += abs(cnt + A[i] - 1)
cnt = 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>
const int MOD = pow(10, 9) + 7;
using namespace std;
int in() {
int temp;
scanf("%d", &temp);
return temp;
}
long long lin() {
long long temp;
scanf("%lld", &temp);
return temp;
}
int solve(vector<int> csum, int N) {
int cumOper = 0;
int sign = csum[0] / abs(csum[0]);
int count = 0;
int signi;
for (auto i = 1; i < N; i++) {
if ((csum[i] + cumOper) == 0) {
count++;
cumOper -= sign;
}
signi = (csum[i] + cumOper) / abs(csum[i] + cumOper);
if (signi != sign) {
sign = signi;
continue;
}
if (sign == 1) {
count += (csum[i] + cumOper + 1);
cumOper += (-1) * (csum[i] + cumOper + 1);
} else {
count += (-(csum[i] + cumOper) + 1);
cumOper += (1) * (-(csum[i] + cumOper) + 1);
}
sign = -sign;
}
return count;
}
int main() {
int N = in();
vector<int> vec;
vector<int> csum;
vec.push_back(in());
csum.push_back(vec.back());
for (auto i = 1; i < N; i++) {
vec.push_back(in());
csum.push_back(csum.back() + vec.back());
}
if (csum[0] != 0) {
cout << solve(csum, N) << endl;
return 0;
}
int tempp;
int tempn;
csum[0] = 1;
tempp = solve(csum, N) + 1;
csum[0] = -1;
tempn = solve(csum, N) + 1;
cout << min(tempp, tempn) << 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 vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
const int INF = 1001001001;
const int MOD = 1000000007;
const long long INFL = (1LL << 60);
const double EPS = 1e-9;
bool meet(vi a) {
bool ret = true;
bool pos = (a[0] > 0);
int sum = a[0];
for (int i = (1); i < (int)(a.size()); i++) {
sum += a[i];
if ((pos && i % 2 && sum >= 0) || (pos && !(i % 2) && sum <= 0) ||
(!pos && i % 2 && sum <= 0) || (!pos && !(i % 2) && sum >= 0)) {
ret = false;
break;
}
}
return ret;
}
int main() {
int N;
cin >> N;
vi a(N);
for (int i = 0; i < (int)(N); i++) cin >> a[i];
bool flg = meet(a);
int res = 0;
int sum = a[0];
bool pos = (a[0] > 0);
for (int i = (1); i < (int)(N); i++) {
if ((pos && i % 2 && sum + a[i] >= 0) ||
(!pos && !(i % 2) && sum + a[i] >= 0)) {
while (true) {
a[i]--;
res++;
if (sum + a[i] == -1) break;
}
} else if ((pos && !(i % 2) && sum + a[i] <= 0) ||
(!pos && i % 2 && sum + a[i] <= 0)) {
while (true) {
a[i]++;
res++;
if (sum + a[i] == 1) break;
}
}
sum += a[i];
}
if (flg) res = 0;
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;
const long MOD = 1000000007;
const int MAX_N = 100000005;
int n;
int a[MAX_N];
int check(long long sum, long long ans) {
for (int i = 1; i < n; i++) {
long long t = sum + a[i];
if ((sum >= 0 && t < 0) || (sum < 0 && t >= 0)) {
sum = t;
if (sum == 0) {
sum = 1;
ans++;
}
continue;
}
long long at;
if (sum >= 0)
at = -1 - sum;
else
at = 1 - sum;
ans = ans + abs(a[i] - at);
a[i] = at;
sum = sum + a[i];
}
return ans;
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long another;
if (a[0] >= 0)
another = -1;
else
another = 1;
long long a1 = check(a[0], 0);
long long a2 = check(another, abs(a[0] - another));
cout << min(a1, a2) << 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) {
vector<int> v;
int res = 0;
int sign = 0;
int n, t;
int sum = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> t;
v.push_back(t);
}
if (v[0] > 0) {
sign = 0;
} else {
sign = 1;
}
sum += v[0];
for (int i = 1; i < v.size(); i++) {
sum += v[i];
if (sign == 0) {
if (sum > 0) {
res += (sum + 1);
sum -= (sum + 1);
} else if (sum == 0) {
res += 1;
sum -= 1;
}
} else {
if (sum < 0) {
res += ((-1 * sum) + 1);
sum += ((-1 * sum) + 1);
} else if (sum == 0) {
res += 1;
sum += 1;
}
}
sign = 1 - sign;
}
if (v[0] > 0) {
sign = 1;
} else {
sign = 0;
}
t = 0;
for (int i = 0; i < v.size(); i++) {
sum += v[i];
if (sign == 0) {
if (sum > 0) {
t += (sum + 1);
sum -= (sum + 1);
} else if (sum == 0) {
t += 1;
sum -= 1;
}
} else {
if (sum < 0) {
t += ((-1 * sum) + 1);
sum += ((-1 * sum) + 1);
} else if (sum == 0) {
t += 1;
sum += 1;
}
}
sign = 1 - sign;
}
res = min(res, t);
cout << res << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int n, cnt = 0;
cin >> n;
bool flag;
ll a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
if (a[0] >= 0) {
flag = true;
} else {
flag = false;
}
int sum = a[0];
for (int i = 1; i < n; i++) {
bool flag2;
int tmp = sum;
sum += a[i];
if (sum == 0) {
if (flag) {
sum -= 1;
flag = false;
cnt++;
} else {
sum += 1;
flag = true;
cnt++;
}
} else {
if (sum > 0) {
flag2 = true;
} else {
flag2 = false;
}
if (flag == flag2) {
if (flag2) {
while (sum >= 0) {
sum--;
cnt++;
}
flag2 = false;
} else {
while (sum <= 0) {
sum++;
cnt++;
}
flag2 = true;
}
}
flag = flag2;
}
}
if (sum == 0) 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 | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> a(N);
for (int i = 0; i < N; i++) cin >> a.at(i);
bool fla = false;
int t = 0, res1 = 0, res2 = 0;
for (int i = 0; i < N; i++) {
int b = a.at(i);
if (fla) {
if (t + b <= 0) {
b = t * -1 + 1;
res1 += b - a.at(i);
}
} else {
if (t + b >= 0) {
b = t * -1 - 1;
res1 += abs(b - a.at(i));
}
}
t += b;
fla = !fla;
}
t = 0;
for (int i = 0; i < N; i++) {
int b = a.at(i);
if (!fla) {
if (t + b <= 0) {
b = t * -1 + 1;
res2 += b - a.at(i);
}
} else {
if (t + b >= 0) {
b = t * -1 - 1;
res2 += abs(b - a.at(i));
}
}
t += b;
fla = !fla;
}
int res = min(res1, res2);
cout << res << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) {
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
String[] inputString = br.readLine().split(" ");
int[] input = new int[N];
for(int i = 0 ; i < N ; i++){
input[i] = Integer.parseInt(inputString[i]);
}
int result = 0;
int base = input[0];
if(base == 0){
base = (input[1] > 0)? -1 : 1;
result += 1;
}
for(int i = 1 ; i < N ; i++){
int temp = base;
base += input[i];
if(temp*base >= 0){
result += Math.sqrt(base*base)+1;
base = (temp > 0)? -1 : 1;
}
}
System.out.println(result);
}catch(Exception e){
e.printStackTrace();
}
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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
if a[0]>0: f=False
else: f=True
for i in range(1,n):
a[i]+=a[i-1]
if not f:
if a[i]>=0:
ans+=a[i]+1
a[i]=-1
else:
if a[i]<=0:
ans+=a[i]*-1+1
a[i]=1
# print(a,ans,f)
f^=True
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void) {
long long A[100001] = {0};
long long N = 0, B = 0;
long long ans = 0;
scanf("%lld", &N);
for (long long i = 0; i < N; i++) scanf("%lld", &A[i]);
if (A[0] != 0) {
B = A[0];
} else if (A[1] > 0) {
B = -1;
ans++;
} else {
B = 1;
ans++;
}
for (long long i = 1; i < N; i++) {
if (B > 0) {
if (B + A[i] < 0) {
B += A[i];
} else {
ans += (B + A[i]) + 1;
B = -1;
}
} else {
if (B + A[i] > 0) {
B += A[i];
} else {
ans += 1 - (B + A[i]);
B = 1;
}
}
}
printf("%lld\n", ans);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long int n;
cin >> n;
long long int a[n], ans = 0;
for (long long int i = 0; i < n; i++) cin >> a[i];
long long int sum = a[0];
for (long long int i = 0; i < n - 1; i++) {
long long int k = sum + a[i + 1];
if (k != 0) {
if ((k > 0 && sum < 0) || (k < 0 && sum > 0)) {
sum = k;
} else {
ans += (abs(k) + 1);
if (k < 0)
sum = 1;
else
sum = -1;
}
} else {
ans++;
if (sum < 0)
sum = 1;
else
sum = -1;
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < (int)(n); i++) cin >> a.at(i);
ll ans = 0, sum = 0;
for (int i = 0; i < (int)(n); i++) {
int now = a.at(i);
if (i == 0) {
if (now == 0) {
if (a.at(1) > 0)
sum--;
else
sum++;
ans++;
} else {
sum += now;
}
continue;
}
if ((sum < 0 && sum + now > 0) || (sum > 0 && sum + now < 0)) {
sum += now;
} else {
int add = abs(sum + now) + 1;
if (sum < 0)
sum = 1;
else
sum = -1;
ans += add;
}
}
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() {
ios::sync_with_stdio(false);
cin.tie(0);
long long n;
cin >> n;
long long a[n];
for (int i = 0; i < n; i++) cin >> a[i];
long long sum[n];
sum[0] = a[0];
long long ans = 0;
if (sum[0] == 0) {
ans = 1;
sum[0] = 1;
}
for (int i = 1; i <= (int)(n - 1); i++) {
long long t = sum[i - 1] + a[i];
if (t == 0) {
ans += 1;
sum[i] = -abs(sum[i - 1]) / sum[i - 1];
} else if (abs(t) / t != abs(sum[i - 1]) / sum[i - 1]) {
sum[i] = t;
} else {
ans += abs(t) + 1;
sum[i] = -abs(t) / t;
}
}
long long ans2 = 0;
if (a[0] == 0) {
ans2 = 1;
sum[0] = -1;
} else {
sum[0] = -abs(a[0]) / a[0];
ans2 = abs(a[0]) + 1;
}
for (int i = 1; i <= (int)(n - 1); i++) {
long long t = sum[i - 1] + a[i];
if (t == 0) {
ans2 += 1;
sum[i] = -abs(sum[i - 1]) / sum[i - 1];
} else if (abs(t) / t != abs(sum[i - 1]) / sum[i - 1]) {
sum[i] = t;
} else {
ans2 += abs(t) + 1;
sum[i] = -abs(t) / t;
}
}
assert(ans2 >= ans);
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, c = 0;
cin >> n;
int sum[n];
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
sum[0] = a[0];
int e = a[0] / abs(a[0]);
for (int i = 1; i < n; i++) {
sum[i] = sum[i - 1] + a[i];
if (sum[i - 1] * sum[i] >= 0) {
c += abs(sum[i] - pow(-1, i) * e);
sum[i] = pow(-1, i) * e;
}
}
cout << c << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int c1 = 0, s1 = 0, 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 (s2 <= 0) {
c2 += 1 - s2;
s2 = 1;
}
if (s1 >= 0) {
c1 += s1 + 1;
s1 = -1;
}
}
}
cout << min(c1, c2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()) {
v.assign(a, vector<T>(b, t));
}
template <class F, class T>
void convert(const F &f, T &t) {
stringstream ss;
ss << f;
ss >> t;
}
int main() {
long long n;
cin >> n;
long long a[n];
for (int i = 0; i < int(n); ++i) {
cin >> a[i];
}
long long sum = 0;
long long ans = 0;
for (int i = 0; i < int(n); ++i) {
long long nextSum = sum + a[i];
if (nextSum == 0) {
if (i + 1 < n) {
a[i] += (a[i + 1] > 0) ? -1 : 1;
} else {
++a[i];
}
nextSum = sum + a[i];
++ans;
}
if ((i > 0) && (sum * nextSum > 0)) {
a[i] += (nextSum > 0 ? -1 : 1) * (abs(nextSum) + 1);
ans += abs(nextSum) + 1;
}
sum += a[i];
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < (int)(n); i++) {
cin >> a[i];
}
long long s = a[0];
long long ss = a[0];
long long ans = 0;
for (int i = 1; i < n; i++) {
s = ss;
ss = s + a[i];
if (ss * s < 0) continue;
if (ss == 0) {
ans += 1;
ss = -s / abs(s);
} else {
ans += abs(ss - (-ss / abs(ss)));
ss = -ss / abs(ss);
}
}
int tmp = -a[0] / abs(a[0]);
long long ans2 = abs(a[0] - tmp);
a[0] = tmp;
s = a[0];
ss = a[0];
for (int i = 1; i < n; i++) {
s = ss;
ss = s + a[i];
if (ss * s < 0) continue;
if (ss == 0) {
ans2 += 1;
ss = -s / abs(s);
} else {
ans2 += abs(ss - (-ss / abs(ss)));
ss = -ss / abs(ss);
}
}
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;
signed main() {
long long n;
cin >> n;
long long a[n + 1];
a[0] = 0;
long long cnta = 0;
long long cntb = 0;
long long sum = 0;
for (long long i = 0; i < n; i++) {
cin >> a[i + 1];
a[i + 1] += a[i];
}
for (long long i = 1; i < n + 1; i++) {
if (i % 2 == 0) {
if (0 <= a[i] + sum) {
cnta += a[i] + sum + 1;
sum -= a[i] + sum + 1;
}
} else {
if (a[i] + sum <= 0) {
cnta += (1 - (a[i] + sum));
sum += (1 - (a[i] - sum));
}
}
}
sum = 0;
for (long long i = 1; i < n + 1; i++) {
if (i % 2 == 0) {
if (a[i] + sum <= 0) {
cntb += (1 - (a[i] + sum));
sum += (1 - (a[i] - sum));
}
} else {
if (0 <= a[i] + sum) {
cntb += a[i] + sum + 1;
sum -= a[i] + sum + 1;
}
}
}
cout << min(cnta, cntb) << 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 | #include <stdio.h>
#include<math.h>
int main(void) {
// your code goes here
long int a[100000];
int n,count= 0;
scanf("%d",&n);
int i;
for(i = 0;i<n;i++)
{
scanf("%ld",&a[i]);
};
int new = a[0]+a[1];
int pos;
if(new <0)
pos= 0;
else
pos = 1;
for(i = 2;i<n;i++)
{
new = a[i]+new;
// printf("%d",new);
pos = !pos;
if(new>0&& pos==0)
{count+=abs(new)+1;
new= -1;}
else if(new<0 && pos )
{count+=abs(new)+1;
new = 1;
}
else if(new ==0)
{
count++;
if(pos==0)
new = -1;
else
new = 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;
int A[n];
for (int i = 0; i < n; i++) cin >> A[i];
int cnt = 0, acm = 0, ans = 0;
for (int i = 0; i < n; i++) {
if (i % 2) {
if (acm + A[i] > 0)
acm += A[i];
else {
cnt += abs(acm + A[i]) + 1;
acm = 1;
}
} else {
if (acm + A[i] < 0)
acm += A[i];
else {
cnt += abs(acm + A[i]) + 1;
acm = -1;
}
}
}
ans = cnt;
cnt = 0;
acm = 0;
for (int i = 0; i < n; i++) {
if ((i + 1) % 2) {
if (acm + A[i] > 0)
acm += A[i];
else {
cnt += abs(acm + A[i]) + 1;
acm = 1;
}
} else {
if (acm + A[i] < 0)
acm += A[i];
else {
cnt += abs(acm + A[i]) + 1;
acm = -1;
}
}
}
ans = min(ans, cnt);
cout << ans << "\n";
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
bool ch = false;
long long N, i;
long long ans = 0, count = 0;
cin >> N;
long long a[N];
cin >> a[0];
ans += a[0];
if (ans > 0)
ch = true;
else
ch = false;
if (ans == 0) {
count += 1;
ans = -1;
}
for (i = 1; i < N; i++) {
cin >> a[i];
if (ch) {
if (ans >= -1 * a[i]) {
count += ans + a[i] + 1;
ans = -1;
} else
ans += a[i];
ch = false;
} else {
if (-1 * ans >= a[i]) {
count += -1 * ans - a[i] + 1;
ans = 1;
} else
ans += a[i];
ch = true;
}
}
long long con = 0;
if (a[0] > 0) {
ans = -1;
ch = false;
} else {
ans = 1;
ch = true;
}
con = a[0] + 1;
for (i = 1; i < N; i++) {
if (ch) {
if (ans >= -1 * a[i]) {
con += ans + a[i] + 1;
ans = -1;
} else
ans += a[i];
ch = false;
} else {
if (-1 * ans >= a[i]) {
con += -1 * ans - a[i] + 1;
ans = 1;
} else
ans += a[i];
ch = true;
}
}
cout << min(count, con) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void) {
int n, i, j, sum = 0, count, cost1 = 0, cost2 = 0;
cin >> n;
vector<int> a(n);
for (i = 0; i < n; i++) {
cin >> a[i];
}
if (a[0] < 0) {
cost1 += abs(a[0]) + 1;
sum = 1;
} else {
sum += a[0];
}
for (i = 1; i < n; i++) {
sum += a[i];
if (sum <= 0 && i % 2 == 0) {
cost1 += abs(sum) + 1;
sum = 1;
}
if (sum >= 0 && i % 2 == 1) {
cost1 += abs(sum) + 1;
sum = -1;
}
}
sum = 0;
if (a[0] >= 0) {
cost2 += abs(a[0]) + 1;
a[0] = -1;
sum = -1;
} else
sum += a[0];
for (i = 1; i < n; i++) {
sum += a[i];
if (sum >= 0 && i % 2 == 0) {
cost2 += abs(sum) + 1;
sum = -1;
}
if (sum <= 0 && i % 2 == 1) {
cost2 += abs(sum) + 1;
sum = 1;
}
}
cout << min(cost1, cost2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const long long linf = 1LL << 50;
const double eps = 1e-10;
const int mod = 1e9 + 7;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n;
cin >> n;
vector<long long> a(n);
cin >> a[0];
long long sum = a[0];
long long ans = 0;
for (int i = 1; i < ((int)n); i++) {
cin >> a[i];
long long now = sum + a[i];
if ((sum > 0 && now < 0) || (sum < 0 && now > 0))
sum = now;
else if (sum > 0) {
ans += abs(now + 1);
sum = -1;
} else {
ans += 1 + abs(now);
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 | python3 | n = int(input())
a = list(map(int, input().split()))
rui = [0] * n
rui[0] = a[0]
cnt = 0
for i in range(1, n):
tmp = rui[i - 1] + a[i]
if tmp == 0:
if rui[i - 1] > 0:
rui[i] = tmp - 1
cnt += 1
else:
rui[i] = tmp + 1
cnt += 1
else:
if rui[i - 1] > 0 and tmp > 0:
rui[i] = -1
cnt += abs(tmp) + 1
elif rui[i - 1] < 0 and tmp < 0:
rui[i] = 1
cnt += abs(tmp) + 1
else:
rui[i] = tmp
print(cnt) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int n;
cin >> n;
vector<ll> a(n);
for (int i = 0; i < n; i++) {
cin >> a.at(i);
}
ll ans = 0;
bool is_positive = (a.at(0) > 0);
ll sum = a.at(0);
for (int i = 1; i < n; i++) {
sum = sum + a.at(i);
if (i % 2 == 1) {
if (is_positive) {
if (sum < 0) {
continue;
} else {
ll target = sum - (-1);
ans += target;
a.at(i) -= target;
sum -= target;
}
} else {
if (sum > 0) {
continue;
} else {
ll target = (1) - sum;
ans += target;
a.at(i) += target;
sum += target;
}
}
} else {
if (is_positive) {
if (sum > 0) {
continue;
} else {
ll target = (1) - sum;
ans += target;
a.at(i) += target;
sum += target;
}
} else {
if (sum < 0) {
continue;
} else {
ll target = sum - (-1);
ans += target;
a.at(i) -= target;
sum -= target;
}
}
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AtCoder
{
class Code3
{
static void Main(string[] args)
{
string s1 = Console.ReadLine();
string s2 = Console.ReadLine();
Console.WriteLine(funcMain(s1,s2));
}
static private string funcMain(string arg1, string arg2)
{
long ret = 0;
long ret1 = 0;
long ret2 = 0;
long sum = 0;
short sign = 0;
for (int i = 0; i <= 1; i++) // 0はそのまま、1は逆符号
{
sum = ret = 0;
foreach (string buf in arg2.Split())
{
if (sum == 0)
{
sum = long.Parse(buf);
if (sum >= 0)
sign = 1;
else
sign = -1;
if (i == 1)
{
ret += Math.Abs(sum) + 1;
sum = sign * -1;
sign *= -1;
}
}
else
{
sum += long.Parse(buf);
if ((sum * sign) >= 0)
{
ret += Math.Abs(sum) + 1;
sum = sign * -1;
}
sign *= -1;
}
}
if (i == 0)
ret1 = ret;
else
ret2 = ret;
}
ret = Math.Min(ret1, ret2);
return ret.ToString();
}
static private void test()
{
string arg1, arg2;
arg1 = "4";
arg2 = "1 -3 1 0";
Console.WriteLine("4" == funcMain(arg1, arg2));
arg1 = "5";
arg2 = "3 -6 4 -5 7";
Console.WriteLine("0" == funcMain(arg1, arg2));
arg1 = "6";
arg2 = "-1 4 3 2 -5 4";
Console.WriteLine("8" == funcMain(arg1, arg2));
arg1 = "6";
arg2 = "-1 -2 -3 -4 -5 -6";
Console.WriteLine("16" == funcMain(arg1, arg2));
arg1 = "3";
arg2 = "1 10 -100";
Console.WriteLine("2" == funcMain(arg1, arg2));
Console.ReadKey();
}
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int sign(long long n) { return ((n > 0) - (n < 0)); }
int main() {
int n;
scanf("%d", &n);
long long sum = 0;
long long ans = 0;
for (int i = 0; i < n; i++) {
int a;
scanf("%d", &a);
if (sum == 0) {
if (a == 0) {
a++;
ans++;
}
} else if (sum + a == 0) {
a - sign(sum);
ans++;
} else if (sign(sum + a) + sign(sum) != 0) {
while (sign(sum + a) + sign(sum) != 0) {
a -= sign(sum);
ans++;
}
}
sum += a;
cerr << 34 << " "
<< "a"
<< ": " << a << endl;
;
cerr << 35 << " "
<< "sum"
<< ": " << sum << endl;
}
printf("%llu\n", ans);
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int n;
cin >> n;
long long int b[100001];
for (int i = 0; i < (n); i++) {
long long int l;
cin >> l;
b[i] = l;
}
for (int i = 0; i < (n); i++) b[i + 1] += b[i];
long long int sm = 0;
long long int dif = 0;
for (int i = 1; i < n; i++) {
if (b[i - 1] * (b[i] + dif) < 0) continue;
int target = (b[i - 1] > 0) ? -1 : 1;
sm += abs(b[i] + dif - target);
dif += -b[i] - dif + target;
}
cout << (sm) << "\n";
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
count = 0
sum_i = a[0]
if a[0] != 0:
for i in range(1, n):
if sum_i < 0:
sum_i += a[i]
if sum_i <= 0:
count += 1 - sum_i
sum_i = 1
elif sum_i > 0:
sum_i += a[i]
if sum_i >= 0:
count += sum_i + 1
sum_i = -1
else:
sum_i = 1
pos_count = 1
for i in range(1, n):
if sum_i < 0:
sum_i += a[i]
if sum_i <= 0:
pos_count += 1 - sum_i
sum_i = 1
elif sum_i > 0:
sum_i += a[i]
if sum_i >= 0:
pos_count += sum_i + 1
sum_i = -1
sum_i = -1
neg_count = 1
for i in range(1, n):
if sum_i < 0:
sum_i += a[i]
if sum_i <= 0:
neg_count += 1 - sum_i
sum_i = 1
elif sum_i > 0:
sum_i += a[i]
if sum_i >= 0:
neg_count += sum_i + 1
sum_i = -1
if neg_count < pos_count:
count = neg_count
else:
count = pos_count
print(count)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | def resolve(List):
# L[0]!=0を起点とする
L = List
cnt = 0
s = L[0]
for i in range(1,len(L)):
a = L[i]
if(s>0 and s+a>=0):
L[i] = -s-1
cnt += (s+a+1)
s = -1
elif(s<0 and s+a<=0):
L[i] = -s+1
cnt += (-s-a+1)
s = 1
else:
s += a
return cnt
def ans(List):
L = List
a = L[0]
c0,c1=0,0
if (a>0):
c0 = resolve(L)
c1 = (a+1) + resolve([-1]+L[1:])
elif (a<0):
c0 = resolve(L)
c1 = (-a+1) + resolve([1]+L[1:])
else:
c0 = 1 + resolve([1]+L[1:])
c1 = 1 + resolve([-1]+L[1:])
return(min(c0,c1))
def main():
N = int(input())
L = [int(x) for x in input().split(' ')]
print(ans(L))
main()
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long a[100000];
int main() {
int n;
long long sum = 0;
long long ans = 0;
bool flag = true;
bool nextflag = true;
bool zeroflag = false;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
if (a[0] < 0) {
flag = false;
}
sum += a[0];
for (int i = 1; i < n; i++) {
sum += a[i];
if (sum < 0) {
nextflag = false;
} else if (sum > 0) {
nextflag = true;
} else {
zeroflag = true;
}
if (flag == nextflag) {
if (nextflag == true) {
if (sum >= 0) {
ans += (sum + 1);
sum = -1;
}
flag = false;
} else {
if (sum <= 0) {
ans += (sum * -1 + 1);
sum = 1;
}
flag = true;
}
} else if (zeroflag == true) {
cerr << "0!" << endl;
if (flag == true) {
sum = -1;
ans++;
flag = false;
} else {
sum = 1;
ans++;
flag = true;
}
} else {
flag = nextflag;
}
zeroflag = false;
cerr << ans << endl;
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python2 | n = input()
a = map(int, raw_input().split())
sum = a[0]
c = 0
for i in range(1,n):
temp = sum + a[i]
if temp*sum > 0:
if sum > 0:
c += abs(-1-sum-a[i])
sum = -1
continue
if sum < 0:
c += abs(1-sum-a[i])
sum = 1
continue
if temp == 0:
c += 1
if sum > 0:
sum = -1
continue
if sum < 0:
sum = 1
continue
if temp*sum < 0:
sum = temp
continue
print c |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> a(n);
vector<long long> sum(n);
for (int i = 0; i < n; i++) {
cin >> a.at(i);
}
long long ans = 0;
if (a.at(0) == 0) {
if (a.at(1) >= 0)
a.at(0)--;
else
a.at(0)++;
ans++;
}
sum.at(0) = a.at(0);
for (int i = 1; i < n; i++) {
sum.at(i) = sum.at(i - 1) + a.at(i);
if (sum.at(i) * sum.at(i - 1) >= 0 && sum.at(i - 1) >= 0) {
a.at(i) -= sum.at(i) + 1;
ans += abs(sum.at(i)) + 1;
sum.at(i) = -1;
} else if (sum.at(i) * sum.at(i - 1) >= 0 && sum.at(i - 1) < 0) {
a.at(i) += sum.at(i) + 1;
ans += abs(sum.at(i)) + 1;
sum.at(i) = 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;
long int check(int sum, long 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);
}
long int ans = 0;
int sum = 0;
bool pre_pm;
sum = T.at(0);
if (0 <= sum) {
pre_pm = true;
long int tmp1 = check(sum, ans, T, N, pre_pm);
pre_pm = false;
long int tmp2 = check(-1, 1 + sum, T, N, pre_pm);
cout << min(tmp1, tmp2) << endl;
} else {
pre_pm = false;
long int tmp1 = check(sum, ans, T, N, pre_pm);
pre_pm = true;
long 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 | python3 | import copy
n = int(input())
a = [int(ai) for ai in input().split()]
def search(a, flip=False):
if not flip:
count = 0
else:
count = abs(a[0]) + 1
a[0] = 1 if a[0] < 0 else -1
a_sum = 0
for i, ai in enumerate(a):
if i == 0:
a_sum += ai
else:
tmp_sum = a_sum
tmp_sum += ai
if tmp_sum < 0 and a_sum < 0:
c = abs(tmp_sum) + 1
elif tmp_sum > 0 and a_sum > 0:
c = -abs(tmp_sum) - 1
elif tmp_sum == 0 and a_sum < 0:
c = 1
elif tmp_sum == 0 and a_sum > 0:
c = -1
else:
c = 0
count += abs(c)
a_sum = tmp_sum + c
return count
print(min(search(a, False), search(a, True))) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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[100000];
int main() {
int n;
long long sum = 0;
long long ans = 0;
bool flag = true;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
if (a[0] < 0) {
flag = false;
}
sum += a[0];
for (int i = 1; i < n; i++) {
sum += a[i];
if (flag == true) {
if (sum >= 0) {
ans += (sum + 1);
sum = -1;
}
flag = false;
} else {
if (sum <= 0) {
ans += (sum * -1 + 1);
sum = 1;
}
flag = true;
}
cerr << ans << endl;
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = [int(i) for i in input().split()]
sum = a[0]
cnt = 0
for i in range(1,n):
if sum>0:
if sum+a[i]>=0:
cnt+=abs(a[i]+1+sum)
sum = -1
else:
sum +=a[i]
else:
if sum+a[i]<=0:
cnt+=abs(a[i]-1+sum)
sum = 1
else:
sum+=a[i]
print(cnt)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long[] nums = new long[n];
for(int i = 0; i < n; i++) {
nums[i] = sc.nextLong();
}
long[] prefixSum = new long[n];
int minCost = 0;
if(nums[0] == 0) {
nums[0] = 1;
minCost = 1 + count(nums);
nums[0] = -1;
minCost = Math.min(minCost, 1 + count(nums));
} else {
minCost = count(nums);
}
System.out.println(minCost);
}
private static int count(long[] nums) {
int cnt = 0;
long prev = nums[0];
long cur = 0;
for(int i = 1; i < nums.length; i++) {
cur = nums[i] + prev;
if(cur * prev < 0) {
prev = cur;
continue;
}
if(prev > 0) {
cnt += cur + 1;
prev = -1;
} else {
cnt += 1 - cur;
prev = 1;
}
}
return cnt;
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
long[] A = new long[N];
for (int i = 0; i < N; i++) {
A[i] = sc.nextInt();
}
System.out.println( solve(N, A) );
}
private static int solve(int N, long[] A) {
long a0 = A[0];
if( a0 > 0 ) {
int p = solve1(N, A, a0, 0);
int m = solve1(N, A, -1, (int)a0 + 1);
return Math.min(p, m);
} else if( a0 < 0 ) {
int p = solve1(N, A, 1, (int)a0 + 1);
int m = solve1(N, A, a0, 0);
return Math.min(p, m);
} else {
int p = solve1(N, A, 1, 1);
int m = solve1(N, A, -1, 1);
return Math.min(p, m);
}
}
private static int solve1(int N, long[] A, long sum, int ans) {
for (int i = 1; i < N; i++) {
long a = A[i];
if( sum > 0 ) {
// 次はminusになるのを期待
if( a + sum >= 0 ) {
// sumが-1になるような値にまで変更する
// a + sum が 5 の場合、6 だけ操作すると -1 にできる
long diff = a + sum + 1;
ans += diff;
sum = -1;
} else {
sum += a;
}
} else {
if( a + sum <= 0 ) {
long diff = (a + sum) * -1 + 1;
ans += diff;
sum = 1;
} else {
sum += a;
}
}
}
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 | cpp | #include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[]) {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
int sum[n];
for (int i = 0; i < n; i++) sum[i] = 0;
for (int i = 0; i < n; i++) {
if (i == 0)
sum[i] = a[i];
else
sum[i] += sum[i - 1] + a[i];
}
int cost_minus = 0;
int tmp[n];
for (int j = 0; j < n; j++) tmp[j] = sum[j];
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
if (tmp[i] >= 0) {
cost_minus += abs(tmp[i]) + 1;
for (int k = 0; k < n; k++)
if (k >= i) tmp[k] -= cost_minus;
}
} else {
if (tmp[i] <= 0) {
cost_minus += abs(tmp[i]) + 1;
for (int k = 0; k < n; k++)
if (k >= i) tmp[k] += cost_minus;
}
}
}
int cost_plus = 0;
for (int j = 0; j < n; j++) tmp[j] = sum[j];
for (int i = 0; i < n; i++) {
if (i % 2 != 0) {
if (tmp[i] >= 0) {
cost_plus += abs(tmp[i]) + 1;
for (int k = 0; k < n; k++)
if (k >= i) tmp[k] -= cost_plus;
}
} else {
if (tmp[i] <= 0) {
cost_plus += abs(tmp[i]) + 1;
for (int k = 0; k < n; k++)
if (k >= i) tmp[k] += cost_plus;
}
}
}
cout << min(cost_minus, cost_plus) << 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;
class Abc059c {
static public void main(String argv[]) {
Scanner sc = new Scanner(System.in);
int num;
num = sc.nextInt();
int a[] = new int[num];
for (int i = 0; i < num; i++) {
a[i] = sc.nextInt();
}
int nowPN = 0;
int curPN = 0;
nowPN = sign(a[0]);
int sum = a[0];
int op = 0;
int totalop = 0;
for (int i = 1; i < num; i++) {
sum = sum + a[i];
nowPN = -nowPN;
curPN = sign(sum);
// System.out.println("nowPN: " + nowPN + " / curPN: " + curPN);
// System.out.println("a[" + i + "]= " + a[i] + " / sum = " + sum);
if (curPN != nowPN) {
if (nowPN == 1) { // ++++
op = 1 - sum;
sum = sum + op;
} if (nowPN == -1) { // ----
op = sum + 1;
sum = sum - op;
}
totalop += op;
// System.out.println(" op: " + op + " / newsum: " + sum);
}
}
System.out.println(totalop);
}
static public int sign(int a) {
if (a == 0) {
return 0;
} else if (a > 0) {
return 1;
} else if (a < 0) {
return -1;
}
return 0;
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | # coding: utf-8
# Your code here!
n = int(input())
a = list(map(int,input().split()))
def operate(ls,x):
sm = 0
ans = 0
ans += abs(ls[0]-x)
k = x
for i in range(1,len(ls)):
sm += k
if sm > 0:
if abs(ls[i]) <= sm or sm < ls[i]:
ans += abs(ls[i]+sm+1)
k = -sm-1
else:
k = ls[i]
if sm < 0:
if abs(ls[i]) <= abs(sm) or sm > ls[i]:
ans += abs(-sm+1-ls[i])
k = -sm+1
else:
k = ls[i]
return ans
anstot = []
if a[0] > 0:
inv = -1
if a[0] < 0:
inv = 1
if a[0] != 0:
anstot.append(operate(a,a[0]))
anstot.append(operate(a,inv))
if a[0] == 0:
for i in range(n):
if a[i] == 0:
a[i] = -1**(i%2)
elif a[i] != 0:
r = i
break
anstot.append(r+operate(a,a[0]))
for i in range(r):
a[i] = -1**((1+i)%2)
anstot.append(r+operate(a,a[0]))
print(min(anstot)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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];
int i;
int temp = 0;
int ans[2];
ans[0] = 0;
ans[1] = 0;
cin >> N;
for (i = 0; i < N; i++) {
cin >> a[i];
}
for (i = 0; i < N; i++) {
temp = temp + a[i];
if (i % 2 == 1 && temp >= 0) {
ans[0] = ans[0] + temp + 1;
a[i] = a[i] - ans[0];
temp = temp - ans[0];
}
if (i % 2 == 0 && temp <= 0) {
ans[0] = ans[0] + 1 - temp;
a[i] = a[i] + ans[0];
temp = temp + ans[0];
}
}
temp = 0;
for (i = 0; i < N; i++) {
temp = temp + a[i];
if (i % 2 == 0 && temp >= 0) {
ans[1] = ans[1] + temp + 1;
a[i] = a[i] - ans[1];
temp = temp - ans[1];
}
if (i % 2 == 1 && temp <= 0) {
ans[1] = ans[1] + 1 - temp;
a[i] = a[i] + ans[1];
temp = temp + ans[1];
}
}
if (ans[0] >= ans[1]) cout << ans[1] << endl;
if (ans[0] < ans[1]) cout << ans[0] << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> data(N);
for (int i = 0; i < N; i++) {
cin >> data[i];
}
int64_t count_odd, count_even;
count_odd = 0;
count_even = 0;
int64_t sum_odd = 0;
int64_t sum_even = 0;
for (int i = 0; i < N; i++) {
sum_even += data[i];
if (i % 2 == 0) {
if (sum_even >= 0) {
count_even += (sum_even + 1);
sum_even = -1;
}
} else {
if (sum_even <= 0) {
count_even -= (sum_even - 1);
sum_even = 1;
}
}
}
for (int i = 0; i < N; i++) {
sum_odd += data[i];
if (i % 2 == 0) {
if (sum_odd <= 0) {
count_odd -= (sum_odd - 1);
sum_odd = 1;
}
} else {
if (sum_odd >= 0) {
count_odd += (sum_odd + 1);
sum_odd = 1;
}
}
}
cout << min(count_even, count_odd) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
int inf = 1e9 + 1000;
long long infi = 1e18 + 100;
long long n;
long long a[100005];
int main() {
cin >> n;
for (int i = 0; i <= (int)(n - 1); i++) cin >> a[i];
a[n] = 0;
long long sum = 0;
long long ans = 0;
for (int i = 0; i <= (int)(n - 1); i++) {
long long p = sum;
sum += a[i];
if (p < 0 && sum <= 0) {
ans += (1 - sum);
sum = 1;
} else if (p > 0 && sum >= 0) {
ans += (sum + 1);
sum = -1;
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int a(int arr[], int n) {
int sum = arr[0];
int c = 0;
for (int i = 1; i < n; i++) {
if (sum > 0) {
if (sum + arr[i] < 0)
sum = sum + arr[i];
else {
c += (sum + arr[i]) + 1;
sum = -1;
}
} else {
if (sum + arr[i] > 0)
sum = sum + arr[i];
else {
c += abs(sum + arr[i]) + 1;
sum = 1;
}
}
}
return c;
}
int main() {
ios_base ::sync_with_stdio(false);
cin.tie(NULL);
;
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) cin >> arr[i];
int ans1 = a(arr, n);
int ans2 = 2 * abs(arr[0]);
arr[0] = -arr[0];
ans2 += a(arr, n);
cout << 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 | UNKNOWN | #include <bits/stdc++.h>
int main(void) {
int n, a;
int i, check = 0;
long long int count = 0, sum = 0;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &a);
sum += a;
if (check == 1 && sum >= 0) {
count += (1 + sum);
sum = -1;
} else if (check == -1 && sum <= 0) {
count += (1 - sum);
sum = 1;
}
if (sum > 0) {
check = 1;
} else {
check = -1;
}
}
printf("%lld", count);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
int n;
cin >> n;
ll ary[n], cnt = 0;
for (int i = 0; i < (n); i++) cin >> ary[i];
ll sum = ary[0];
for (int i = 0; i < (n - 1); i++) {
if (sum * (sum + ary[i + 1]) >= 0) {
cnt += abs(-(sum + (sum > 0 ? 1 : -1)) - ary[i + 1]);
ary[i + 1] = -(sum + (sum > 0 ? 1 : -1));
}
sum += ary[i + 1];
}
cout << cnt << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | 'use strict'
let input = require("fs").readFileSync("/dev/stdin", "utf8");
let Nums = input.split('\n');
let amount = Nums[0]*1;
let arr = Nums[1].split(" ").map(x => x*1);
let sum = 0;
let ans = 0;
// 一番最初の正負判定フラグ
let isInitPlus = arr[0] > 0 ? true : false;
for(let i = 0; i < amount; i++){
// 和
sum += arr[i];
if((sum > 0) != isInitPlus){
ans += Math.abs(sum) + 1;
sum = isInitPlus == true? 1 : -1;
}
isInitPlus =!isInitPlus;
}
if(sum == 0){
ans += 1;
}
console.log(ans); |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException{
Sequence solver = new Sequence();
solver.readInput();
solver.solve();
solver.writeOutput();
}
static class Sequence {
private int n;
private int a[];
private int output;
private Scanner scanner;
public Sequence() {
this.scanner = new Scanner(System.in);
}
public void readInput() {
n = Integer.parseInt(scanner.next());
a = new int[n];
for(int i=0; i<n; i++) {
a[i] = Integer.parseInt(scanner.next());
}
}
private int count(boolean sign) {
int count=0;
int sum=0;
for(int i=0; i<n; i++) {
sum += a[i];
if((i%2==0) == sign) {
// a[i]までの合計を正にするとき
if(sum<=0) {
count += Math.abs(sum)+1;
sum = 1;
}
} else {
// a[i]までの合計を負にするとき
if(0<=sum) {
count += Math.abs(sum)+1;
sum = -1;
}
}
}
return count;
}
public void solve() {
output = Math.min(this.count(true), this.count(false));
}
public void writeOutput() {
System.out.println(output);
}
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n=int(input())
N=list(map(int,input().split()))
emptylists=[]
number=0
for i in range(n):
number+=N[i]
emptylists.append(number)
emptylists.append(1)
ans1=0
i=0
use=0
while i<n:
#偶数番が正、奇数番が負の時
if i%2==0:
#奇数番め
# if emptylists[i]+use<0:
if emptylists[i]+use>=0:
ans1+=emptylists[i]+use+1
use=-1-emptylists[i]
if i%2!=0:
#偶数番め
# if emptylists[i]+use>0:
if emptylists[i]+use<=0:
ans1+=1-emptylists[i]-use
use=1-emptylists[i]
i+=1
ans2=0
j=0
uses=0
while j<n:
#偶数番が正、奇数番が負の時
if j%2==0:
#奇数番め
# if emptylists[i]+uses<0:
if emptylists[j]+uses>=0:
ans2+=emptylists[j]+uses+1
uses=-1-emptylists[j]
if j%2!=0:
#偶数番め
# if emptylists[j]+uses>0:
if emptylists[j]+uses<=0:
ans2+=1-emptylists[j]-uses
uses=1-emptylists[j]
j+=1
print(min(ans1,ans2))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
vector<int> num;
long long sequence(int s1, int c1, int s2, int c2) {
for (int i = 1; i < num.size(); i++) {
if (s1 > 0) {
if (s1 + num[i] >= 0) {
c1 += 1 + num[i] + s1;
s1 = -1;
} else {
s1 += num[i];
}
} else {
if (s1 + num[i] <= 0) {
c1 += 1 - num[i] - s1;
s1 = 1;
} else {
s1 += num[i];
}
}
if (s2 > 0) {
if (s2 + num[i] >= 0) {
c2 += 1 + num[i] + s2;
s2 = -1;
} else {
s2 += num[i];
}
} else {
if (s2 + num[i] <= 0) {
c2 += 1 - num[i] - s2;
s2 = 1;
} else {
s2 += num[i];
}
}
}
return min(c1, c2);
}
int main() {
int n;
scanf("%d", &n);
num.resize(n);
for (int i = 0; i < n; i++) scanf("%d", &num[i]);
long long ans;
if (num[0] == 0) {
ans = sequence(1, 1, -1, 1);
} else if (num[0] > 0) {
ans = sequence(num[0], 0, -1, num[0] + 1);
} else {
ans = sequence(1, abs(num[0]) + 1, num[0], 0);
}
printf("%lld", 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())
l=[int(i) for i in input().split()]
sm=l[0]
req=0
if sm==0:
sm+=1
req+=1
pr=1
elif sm<0:
pr=0
else:
pr=1
for i in range(1,n):
if pr==1:
n1=sm+l[i]
if n1<0:
sm+=l[i]
pass
else:
sm+=l[i]
req+=sm+1
sm=-1
else:
n1=sm+l[i]
if n1>0:
sm+=l[i]
pass
else:
sm+=l[i]
req+=(-sm+1)
sm=1
pr=1-pr
print(req)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
const long long LLINF = 1LL << 60;
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
long long int i, n;
long long int sum = 0, ans = 0, minans;
cin >> n;
vector<int> v(n, 0), w(n, 0);
for (i = 0; i < n; i++) {
cin >> v[i];
}
sum = v[0];
w[0] = v[0];
for (i = 1; i < n; i++) {
if ((sum < 0 && sum + v[i] > 0) || (sum > 0 && sum + v[i] < 0)) {
w[i] = v[i];
sum += w[i];
continue;
}
if (sum < 0) {
ans += abs(-1 * sum + 1 - v[i]);
w[i] = -1 * sum + 1;
sum += w[i];
if (sum == 0) {
w[i]++;
sum++;
}
} else {
ans += abs(-1 * sum - 1 - v[i]);
w[i] = -1 * sum - 1;
sum += w[i];
if (sum == 0) {
w[i]--;
sum--;
}
}
}
minans = ans;
ans = 0;
w.clear();
sum = -1 * v[0];
w[0] = -1 * v[0];
for (i = 1; i < n; i++) {
if (sum < 0) {
ans += abs(-1 * sum + 1 - v[i]);
w[i] = -1 * sum + 1;
sum += w[i];
if (sum == 0) {
w[i]++;
sum++;
}
} else {
ans += abs(-1 * sum - 1 - v[i]);
w[i] = -1 * sum - 1;
sum += w[i];
if (sum == 0) {
w[i]--;
sum--;
}
}
}
minans = min(ans, minans);
cout << minans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long int n;
cin >> n;
long long int i;
vector<long long int> v(n);
vector<long long int> prefix(n);
for (i = 0; i < n; i++) cin >> v[i];
prefix[0] = v[0];
long long int ans = 0;
for (i = 1; i < n; i++) {
prefix[i] = prefix[i - 1] + v[i];
long long int check = prefix[i - 1] * prefix[i];
if (check >= 0) {
ans = ans + abs(prefix[i]) + 1;
if (prefix[i - 1] < 0)
prefix[i] = 1;
else
prefix[i] = -1;
}
}
cout << ans << "\n";
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int calc(vector<int>& t, bool topPlus) {
int parityA, parityB;
if (topPlus) {
parityA = 0;
parityB = 1;
} else {
parityA = 1;
parityB = 0;
}
int sum = 0;
int cnt = 0;
for (int i = 0; i < t.size(); ++i) {
sum += t.at(i);
if (i % 2 == parityA && sum <= 0) {
cnt += (1 - sum);
sum = 1;
} else if (i % 2 == parityB && sum >= 0) {
cnt += (1 + sum);
sum = -1;
}
}
return cnt;
}
int main() {
int N;
cin >> N;
vector<int> t(N);
for (int i = 0; i < N; ++i) {
cin >> t.at(i);
}
int cnt1 = calc(t, true);
int cnt2 = calc(t, false);
int cnt = min(cnt1, cnt2);
cout << cnt << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void) {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<int> vi = vector<int>(n, 0);
for (int i = 0; i < n; ++i) {
cin >> vi[i];
}
int target;
if (vi[0] > 0)
target = 1;
else
target = -1;
int sum = 0;
int cnt = 0;
for (int i = 0; i < n; ++i) {
sum += vi[i];
if (sum * target <= 0) {
cnt += (target > 0) ? target - sum : sum - target;
sum = target;
}
target = -target;
}
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 | let id x = x
let n = Scanf.scanf "%d\n" id
let l = Array.init n (fun _ -> Scanf.scanf "%d " id) |> Array.to_list
let c = ref 0
let () =
let _ =
List.fold_left
(fun sum el ->
if sum < 0 then
let new_sum = sum + el in
if new_sum > 0 then new_sum
else
let () = c := !c + abs new_sum + 1 in
1
else
let new_sum = sum + el in
if new_sum < 0 then new_sum
else
let () = c := !c + new_sum + 1 in
-1)
(List.hd l) (List.tl l)
in
Printf.printf "%d\n" !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 | java | import java.util.Scanner;
public class Main {
public static double sequence(int a[], double start) {
double count = 0.0, presum = -1.0 * start, sum = 0.0;
for(int i : a) {
sum += (double)i;
if(i == 0)sum += start;
if(sum * presum > 0) {
double min = Math.abs(sum) + 1;
if(presum > 0)sum -= min;
else sum += min;
count += min;
}
if(sum == 0) {
if(presum > 0)sum--;
else sum++;
++count;
}
presum = sum;
}
return count;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n, a[];
double count = 0;
n = sc.nextInt();
a = new int[n];
for(int i = 0; i < n; ++i) a[i] = sc.nextInt();
sc.close();
if(a[0] == 0)a[0]++;
int tmp = Math.abs(a[0]) + 1;
if(a[0] > 0)tmp = a[0] - tmp;
else tmp = a[0] + tmp;
count = Math.min(sequence(a, (double)a[0]),sequence(a, (double)tmp));
System.out.printf("%.0f\n", 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() {
long long n;
long long a[100000];
bool flag = true;
long long sum = 0;
long long ans = 0;
cin >> n;
for (int i = 0; i < (int)(n); i++) {
cin >> a[i];
}
if (a[0] > 0) {
flag = true;
} else if (a[0] < 0) {
flag = false;
} else {
if (a[1] >= 0) {
a[0] = -1;
flag = false;
ans = 1;
} else {
a[0] = 1;
flag = true;
ans = 1;
}
}
sum = a[0];
for (int i = (int)(1); i < (int)(n); i++) {
sum += a[i];
if (flag == true) {
if (sum >= 0) {
ans += sum + 1;
sum = -1;
}
}
if (flag == false) {
if (sum <= 0) {
ans += -sum + 1;
sum = 1;
}
}
if (sum > 0) {
flag = true;
} else {
flag = false;
}
}
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>
auto calc(std::vector<int64_t>& vec, int64_t sum) -> uint64_t {
uint64_t result = 0;
assert(sum != 0);
bool is_sum_negative = sum < 0;
for (int i = 1; i < vec.size(); ++i) {
int tmp_sum = sum + vec[i];
auto tmp = std::abs(sum) + 1;
if (is_sum_negative) {
if (tmp_sum <= 0) {
sum += tmp;
result += std::abs(tmp - vec[i]);
} else {
sum = tmp_sum;
}
} else {
if (tmp_sum >= 0) {
sum -= tmp;
result += std::abs(-tmp - vec[i]);
} else {
sum = tmp_sum;
}
}
is_sum_negative = !is_sum_negative;
}
return result;
}
int main(int argc, char const* argv[]) {
uint64_t n;
std::cin >> n;
auto vec = std::vector<int64_t>(n);
for (auto& v : vec) {
std::cin >> v;
}
int64_t sum = vec[0];
auto result_0 = std::numeric_limits<uint64_t>::max();
auto result_1 = result_0;
if (sum == 0) {
sum = -1;
result_0 = 1;
result_0 += calc(vec, sum);
sum = 1;
result_1 = 1;
result_1 += calc(vec, sum);
} else {
result_0 = calc(vec, sum);
}
auto result = std::min(result_0, result_1);
std::cout << result << std::endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long N;
cin >> N;
long A[N];
for (long i = 0; i < N; i++) cin >> A[i];
long numSign = 1, sum = 0, actNum = 0;
for (long i = 0; i < N; i++) {
if (A[i] > 0)
break;
else if (A[i] < 0) {
numSign *= -1;
break;
} else
numSign *= -1;
}
for (long i = 0; i < N; i++) {
sum += A[i];
if (numSign == 1) {
if (sum <= 0) {
actNum += 1 - sum;
sum = 1;
}
} else {
if (sum >= 0) {
actNum += sum - -1;
sum = -1;
}
}
numSign *= -1;
}
cout << actNum << 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()))
now_a = a[0]
count = 0
'''
True = positive
False = negative
'''
sign = True
if now_a < 0:
sign = False
for i in range(1, n):
next_a = now_a + a[i]
if sign:
if next_a >= 0:
count += next_a + 1
now_a = -1
else:
now_a = next_a
sign = False
else:
if next_a <= 0:
count += abs(next_a) + 1
now_a = 1
else:
now_a = next_a
sign = True
print(count)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[100100];
for (int i = 0; i < n; i++) cin >> a[i];
long long ans = 0, sum = a[0];
if (sum == 0) {
sum++;
ans++;
}
int sign = (sum > 0 ? 1 : -1);
for (int i = 1; i < n; i++) {
sum += a[i];
if (sum == 0) {
sum += -1 * sign;
ans++;
} else if (sign > 0 && sum > 0) {
long long x = sum + 1;
sum -= x;
ans += x;
} else if (sign < 0 && sum < 0) {
long long y = abs(sum) + 1;
sum += y;
ans += y;
}
sign *= -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;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a.at(i);
}
int64_t sumi = 0, val1 = 0;
int ne = 0;
if (a.at(0) == 0) {
while (a.at(ne) == 0) {
if (ne == 0)
val1++;
else
val1 += 2;
ne++;
if (ne == n) break;
}
}
int64_t val2 = val1;
for (int i = ne; i < n; i++) {
if (a.at(0) == 0) {
if (ne % 2 == 0)
sumi = -1;
else
sumi = 1;
}
if (i == 0) {
sumi = a.at(i);
continue;
}
if (i % 2 == 1) {
if (sumi + a.at(i) < 0)
sumi += a.at(i);
else {
val1 += (sumi + a.at(i) + 1);
sumi = -1;
}
} else {
if (sumi + a.at(i) > 0)
sumi += a.at(i);
else {
val1 += (abs(sumi + a.at(i)) + 1);
sumi = 1;
}
}
}
for (int i = ne; i < n; i++) {
if (a.at(0) == 0) {
if (ne % 2 == 0)
sumi = 1;
else
sumi = -1;
}
if (i == 0) {
sumi = a.at(i);
continue;
}
if (i % 2 == 1) {
if (sumi + a.at(i) > 0)
sumi += a.at(i);
else {
val2 += (abs(sumi + a.at(i)) + 1);
sumi = 1;
}
} else {
if (sumi + a.at(i) < 0)
sumi += a.at(i);
else {
val2 += (sumi + a.at(i) + 1);
sumi = -1;
}
}
}
cout << min(val1, val2) << 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 <typename T>
using V = vector<T>;
template <typename T, typename U>
using P = pair<T, U>;
int n;
V<ll> a;
int solve(bool first_plus) {
bool will_plus = first_plus;
int manip_count = 0;
ll sum = 0;
for (int i = 0; i < (n); ++i) {
int not_enough = 0;
if (will_plus) {
if (!(sum + a[i] > 0)) {
not_enough = 1 - (sum + a[i]);
manip_count += not_enough;
}
} else {
if (!(sum + a[i] < 0)) {
not_enough = -1 - (sum + a[i]);
manip_count += abs(not_enough);
}
}
sum += a[i] + not_enough;
will_plus = !will_plus;
}
return manip_count;
}
int main() {
cin >> n;
a.resize(n);
for (int i = 0; i < (n); ++i) {
cin >> a[i];
}
int first_plus = solve(true);
int first_minus = solve(false);
cout << min(first_plus, first_minus) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n;
int flag[100005], k[100005];
long long a[100005], sum[100005], ans, b[100005], tot[100005], ant;
int main() {
int m = 0;
scanf("%d", &n);
scanf("%lld", &a[1]);
b[1] = a[1];
sum[1] = a[1];
tot[1] = sum[1];
if (sum[1] > 0) flag[1] = 1;
if (sum[1] < 0) flag[1] = 0;
if (sum[1] == 0) m = 1;
if (m == 0) {
for (int i = 2; i <= n; i++) {
scanf("%lld", &a[i]);
sum[i] = a[i] + sum[i - 1];
if (sum[i] > 0) flag[i] = 1;
if (sum[i] < 0) flag[i] = 0;
if (flag[i - 1] == 1) {
if (sum[i] >= 0) {
ans += sum[i] + 1;
sum[i] = -1;
flag[i] = 0;
}
} else {
if (sum[i] <= 0) {
ans += 1 - sum[i];
sum[i] = 1;
flag[i] = 1;
}
}
}
printf("%lld\n", ans);
} else {
for (int i = 2; i <= n; i++) {
scanf("%lld", &a[i]);
flag[1] = 0;
ans = 1;
b[i] = a[i];
sum[i] = a[i] + sum[i - 1];
if (sum[i] > 0) flag[i] = 1;
if (sum[i] < 0) flag[i] = 0;
if (flag[i - 1] == 1) {
if (sum[i] >= 0) {
ans += sum[i] + 1;
sum[i] = -1;
flag[i] = 0;
}
} else {
if (sum[i] <= 0) {
ans += 1 - sum[i];
sum[i] = 1;
flag[i] = 1;
}
}
}
k[1] = 1;
ant = 1;
for (int i = 2; i <= n; i++) {
tot[i] = b[i] + tot[i - 1];
if (tot[i] > 0) k[i] = 1;
if (tot[i] < 0) k[i] = 0;
if (k[i - 1] == 1) {
if (tot[i] >= 0) {
ant += tot[i] + 1;
tot[i] = -1;
k[i] = 0;
}
} else {
if (tot[i] <= 0) {
ant += 1 - tot[i];
tot[i] = 1;
k[i] = 1;
}
}
}
printf("%lld\n", min(ant, ans));
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
for (int j = 0; j < n; j++) {
cin >> a[j];
}
int ans = 0;
int sum = a[0];
for (int i = 1; i < n; i++) {
int sum_old = sum;
sum = sum + a[i];
if (sum * sum_old < 0) {
} else {
ans = ans + abs(sum) + 1;
sum = (sum_old / abs(sum_old)) * -1;
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll llbs(ll x) {
if (x < 0) return -x;
return x;
}
int main() {
int N;
cin >> N;
vector<ll> a(N);
for (int i = 0; i < N; i++) cin >> a.at(i);
ll sumO = a.at(0), sumE = a.at(0), countO = 0, countE = 0;
for (int i = 1; i < N; i++) {
ll O = a.at(i), E = a.at(i);
if (i % 2 == 0) {
if (sumE + E <= 0) {
countE += llbs(1 - (sumE + E));
E = 1 - sumE;
}
if (sumO + O >= 0) {
countO += llbs(-1 - (sumO + O));
O = -1 - sumO;
}
} else {
if (sumO + O <= 0) {
countO += llbs(1 - (sumO + O));
O = 1 - sumO;
}
if (sumE + E >= 0) {
countE += llbs(-1 - (sumE + E));
E = -1 - sumE;
}
}
sumE += E;
sumO += O;
}
cout << min(countE, countO) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long sum = a[0], kaisu = 0;
if (sum == 0) {
kaisu++;
sum++;
}
for (int i = 1; i < n; i++) {
long long presum = sum;
sum += a[i];
if (presum > 0) {
if (sum >= 0) {
kaisu += sum + 1;
a[i] = a[i] - sum - 1;
sum = -1;
}
}
if (presum < 0) {
if (sum <= 0) {
sum = 1;
kaisu += 1 - sum;
}
}
}
cout << kaisu << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | def sign(x):
return (x > 0) - (x < 0)
def main():
n = int(input())
a = [int(an) for an in input().split()]
total = a[0]
sign_total = sign(a[0])
ans = 0
for i in range(1, n):
total += a[i]
sign_tmp = sign(total)
if total == 0 or sign_total == sign_tmp:
val = 0
if sign_total > 0:
val = total + 1
ans += val
else:
val = total - 1
ans -= val
total -= val
sign_total *= -1
print(ans)
if __name__ == "__main__":
main()
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
template <class T>
using V = vector<T>;
long long GCD(long long a, long long b) { return b ? GCD(b, a % b) : a; }
long long LCM(long long a, long long b) { return a / GCD(a, b) * b; }
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};
int ddx[8] = {-1, 0, 1, 0, 1, 1, -1, -1};
int ddy[8] = {0, -1, 0, 1, 1, -1, 1, -1};
long long int cmp(pair<long long int, long long int> a,
pair<long long int, long long int> b) {
if (a.second != b.second)
return a.second < b.second;
else
return a.first < b.first;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
long long int n;
cin >> n;
V<long long int> a(n);
for (long long int i = 0; i < n; i++) cin >> a[i];
bool abc = 0;
long long int ans1 = 0, ans2 = 0;
bool yn = 0;
if (a[0] > 0) {
yn = 1;
} else if (a[0] < 0)
yn = 0;
else if (a[0] == 0) {
abc = 1;
yn = 1;
ans1++;
ans2++;
a[0] = 1;
}
long long int sum = a[0];
for (long long int i = 1; i < n; i++) {
if (yn == 1) {
if (sum + a[i] < 0) {
sum += a[i];
} else {
ans1 += sum + a[i] + 1;
sum = -1;
}
} else {
if (sum + a[i] > 0) {
sum += a[i];
} else {
ans1 += 1 - (sum + a[i]);
sum = 1;
}
}
yn = 1 - yn;
}
if (abc == 1) {
yn = 0;
a[0] = -1;
long long int sum = a[0];
for (long long int i = 1; i < n; i++) {
if (yn == 1) {
if (sum + a[i] < 0) {
sum += a[i];
} else {
ans2 += sum + a[i] + 1;
sum = -1;
}
} else {
if (sum + a[i] > 0) {
sum += a[i];
} else {
ans2 += 1 - (sum + a[i]);
sum = 1;
}
}
yn = 1 - yn;
}
}
if (abc == 0)
cout << ans1 << endl;
else
cout << (ans1 < ans2 ? 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;
__int64 dp[100005][2],s[100005][2];
int a[100005];
int main()
{
//freopen("1.txt","w",stdout);
int n;
scanf("%d",&n);
for(int i=0;i<n;++i) scanf("%d",&a[i]);
if(a[0]<0)
{
dp[0][0]=0;
s[0][0]=a[0];
dp[0][1]=1-a[0];
s[0][1]=1;
}
else if(a[0]>0)
{
dp[0][1]=0;
s[0][1]=a[0];
dp[0][0]=1+a[0];
s[0][0]=-1;
}
else
{
dp[0][0]=1;
s[0][0]=-1;
dp[0][1]=1;
s[0][1]=1;
}
for(int i=1;i<n;++i)
{
__int64 x=s[i-1][1];
x+=a[i];
if(x<0)
{
dp[i][0]=0;
s[i][0]=x;
}
else if(x>0)
{
dp[i][0]=1+x;
s[i][0]=-1;
}
else
{
dp[i][0]=1;
s[i][0]=-1;
}
dp[i][0]+=dp[i-1][1];
x=s[i-1][0];
x+=a[i];
if(x<0)
{
dp[i][1]=1-x;
s[i][1]=1;
}
else if(x>0)
{
dp[i][1]=0;
s[i][1]=x;
}
else
{
dp[i][1]=1;
s[i][1]=1;
}
dp[i][1]+=dp[i-1][0];
}
printf("%I64d\n",min(dp[n-1][0],dp[n-1][1]));
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long INF = 300000000;
const long long MOD = 1000000007;
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
int main() {
int n;
cin >> n;
long long a[100100];
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
long long ans = INF;
for (int i = 0; i < 2; ++i) {
long long count = 0;
int su = 0;
for (int j = 0; j < n; ++j) {
su += a[j];
if (i == 0) {
if (j % 2 == 0 && su <= 0) {
count += -su + 1;
su = 1;
} else if (j % 2 == 1 && su >= 0) {
count += su + 1;
su = -1;
}
}
if (i == 1) {
if (j % 2 == 0 && su >= 0) {
count += su + 1;
su = -1;
} else if (j % 2 == 1 && su <= 0) {
count += -su + 1;
su = 1;
}
}
}
ans = min(ans, count);
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int,input().split()))
ans = 0
for i in range(n):
if i == 0:
if a[i] == 0:
f = 1
a[i] = 1
elif a[0] > 0:
f = 1
elif a[0] < 0:
f = 0
else:
o = sum(a[:i])
if f == 1:
if a[i] + o > 0:
c = -1 - o
ans += abs(c - a[i])
a[i] = c
f = 0
else:
if a[i] + o == 0:
a[i] -= 1
ans += 1
f = 0
elif f == 0:
if a[i] + o < 0:
c = 1 - o
ans += abs(c - a[i])
a[i] = c
f = 1
else:
if a[i] + o == 0:
a[i] += 1
ans += 1
f = 1
#print(a)
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
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 (a > b) {
a = b;
return 1;
}
return 0;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
long long N;
cin >> N;
vector<long long> A(N, 0);
for (long long i = 0; i < (long long)(N); i++) {
cin >> A[i];
}
long long ansP = 0, ansM = 0;
long long sumP = 0, sumM = 0;
for (long long i = 0; i < (long long)(N); i++) {
if (sumP <= 0) {
if (A[i] >= 0) {
sumP += A[i];
if (sumP <= 0) {
ansP += 1 - sumP;
sumP = 1;
}
} else {
ansP += 1 - A[i];
sumP += 1;
if (sumP <= 0) {
ansP += 1 - sumP;
sumP = 1;
}
}
} else {
if (A[i] <= 0) {
sumP += A[i];
if (sumP >= 0) {
ansP += 1 + sumP;
sumP = -1;
}
} else {
ansP += 1 + A[i];
sumP += -1;
if (sumP >= 0) {
ansP += 1 + sumP;
sumP = 1;
}
}
}
if (sumM < 0) {
if (A[i] >= 0) {
sumM += A[i];
if (sumM <= 0) {
ansM += 1 - sumM;
sumM = 1;
}
} else {
ansM += 1 - A[i];
sumM += 1;
if (sumM <= 0) {
ansM += 1 - sumM;
sumM = 1;
}
}
} else {
if (A[i] <= 0) {
sumM += A[i];
if (sumM >= 0) {
ansM += 1 + sumM;
sumM = -1;
}
} else {
ansM += 1 + A[i];
sumM += -1;
if (sumM >= 0) {
ansM += 1 + sumM;
sumM = -1;
}
}
}
}
long long ans = min(ansP, ansM);
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 | def sign(x):
if x<0:
return -1
elif x>0:
return 1
else:
retrun 0
n = int(input())
a = list(map(int,input().split()))
cumulative_sum = a[0]
flag = sign(cumulative_sum)
ans = 0
for i in range(1,n):
cumulative_sum += a[i]
if sign(cumulative_sum) == flag or sign(cumulative_sum) == 0:
ans += abs(-flag - cumulative_sum)
flag = sign(cumulative_sum)
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = scan.nextLong();
}
long sum1 = 0;
long sum2 = 0;
long ans1 = 0;
long ans2 = 0;
for (int i = 0; i < n; i++) {//偶数添字が正
sum1 += a[i];
if (i%2 == 0) {
if (sum1 > 0) continue;
else {
ans1 += (1 + Math.abs(sum1));
sum1 = 1;
}
}
else if (i%2 == 1) {
if (sum1 < 0) continue;
else {
ans1 += (sum1 + 1);
sum1 = -1;
}
}
}
for (int i = 0; i < n; i++) {//奇数添字が正
sum2 += a[i];
if (i%2 == 1) {
if (sum2 > 0) continue;
else {
ans2 += (1 + Math.abs(sum2));
sum2 = 1;
}
}
else if (i%2 == 0) {
if (sum2 < 0) continue;
else {
ans2 += (sum2 + 1);
sum2 = -1;
}
}
}
if (ans1 == 0) {
ans1++;
}
if (ans2 == 0) {
ans2++;
}
System.out.println(Math.min(ans1, ans2));
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | def f(a)
prev = a[0]
total = 0
g = lambda{|car, cdr|
total = 0
sum = car
cdr.each{|curr|
case sum <=> 0
when 1;
new_curr = [curr, -sum-1].min
sum += new_curr
total += curr - new_curr
when -1;
new_curr = [curr, -sum+1].max
sum += new_curr
total += new_curr - curr
end
}
total += 1 if sum == 0
total
}
x = g.(a[0], a[1..-1])
y = g.(a[0] > 0 ? -1 : 1, a[1..-1])
[x, y].min
end
N = gets.to_i
A = gets.split.take(N).map(&:to_i)
p f(A)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
int i = 0;
long long sum, count = 0;
if (a[0] == 0) {
while (a[i] == 0) {
i++;
}
if (i == 1) {
sum = (a[i] > 0) ? -1 : 1;
count = 1;
} else if (i > 1) {
sum = (a[i] > 0) ? -1 : 1;
count = 1 + 2 * (i - 1);
}
} else {
sum = a[0];
i = 1;
}
while (i < n) {
if (sum > 0) {
if (sum + a[i] >= 0) {
count += abs(a[i] - (-1 - sum));
a[i] = -1 - sum;
}
} else {
if (sum + a[i] <= 0) {
count += abs(a[i] - (1 - sum));
a[i] = 1 - sum;
}
}
sum += a[i];
i++;
}
cout << count << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using mii = map<int, int>;
using pqls = priority_queue<long long>;
using pqlg = priority_queue<long long, vector<long long>, greater<long long>>;
using mll = map<long long, long long>;
using pll = pair<long long, long long>;
using sll = set<long long>;
long long divup(long long a, long long b);
long long kaijou(long long i);
long long P(long long n, long long k);
long long C(long long n, long long k);
long long GCD(long long a, long long b);
long long LCM(long long a, long long b);
bool prime(long long N);
double distance(vector<long long> p, vector<long long> q, long long n);
void press(vector<long long> &v);
void ranking(vector<long long> &v);
void erase(vector<long long> &v, long long i);
void unique(vector<long long> &v);
void printv(vector<long long> v);
vector<ll> keta(ll x);
long long modpow(long long a, long long n, long long mod);
long long modinv(long long a, long long mod);
vector<long long> inputv(long long n);
vector<long long> yakusuu(int n);
map<long long, long long> soinsuu(long long n);
vector<vector<long long>> maze(long long i, long long j, vector<string> &s);
vector<long long> eratos(long long n);
set<long long> eraset(long long n);
long long divup(long long a, long long b) {
long long x = abs(a);
long long y = abs(b);
long long z = (x + y - 1) / y;
if ((a < 0 && b > 0) || (a > 0 && b < 0))
return -z;
else if (a == 0)
return 0;
else
return z;
}
long long kaijou(long long i) {
if (i == 0) return 1;
long long j = 1;
for (long long k = 1; k <= i; k++) {
j *= k;
}
return j;
}
long long P(long long n, long long k) {
if (n < k) return 0;
long long y = 1;
for (long long i = 0; i < k; i++) {
y *= (n - i);
}
return y;
}
long long C(long long n, long long k) {
if (n < k) return 0;
return P(n, k) / kaijou(k);
}
long long GCD(long long a, long long b) {
if (a < b) swap(a, b);
long long d = a % b;
if (d == 0) {
return b;
}
return GCD(b, d);
}
long long LCM(long long a, long long b) { return (a / GCD(a, b)) * b; }
bool prime(long long N) {
if (N == 1) {
return false;
}
if (N < 0) return false;
long long p = sqrt(N);
for (long long i = 2; i <= p; i++) {
if (N % i == 0) {
return false;
}
}
return true;
}
double distance(vector<long long> p, vector<long long> q, long long n) {
double x = 0;
for (long long i = 0; i < n; i++) {
x += pow((p.at(i) - q.at(i)), 2);
}
return sqrt(x);
}
void press(vector<long long> &v) {
long long n = v.size();
vector<long long> w(n);
map<long long, long long> m;
for (auto &p : v) {
m[p] = 0;
}
long long i = 0;
for (auto &p : m) {
p.second = i;
i++;
}
for (long long i = 0; i < n; i++) {
w.at(i) = m[v.at(i)];
}
v = w;
return;
}
void ranking(vector<long long> &v) {
long long n = v.size();
map<long long, long long> m;
long long i;
for (i = 0; i < n; i++) {
m[v.at(i)] = i;
}
vector<long long> w(n);
i = 0;
for (auto &p : m) {
v.at(i) = p.second;
i++;
}
return;
}
void erase(vector<long long> &v, long long i) {
long long n = v.size();
if (i > n - 1) return;
for (long long j = i; j < n - 1; j++) {
v.at(j) = v.at(j + 1);
}
v.pop_back();
return;
}
void unique(vector<long long> &v) {
long long n = v.size();
set<long long> s;
long long i = 0;
while (i < n) {
if (s.count(v.at(i))) {
erase(v, i);
n--;
} else {
s.insert(v.at(i));
i++;
}
}
return;
}
void printv(vector<long long> v) {
cout << "{ ";
for (auto &p : v) {
cout << p << ",";
}
cout << "}" << endl;
}
vector<ll> keta(ll x) {
if (x == 0) return {0};
ll n = log10(x) + 1;
vll w(n, 0);
for (ll i = 0; i < n; i++) {
ll p;
p = x % 10;
x = x / 10;
w[n - 1 - i] = p;
}
return w;
}
long long modpow(long long a, long long n, long long mod) {
long long res = 1;
while (n > 0) {
if (n & 1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
long long modinv(long long a, long long mod) { return modpow(a, mod - 2, mod); }
vector<long long> inputv(long long n) {
vector<long long> v(n);
for (long long i = 0; i < n; i++) {
cin >> v[i];
}
return v;
}
vector<long long> yakusuu(long long n) {
vector<long long> ret;
for (long long i = 1; i <= sqrt(n); ++i) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n) {
ret.push_back(n / i);
}
}
}
sort(ret.begin(), ret.end());
return ret;
}
map<long long, long long> soinsuu(long long n) {
map<long long, long long> m;
long long p = sqrt(n);
while (n % 2 == 0) {
n /= 2;
if (m.count(2)) {
m[2]++;
} else {
m[2] = 1;
}
}
for (long long i = 3; i * i <= n; i += 2) {
while (n % i == 0) {
n /= i;
if (m.count(i)) {
m[i]++;
} else {
m[i] = 1;
}
}
}
if (n != 1) m[n] = 1;
return m;
}
vector<vector<long long>> maze(ll i, ll j, vector<string> &s) {
ll h = s.size();
ll w = s[0].size();
queue<vector<long long>> q;
vector<vector<long long>> dis(h, vll(w, -1));
q.push({i, j});
dis[i][j] = 0;
while (!q.empty()) {
auto v = q.front();
q.pop();
if (v[0] > 0 && s[v[0] - 1][v[1]] == '.' && dis[v[0] - 1][v[1]] == -1) {
dis[v[0] - 1][v[1]] = dis[v[0]][v[1]] + 1;
q.push({v[0] - 1, v[1]});
}
if (v[1] > 0 && s[v[0]][v[1] - 1] == '.' && dis[v[0]][v[1] - 1] == -1) {
dis[v[0]][v[1] - 1] = dis[v[0]][v[1]] + 1;
q.push({v[0], v[1] - 1});
}
if (v[0] < h - 1 && s[v[0] + 1][v[1]] == '.' && dis[v[0] + 1][v[1]] == -1) {
dis[v[0] + 1][v[1]] = dis[v[0]][v[1]] + 1;
q.push({v[0] + 1, v[1]});
}
if (v[1] < w - 1 && s[v[0]][v[1] + 1] == '.' && dis[v[0]][v[1] + 1] == -1) {
dis[v[0]][v[1] + 1] = dis[v[0]][v[1]] + 1;
q.push({v[0], v[1] + 1});
}
}
return dis;
}
long long modC(long long n, long long k, long long mod) {
if (n < k) return 0;
long long p = 1, q = 1;
for (long long i = 0; i < k; i++) {
p = p * (n - i) % mod;
q = q * (i + 1) % mod;
}
return p * modinv(q, mod) % mod;
}
long long POW(long long a, long long n) {
long long res = 1;
while (n > 0) {
if (n & 1) res = res * a;
a = a * a;
n >>= 1;
}
return res;
}
vector<long long> eratos(long long n) {
if (n < 2) return {};
vll v(n - 1);
for (long long i = 0; i < n - 1; i++) {
v[i] = i + 2;
}
ll i = 0;
while (i < n - 1) {
ll p = v[i];
for (ll j = i + 1; j < n - 1; j++) {
if (v[j] % p == 0) {
v.erase(v.begin() + j);
n--;
}
}
i++;
}
v.resize(n - 1);
return v;
}
set<long long> eraset(long long n) {
set<long long> s;
vll v = eratos(n);
for (auto &t : v) {
s.insert(t);
}
return s;
}
vll line(ll x1, ll y1, ll x2, ll y2) {
vector<ll> v(3);
v[0] = y1 - y2;
v[1] = x2 - x1;
v[2] = -x1 * (y1 - y2) + y1 * (x1 - x2);
return v;
}
double dis(vll v, ll x, ll y) {
double s = sqrt(v[0] * v[0] + v[1] * v[1]);
return (double)abs(v[0] * x + v[1] * y + v[2]) / s;
}
ll const mod = 1e9 + 7;
int main() {
ll n;
cin >> n;
auto a = inputv(n);
ll l = 0;
ll res = 0;
for (long long i = 0; i < n; i++) {
if (l == 0 && a[0] == 0) {
for (long long j = 0; j < n - 1; j++) {
if (a[j + 1] != 0) {
a[0] = a[j + 1] / abs(a[j + 1]);
if ((j + 1) & 1) a[0] *= (-1);
break;
}
}
if (!a[0]) a[0] = 1;
res++;
} else if (l < 0) {
if (a[i] < -l + 1) {
res += -l + 1 - a[i];
a[i] = -l + 1;
l = 1;
} else {
l += a[i];
}
} else if (l > 0) {
if (a[i] > -l - 1) {
res += abs(a[i] - (-l - 1));
a[i] = -l - 1;
l = -1;
} else {
l += a[i];
}
} else if (l == 0) {
l = a[i];
}
}
cout << res << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int N, A[100000];
int main() {
cin >> N;
for (int i = 0; i < N; i++) {
cin >> A[i];
}
long long sum = A[0], cnt = 0;
if (sum < 0) {
cnt += 1 - sum;
sum = 1;
}
for (int i = 1; i < N; i++) {
sum += A[i];
if (i % 2 == 1) {
if (sum >= 0) {
cnt += sum + 1;
sum = -1;
}
} else {
if (sum <= 0) {
cnt += 1 - sum;
sum = 1;
}
}
}
long long sum2 = A[0], cnt2 = 0;
if (sum2 > 0) {
cnt2 += sum2 + 1;
sum2 = -1;
}
for (int i = 1; i < N; i++) {
sum2 += A[i];
if (i % 2 == 1) {
if (sum2 <= 0) {
cnt2 += 1 - sum2;
sum2 = 1;
}
} else {
if (sum2 >= 0) {
cnt2 += sum2 + 1;
sum2 = -1;
}
}
}
cout << min(cnt, cnt2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.function.IntFunction;
import java.util.function.Supplier;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner();
int n=scanner.nextInt();
long[] a=new long[n+1];
for(int i=1;i<=n;i++){
a[i]=scanner.nextInt();
}
Arrays.parallelPrefix(a,(c,b)->c+b);
//put(Arrays.toString(a));
long ans=0;
long ruiseki=0;
for(int i=1;i<=n;i++){
//put(format("i=%d",i));
//put(format("ruiseki=%d",ruiseki));
long val=a[i]+ruiseki;
long val_=a[i-1]+ruiseki;
//put(format("val=%d",val));
//put(format("val_=%d",val_));
if(val==0){
long bit=a[i-1]/Math.abs(a[i-1]);
ruiseki+=bit*1;
ans+=Math.abs(bit);
}else if(val>0&&val_>0){
ruiseki-=(val+1);
ans+=Math.abs(val+1);
}else if(val<0&&val_<0){
ruiseki+=Math.abs(val)+1;
ans+=Math.abs(val)+1;
}
//put(ans);
//put();
}
put(ans);
}
public static void print(Object object){
System.out.print(object);
}
public static void put(Object object) {
System.out.println(object);
}
public static void put(){
System.out.println();
}
public static String format(String string, Object... args) {
return String.format(string, args);
}
}
final class Scanner {
private final InputStream in = System.in;
private final byte[] buffer = new byte[1024];
private int ptr = 0;
private int buflen = 0;
private boolean hasNextByte() {
if (ptr < buflen) {
return true;
} else {
ptr = 0;
try {
buflen = in.read(buffer);
} catch (IOException e) {
e.printStackTrace();
}
if (buflen <= 0) {
return false;
}
}
return true;
}
private int readByte() {
if (hasNextByte())
return buffer[ptr++];
else
return -1;
}
private boolean isPrintableChar(int c) {
return 33 <= c && c <= 126;
}
public boolean hasNext() {
while (hasNextByte() && !isPrintableChar(buffer[ptr]))
ptr++;
return hasNextByte();
}
public String next() {
if (!hasNext())
throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while (isPrintableChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public long nextLong() {
if (!hasNext())
throw new NoSuchElementException();
long n = 0;
boolean minus = false;
int b = readByte();
if (b == '-') {
minus = true;
b = readByte();
}
if (b < '0' || '9' < b) {
throw new NumberFormatException();
}
while (true) {
if ('0' <= b && b <= '9') {
n *= 10;
n += b - '0';
} else if (b == -1 || !isPrintableChar(b)) {
return minus ? -n : n;
} else {
throw new NumberFormatException();
}
b = readByte();
}
}
public int nextInt() {
long nl = nextLong();
if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE)
throw new NumberFormatException();
return (int) nl;
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
final class Pair {
final public int x, y;
Pair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int hashCode() {
return x+y;
}
@Override
public boolean equals(Object obj) {
boolean result=super.equals(obj);
if(obj.getClass()!=this.getClass()){
return false;
}
Pair pair=(Pair)obj;
if(this.x==pair.x&&this.y==pair.y) return true;
return false;
}
@Override
public String toString() {
return String.format("(%d,%d)", x, y);
}
}
final class Tuple<T,V>{
//immutabl1でないことに注意(T,Vがmutableの場合変更可能)
final public T t;
final public V v;
Tuple(T t,V v){
this.t=t;
this.v=v;
}
@Override
public int hashCode() {
return (t.hashCode()+v.hashCode());
}
@Override
public boolean equals(Object obj) {
if(obj.getClass()!=this.getClass()){
return false;
}
Tuple<T,V> tuple=(Tuple)obj;
return tuple.t.equals(this.t)&&tuple.v.equals(this.v);
}
@Override
public String toString() {
return String.format("<Tuple>=<%s,%s>",t,v);
}
}
final class LowerBoundComparator<T extends Comparable<? super T>>
implements Comparator<T>
{
public int compare(T x, T y)
{
return (x.compareTo(y) >= 0) ? 1 : -1;
}
}
final class UpperBoundComparator<T extends Comparable<? super T>>
implements Comparator<T>
{
public int compare(T x, T y)
{
return (x.compareTo(y) > 0) ? 1 : -1;
}
}
final class Util {
static long gcd(long a,long b){
if(a%b==0)return b;
return gcd(b,a%b);
}
static long lcm(long a,long b){
long gcd=gcd(a,b);
long result=b/gcd;
return a*result;
}
static int kaijoMod(int n,int mod){
if(n<1) return -1;
long result=1;
for(int i=n;i>1;i--){
result*=i;
result%=mod;
}
return (int)result;
}
static <T extends Comparable> Map<T,Integer> count(List<T> list){
//副作用
Collections.sort(list);
Map<T,Integer> result=new HashMap<>();
int l=0,r=0;
while(l<list.size()){
while(r<list.size()-1&&list.get(r).equals(r+1)){
r++;
}
result.put(list.get(r),r-l+1);
r++;
l=r;
}
return result;
}
static Map<Integer,Integer> count(int[] array){
//副作用
Arrays.sort(array);
Map<Integer,Integer> result=new HashMap<>();
int l=0,r=0;
while(l<array.length){
while(r<array.length-1&&array[r]==array[r+1]){
r++;
}
result.put(array[l],r-l+1);
r++;
l=r;
}
return result;
}
static String toStringBWS(Iterable iterable){
Iterator ite=iterable.iterator();
return toStringBWS(ite);
}
static String toStringBWS(Iterator ite){
StringBuilder sb=new StringBuilder();
sb.append(ite.next());
while(ite.hasNext()){
sb.append(" ");
sb.append(ite.next());
}
return sb.toString();
}
static String toStringBWS(int[] array){
StringBuilder sb=new StringBuilder();
for(int i=0;i<array.length-1;i++){
sb.append(array[i]);
sb.append(" ");
}
sb.append(array[array.length-1]);
return sb.toString();
}
static String toStringBWS(long[] array){
StringBuilder sb=new StringBuilder();
for(int i=0;i<array.length-1;i++){
sb.append(array[i]);
sb.append(" ");
}
sb.append(array[array.length-1]);
return sb.toString();
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int DX[] = {1, 1, 0, -1, -1, -1, 0, 1};
int DY[] = {0, -1, -1, -1, 0, 1, 1, 1};
int n;
ll hoge(ll a[]) {
ll ans = 0;
ll temp = 0;
for (int(i) = 0; (i) < (n); (i)++) {
if (temp > 0 && temp + a[i] > 0) {
ans += abs(-1 - temp - a[i]);
temp = -1;
} else if (temp < 0 && temp + a[i] < 0) {
ans += abs(1 - temp - a[i]);
temp = 1;
} else if (temp + a[i] == 0) {
if (temp > 0) {
temp = -1;
} else {
temp = 1;
}
ans += 1;
} else {
temp += a[i];
}
cout << temp << ", ";
}
cout << endl;
return ans;
}
void solve() {
cin >> n;
ll a[n];
for (int(i) = 0; (i) < (n); (i)++) cin >> a[i];
ll ans1 = hoge(a);
ll temp = 0;
if (a[0] > 0) {
temp += (a[0] * (-1) - 1);
a[0] = -1;
} else if (a[0] < 0) {
temp = (a[0] * (-1) + 1);
a[0] = 1;
} else {
temp = 1;
a[0] = -1;
}
ll ans2 = hoge(a) + temp;
cout << min(ans1, ans2) << endl;
}
int main() {
solve();
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
using System.Collections.Generic;
using System.Linq;
using static Input;
public class Prog
{
private const int INF = 1000000007;
public static void Main()
{
int n = NextInt();
int[] a = LineInt();
//凸凹の2パターンっぽい
int costA = 0;
int costB = 0;
int now = 0;
for (int i = 0; i < a.Length; i++)
{
if (i % 2 == 0)
{
if (now + a[i] > 0)
{
//ok
now += a[i];
}
else
{
//ng
costA += Math.Abs(now + a[i]) + 1;
now = 1;
}
}
else
{
if (now + a[i] < 0)
{
//ok
now += a[i];
}
else
{
//ng
costA += Math.Abs(now + a[i]) + 1;
now = -1;
}
}
}
now = 0;
for (int i = 0; i < a.Length; i++)
{
if (i % 2 == 1)
{
if (now + a[i] > 0)
{
//ok
now += a[i];
}
else
{
//ng
costB += Math.Abs(now + a[i]) + 1;
now = 1;
}
}
else
{
if (now + a[i] < 0)
{
//ok
now += a[i];
}
else
{
//ng
costB += Math.Abs(now + a[i]) + 1;
now = -1;
}
}
}
Console.WriteLine(Math.Min(costA, costB));
}
}
public class Input
{
private static Queue<string> q = new Queue<string>();
private static void Confirm() { if (q.Count == 0) foreach (var s in Console.ReadLine().Split(' ')) q.Enqueue(s); }
public static int NextInt() { Confirm(); return int.Parse(q.Dequeue()); }
public static long NextLong() { Confirm(); return long.Parse(q.Dequeue()); }
public static string NextString() { Confirm(); return q.Dequeue(); }
public static double NextDouble() { Confirm(); return double.Parse(q.Dequeue()); }
public static int[] LineInt() { return Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); }
public static long[] LineLong() { return Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); }
public static string[] LineString() { return Console.ReadLine().Split(' ').ToArray(); }
public static double[] LineDouble() { return Console.ReadLine().Split(' ').Select(double.Parse).ToArray(); }
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
int b[n];
int a[n];
for (int i = 1; i <= n; i++) {
cin >> a[i];
b[i] = a[i];
}
long long int sum = 0;
int count = 0, count1 = 0;
int prev;
for (int i = 1; i <= n; i++) {
if (i != 1) {
if (i % 2 == 0) {
if (sum + a[i] <= 0) {
count += abs(sum + a[i]) + 1;
a[i] = 1;
sum = 1;
} else {
sum += a[i];
}
} else {
if (sum + a[i] >= 0) {
count += abs(sum + a[i]) + 1;
a[i] = -1;
sum = -1;
} else {
sum += a[i];
}
}
} else {
if (a[i] >= 0) {
sum += -1;
count += a[i] + 1;
a[i] = -1;
} else if (a[i] < 0) {
sum += a[i];
}
}
}
for (int i = 0; i < n; i++) a[i] = b[i];
sum = 0;
for (int i = 1; i <= n; i++) {
if (i != 1) {
if (i % 2 != 0) {
if (sum + a[i] <= 0) {
count1 += abs(sum + a[i]) + 1;
a[i] = 1;
sum = 1;
} else {
sum += a[i];
}
} else {
if (sum + a[i] >= 0) {
count1 += abs(sum + a[i]) + 1;
a[i] = -1;
sum = -1;
} else {
sum += a[i];
}
}
} else {
if (a[i] <= 0) {
sum += -1;
count1 += a[i] + 1;
a[i] = -1;
} else if (a[i] > 0) {
sum += a[i];
}
}
}
cout << min(count, count1) << '\n';
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main(string[] args) {
new Calc().Solve();
}
public class Calc {
public Calc() { }
public void Solve() {
int n = Utils.ReadLine<int>();
var a = Utils.ReadLine<int>(' ');
int sum = 0;
int x = 0, y = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 0) {
if (sum < 0) continue;
x += Math.Abs(sum) + 1;
sum = -1;
} else {
if (sum > 0) continue;
x += Math.Abs(sum) + 1;
sum = 1;
}
}
sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 1) {
if (sum < 0) continue;
y += Math.Abs(sum) + 1;
sum = -1;
} else {
if (sum > 0) continue;
y += Math.Abs(sum) + 1;
sum = 1;
}
}
Math.Min(x, y).WriteLine();
return;
}
}
}
public static class Utils {
public static T ReadLine<T>() {
return (T)Convert.ChangeType(Console.ReadLine(), typeof(T));
}
public static T[] ReadLine<T>(params char[] separators) {
return Console.ReadLine()
.Split(separators)
.Where(_ => _.Length > 0)
.Select(_ => (T)Convert.ChangeType(_, typeof(T)))
.ToArray();
}
public static List<T> ReadLines<T>(int readCount) {
List<T> rt = new List<T>();
for (int i = 0; i < readCount; i++) {
rt.Add(ReadLine<T>());
}
return rt;
}
public static string Docking<T>(this IEnumerable<T> s, int sequenceRange, Func<T, string> filter = null) {
string str = "";
int c = 0;
foreach (var item in s) {
str += filter == null ? item.ToString() : filter(item);
c++;
if (c == sequenceRange) break;
}
return str;
}
public static string Docking<T>(this IEnumerable<T> s, Func<T, string> filter = null) {
return s.Docking(s.Count(), filter);
}
public static string RangeDocking<T>(this IEnumerable<T> s, int start, int end, Func<T, string> filter = null) {
string str = "";
end = end < s.Count() ? end : s.Count();
var items = s.ToArray();
for (int i = start; i < end; i++) {
str += filter == null ? items[i].ToString() : filter(items[i]);
}
return str;
}
public static int IntParse(this string n) {
return int.Parse(n);
}
public static void WriteLine(this object obj) {
Console.WriteLine(obj);
}
public static void AddTo<T>(this T obj,List<T> list) {
list.Add(obj);
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long n, ai;
vector<long long> a;
bool lp, cp;
long long calc(vector<long long> &a, bool even) {
long long c = 0, sum = 0;
bool lp = even, cp;
for (int(i) = 0; (i) < (n); (i)++) {
sum += a[i];
cp = 0 < sum;
if (lp == cp) {
c += abs(sum) + 1;
sum = lp ? -1 : 1;
}
lp = !lp;
}
return c;
}
int main() {
cin >> n;
for (int(i) = 0; (i) < (n); (i)++) {
cin >> ai;
a.push_back(ai);
}
cout << min(calc(a, true), calc(a, false)) << '\n';
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | package main
import (
"bufio"
"errors"
"fmt"
"math"
"os"
"strconv"
"strings"
)
var sc = bufio.NewScanner(os.Stdin)
// NextLine reads a line text from stdin, and then returns its string.
func NextLine() string {
sc.Scan()
return sc.Text()
}
// NextIntsLine reads a line text, that consists of **ONLY INTEGERS DELIMITED BY SPACES**, from stdin.
// And then returns intergers slice.
func NextIntsLine() []int {
ints := []int{}
intsStr := NextLine()
tmp := strings.Split(intsStr, " ")
for _, s := range tmp {
integer, _ := strconv.Atoi(s)
ints = append(ints, integer)
}
return ints
}
func NextStringsLine() []string {
strs := []string{}
stringsStr := NextLine()
tmp := strings.Split(stringsStr, " ")
for _, s := range tmp {
strs = append(strs, s)
}
return strs
}
// NextRunesLine reads a line text, that consists of **ONLY CHARACTERS ARRANGED CONTINUOUSLY**, from stdin.
// Ant then returns runes slice.
func NextRunesLine() []rune {
return []rune(NextLine())
}
// Max returns the max integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Max(integers ...int) int {
m := integers[0]
for i, integer := range integers {
if i == 0 {
continue
}
if m < integer {
m = integer
}
}
return m
}
// Min returns the min integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Min(integers ...int) int {
m := integers[0]
for i, integer := range integers {
if i == 0 {
continue
}
if m > integer {
m = integer
}
}
return m
}
// PowInt is integer version of math.Pow
func PowInt(a, e int) int {
if a < 0 || e < 0 {
panic(errors.New("[argument error]: PowInt does not accept negative integers"))
}
fa := float64(a)
fe := float64(e)
fanswer := math.Pow(fa, fe)
return int(fanswer)
}
// AbsInt is integer version of math.Abs
func AbsInt(a int) int {
fa := float64(a)
fanswer := math.Abs(fa)
return int(fanswer)
}
// DeleteElement returns a *NEW* slice, that have the same and minimum length and capacity.
// DeleteElement makes a new slice by using easy slice literal.
func DeleteElement(s []int, i int) []int {
if i < 0 || len(s) <= i {
panic(errors.New("[index error]"))
}
// appendのみの実装
n := make([]int, 0, len(s)-1)
n = append(n, s[:i]...)
n = append(n, s[i+1:]...)
return n
}
// Concat returns a *NEW* slice, that have the same and minimum length and capacity.
func Concat(s, t []rune) []rune {
n := make([]rune, 0, len(s)+len(t))
n = append(n, s...)
n = append(n, t...)
return n
}
// sort package (snippets)
//sort.Sort(sort.IntSlice(s))
//sort.Sort(sort.Reverse(sort.IntSlice(s)))
//sort.Sort(sort.Float64Slice(s))
//sort.Sort(sort.StringSlice(s))
// copy function
//a = []int{0, 1, 2}
//b = make([]int, len(a))
//copy(b, a)
/*******************************************************************/
var n int
var A []int
func main() {
tmp := NextIntsLine()
n = tmp[0]
A = NextIntsLine()
S := make([]int, len(A))
S[0] = A[0]
for i := 1; i < len(A); i++ {
sum := S[i-1]
S[i] = sum + A[i]
}
// 最初を正とする場合と負とする場合の両方を試す
answers := []int{}
for _, firstSign := range []int{1, -1} {
comp, answer := 0, 0
if (firstSign == 1 && S[0] <= 0) || (firstSign == -1 && S[0] >= 0) {
comp = firstSign - S[0]
answer = AbsInt(comp)
}
for i := 1; i < len(S); i++ {
var befSign int
if S[i-1]+comp < 0 {
befSign = -1
} else {
befSign = 1
}
if (befSign == -1 && S[i]+comp > 0) || (befSign == 1 && S[i]+comp < 0) {
continue
}
x := -befSign - (S[i] + comp)
answer += AbsInt(x)
comp += x
}
answers = append(answers, answer)
}
fmt.Println(Min(answers...))
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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);
int index_not0 = N;
bool flag = 0;
for (int i = 0; i < N; i++) {
cin >> A[i];
if (!flag && A[i] != 0) {
index_not0 = i;
flag = 1;
}
}
long long ans = 0;
vector<long long> sum(N);
for (int i = 0; i < index_not0; i++) {
ans += i + 1;
}
bool minus = 0;
if (index_not0 != N) {
if (A[index_not0] > 0)
minus = 0;
else
minus = 1;
}
for (int i = index_not0; i < N; i++) {
if (i == 0)
sum[i] = A[i];
else
sum[i] = sum[i - 1] + A[i];
if (minus == 0) {
if ((i - index_not0) % 2 == 0) {
if (sum[i] < 1) {
ans += 1 - sum[i];
sum[i] = 1;
}
} else {
if (sum[i] > -1) {
ans += sum[i] + 1;
sum[i] = -1;
}
}
} else {
if ((i - index_not0) % 2 == 1) {
if (sum[i] < 1) {
ans += 1 - sum[i];
sum[i] = 1;
}
} else {
if (sum[i] > -1) {
ans += sum[i] + 1;
sum[i] = -1;
}
}
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long INF = 300000000;
const long long MOD = 1000000007;
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
int main() {
int n;
cin >> n;
long long a[100100];
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
long long ans = INF;
for (int i = 0; i < 2; ++i) {
long long count = 0;
long long su = 0;
for (int j = 0; j < n; ++j) {
su += a[j];
if (i == 0) {
if (j % 2 == 1 && su <= 0) {
count += -su + 1;
su = 1;
} else if (j % 2 == 0 && su >= 0) {
count += su + 1;
su = -1;
}
}
if (i == 1) {
if (j % 2 == 1 && su >= 0) {
count += su + 1;
su = -1;
} else if (j % 2 == 0 && su <= 0) {
count += -su + 1;
su = 1;
}
}
}
ans = min(ans, count);
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
A = list(map(int,input().split()))
totals = [0] * N
totals[0] = A[0]
con = 0
if totals[0] == 0:
for i in range(1,N):
if A[i] != 0:
f = A[i]
if f > 0:
totals[0] = -1
else:
totals[0] = 1
break
else:
totals[0] = 1
con += 1
for i in range(1,N):
totals[i] = totals[i - 1] + A[i]
#チェック
##符号が等しいか、ゼロなら、A[i - 1]と異符号で絶対値が1の数にする
if totals[i - 1] * totals[i] >= 0:
if totals[i - 1] < 0:
con += abs(1 - totals[i])
totals[i] = 1
else:
con += abs(-1 - totals[i])
totals[i] = -1
print(con)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = [int(a) for a in input().split()]
times = 0
previous = a[0]
for i in range(1, n):
if previous < 0:
if previous + a[i] > 0:
previous = previous + a[i]
else:
times += 1 - (previous + a[i])
previous = 1
elif previous > 0:
if previous + a[i] < 0:
previous = previous + a[i]
else:
times += abs(-1 - (previous + a[i]))
previous = -1
times2 = 0
# if a[0] > 0:
# times2 += abs(-1 - a[0])
# previous = -1
# else:
# times2 += 1 - a[0]
# previous = 1
previous = -a[0]
for i in range(1, n):
if previous < 0:
if previous + a[i] > 0:
previous = previous + a[i]
else:
times2 += 1 - (previous + a[i])
previous = 1
elif previous > 0:
if previous + a[i] < 0:
previous = previous + a[i]
else:
times2 += abs(-1 - (previous + a[i]))
previous = -1
print(min(times, times2))
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.