Search is not available for this dataset
name
stringlengths 2
88
| description
stringlengths 31
8.62k
| public_tests
dict | private_tests
dict | solution_type
stringclasses 2
values | programming_language
stringclasses 5
values | solution
stringlengths 1
983k
|
---|---|---|---|---|---|---|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | def main():
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
A = []
A_append = A.append
cnt = 0
for i in range(n-1):
A_append((a[i]))
x = sum(A) + a[i+1]
if sum(A) > 0 and x > 0:
y = abs(x)+1
cnt += y
a[i+1] -= y
elif sum(A) < 0 and x < 0:
y = abs(sum(A) - a[i+1])-1
cnt += y
a[i+1] += y
if sum(a) == 0:
cnt += 1
print(cnt)
if __name__ == '__main__':
main() |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
long long ans = 0;
long long s_i = 0;
for (int i = 0; i < n; i++) {
if (i == 0) {
if (a[i] == 0) {
if (a[i + 1] > 0) {
ans += 1;
s_i = -1;
} else {
ans += 1;
s_i = 1;
}
} else {
s_i = a[i];
}
} else {
if (s_i > 0) {
if (s_i + a[i] <= -1) {
s_i = s_i + a[i];
} else {
ans += abs(-1 - s_i - a[i]);
s_i = -1;
}
} else {
if (s_i + a[i] >= 1) {
s_i = s_i + a[i];
} else {
ans += abs(1 - s_i - a[i]);
s_i = 1;
}
}
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | package main
import "fmt"
func main() {
var n int
fmt.Scan(&n)
sum := 0
cnt := 0
plus := false
a := make([]int, n)
base := 0
for i := 0; i < n; i++ {
fmt.Scan(&a[i])
if base == 0 && a[i] != 0 {
base = i
}
}
if base % 2 == 0 {
plus = true
} else {
plus = false
}
if a[base] < 0 {
plus = !plus
}
for i := 0; i < n; i++ {
sum += a[i]
if plus {
for sum <= 0 {
sum++
cnt++
}
plus = !plus
} else {
for sum >= 0 {
sum--
cnt++
}
plus = !plus
}
}
fmt.Println(cnt)
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int ans;
int t;
cin >> t;
int sum = t;
for (int i = 0; i < n - 1; i++) {
cin >> t;
if (sum == 0) {
if (t > 0) {
ans++;
sum -= 1;
} else {
ans++;
sum += 1;
}
}
if (sum > 0) {
if (sum + t > 0) {
ans += (sum + t + 1);
t -= (sum + t + 1);
} else if (sum + t == 0) {
ans++;
t -= 1;
}
} else if (sum < 0) {
if (sum + t < 0) {
ans += (abs(sum + t) + 1);
t += (abs(sum + t) + 1);
} else if (sum + t == 0) {
ans++;
t += 1;
}
}
sum += t;
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | def examC():
N = I()
a = LI()
ans = 0
if a[0]<0:
for i in range(N):
a[i] = -a[i]
if a[0]==0:
ans +=1
a[0]=1
sumA = a[0]
for i in range(1,N):
cur = sumA+a[i]
if cur*sumA>0:
if sumA<0:
ans += (-cur+1)
sumA = 1
else:
ans += cur+1
sumA = -1
elif cur*sumA==0:
if sumA<0:
ans += 1
sumA = 1
else:
ans += 1
sumA = -1
else:
sumA = cur
print(ans)
import sys
import copy
import bisect
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')
examC()
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
long long ans, cnt, tmp;
long long z[100005];
int main() {
freopen("in.txt", "r", stdin);
int n, flag;
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%lld", &z[i]);
if (z[0] > 0) {
flag = 1;
ans = z[0];
for (int i = 1; i < n; i++) {
ans += z[i];
if (flag == 1) {
if (ans >= 0) {
cnt += ans + 1;
ans = -1;
}
flag = -1;
} else {
if (ans <= 0) {
cnt += 1 - ans;
ans = 1;
}
flag = 1;
}
}
tmp = cnt;
flag = -1;
ans = -1;
cnt = z[0] + 1;
for (int i = 1; i < n; i++) {
ans += z[i];
if (flag == 1) {
if (ans >= 0) {
cnt += ans + 1;
ans = -1;
}
flag = -1;
} else {
if (ans <= 0) {
cnt += 1 - ans;
ans = 1;
}
flag = 1;
}
}
cnt = std::min(cnt, tmp);
} else if (z[0] < 0) {
flag = -1;
ans = z[0];
for (int i = 1; i < n; i++) {
ans += z[i];
if (flag == 1) {
if (ans >= 0) {
cnt += ans + 1;
ans = -1;
}
flag = -1;
} else {
if (ans <= 0) {
cnt += 1 - ans;
ans = 1;
}
flag = 1;
}
}
tmp = cnt;
flag = 1;
ans = 1;
cnt = 1 - z[0];
for (int i = 1; i < n; i++) {
ans += z[i];
if (flag == 1) {
if (ans >= 0) {
cnt += ans + 1;
ans = -1;
}
flag = -1;
} else {
if (ans <= 0) {
cnt += 1 - ans;
ans = 1;
}
flag = 1;
}
}
cnt = std::min(cnt, tmp);
} else {
flag = cnt = ans = 1;
for (int i = 1; i < n; i++) {
ans += z[i];
if (flag == 1) {
if (ans >= 0) {
cnt += ans + 1;
ans = -1;
}
flag = -1;
} else {
if (ans <= 0) {
cnt += 1 - ans;
ans = 1;
}
flag = 1;
}
}
tmp = cnt;
flag = ans = -1;
cnt = 1;
for (int i = 1; i < n; i++) {
ans += z[i];
if (flag == 1) {
if (ans >= 0) {
cnt += ans + 1;
ans = -1;
}
flag = -1;
} else {
if (ans <= 0) {
cnt += 1 - ans;
ans = 1;
}
flag = 1;
}
}
cnt = std::min(cnt, tmp);
}
printf("%lld\n", cnt);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
long long a[n];
for (int i = 0; i < n; i++) cin >> a[i];
long long c1 = 0, c2 = 0, sum = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
sum = sum + a[i];
if (sum < 0) continue;
c1 += abs(-1 - sum);
sum = -1;
} else {
sum = sum + a[i];
if (sum > 0) continue;
c1 += abs(1 - sum);
sum = 1;
}
}
for (int i = 0; i < n; i++) {
if (i % 2 == 1) {
sum = sum + a[i];
if (sum < 0) continue;
c2 += abs(-1 - sum);
sum = -1;
} else {
sum = sum + a[i];
if (sum > 0) continue;
c2 += abs(1 - sum);
sum = 1;
}
}
cout << min(c1, c2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> v(n);
for (int i = 0; i < (n); i++) cin >> v[i];
auto sign = [](int x) {
if (x == 0)
return 0;
else if (x > 0)
return 1;
else
return -1;
};
int pos = 0, neg = 0;
for (int i = 0; i < (2); i++) {
int now = 0;
int prevsign = (i == 0) ? -1 : 1;
int count = 0;
for (int j = 0; j < (n); j++) {
now += v[j];
int s = sign(now);
if (prevsign == s || s == 0) {
count += abs(now) + 1;
if (prevsign == 1) {
now = -1;
} else {
now = 1;
}
}
prevsign = sign(now);
}
if (i == 0)
pos = count;
else
neg = count;
}
cout << min(pos, neg) << 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;
static const long long maxLL = (long long)1 << 62;
long long a[100001] = {};
long long s[100001] = {};
int main() {
long long n;
cin >> n;
for (long long i = 1; i <= n; i++) {
cin >> a[i];
}
long long cnt = 0;
for (long long i = 1; i <= n; i++) {
s[i] = s[i - 1] + a[i];
if (i > 1) {
if (s[i] == 0) {
s[i] = s[i - 1] * -1;
cnt++;
}
if (i > 1 && s[i - 1] * s[i] > 0) {
cnt = abs(s[i]) + 1;
if (s[i] > 0)
s[i] -= cnt;
else if (s[i] < 0)
s[i] += cnt;
}
}
}
cout << cnt << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
int count = 0;
vector<int> a(100000);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
if (a[0] == 0) {
if (a[1] > 0) {
a[0]--;
count++;
} else {
a[0]++;
count++;
}
}
vector<long long int> sum(100000);
sum[0] = a[0];
for (int i = 1; i < n; i++) {
sum[i] = sum[i - 1] + a[i];
if (sum[i] * sum[i - 1] >= 0) {
if (sum[i - 1] < 0) {
count += 1 - sum[i];
sum[i] = 1;
} else {
count += sum[i] + 1;
sum[i] = -1;
}
}
}
cout << count;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long n;
cin >> n;
vector<long> a(n);
for (long i = 0; i < n; i++) cin >> a[i];
long sum = a[0];
long j = 0;
for (long i = 1; i < n; i++) {
if (sum * (sum + a[i]) < 0)
sum += a[i];
else {
j += abs(sum + a[i]) + 1;
if (sum < 0)
sum = 1;
else if (sum > 0)
sum = -1;
}
}
cout << j << 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;
const long long MOD = 1e9 + 7;
const int INF = 1e9 + 7;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<ll> a(n);
for (int i = 0; i < n; ++i) cin >> a[i];
ll cnt = 0;
ll sub_sum = a[0];
for (int i = 1; i < n; ++i) {
ll sum = sub_sum + a[i];
ll sgn = sub_sum / abs(sub_sum) * sum;
if (sgn >= 0) {
ll b = -1 * sub_sum / abs(sub_sum);
sum = b;
cnt += abs(b - sub_sum - a[i]);
}
sub_sum = sum;
}
ll ans = cnt;
cnt = abs(a[0]) + 1;
sub_sum = (a[0] > 0 ? -1 : 1);
for (int i = 1; i < n; ++i) {
ll sum = sub_sum + a[i];
ll sgn = sub_sum / abs(sub_sum) * sum;
if (sgn >= 0) {
ll b = -1 * sub_sum / abs(sub_sum);
sum = b;
cnt += abs(b - sub_sum - a[i]);
}
sub_sum = sum;
}
ans = min(ans, cnt);
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | /// @file
/// @version Wrong Answer.
#include<bits/stdc++.h>
using namespace std;
#define int int64_t
main(){
int n;
cin>>n;
vector<int> a(n);
for(auto&x:a)cin>>x;
int n_actions0=0; // when even elements are positive.
{
int sum=0;
for(int i=0;i<a.size();++i){
bool want_positive=i%2==0;
sum+=a[i];
bool is_positive=sum>0;
if(is_positive==want_positive||sum==0)continue;
n_actions0+=abs(sum)+1;
sum=sum>0?-1:1;
}
}
int n_actions1=0; // when odd elements are positive.
{
int sum=0;
for(int i=0;i<a.size();++i){
bool want_positive=i%2!=0;
sum+=a[i];
bool is_positive=sum>0;
if(is_positive==want_positive||sum==0)continue;
n_actions1+=abs(sum)+1;
sum=sum>0?-1:1;
}
}
cout<<min(n_actions0,n_actions1)<<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 | import scala.io.StdIn
import scala.annotation.tailrec
object Main extends App {
val n = StdIn.readInt
val a = StdIn.readLine.split(" ").map(_.toLong)
val init1 = if(a.head < 0) 1 - a.head else 0
val ans1 = a.tail./:(a.head + init1, init1.abs)((acc,i) => {
val (bsum, bcnt) = acc
val sum = bsum + i
val cnt = if(bsum < 0 && sum < 0) 1 - sum
else if(bsum > 0 && sum > 0) -1 - sum
else if(sum == 0) if(bsum < 0) 1 else -1
else 0
// println(sum + " " + cnt)
(sum+cnt, bcnt+cnt.abs)
})._2
val init2 = if(a.head < 0) 0 else -1 - a.head
val ans2 = a.tail./:(a.head + init2, init2.abs)((acc,i) => {
val (bsum, bcnt) = acc
val sum = bsum + i
val cnt = if(bsum < 0 && sum < 0) 1 - sum
else if(bsum > 0 && sum > 0) -1 - sum
else if(sum == 0) if(bsum < 0) 1 else -1
else 0
// println(sum + " " + cnt)
(sum+cnt, bcnt+cnt.abs)
})._2
println(math.min(ans1,ans2))
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
inline char nc() { return getchar(); }
inline void read(int &x) {
char c = nc();
int b = 1;
for (; !(c >= '0' && c <= '9'); c = nc())
if (c == '-') b = -1;
for (x = 0; c >= '0' && c <= '9'; x = x * 10 + c - '0', c = nc())
;
x *= b;
}
inline void read(long long &x) {
char c = nc();
long long b = 1;
for (; !(c >= '0' && c <= '9'); c = nc())
if (c == '-') b = -1;
for (x = 0; c >= '0' && c <= '9'; x = x * 10 + c - '0', c = nc())
;
x *= b;
}
inline int read(char *s) {
char c = nc();
int len = 0;
for (; !(c >= '0' && c <= '9'); c = nc())
if (c == EOF) return 0;
for (; (c >= '0' && c <= '9'); s[len++] = c, c = nc())
;
s[len++] = '\0';
return len;
}
inline void read(char &x) {
for (x = nc(); !(x >= 'A' || x <= 'B'); x = nc())
;
}
int wt, ss[19];
inline void print(int x) {
if (x < 0) x = -x, putchar('-');
if (!x)
putchar(48);
else {
for (wt = 0; x; ss[++wt] = x % 10, x /= 10)
;
for (; wt; putchar(ss[wt] + 48), wt--)
;
}
}
inline void print(long long x) {
if (x < 0) x = -x, putchar('-');
if (!x)
putchar(48);
else {
for (wt = 0; x; ss[++wt] = x % 10, x /= 10)
;
for (; wt; putchar(ss[wt] + 48), wt--)
;
}
}
int n, a[100010], s[100010];
int main() {
read(n);
for (int i = 0; i < n; i++) read(a[i]);
s[0] = a[0];
for (int i = 1; i < n; i++) s[i] = s[i - 1] + a[i];
int ans1 = 0, p = 0;
for (int i = 0; i < n; i++)
if (i % 2 == 0) {
if (s[i] + p <= 0) ans1 += 1 - (s[i] + p), p += 1 - (s[i] + p);
} else if (s[i] + p >= 0)
ans1 += (s[i] + p + 1), p -= (s[i] + p + 1);
int ans2 = 0;
p = 0;
for (int i = 0; i < n; i++)
if (i % 2 != 0) {
if (s[i] + p <= 0) ans2 += 1 - (s[i] + p), p += 1 - (s[i] + p);
} else if (s[i] + p >= 0)
ans2 += (s[i] + p + 1), p -= (s[i] + p + 1);
print(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 i;
int n;
int s[10050] = {0};
int count;
int wa = 0;
int ans = 0;
scanf("%d", &n);
for (i = 0; i < n; i++) scanf("%d", &s[i]);
if (s[0] < 0) {
count = 1;
} else {
count = 0;
}
wa = s[0];
for (i = 1; i < n; i++) {
if ((wa + s[i] == 0) && (count == 0)) {
count = 1;
ans += 1;
s[i]++;
wa += s[i];
} else if ((wa + s[i] > 0) && (count == 0)) {
count = 1;
ans += abs(wa) + 1 - s[i];
s[i] = abs(wa) - s[i];
wa += s[i];
} else if ((wa + s[i] < 0) && (count == 0)) {
count = 1;
wa += s[i];
printf("ans == %d\n", ans);
} else if ((wa + s[i] < 0) && (count == 1)) {
count = 0;
ans += abs(wa) - s[i] + 1;
s[i] = abs(wa) + s[i];
wa += s[i];
} else if ((wa + s[i] == 0) && (count == 1)) {
count = 0;
ans += 1;
s[i]--;
wa += s[i];
printf("ans == %d\n", ans);
} else if ((wa + s[i] > 0) && (count == 1)) {
count = 0;
wa = s[i];
}
}
printf("%d\n", ans);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import sys
input = sys.stdin.readline
n = int(input())
a = [int(x) for x in input().split()]
# スタート+
if a[0] <= 0:
A1 = 1
ans1 += abs(a[0]) + 1
else:
A1 = a[0]
for i in range(1, n):
nextA = A1 + a[i]
if (A1 > 0 and nextA < 0) or (A1 < 0 and nextA > 0):
A1 = nextA
elif nextA == 0 and A1 > 0:
ans1 += 1
A1 = -1
elif nextA == 0 and A1 < 0:
ans1 += 1
A1 = 1
elif A1 > 0:
ans1 += abs(nextA) + 1
A1 = -1
else:
ans1 += abs(nextA) + 1
A1 = 1
# スタート-
if a[0] >= 0:
ans2 = abs(a[0]) + 1
A2 = -1
else:
A2 = a[0]
for i in range(1, n):
nextA = A2 + a[i]
if (A2 > 0 and nextA < 0) or (A2 < 0 and nextA > 0):
A2 = nextA
elif nextA == 0 and A2 > 0:
ans2 += 1
A2 = -1
elif nextA == 0 and A2 < 0:
ans2 += 1
A2 = 1
elif A2 > 0:
ans2 += abs(nextA) + 1
A2 = -1
else:
ans2 += abs(nextA) + 1
A2 = 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 | python3 | n = int(input())
an = map(int, input().split())
sum_an = []
tmp = 0
for a in an:
tmp += a
sum_an.append(tmp)
c = 0
before_positive = sum_an[0] <= 0
for i in range(n):
if before_positive and sum_an[i] >= 0:
p = abs(sum_an[i]) + 1
c += p
for j in range(i, n):
sum_an[j] -= p
before_positive = False
elif not before_positive and sum_an[i] <= 0:
p = abs(sum_an[i]) + 1
c += p
for j in range(i, n):
sum_an[j] += p
before_positive = True
else:
before_positive = not before_positive
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 inf = 1e9;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
int ans = inf;
int tmp = 0;
int cnt = 0;
for (int i = 0; i < n; i++) {
tmp += a[i];
if (i % 2 == 0 && tmp <= 0) {
cnt += (1 - tmp);
tmp = 1;
} else if (i % 2 == 1 && tmp >= 0) {
cnt += (1 + tmp);
tmp = -1;
}
}
ans = min(ans, cnt);
tmp = 0;
cnt = 0;
for (int i = 0; i < n; i++) {
tmp += a[i];
if (i % 2 == 1 && tmp <= 0) {
cnt += (1 - tmp);
tmp = 1;
} else if (i % 2 == 0 && tmp >= 0) {
cnt += (1 + tmp);
tmp = -1;
}
}
ans = min(ans, cnt);
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<long long> A(N);
vector<long long> sum(N + 1, 0);
for (int i = 0; i < N; i++) {
cin >> A[i];
sum[i + 1] = sum[i] + A[i];
}
long long ans = 0;
long long ch = 0;
for (int i = 1; i <= N; i++) {
sum[i] += ch;
long long bf = ch;
if (sum[i] == 0) {
if (sum[i - 1] != 0) {
if (sum[i - 1] < 0) {
ch++;
sum[i]++;
} else {
ch--;
sum[i]--;
}
} else {
if (sum[i + 1] < 0) {
ch++;
sum[i]++;
} else {
ch--;
sum[i]--;
}
}
} else {
if (sum[i - 1] * sum[i] > 0) {
sum[i] /= (-sum[i]);
ch -= abs(sum[i]) + 1;
}
}
ans += abs(ch - bf);
}
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 | (* O(n) *)
let n = Scanf.scanf " %d" @@ (+) 0
let a_s = Array.init n @@ fun _ -> Scanf.scanf " %d" @@ (+) 0
let ans = ref max_int
let _ =
for m = 0 to 1 do
let sum = ref a_s.(0) in
let c = ref 0 in
if a_s.(0) > 0 then if m = 0 then (sum := -1; c := abs !sum + 1) else ()
else if m = 1 then (sum := 1; c := abs !sum + 1);
for i = 1 to n - 1 do
if i mod 2 = m then
(sum := !sum + a_s.(i);
if !sum >= 0 then (c := !c + abs !sum + 1; sum := -1))
else
(sum := !sum + a_s.(i);
if !sum <= 0 then (c := !c + abs !sum + 1; sum := 1))
done;
ans := min !ans !c
done;
Printf.printf "%d\n" !ans |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n, a[100000];
int ans1 = 0, ans2 = 0;
long long sum1 = 1, sum2 = -1;
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
if (a[0] > 0) {
sum1 = a[0];
ans2 = a[0] + 1;
} else if (a[0] < 0) {
sum2 = a[0];
ans1 = abs(a[0]) + 1;
} else {
ans1 = 1;
ans2 = 1;
}
for (int i = 1; i < n; i++) {
if (i % 2 == 1) {
if (0 > a[i] && abs(a[i]) > sum1)
sum1 += a[i];
else {
ans1 += abs(-(sum1 + 1) - a[i]);
sum1 = -1;
}
if (0 < a[i] && a[i] > abs(sum2))
sum2 += a[i];
else {
ans2 += abs(abs(sum2) + 1 - a[i]);
sum2 = 1;
}
} else {
if (0 < a[i] && a[i] > abs(sum1))
sum1 += a[i];
else {
ans1 += abs(abs(sum1) + 1 - a[i]);
sum1 = 1;
}
if (0 > a[i] && abs(a[i]) > sum2)
sum2 += a[i];
else {
ans2 += abs(-(sum2 + 1) - a[i]);
sum2 = -1;
}
}
}
cout << min(ans1, ans2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n;
long long func(vector<long long> a, int fugo) {
long long ans = 0;
long long offset = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == fugo) {
if (a[i] <= offset) {
ans += offset - (a[i] - 1);
offset = a[i] - 1;
}
} else {
if (a[i] >= offset) {
ans += (a[i] + 1) - offset;
offset = a[i] + 1;
}
}
}
return ans;
}
int main() {
cin >> n;
vector<long long> a;
int sum_tmp = 0;
for (int i = 0; i < n; i++) {
int tmp;
cin >> tmp;
sum_tmp += tmp;
a.push_back(sum_tmp);
}
long long ans = min(func(a, 0), func(a, 1));
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int MaxN = 1e5 + 5;
int box[MaxN];
int n;
long long solve1() {
long long sum = box[1];
long long res = 0;
if (sum == 0) {
sum = 1;
res = 1;
}
for (int i = 2; i <= n; i++) {
long long temp = box[i] + sum;
if (temp * box[i] >= 0) {
res += abs(temp) + 1;
if (sum > 0)
sum = -1;
else
sum = 1;
} else
sum = temp;
}
return res;
}
long long solve2() {
long long sum = box[1];
long long res = 0;
if (sum == 0) {
sum = -1;
res = 1;
}
for (int i = 2; i <= n; i++) {
long long temp = box[i] + sum;
if (temp * sum >= 0) {
res += abs(temp) + 1;
if (sum > 0)
sum = -1;
else
sum = 1;
} else
sum = temp;
}
return res;
}
int main() {
while (~scanf("%d", &n)) {
for (int i = 1; i <= n; i++) scanf("%d", &box[i]);
long long ans = 1LL << 60;
ans = min(ans, solve1());
ans = min(ans, solve2());
printf("%lld\n", ans);
}
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
class Program
{
static void Main()
{
int n = int.Parse(Console.ReadLine());
var inp = Array.ConvertAll(Console.ReadLine().Split(), long.Parse);
long ans = 0;
long cumsum = inp[0];
for (int i = 1; i < n; i++)
{
if (Math.Abs(inp[i]) <= Math.Abs(cumsum))
{
long temp = Math.Abs(cumsum) - Math.Abs(inp[i]) + 1;
if (inp[i] * cumsum > 0)
{
ans += temp + Math.Abs(cumsum);
inp[i] += inp[i] > 0 ? -(temp + cumsum) : temp + cumsum;
}
else
{
ans += temp;
inp[i] += inp[i] > 0 ? temp : -temp;
}
}
else if (inp[i] * cumsum > 0)
{
ans += Math.Abs(cumsum) + 1;
inp[i] += inp[i] > 0 ? -inp[i] - Math.Abs(cumsum) - 1 : inp[i] + Math.Abs(cumsum) + 1;
}
cumsum += inp[i];
}
Console.WriteLine(ans);
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n=int(input())
a=list(map(int,input().split()))
b=a[:]
x=0#pmpm
y=0
sum1=0
sum2=0
for i in range(n):
sum1+=a[i]
sum2+=b[i]
if(i%2==0):
if(sum1<=0):
x+=1-sum1
a[i]+=1-sum1
if(sum2>=0):
y+=sum2+1
b[i]-=sum2+1
else:
if(sum1>=0):
x+=sum1+1
a[i]-=1+sum1
if(sum2<=0):
y+=1-sum2
b[i]+=1-sum2
print(min(x,y)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void) {
int n;
cin >> n;
long long int a[n], sum = 0, count = 0;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) {
sum += a[i];
if (sum == 0) {
if (a[i + 1] > 0) {
a[i]--;
count++;
} else if (a[i + 1] < 0) {
a[i]++;
count++;
} else if (a[i + 1] == 0) {
a[i]++;
a[i + 1] -= 2;
count += 3;
}
continue;
} else if (sum > 0) {
if (sum + a[i + 1] > 0) {
count += sum + a[i + 1] + 1;
a[i + 1] -= sum + a[i + 1] + 1;
continue;
}
} else if (sum < 0) {
if (sum + a[i + 1] < 0) {
count += abs(sum + a[i + 1] + 1);
a[i + 1] += abs(sum + a[i + 1] + 1);
while (sum + a[i + 1] <= 0) {
a[i + 1]++;
count++;
}
}
}
}
cout << 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;
template <class T>
void scan(vector<T>& a, long long n, istream& cin) {
T c;
for (long long(i) = 0; (i) < (n); ++(i)) {
cin >> c;
a.push_back(c);
}
}
using vs = vector<string>;
using vi = vector<long long>;
using pii = pair<long long, long long>;
using psi = pair<string, long long>;
using vvi = vector<vi>;
using pss = pair<string, string>;
using vpii = vector<pii>;
template <class T>
bool valid(T x, T w) {
return 0 <= x && x < w;
}
long long dx[4] = {1, -1, 0, 0};
long long dy[4] = {0, 0, 1, -1};
long long dp[100010];
vi a;
long long n;
long long f(long long a_0) {
for (long long(i) = 0; (i) < (100010); ++(i)) {
dp[i] = 0;
}
long long s = a_0;
for (long long(i) = 0; (i) < (n - 1); ++(i)) {
if (s * (s + a[i + 1]) < 0) {
s += a[i + 1];
dp[i + 1] = dp[i];
} else {
dp[i + 1] = dp[i] + abs(s + a[i + 1]) + 1;
s = s < 0 ? 1 : -1;
}
}
return dp[n - 1];
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
;
;
;
cin >> n;
for (long long(i) = 0; (i) < (n); ++(i)) {
long long c;
cin >> c;
a.push_back(c);
}
long long k;
if (a[0] == 0) {
k = f(-1) + 1;
k = min(f(-1) + 1, k);
} else {
k = f(a[0]);
}
cout << k << 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];
}
double c1 = 0, s1 = 0;
double c2 = 0, s2 = 0;
for (int i = 0; i < n; i++) {
s1 += a[i];
s2 += a[i];
if (i % 2 == 0) {
if (s1 <= 0) {
c1 += 1 - s1;
s1 = 1;
}
if (s2 >= 0) {
c2 += s2 + 1;
s2 = -1;
}
} else {
if (s1 >= 0) {
c1 += s1 + 1;
s1 = -1;
}
if (s2 <= 0) {
c2 += 1 - s2;
s2 = 1;
}
}
}
printf("%lld\n", min(c1, c2));
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int solve(vector<int>& a) {
int res = 0;
int sum = 0;
for (int i = 0; i < a.size(); ++i) {
if (i % 2 == 0) {
if (sum + a[i] <= 0) {
res += abs(sum + a[i] - 1);
sum = 1;
} else {
sum += a[i];
}
} else {
if (sum + a[i] >= 0) {
res += abs(sum + a[i] + 1);
sum = -1;
} else {
sum += a[i];
}
}
}
return res;
}
int main() {
auto& in = cin;
size_t n;
in >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i) in >> a[i];
int ans = solve(a);
for (int i = 0; i < n; ++i) a[i] *= -1;
ans = min(ans, solve(a));
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | fun main(args: Array<String>) {
val n = readLine()?.toInt() ?: return
val aList = readLine()?.split(" ")?.map { it.toLong() } ?: return
// |sum(ai) - sum(ai+1)| = 0 になるように操作を行っていく
// もとの配列で和の等号が期待どおりであれば何も操作しない
var count = 0L
var sum = aList[0]
// aList[0] == 0 の場合には最初の符号を決める必要がある
if (sum == 0L) {
var i = 1
// listで最初に現れる0以外の数を探す
while(i < n && aList[i] != 0L) {
i++
}
val value = aList[i]
// 配列がすべて0で構成されている場合は適当に符号を決める
if (value == 0L) {
sum++
count++
} else {
// 0の個数が奇数か偶数かで場合分け
val sign = if ((i % 2) == 0) {
value > 0
} else {
value < 0
}
sum += if (sign) 1 else -1
count++
}
}
for (i in 1 until n) {
val sign = sum < 0
if (sign == (sum + aList[i] > 0)) {
sum += aList[i]
continue
}
val expected = if (sign) Math.abs(sum) + 1 else -Math.abs(sum) - 1
val diff = Math.abs(expected - aList[i])
sum += expected
count += diff
}
println(count)
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <iostream>
#include <string>
#include <vector>
#include <numeric>
#include <queue>
#include <unordered_map>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <sstream>
#include <stack>
#include <map>
#include <set>
#include <ios>
#include <cctype>
#include <cstdio>
#include <functional>
#include <cassert>
#define REP(i,a) for(int i = 0;i < (a);++i)
#define FOR(i,a,b) for(int i = (a);i < (b); ++i)
#define FORR(i,a,b) for(int i = (a) - 1;i >=(b);--i)
#define ALL(obj) (obj).begin(),(obj).end()
#define SIZE(obj) (int)(obj).sizeT()
#define YESNO(cond,yes,no){cout <<((cond)?(yes):(no))<<endl; }
#define SORT(list) sort(ALL((list)));
#define RSORT(list) sort((list).rbegin(),(list).rend())
#define ASSERT(cond,mes) assert(cond && mes)
using namespace std;
using ll = long long;
constexpr int MOD = 1'000'000'007;
constexpr int INF = 1'050'000'000;
template<typename T>
T round_up(const T& a, const T& b) {
return (a + (b - 1)) / b;
}
template <typename T1, typename T2>
istream& operator>>(istream& is, pair<T1, T2>& p) {
is >> p.first >> p.second;
return is;
}
template <typename T1, typename T2>
ostream& operator<<(ostream& os, pair<T1, T2>& p) {
os << p.first << p.second;
return os;
}
template <typename T>
istream& operator>>(istream& is, vector<T>& v) {
REP(i, (int)v.size())is >> v[i];
return is;
}
template <typename T>
ostream& operator<<(ostream& os, vector<T>& v) {
REP(i, (int)v.size())os << v[i] << endl;
return os;
}
template <typename T>
T clamp(T& n, T a, T b) {
if (n < a)n = a;
if (n > b)n = b;
return n;
}
template <typename T>
static T GCD(T u, T v) {
T r;
while (v != 0) {
r = u % v;
u = v;
v = r;
}
return u;
}
template <typename T>
static T LCM(T u, T v) {
return u / GCD(u, v) * v;
}
template <typename T>
static int sign(T t) {
if (t > 0)return 1;
else if (t < 0)return -1;
else return 0;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
std::cout << fixed << setprecision(20);
int N;
cin >> N;
vector<int>A(N);
cin >> A;
ll cnt = 0;
ll sum = 0;
REP(i, N) {
ll next = sum + A[i];
if (sign(next) == 0 || sign(next) == sign(sum)) {
cnt += abs(next) + 1;
next = -sign(sum);
}
sum = next;
}
sum = 0;
ll cnt2 = 0;
REP(i, N) {
ll next = sum + A[i];
if (sign(next) == 0 || sign(next) == sign(sum)) {
cnt2 += abs(next) + 1;
next = -sign(sum);
}
sum = next;
}
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 | cpp | #include <bits/stdc++.h>
using namespace std;
int INF = 100000000;
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < (int)(n); i++) {
cin >> a[i];
}
long long sum = 0, ans_p = 0;
for (int i = 0; i < (int)(n); i++) {
sum += a[i];
if (a[i] % 2 == 0 & sum < 1) {
ans_p += 1 - sum;
sum = 1;
} else if (a[i] % 2 == 1 && sum > -1) {
ans_p += sum + 1;
sum = -1;
}
}
long long ans_m = 0;
sum = 0;
for (int i = 0; i < (int)(n); i++) {
sum += a[i];
if (a[i] % 2 == 1 & sum < 1) {
ans_m += 1 - sum;
sum = 1;
} else if (a[i] % 2 == 0 && sum > -1) {
ans_m += sum + 1;
sum = -1;
}
}
cout << min(ans_p, ans_m) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
A = list(map(int, input().split()))
currentSum = 0
count = 0
for i in range(N):
restSum = currentSum
currentSum += A[i]
if currentSum <= 0 and restSum < 0:
count += abs(currentSum) + 1
currentSum = 1
elif currentSum >= 0 and restSum > 0:
count += abs(currentSum) + 1
currentSum = -1
print(count)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
i = input()
i = i.split()
for item in range(len(i)):
i[item] = int(i[item])
totn = 0
totp = 0
countp = 0
countn = 0
for x in range(len(i)):
totp += i[x]
totn += i[x]
if x == 0:
if totp == 0:
totn = -1
countn = 1
totp = 1
countn = 1
elif totp < 0:
countp = abs(totp) + 1
totp = 1
elif totp > 0:
countn = abs(totn) + 1
totn = -1
elif x %2 == 1:
if totn == 0:
countn += 1
totn += 1
elif totn < 0:
countn += abs(totn) + 1
totn = 1
if totp == 0:
countp += 1
totp -= 1
elif totp > 0:
countp += abs(totp) + 1
totp = -1
elif x %2 == 0:
if totn == 0:
countn += 1
totn -= 1
elif totn > 0:
countn += abs(totn) + 1
totn = -1
if totp == 0:
countp += 1
totp += 1
elif totp < 0:
countp += abs(totp) + 1
totp = 1
'''print('totn', totn)
print('countn', countn)
print('totp', totp)
print('countp', countp) '''
count = min(countn, countp)
print(count) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
const long long INF = 1LL << 60;
long long foo(const vector<long long>& a, bool oddPositive) {
size_t n = a.size();
vector<long long> b1(n);
long long ans = 0;
b1[0] = a[0];
if (oddPositive && a[0] < 0) {
b1[0] = 1;
ans += abs(a[0] + 1);
}
if (!oddPositive && a[0] > 0) {
b1[0] = -1;
ans += abs(a[0] + 1);
}
for (int i = 1; i < n; i++) {
if ((b1[i - 1] + a[i]) * b1[i - 1] < 0) {
b1[i] = b1[i - 1] + a[i];
continue;
}
if (a[i] + b1[i - 1] == 0) {
ans += 1;
} else if (b1[i - 1] * a[i] > 0) {
ans += abs(a[i]) + abs(b1[i - 1]) + 1;
} else {
ans += abs(b1[i - 1]) - abs(a[i]) + 1;
}
b1[i] = b1[i - 1] < 0 ? 1 : -1;
}
return ans;
}
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; ++i) {
long long tmp = 0;
cin >> tmp;
a[i] = tmp;
}
long long ans1 = foo(a, true);
long long ans2 = foo(a, false);
cout << min(ans1, ans2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int a[maxn];
int main() {
int n;
long long sum, num = 1e18;
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
sum = a[0];
long long cnt = 0;
if (sum == 0) {
sum = 1;
cnt++;
for (int i = 1; i < n; i++) {
if (sum > 0) {
long long t = sum + a[i];
if (t < 0)
sum = t;
else {
long long b = abs(t + 1);
cnt += b;
sum = -1;
}
} else if (sum < 0) {
long long t = sum + a[i];
if (t > 0)
sum = t;
else {
long long b = abs(1 - t);
cnt += b;
sum = 1;
}
}
}
num = min(num, cnt);
cnt = 0;
sum = -1;
cnt++;
for (int i = 1; i < n; i++) {
if (sum > 0) {
long long t = sum + a[i];
if (t < 0)
sum = t;
else {
long long b = abs(t + 1);
cnt += b;
sum = -1;
}
} else if (sum < 0) {
long long t = sum + a[i];
if (t > 0)
sum = t;
else {
long long b = abs(1 - t);
cnt += b;
sum = 1;
}
}
}
num = min(num, cnt);
printf("%lld\n", num);
return 0;
}
for (int i = 1; i < n; i++) {
if (sum > 0) {
long long t = sum + a[i];
if (t < 0)
sum = t;
else {
long long b = abs(t + 1);
cnt += b;
sum = -1;
}
} else if (sum < 0) {
long long t = sum + a[i];
if (t > 0)
sum = t;
else {
long long b = abs(1 - t);
cnt += b;
sum = 1;
}
}
}
printf("%lld\n", cnt);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long a[100010];
long long a1[100010];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
a1[i] = a[i];
}
int count = 0;
int c;
int ans1 = 0;
int ans2 = 0;
int old_a;
if (a[0] != 0) {
for (int i = 0; i < n; i++) {
c = count + a[i];
if (count > 0 && c >= 0) {
ans1 += c - (-1);
a[i] -= c - (-1);
c = count + a[i];
if (c == 0) {
a[i]--;
ans1++;
}
} else if (count < 0 && c <= 0) {
ans1 += 1 - c;
a[i] += 1 - c;
c = count + a[i];
if (c == 0) {
a[i]++;
ans1++;
}
}
count += a[i];
}
} else {
a[0] = 1;
ans1++;
count += a[0];
for (int i = 1; i < n; i++) {
c = count + a[i];
if (count > 0 && c >= 0) {
ans1 += c - (-1);
a[i] -= c - (-1);
c = count + a[i];
if (c == 0) {
a[i]--;
ans1++;
}
} else if (count < 0 && c <= 0) {
ans1 += 1 - c;
a[i] += 1 - c;
c = count + a[i];
if (c == 0) {
a[i]++;
ans1++;
}
}
count += a[i];
}
a[0] = -1;
ans2++;
count += a[0];
}
cout << endl;
cout << ans1 << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | #!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')
#A
def A():
a = input().split()
a = list(map(lambda x: x.capitalize(), a))
a,b,c = a
print(a[0]+b[0]+c[0])
return
#B
def B():
a = II()
b = II()
if a > b:
print("GREATER")
if a < b:
print("LESS")
if a == b:
print("EQUAL")
return
#C
def C():
II()
a = LI()
def f(suma, b):
for i in a[1:]:
if (suma + i) * suma < 0:
suma += i
continue
b += abs(suma + i) + 1
suma = -1 * (suma > 0) or 1
return b
if a[0] == 0:
ans = min(f(1, 1), f(-1, 1))
else:
ans = min(f(a[0], 0), f(-a[0], 2 * abs(a[0])))
print(ans)
return
#D
def D():
s = S()
for i in range(len(s) - 1):
if s[i] == s[i+1]:
print(i + 1, i + 2)
return
for i in range(len(s) - 2):
if s[i] == s[i + 2]:
print(i + 1, i + 3)
return
print(-1, -1)
return
#Solve
if __name__ == '__main__':
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 | 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 isFirstPlus = arr[0] > 0? true: false;
let sum = 0;
let ans = 0;
// とりあえず偶数で奇数がプラスの時に正常動作するものを書く
for(let i = 0; i < amount; i++){
sum += arr[i];
if((sum >= 0) != isFirstPlus){
// 不備の時の処理(+1なのは0からどちらかに1つ増やしたいから)
ans += Math.abs(sum) + 1;
// 場合わけでsumを1か-1に戻す処理
if(sum >= 0){
sum = -1;
} else {
sum = 1;
}
}
// 偶奇で判定を逆転する
isFirstPlus = !isFirstPlus;
}
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.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] a = new int[n];
int[] s = new int[n];
int sum = 0;
a[0] = scanner.nextInt();
sum += a[0];
boolean check;
if(a[0] < 0){
check = false;
}else{
check = true;
}
int count = 0;
for(int i=1;i<n;i++){
a[i] = scanner.nextInt();
int x = sum + a[i];
int y = 0;
if(check && x >= 0){
y = -1 - x;
}else if(!check && x < 0){
y = 1 - x;
}else if(!check && x == 0){
y = 1;
}
a[i] += y;
count += Math.abs(y);
sum += a[i];
//System.out.println(y + " " + a[i] + " " + sum);
check = !check;
}
if(sum == 0){
count ++;
}
System.out.println(count);
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <iostream>
#include <algorithm>
using namespace std;
int main(){
bool ch = false;//falseの時は和が一個前で負
long long N,i;
long long ans=0,count=0;
cin >> N;
long long a[N];
cin >> a[0];
ans+=a;
if(ans>0)ch = true;
else ch = false;
for(i=1;i<N;i++){
cin >> a[i];
if(ch/*ans > 0*/){
if(ans>=-a[i]){
count += ans+a[i]+1;
ans = -1;
}
else ans += a[i];
ch = false;
}else /*if(ans < 0)*/{
if(ans<=-a[i]){
count += -ans-a[i]+1;
ans = 1;
}
else ans += a[i];
ch = true;
}
//else if(ans == 0)ans=a;
//cout << ans << " " << count << endl;
}
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/*ans > 0*/){
if(ans>=-a[i]){
count += ans+a[i]+1;
ans = -1;
}
else ans += a[i];
ch = false;
}else /*if(ans < 0)*/{
if(ans<=-a[i]){
count += -ans-a[i]+1;
ans = 1;
}
else ans += a[i];
ch = true;
}
//else if(ans == 0)ans=a;
//cout << ans << " " << count << endl;
}
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() {
int n;
cin >> n;
vector<int> a(n);
for (long long(i) = (0); (i) < (long long)(n); ++(i)) cin >> a[i];
long long sum = a[0];
long long ans = 0;
for (int i = 1; i < n; ++i) {
if ((sum > 0 and sum + a[i] < 0) or (sum < 0 and sum + a[i] > 0)) {
sum += a[i];
} else {
if (sum > 0) {
sum += a[i];
for (; sum >= 0; --sum) {
++ans;
}
} else {
sum += a[i];
for (; sum <= 0; ++sum) {
++ans;
}
}
}
}
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 long mod = 1000000007;
int main() {
int n;
cin >> n;
long long cnt = 0;
long long sum = 0;
for (int i = 0; i < n; ++i) {
long long t;
cin >> t;
if (i == 0) {
sum += t;
} else {
long long tsum = sum + t;
long long sign = (sum > 0) ? 1 : -1;
if (tsum * sum > 0) {
cnt += abs(tsum) + 1;
sum = -1 * sign;
} else {
sum = tsum;
if (sum == 0) {
sum = -1 * sign;
cnt += 1;
}
}
}
}
cout << cnt << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
scanf("%d", &n);
vector<long long> a;
for (int i = 0; i < n; i++) {
long long an;
scanf("%lld", &an);
a.push_back(an);
}
long long op_count = 0;
long long now_sum = 0;
if (a[0] == 0) {
int numOfZeros = 1;
while (a[numOfZeros] == 0) {
numOfZeros++;
}
if (a[numOfZeros] > 0) {
if (numOfZeros % 2 == 0) {
a[0] = 1;
} else {
a[0] = -1;
}
} else {
if (numOfZeros % 2 == 0) {
a[0] = -1;
} else {
a[0] = 1;
}
}
}
long long adding = a[0] > 0 ? -1 : 1;
for (int i = 0; i < n; i++) {
now_sum += a[i];
adding *= -1;
if (now_sum == 0) {
a[i] += adding;
now_sum += adding;
op_count++;
continue;
}
if (adding > 0) {
const long long last = 1 - now_sum;
if (last > 1) {
a[i] += last;
now_sum += last;
op_count += abs(last);
}
} else {
const long long last = -1 - now_sum;
if (last < -1) {
a[i] += last;
now_sum += last;
op_count += abs(last);
}
}
}
printf("%lld\n", op_count);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long a[100010];
long long sum1 = 0;
long long sum2 = 0;
long long res1 = 0;
long long res2 = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
sum1 += a[i];
sum2 += a[i];
if (i % 2 == 0) {
if (sum1 < 0)
continue;
else if (sum1 >= 0) {
res1 += sum1 + 1;
sum1 = -1;
}
} else if (i % 2 == 1) {
if (sum1 > 0)
continue;
else if (sum1 <= 0) {
res1 += -sum1 + 1;
sum1 = 1;
}
}
if (i % 2 == 0) {
if (sum2 > 0)
continue;
else if (sum2 <= 0) {
res2 += -sum2 + 1;
sum2 = 1;
}
} else if (i % 2 == 1) {
if (sum2 < 0)
continue;
else if (sum2 >= 0) {
res2 += sum2 + 1;
sum2 = -1;
}
}
}
cout << min(res1, res2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
arr = [int(x) for x in input().split()]
def exec(sign):
a = [x for x in arr]
res = 0
if a[0] == 0:
a[0] = sign
res += 1
x = 0
for i in range(n-1):
x += a[i]
tmp = sign - (x + a[i+1])
if sign < 0:
tmp = min(tmp, 0)
else:
tmp = max(tmp, 0)
res += abs(tmp)
a[i+1] += tmp
sign *= (-1)
return res
print(min(exec(1), exec(-1)))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | package main
import (
"bufio"
"fmt"
"os"
"strings"
"strconv"
)
func main() {
sc := bufio.NewScanner(os.Stdin)
sc.Buffer(make([]byte, 64*1024*1024), 64*1024*1024)
sc.Scan()
n, _ := strconv.Atoi(sc.Text())
sc.Scan()
aArr := strings.Split(sc.Text(), " ")
a := make([]int, n)
for i := 0; i < n; i++ {
a[i], _ = strconv.Atoi(aArr[i])
}
cnt := 0
sum := a[0]
for i := 1; i < n; i++ {
if (sum+a[i])*sum < 0 {
sum += a[i]
continue
} else if sum+a[i] < 0 {
cnt += 1-(sum+a[i])
sum = 1
} else if sum+a[i] > 0{
cnt += 1+(sum+a[i])
sum = -1
} else {
cnt += 1
if sum > 0 {
sum = -1
} else {
sum = 1
}
}
}
fmt.Println(cnt)
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <bits/stdc++.h>
int main(void) {
int num_integer;
if (scanf("%d", &num_integer) != 1) {
puts("num_integer input error.");
return 1;
}
int integer, sum_p = 0, op_count_p = 0, sum_m = 0, op_count_m = 0;
for (int i = 0; i < num_integer; i++) {
if (scanf("%d", &integer) != 1) {
puts("integer input error.");
}
sum_p += integer;
sum_m += integer;
if (i % 2 == 0) {
if (sum_p <= 0) {
op_count_p += -sum_p + 1;
sum_p = 1;
}
if (sum_m >= 0) {
op_count_m += sum_m + 1;
sum_m = -1;
}
} else {
if (sum_p >= 0) {
op_count_p += sum_p + 1;
sum_p = -1;
}
if (sum_m <= 0) {
op_count_m += -sum_m + 1;
sum_m = 1;
}
}
}
printf("%d", (op_count_p < op_count_m) ? op_count_p : op_count_m);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n=int(input())
A=list(map(int,input().split()))
a,memo1,memo2=A[0],0,0
if a<0:
memo1+=1-a
a=1
for i in range(2,n):
if a*(a+A[i])<=-1:a=a+A[i]
else:
memo1+=abs(a+A[i])+1
if a>=1:a=-1
else:a=-1
if a>0:
memo2+=a+1
a=-1
for i in range(2,n):
if a*(a+A[i])<=-1:a=a+A[i]
else:
memo2+=abs(a+A[i])+1
if a>=1:a=-1
else:a=-1
print(min(memo1,memo2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.*;
public class Main {
private static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int n = sc.nextInt();
long[] a = new long[n];
for (int i = 0;i < n;i++) a[i] = sc.nextLong();
if (is(a)) {
System.out.println(n*2-1);
return;
}
long ret = calc(a,true);
ret = Math.min(calc(a,false),ret);
System.out.println(ret);
}
private static boolean is(long[] a) {
for (int i = 0;i < a.length;i++) {
if (a[i]!=0) return false;
}
return true;
}
private static long calc(long[] a, boolean b) {
long sum = a[0];
long ret = 0;
if (b) {
ret = Math.abs(sum)+1;
if (sum<0) {
sum = 1;
} else {
sum = -1;
}
}
long tmp = 0;
for (int i = 1;i < a.length;i++) {
long num = a[i];
tmp = sum;
sum += num;
if ((tmp<0&&sum>=0)||(tmp>=0&&sum<0)) continue;
long l = Math.abs(sum)+1;
if (sum>=0) {
sum -= l;
} else {
sum += l;
}
ret += l;
}
if (sum==0) ret++;
return ret;
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::{BufWriter, Write};
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
($($r:tt)*) => {
let stdin = std::io::stdin();
let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
let mut next = move || -> String{
bytes
.by_ref()
.map(|r|r.unwrap() as char)
.skip_while(|c|c.is_whitespace())
.take_while(|c|!c.is_whitespace())
.collect()
};
input_inner!{next, $($r)*}
};
}
macro_rules! input_inner {
($next:expr) => {};
($next:expr, ) => {};
($next:expr, $var:ident : $t:tt $($r:tt)*) => {
let $var = read_value!($next, $t);
input_inner!{$next $($r)*}
};
}
macro_rules! read_value {
($next:expr, [graph1; $len:expr]) => {{
let mut g = vec![vec![]; $len];
let ab = read_value!($next, [(usize1, usize1)]);
for (a, b) in ab {
g[a].push(b);
g[b].push(a);
}
g
}};
($next:expr, ( $($t:tt),* )) => {
( $(read_value!($next, $t)),* )
};
($next:expr, [ $t:tt ; $len:expr ]) => {
(0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
};
($next:expr, chars) => {
read_value!($next, String).chars().collect::<Vec<char>>()
};
($next:expr, usize1) => (read_value!($next, usize) - 1);
($next:expr, [ $t:tt ]) => {{
let len = read_value!($next, usize);
read_value!($next, [$t; len])
}};
($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}
#[allow(unused)]
macro_rules! debug {
($($format:tt)*) => (write!(std::io::stderr(), $($format)*).unwrap());
}
#[allow(unused)]
macro_rules! debugln {
($($format:tt)*) => (writeln!(std::io::stderr(), $($format)*).unwrap());
}
/*
mod mod_int {
use std::ops::*;
pub trait Mod: Copy { fn m() -> i64; }
#[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct ModInt<M> { pub x: i64, phantom: ::std::marker::PhantomData<M> }
impl<M: Mod> ModInt<M> {
// x >= 0
pub fn new(x: i64) -> Self { ModInt::new_internal(x % M::m()) }
fn new_internal(x: i64) -> Self {
ModInt { x: x, phantom: ::std::marker::PhantomData }
}
pub fn pow(self, mut e: i64) -> Self {
debug_assert!(e >= 0);
let mut sum = ModInt::new_internal(1);
let mut cur = self;
while e > 0 {
if e % 2 != 0 { sum *= cur; }
cur *= cur;
e /= 2;
}
sum
}
#[allow(dead_code)]
pub fn inv(self) -> Self { self.pow(M::m() - 2) }
}
impl<M: Mod, T: Into<ModInt<M>>> Add<T> for ModInt<M> {
type Output = Self;
fn add(self, other: T) -> Self {
let other = other.into();
let mut sum = self.x + other.x;
if sum >= M::m() { sum -= M::m(); }
ModInt::new_internal(sum)
}
}
impl<M: Mod, T: Into<ModInt<M>>> Sub<T> for ModInt<M> {
type Output = Self;
fn sub(self, other: T) -> Self {
let other = other.into();
let mut sum = self.x - other.x;
if sum < 0 { sum += M::m(); }
ModInt::new_internal(sum)
}
}
impl<M: Mod, T: Into<ModInt<M>>> Mul<T> for ModInt<M> {
type Output = Self;
fn mul(self, other: T) -> Self { ModInt::new(self.x * other.into().x % M::m()) }
}
impl<M: Mod, T: Into<ModInt<M>>> AddAssign<T> for ModInt<M> {
fn add_assign(&mut self, other: T) { *self = *self + other; }
}
impl<M: Mod, T: Into<ModInt<M>>> SubAssign<T> for ModInt<M> {
fn sub_assign(&mut self, other: T) { *self = *self - other; }
}
impl<M: Mod, T: Into<ModInt<M>>> MulAssign<T> for ModInt<M> {
fn mul_assign(&mut self, other: T) { *self = *self * other; }
}
impl<M: Mod> Neg for ModInt<M> {
type Output = Self;
fn neg(self) -> Self { ModInt::new(0) - self }
}
impl<M> ::std::fmt::Display for ModInt<M> {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
self.x.fmt(f)
}
}
impl<M: Mod> ::std::fmt::Debug for ModInt<M> {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
let (mut a, mut b, _) = red(self.x, M::m());
if b < 0 {
a = -a;
b = -b;
}
write!(f, "{}/{}", a, b)
}
}
impl<M: Mod> From<i64> for ModInt<M> {
fn from(x: i64) -> Self { Self::new(x) }
}
// Finds the simplest fraction x/y congruent to r mod p.
// The return value (x, y, z) satisfies x = y * r + z * p.
fn red(r: i64, p: i64) -> (i64, i64, i64) {
if r.abs() <= 10000 {
return (r, 1, 0);
}
let mut nxt_r = p % r;
let mut q = p / r;
if 2 * nxt_r >= r {
nxt_r -= r;
q += 1;
}
if 2 * nxt_r <= -r {
nxt_r += r;
q -= 1;
}
let (x, z, y) = red(nxt_r, r);
(x, y - q * z, z)
}
} // mod mod_int
macro_rules! define_mod {
($struct_name: ident, $modulo: expr) => {
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
struct $struct_name {}
impl mod_int::Mod for $struct_name { fn m() -> i64 { $modulo } }
}
}
const MOD: i64 = 1_000_000_007;
define_mod!(P, MOD);
type ModInt = mod_int::ModInt<P>;
//n^p mod m
fn repeat_square(n: i64, p: i64, m: i64) -> i64 {
if p == 0 {
1
} else if p == 1 {
n % m
} else if p % 2 == 0 {
repeat_square(n, p / 2, m).pow(2) % m
} else {
(n * repeat_square(n, p - 1, m)) % m
}
}
fn ncr_mod(n: i64, r: i64, m: i64) -> i64 {
let mut denominator = n;
let mut numerator = 1;
for i in 1..r {
denominator = (denominator * (n - i)) % m;
numerator = (numerator * (i + 1)) % m;
}
(denominator * repeat_square(numerator, m - 2, m)) % m
}
*/
fn solve() {
let out = std::io::stdout();
let mut out = BufWriter::new(out.lock());
macro_rules! puts {
($($format:tt)*) => (let _ = write!(out,$($format)*););
}
input! {
n: usize,
a: [i32; n],
}
let mut s = a.clone();
for i in 1..n {
s[i] += s[i-1];
}
let a = s;
let mut ans = std::i64::MAX;
let mut sum = 0;
let mut add = 0;
if a[0] <= 0 {
let d = -a[0] + 1;
add += d;
sum += d;
}
//+ - + -
for i in 1..n {
if i % 2 == 1 {
if a[i] + add >= 0 {
let d = a[i] + add + 1;
add += -d;
sum += d;
}
} else {
if a[i] + add <= 0 {
let d = -(a[i] + add) + 1;
add += d;
sum += d;
}
}
}
ans = std::cmp::min(ans, sum);
let mut sum = 0;
let mut add = 0;
if a[0] >= 0 {
let d = a[0] + 1;
add += -d;
sum += d;
}
// - + - +
for i in 1..n {
if i % 2 == 0 {
if a[i] + add >= 0 {
let d = a[i] + add + 1;
add += -d;
sum += d;
}
} else {
if a[i] + add <= 0 {
let d = -(a[i] + add) + 1;
add += d;
sum += d;
}
}
}
ans = std::cmp::min(ans, sum);
puts!("{}\n",ans);
}
fn main() {
// In order to avoid potential stack overflow, spawn a new thread.
let stack_size = 104_857_600; // 100 MB
let thd = std::thread::Builder::new().stack_size(stack_size);
thd.spawn(|| solve()).unwrap().join().unwrap();
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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 | function Main(s) {
var s = s.split("\n");
var n = parseInt(s[0], 10);
var a = s[1].split(" ").map(e => parseInt(e, 10));
var acc = 0, cnt = 0, arr = [];
for (var i = 0; i < n; i++) {
acc += a[i];
if (i === 0) {
if (a[i + 1] >= 0) {
acc--;
cnt++;
} else {
acc++;
cnt++;
}
} else {
if (arr[i - 1] > 0) {
if (acc >= 0) {
cnt += (acc + 1);
acc -= (acc + 1);
}
} else {
if (acc <= 0) {
cnt += (Math.abs(acc) + 1);
acc += (Math.abs(acc) + 1);
}
}
}
arr.push(acc);
}
console.log(cnt);
}
Main(require("fs").readFileSync("/dev/stdin", "utf8")); |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(N);
for(int i = 0; i < N; i++){
cin >> a[i];
}
long long sum = 0;
long long ans = 0;
for(int i = 0; i < N; i++){
sum += a[i];
if(i % 2 == 0){
if(sum < 0){
ans += -(sum) + 1;
sum = 1;
} else if(sum == 0){
ans++;
sum = 1;
}
} else {
if(sum > 0){
ans += sum + 1;
sum = -1;
} else if(sum == 0){
ans++;
sum = -1;
}
}
}
sum = 0;
long long ans2 = 0;
for(int i = 0; i < N; i++){
sum += a[i];
if(i % 2 == 0){
if(sum < 0){
ans2 += -(sum) + 1;
sum = 1;
} else if(sum == 0){
ans2++;
sum = 1;
}
} else {
if(sum > 0){
ans2 += sum + 1;
sum = -1;
} else if(sum == 0){
ans2++;
sum = -1;
}
}
}
cout << min(ans,ans2) << endl;
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long int;
const ll INF = (1LL << 32);
const ll MOD = (ll)1e9 + 7;
const double EPS = 1e-9;
ll dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
ll dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
signed main() {
ios::sync_with_stdio(false);
ll n;
cin >> n;
vector<ll> a;
for (ll i = 0; i < n; i++) {
ll x;
cin >> x;
a.push_back(x);
}
ll sum = a[0];
ll ans = 0;
for (ll i = (1); i < (n); i++) {
if (sum > 0 and (sum + a[i]) > 0) {
while (sum + a[i] != -1) {
a[i]--;
ans++;
}
} else if (sum < 0 and (sum + a[i]) < 0) {
while (sum + a[i] != 1) {
a[i]++;
ans++;
}
}
sum += a[i];
if (sum == 0) {
while (true) {
}
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
int a[100010];
int main() {
int n, i, j, k;
long long s, x, y;
while (scanf("%d", &n) != EOF) {
for (i = 0; i < n; i++) scanf("%d", &a[i]);
s = 0;
if (a[0] < 0) {
x = -1;
s = s - a[0] - 1;
} else if (a[0] > 0) {
x = 1;
s = s + x - 1;
}
for (i = 1; i < n; i++) {
y = x;
x = x + a[i];
if (x * y < 0) continue;
if (y < 0) {
s = s - x + 1;
x = 1;
} else if (y > 0) {
s = s + x + 1;
x = -1;
}
}
printf("%lld\n", s);
}
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 | # 参考: https://beta.atcoder.jp/contests/abc059/submissions/3269205
N = gets.to_i
AS = gets.split.map(&:to_i)
results = [1, -1].map do |s|
sum = 0
count = 0
AS.each do |a|
s = s < 0 ? 1 : -1
sum += a
if sum == 0
count += 1
sum = s ? -1 : 1
next
end
if s > 0
if sum > 0
count += sum + 1
sum = -1
end
else
if sum < 0
count += -sum + 1
sum = 1
end
end
end
count
end
p results.min |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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;
bool DifSign(vector<ll> a, int i) {
if ((a[i] > 0 && a[i + 1] < 0) || (a[i] < 0 && a[i + 1] > 0)) {
return true;
} else {
return false;
}
}
int main() {
int n;
cin >> n;
vector<ll> a(n);
for (int i = 0; i < (n); i++) cin >> a[i];
ll s = a[0];
for (int i = 1; i < n; i++) {
a[i] += s;
s = a[i];
}
vector<ll> b = a;
int cnt, ans1 = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0 && a[i] <= 0) {
cnt = 0;
while (a[i] <= 0) {
a[i]++;
cnt++;
ans1++;
}
for (int j = i + 1; j < n; j++) {
a[j] += cnt;
}
continue;
}
if (i % 2 == 1 && a[i] >= 0) {
cnt = 0;
while (a[i] >= 0) {
a[i]--;
cnt++;
ans1++;
}
for (int j = i + 1; j < n; j++) {
a[j] -= cnt;
}
continue;
}
}
int ans2 = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 1 && b[i] <= 0) {
cnt = 0;
while (b[i] <= 0) {
b[i]++;
cnt++;
ans2++;
}
for (int j = i + 1; j < n; j++) {
a[j] += cnt;
}
continue;
}
if (i % 2 == 0 && b[i] >= 0) {
cnt = 0;
while (b[i] >= 0) {
b[i]--;
cnt++;
ans2++;
}
for (int j = i + 1; j < n; j++) {
b[j] -= cnt;
}
}
continue;
}
cout << min(ans1, ans2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect
from heapq import heappush, heappop
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
n = INT()
a = LIST()
if a[0] != 0:
tmp_sum = a[0]
ans = 0
else:
if a[1] == 0:
tmp_sum = 1
else:
tmp_sum = -1
ans = 1
a = a[1:]
for x in a:
if (tmp_sum + x) * tmp_sum < 0: # 異符号
tmp_sum += x
else:
if tmp_sum > 0:
ans += abs(-tmp_sum-1-x)
tmp_sum = -1
else:
ans += abs(-tmp_sum+1-x)
tmp_sum = 1
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n, a[100000];
int main(void) {
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
int sum = 0, res = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 0) {
if (sum <= 0) {
res += 1 - sum;
sum = 1;
}
} else {
if (sum >= 0) {
res += 1 + sum;
sum = -1;
}
}
}
int pon = 0, mat = 0;
for (int i = 0; i < n; i++) {
mat += a[i];
if (i % 2 != 0) {
if (mat <= 0) {
pon += 1 - mat;
mat = 1;
}
} else {
if (mat >= 0) {
pon += 1 + mat;
mat = -1;
}
}
}
cout << min(res, pon) << 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 <bits/stdc++.h>
int main() {
int n;
int a[100001];
int i;
int sum;
int ans1 = 0, ans2 = 0;
int ans;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
sum = a[0];
if (a[0] <= 0) {
ans1 += -a[0] + 1;
sum += ans1;
}
for (i = 1; i < n; i++) {
sum += a[i];
if (i % 2 == 0) {
if (sum <= 0) {
ans1 += -sum + 1;
sum += ans1;
}
} else if (i % 2 == 1) {
if (sum >= 0) {
ans1 += sum + 1;
sum -= ans1;
}
}
}
sum = a[0];
if (a[0] >= 0) {
ans2 += a[0] + 1;
sum += ans;
}
for (i = 1; i < n; i++) {
sum += a[i];
if (i % 2 == 1) {
if (sum <= 0) {
ans2 += -sum + 1;
sum += ans2;
}
} else if (i % 2 == 0) {
if (sum >= 0) {
ans2 += sum + 1;
sum -= ans2;
}
}
}
if (ans1 >= ans2)
ans = ans2;
else
ans = ans1;
printf("%d", ans);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int64_t n;
cin >> n;
int64_t count = 0;
int64_t sum = 0;
vector<int> a(n);
for (int i = 0; i < (int)(n); i++) cin >> a.at(i);
int Ah = 0;
int Bh = 0;
int64_t sumA = 0;
int64_t sumB = 0;
if (a.at(0) == 0) {
if (a.at(1) > 0)
a.at(0) = -1;
else
a.at(0) = 1;
count++;
}
for (int i = 0; i < n - 1; i++) {
sumA += a.at(i);
Ah = 0;
Bh = 0;
for (;;) {
sumB = sumA + a.at(i + 1);
if (sumA > 0)
Ah = 1;
else
Ah = -1;
if (sumB > 0)
Bh = 1;
else if (sumB < 0)
Bh = -1;
else
Bh = 0;
if ((Ah == 1 && Bh == -1) || (Ah == -1 && Bh == 1))
break;
else if (Ah == 1 && Bh != -1) {
a.at(i + 1) -= abs(sumB) + 1;
count += abs(sumB) + 1;
break;
} else if (Ah == -1 && Bh != 1) {
a.at(i + 1) += abs(sumB) + 1;
count += abs(sumB) + 1;
break;
}
}
}
cout << count << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import sys, os
f = lambda:list(map(int,input().split()))
if 'local' in os.environ :
sys.stdin = open('./input.txt', 'r')
def solve():
n = f()[0]
a = f()
suma = [0] * n
minop = 1e9
for greater0 in [True, False]:
oper = 0
for i in range(n):
if i == 0:
suma[i] = a[i]
else:
suma[i] = a[i] + suma[i-1]
greater0 = not greater0
if greater0 and suma[i]<=0:
oper += 1 - suma[i]
suma[i] = 1
continue
if (not greater0) and suma[i]>=0:
oper += 1 + suma[i]
suma[i] = -1
continue
minop = min(minop, oper)
print(minop)
solve()
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
unsigned long long f(int a[], int n, bool plus) {
unsigned long long ans = 0;
int sum = a[0];
if (sum == 0) {
ans++;
if (plus) {
sum++;
} else {
sum--;
}
} else {
plus = (sum > 0);
}
for (int i = 1; i < n; i++) {
sum += a[i];
if ((plus && sum < 0) || (!plus && sum > 0)) {
plus = !plus;
continue;
}
if (plus) {
ans += (sum + 1);
sum = -1;
} else {
ans += (-sum + 1);
sum = 1;
}
plus = !plus;
}
return ans;
}
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
unsigned long long ans1 = f(a, n, true);
unsigned long long ans2 = f(a, n, false);
cout << min(ans1, ans2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
struct __ {
__() {
ios_base::Init i;
ios_base::sync_with_stdio(0);
cin.tie(0);
}
} __;
int main() {
int n;
cin >> n;
vector<long long> v(n);
for (int i = 0; i < n; ++i) {
cin >> v[i];
}
vector<long long> a = v;
long long cnt = 0;
if (v[0] <= 0) {
v[0] = 1;
cnt += 1 + abs(v[0]);
}
long long ans = v[0];
for (int i = 1; i < n; ++i) {
ans += v[i];
if (i % 2 == 0 && ans <= 0) {
long long x = min(1 + abs(ans), abs(ans - v[i]) - 1);
cnt += 1 + abs(ans);
v[i] = v[i] + (1 + abs(ans) - x);
v[i - 1] = v[i - 1] - x;
ans = 1;
}
if (i % 2 == 1 && ans >= 0) {
long long x = min(1 + ans, ans - v[i] - 1);
v[i] = v[i] - (1 + ans - x);
v[i - 1] = v[i - 1] - x;
cnt += 1 + ans;
ans = -1;
}
}
v = a;
long long res = cnt;
cnt = 0;
if (v[0] >= 0) {
v[0] = -1;
cnt += 1 + v[0];
}
ans = v[0];
for (int i = 1; i < n; ++i) {
ans += v[i];
if (i % 2 == 1 && ans <= 0) {
long long x = min(1 + abs(ans), abs(ans - v[i]) - 1);
cnt += 1 + abs(ans);
v[i] = v[i] + (1 + abs(ans) - x);
v[i - 1] = v[i - 1] - x;
ans = 1;
}
if (i % 2 == 0 && ans >= 0) {
long long x = min(1 + ans, ans - v[i] - 1);
v[i] = v[i] - (1 + ans - x);
v[i - 1] = v[i - 1] - x;
cnt += 1 + ans;
ans = -1;
}
}
cout << min(res, cnt);
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | def decision(i):
sum_for_i = sum(A[:i+1]) # 0~i までの sum
sum_before_i = sum(A[:i]) # 0~i-1までの sum
if sum_for_i == 0:
if sum_before_i <0:
return 1
if sum_before_i >0:
return 2
if sum_before_i > 0 and sum_for_i >0:
return 3
if sum_before_i < 0 and sum_for_i <0:
return 4
return 0
n = int(input())
original_A = [int(x) for x in input().split()]
A = original_A.copy()
for i in range(1, n):
result = decision(i)
if result == 0:
continue
elif result == 1:
A[i] += 1
elif result == 2:
A[i] -= 1
elif result == 3:
A[i] -= (abs((sum(A[:i+1]))+1))
elif result == 4:
A[i] += (abs(sum(A[:i+1]))+1)
print(sum([abs(original_A[x] - A[x]) for x in range(n)])) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n=int(input())
A=list(map(int,input().split()))
ans=0
if A[0]==0:
for i in range(1,n):
if A[i]!=0:
if A[i]>0:
if i%2==0:
A[0]=1
ans+=1
else:
A[0]=-1
ans+=1
elif A[i]<0:
if i%2==0:
A[0]=-1
ans+=1
else:
A[0]=1
ans+=1
S=A[0]
for i in range(1,n):
if S>0:
if A[i]+S<0:
S=S+A[i]
else:
ans+=A[i]+S+1
S=-1
else:
if A[i]+S>0:
S=S+A[i]
else:
ans+=1-(A[i]+S)
S=1
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
long long a[n], b[n];
cin >> a[0];
b[0] = a[0];
for (long long i = 1; i < n; i++) {
cin >> a[i];
b[i] = b[i - 1] + a[i];
}
if (count(a, a + n, 0) == n) {
cout << 2 * n - 1 << endl;
return 0;
}
long long sgn = 1;
long long ans = 0;
long long shift = 0;
for (long long i = 0; i < n; i++) {
if ((b[i] + shift) * sgn <= 0) {
ans += abs(b[i] + shift) + 1;
shift += -b[i] + (b[i] == 0 ? 0 : -b[i] / abs(b[i]));
}
sgn *= -1;
}
sgn = -1;
shift = 0;
long long ans2 = 0;
for (long long i = 0; i < n; i++) {
if ((b[i] + shift) * sgn <= 0) {
ans2 += abs(b[i] + shift) + 1;
shift += -b[i] + (b[i] == 0 ? 0 : -b[i] / abs(b[i]));
}
sgn *= -1;
}
cout << min(ans, ans2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
using System.Collections.Generic;
using System.Linq;
namespace ProgramingStydying
{
class Program
{
static void Main(string[] args)
{
var n = int.Parse(Console.ReadLine());
var a = Console.ReadLine().Split().Select(int.Parse).ToList();
var ans = 0;
if(a[0] == 0)
{
ans = Math.Min(Solve(n, a, 1), Solve(n, a, -1)) + 1;
}
else
{
ans = Solve(n, a, a[0]);
}
Console.WriteLine(ans);
}
static int Solve(int n, List<int> a, int sum)
{
var ans = 0;
for (int i = 1; i < n; i++)
{
if (sum > 0)
{
sum += a[i];
if (sum < 0)
{
continue;
}
else
{
ans += sum + 1;
sum = -1;
}
}
else if (sum < 0)
{
sum += a[i];
if (sum > 0)
{
continue;
}
else
{
ans += -sum + 1;
sum = 1;
}
}
}
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 N;
cin >> N;
long long int a[N];
for (int i = 0; i < N; i++) {
cin >> a[i];
}
int x = 0, y = 0;
int s = 0;
for (int i = 0; i < N; i++) {
s += a[i];
if (i % 2 == 1) {
if (s > 0) {
x += s + 1;
s = -1;
}
} else {
if (s < 0) {
x += 1 - s;
s = 1;
}
}
}
s = 0;
for (int i = 0; i < N; i++) {
s += a[i];
if (i % 2 == 1) {
if (s < 0) {
y += 1 - s;
s = -1;
}
} else {
if (s > 0) {
y += s + 1;
s = 1;
}
}
}
int ans = min(x, y);
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <bits/stdc++.h>
int main(void) {
char buf[1500000];
FILE *fp = stdin;
if (!fgets(buf, 1500000, fp)) return 0;
int num;
sscanf(buf, "%d", &num);
if (!fgets(buf, 1500000, fp)) return 0;
char *tmpbuf = buf;
int i;
long sum_plus = 0;
long sum_minus = 0;
long res_plus = 0;
long res_minus = 0;
for (i = 0; i < num; i++) {
char *tmp = strtok(tmpbuf, " ");
long n = strtol(tmp, NULL, 10);
sum_plus += n;
sum_minus += n;
if (i % 2) {
if (sum_plus >= 0) {
res_plus += labs(sum_plus + 1);
sum_plus = -1;
}
if (sum_minus <= 0) {
res_minus += labs(sum_minus - 1);
sum_minus = 1;
}
} else {
if (sum_plus <= 0) {
res_plus += labs(sum_plus - 1);
sum_plus = 1;
}
if (sum_minus >= 0) {
res_minus += labs(sum_minus + 1);
sum_minus = -1;
}
}
tmpbuf = NULL;
}
int res = ((res_plus) < (res_minus) ? (res_plus) : (res_minus));
printf("%d\n", res);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
template <class T, class S>
void cmin(T &a, const S &b) {
if (a > b) a = b;
}
template <class T, class S>
void cmax(T &a, const S &b) {
if (a < b) a = b;
}
using namespace std;
signed main() {
long long int n;
cin >> n;
vector<long long int> v(n), sum(n);
for (long long int i = 0; i < n; i++) cin >> v[i];
bool flag = false;
long long int ans = 0;
for (long long int i = 0; i < n; i++) {
if (i == 0) {
sum[0] = v[0];
if (v[i] > 0)
flag = true;
else if (v[i] < 0)
flag = false;
else {
ans++;
flag = true;
sum[i] = 1;
}
continue;
}
sum[i] = v[i] + sum[i - 1];
if (flag) {
if (sum[i] < 0)
flag = false;
else if (sum[i] > 0) {
ans += abs(sum[i]) + 1;
sum[i] = -1;
} else {
ans++;
sum[i] = -1;
}
flag = false;
} else {
if (sum[i] > 0)
flag = true;
else if (sum[i] <= 0) {
ans += abs(sum[i]) + 1;
sum[i] = 1;
}
flag = true;
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import sys
input = sys.stdin.readline
n = int(input())
a = [int(x) for x in input().split()]
A = a[0]
ans = 0
for i in range(1, n):
nextA = A + a[i]
if (A > 0 and nextA < 0) or (A < 0 and nextA > 0):
A = nextA
elif A > 0:
ans += nextA + 1
A = -1
else:
ans += abs(nextA) + 1
A = 1
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int sum = 0;
long long cnt = 0;
for (int i = 0; i < n; ++i) {
int a;
cin >> a;
int _sum = sum + a;
if (sum > 0 && _sum >= 0) {
cnt += _sum + 1;
sum = -1;
} else if (sum < 0 && _sum <= 0) {
cnt += -_sum + 1;
sum = 1;
} else
sum = _sum;
}
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;
long long n, a[100005], sum, ans, m;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
sum = a[1];
if (sum == 0) {
ans = 1;
if (a[2] >= 0)
sum = -1;
else
sum = 1;
}
for (int i = 2; i <= n; i++) {
if (sum < 0) {
m = abs(sum) + 1;
if (a[i] < m)
ans += m - a[i], sum += m;
else
sum += a[i];
} else {
m = -1 - sum;
if (a[i] > m)
ans += a[i] - m, sum += m;
else
sum += a[i];
}
}
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 | python3 | n = int(input())
a = list(map(int, input().split()))
s = a[0]
ans = 0
if s == 0:
s += 1
ans +=1
for i in range(n-1):
if s > 0:
s += a[i+1]
if s >= 0:
ans += abs(s) + 1
s = -1
"""
print(i)
print('s > 0')
print(ans)
print(s)
"""
elif s < 0:
s += a[i+1]
if s <= 0:
ans += abs(s) + 1
s = 1
"""
print(i)
print('s < 0')
print(ans)
print(s)
"""
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
#####segfunc######
def segfunc(x,y):
return x+y
def init(init_val):
#set_val
for i in range(len(init_val)):
seg[i+num-1]=init_val[i]
#built
for i in range(num-2,-1,-1) :
seg[i]=segfunc(seg[2*i+1],seg[2*i+2])
def update(k,x):
k += num-1
seg[k] = x
while k:
k = (k-1)//2
seg[k] = segfunc(seg[k*2+1],seg[k*2+2])
def query(p,q):
if q<=p:
return ide_ele
p += num-1
q += num-2
res=ide_ele
while q-p>1:
if p&1 == 0:
res = segfunc(res,seg[p])
if q&1 == 1:
res = segfunc(res,seg[q])
q -= 1
p = p//2
q = (q-1)//2
if p == q:
res = segfunc(res,seg[p])
else:
res = segfunc(segfunc(res,seg[p]),seg[q])
return res
#####単位元######
ide_ele = 0
num =2**(n-1).bit_length()
seg=[ide_ele]*(2*num - 1)
init(a)
ans = 0
pre_sum = (-1) * a[0]
for i in range(n):
q_sum = query(0,i+1)
if q_sum * pre_sum >= 0:
if pre_sum < 0:
update(i, abs(pre_sum) + 1)
else:
update(i, (-1) * (pre_sum+1))
pre_sum = query(0,i+1)
for i in range(n):
ans += abs(a[i] - seg[i + num - 1])
a[i] = seg[i+num-1]
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
A = list(map(int,input().split()))
cnt = 0
for i in range(1,n):
if A[0] >= 0:
if i%2!=0 and A[i]>=0:
cnt += A[i]
A[i] -= A[i]
while sum(A[:i+1])>=0:
A[i] -= 1
cnt += 1
elif i%2==0 and A[i]<0:
cnt += abs(A[i])
A[i] += abs(A[i])
while sum(A[:i+1])<=0:
A[i] += 1
cnt += 1
else:
if A[i] >= 0:
while sum(A[:i+1])<=0:
A[i] += 1
cnt += 1
else:
while sum(A[:i+1])>=0:
A[i] -= 1
cnt += 1
else:
if i%2!=0 and A[i]<0:
cnt += abs(A[i])
A[i] += abs(A[i])
while sum(A[:i+1])<=0:
A[i] += 1
cnt += 1
elif i%2==0 and A[i]>=0:
cnt += A[i]
A[i] -= A[i]
while sum(A[:i+1])>=0:
A[i] -= 1
cnt += 1
else:
if A[i] >= 0:
while sum(A[:i+1])<=0:
A[i] += 1
cnt += 1
else:
while sum(A[:i+1])>=0:
A[i] -= 1
cnt += 1
print(cnt) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long num_operate(long n, long sum, long* a) {
long j;
for (long i = 1; i < n; i++) {
if (sum * (sum + a[i]) < 0)
sum += a[i];
else {
j += abs(sum + a[i]) + 1;
if (sum < 0)
sum = 1;
else
sum = -1;
}
}
return j;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
long n;
cin >> n;
vector<long> a(n);
for (long i = 0; i < n; i++) cin >> a[i];
long sum = a[0];
if (sum == 0) {
long cnt1 = num_operate(n, 1, &a.front()) + 1;
long cnt2 = num_operate(n, -1, &a.front()) + 1;
cout << min(cnt1, cnt2) << endl;
} else {
long cnt = num_operate(n, sum, &a.front());
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 | python3 | n = int(input())
*a, = map(int, input().split())
is_plus = False if a[0] > 0 else True
s = a[0]
ans = 0
for i in range(1, n):
t = 0
if is_plus and s+a[i] < 0:
t = abs(s-a[i]) - 1
a[i] += t
elif not is_plus and s+a[i] > 0:
t = abs(s+a[i]) + 1
a[i] -= t
s += a[i]
if s != 0:
ans += t
else:
ans += t-1 if a[-1] < 0 else t+1
is_plus = not is_plus
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python2 | # -*- coding:utf-8 -*-
n = int(raw_input())
numlist = (raw_input()).split(' ')
sumlist = [int(numlist[0])]
count = 0
for i in range(1, n):
sumlist.append(sumlist[i-1] + int(numlist[i]))
while (True):
if (sumlist[i-1] > 0 and sumlist[i] > 1): #i-1,i番目までのsumがともに正
numlist[i] = int(numlist[i]) - 1
sumlist[i] -= 1
count += 1
elif (sumlist[i-1] < 0 and sumlist[i] < 0): #i-1,i番目までのsumがともに負
numlist[i] = int(numlist[i]) + 1
sumlist[i] += 1
count += 1
elif (sumlist[i] == 0): #i番目までのsum=0
if (sumlist[i-1] > 0):
numlist[i] = int(numlist[i]) - 1
sumlist[i] -= 1
if (sumlist[i-1] < 0):
numlist[i] = int(numlist[i]) + 1
sumlist[i] += 1
count += 1
else:
break
print count
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int,input().split()))
num = a[0]
ans = 0
if a[0]>0:
for i in range(1,n):
num += a[i]
if i%2==1:
if num>=0:
ans += num+1
num = -1
else:
if num<=0:
ans += 1-num
num = 1
elif a[0]<0:
for i in range(1,n):
num += a[i]
if i%2==1:
if num <= 0:
ans += 1-num
num = 1
else:
if num >= 0:
ans += num+1
num = -1
elif a[0]= 0:
ansp = 1
ansm = 1
for i in range(1,n):
num += a[i]
if i%2==1:
if num>=0:
ansp += num+1
num = -1
else:
if num<=0:
ansp += 1-num
num = 1
for i in range(1,n):
num += a[i]
if i%2==1:
if num <= 0:
ansm += 1-num
num = 1
else:
if num >= 0:
ansm += num+1
num = -1
ans = min(ansp,ansm)
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) {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
int counter = 0;
if (a[0] >= 0) {
for (int i = 0; i < n; i++) {
int total = 0;
for (int j = 0; j <= i; j++) {
total += a[j];
}
if (i % 2 == 0 && total < 0) {
int tmp = total;
int tmp2 = a[i];
while (tmp <= 0) {
tmp++;
tmp2++;
counter++;
}
a[i] = tmp2;
} else if (i % 2 != 0 && total >= 0) {
int tmp = total;
int tmp2 = a[i];
while (tmp >= 0) {
tmp--;
tmp2--;
counter++;
}
a[i] = tmp2;
}
}
} else {
for (int i = 0; i < n; i++) {
int total = 0;
for (int j = 0; j <= i; j++) {
total += a[j];
}
if (i % 2 == 0 && total >= 0) {
int tmp = total;
int tmp2 = a[i];
while (tmp >= 0) {
tmp--;
tmp2--;
counter++;
}
a[i] = tmp2;
} else if (i % 2 != 0 && total < total) {
int tmp = total;
int tmp2 = a[i];
while (tmp <= 0) {
tmp++;
tmp2++;
counter++;
}
a[i] = tmp2;
}
}
}
cout << counter << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | function Main(s) {
var s = s.split("\n");
var n = parseInt(s[0], 10);
var a = s[1].split(" ").map(e => parseInt(e, 10));
var acc = 0, cnt = 0, arr = [];
for (var i = 0; i < n; i++) {
acc += a[i];
if (i === 0 && acc === 0) {
if (a[i + 1] >= 0) {
acc--;
cnt++;
} else {
acc++;
cnt++;
}
} else {
if (arr[i - 1] > 0) {
if (acc >= 0) {
cnt += (acc + 1);
acc -= (acc + 1);
}
} else {
if (acc <= 0) {
cnt += (Math.abs(acc) + 1);
acc += (Math.abs(acc) + 1);
}
}
}
arr.push(acc);
}
console.log(cnt);
}
Main(require("fs").readFileSync("/dev/stdin", "utf8")); |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n = 0, a[100000] = {}, b[100000] = {};
cin >> n;
for (int i = 0; i < (int)n; ++i) {
cin >> a[i];
if (i == 0) {
b[0] = a[0];
} else {
b[i] = a[i] + a[i - 1];
}
}
int count_p = 0, count_q = 0;
for (int i = 0; i < (int)n; ++i) {
if (i % 2 == 0) {
if (b[i] >= 0) {
count_p += abs(-1 - b[i]);
b[i] = -1;
}
} else {
if (b[i] <= 0) {
count_p += abs(1 - b[i]);
b[i] = 1;
}
}
}
for (int i = 0; i < (int)n; ++i) {
if (i % 2 == 0) {
if (b[i] <= 0) {
count_q += abs(1 - b[i]);
b[i] = 1;
}
} else {
if (b[i] >= 0) {
count_q += abs(-1 - b[i]);
b[i] = -1;
}
}
}
cout << std::min(count_p, count_q) << 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 s1, s2, c1, c2;
int main() {
int n, a;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a);
s1 += a;
s2 += a;
if (i % 2) {
if (s1 <= 0) c1 += 1 - s1, s1 = 1;
if (s2 >= 0) c2 += 1 + s2, s2 = -1;
} else {
if (s1 >= 0) c1 += 1 + s1, s1 = -1;
if (s2 <= 0) c2 += 1 - s2, s2 = 1;
}
}
cout << min(c1, c2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
a1 = [a[0]] * n
a2 = [-a[0]] * n
b = a[0]
b1 = a2[0]
ans = 0
ans1 = abs(a[0] * 2)
def f(x):
if x == 0:
return 0
else:
return x // abs(x)
for i in range(1, n):
if a1[i - 1] * a[i] >= 0:
a1[i] = -a[i]
else:
a1[i] = a[i]
if b * (b + a1[i]) >= 0:
a1[i] = -f(a1[i - 1]) - b
if b + a1[i] == 0:
a1[i] += f(a1[i])
ans += abs(a1[i] - a[i])
b += a1[i]
for i in range(1, n):
if a2[i - 1] * a[i] >= 0:
a2[i] = -a[i]
else:
a2[i] = a[i]
if b * (b + a2[i]) >= 0:
a2[i] = -f(a2[i - 1]) - b1
if b1 + a2[i] == 0:
a2[i] += f(a2[i])
ans1 += abs(a2[i] - a[i])
b1 += a2[i]
print(min(ans1, ans)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | N=gets.to_i
list=gets.split(" ").map(&:to_i)
sum = 0
res = 0
if(list[0] == 0) then
temp = 0
flag= true
N-1.times{ |i|
temp += list[i+1]
if(temp > 0 && flag) then
res += 1
list[0] = 1
flag = false
elsif(temp < 0 && flag) then
res +=1
list[0] = -1
flag = false
end
if(i == N-1) then
res+=1
list[0]=1
end
}
end
N.times{|i|
before_sum = sum
sum += list[i]
if (sum*before_sum> 0) then
if(sum > 0) then
res += (sum+1)
sum = -1
else
res += (-sum+1)
sum = 1
end
elsif sum*before_sum==0 then
if(before_sum < 0 )then
res += 1
sum = 1
elsif(before_sum > 0) then
sum = -1
res += 1
end
end
}
puts(res)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 7, MOD = 1e9 + 7;
const long long LINF = 1e18;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int main() {
int n;
cin >> n;
int a[n];
long long Sum1[n], Sum2[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
Sum1[i] = Sum2[i] = a[i];
if (i) {
Sum1[i] = Sum1[i - 1] + Sum1[i];
Sum2[i] = Sum1[i];
}
}
long long sum1 = 0;
long long sum2 = 0;
long long tmp1 = 0, tmp2 = 0;
for (int i = 0; i < n; i++) {
Sum1[i] += tmp1;
Sum2[i] += tmp2;
if (i % 2 == 0) {
if (Sum1[i] >= 0) {
sum1 += 1 + Sum1[i];
tmp1 += -1 - Sum1[i];
}
if (Sum2[i] <= 0) {
sum2 += 1 + (-1 * Sum2[i]);
tmp2 += 1 - Sum2[i];
}
} else {
if (Sum1[i] <= 0) {
sum1 += 1 + (-1 * Sum1[i]);
tmp1 += 1 - Sum1[i];
}
if (Sum2[i] >= 0) {
sum2 += 1 + Sum2[i];
tmp2 += 1 - Sum2[i];
}
}
}
cout << min(sum1, sum2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void) {
int n;
cin >> n;
long long a[n];
for (int(i) = 0; (i) < (n); (i)++) cin >> a[i];
long long ans1 = 0, ans2 = 0;
long long now = a[0];
for (int(i) = 0; (i) < (n - 1); (i)++) {
if (i == 0) {
if (now > 0) {
now = -1;
ans1 += now + 1;
}
}
if ((now + a[i + 1]) * now < 0) {
now += a[i + 1];
continue;
} else {
if (now < 0) {
ans1 += (1 - now) - a[i + 1];
now = 1;
} else {
ans1 += a[i + 1] + (now + 1);
now = -1;
}
}
}
now = a[0];
for (int(i) = 0; (i) < (n - 1); (i)++) {
if (i == 0 && now < 0) {
now = 1;
ans2 += 1 - now;
}
if ((now + a[i + 1]) * now < 0) {
now += a[i + 1];
continue;
} else {
if (now < 0) {
ans2 += (1 - now) - a[i + 1];
now = 1;
} else {
ans2 += a[i + 1] + (now + 1);
now = -1;
}
}
}
cout << min(ans1, ans2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long sum = 0, k, ans = 0;
cin >> k;
sum = k;
for (int i = 1; i < n; i++) {
cin >> k;
if (sum > 0 && sum + k >= 0) {
ans += (sum + k + 1);
sum = -1;
} else if (sum < 0 && sum + k <= 0) {
ans += (-(sum + k) + 1);
sum = 1;
} else {
sum += k;
}
}
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;
void solve() {
string a, b;
cin >> a >> b;
if (a.length() < b.length()) {
cout << "LESS \n";
return;
}
if (a.length() > b.length()) {
cout << "GREATER \n";
return;
}
for (int i = 0; i < a.length(); i++) {
if (a[i] - '0' < b[i] - '0') {
cout << "LESS \n";
return;
}
if (a[i] - '0' > b[i] - '0') {
cout << "GREATER \n";
return;
}
}
cout << "EQUAL \n";
}
int main() {
solve();
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll mod = 1e9 + 7;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int main() {
ll n;
cin >> n;
ll a[n];
ll sum = 0;
ll cnt = 0;
cin >> a[1];
sum += a[1];
bool flg;
if (sum > 0) {
flg = true;
} else if (sum < 0) {
flg = false;
}
for (ll i = 1; i < n; i++) {
cin >> a[i];
sum += a[i];
if (flg && sum >= 0) {
cnt += sum + 1;
sum = -1;
} else if (!flg && sum <= 0) {
cnt += 1 - sum;
sum = 1;
}
}
cout << sum << 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 ans = 1e9, flag = 1, sum = 0, cnt = 0;
for (int i = 0; i < (n); i++) {
if (flag == 1) {
if (sum + a[i] <= 0) {
cnt += 1 - sum - a[i];
sum = 1;
} else
sum += a[i];
} else {
if (sum + a[i] >= 0) {
cnt += abs(-1 - sum - a[i]);
sum = -1;
} else
sum += a[i];
}
flag = -flag;
}
ans = min(ans, cnt);
flag = -1, sum = 0, cnt = 0;
for (int i = 0; i < (n); i++) {
if (flag == 1) {
if (sum + a[i] <= 0) {
cnt += 1 - sum - a[i];
sum = 1;
} else
sum += a[i];
} else {
if (sum + a[i] >= 0) {
cnt += abs(-1 - sum - a[i]);
sum = -1;
} else
sum += a[i];
}
flag = -flag;
}
ans = min(ans, cnt);
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long a[100000];
int n;
void solve() {
long long ans = 0;
long long sum0 = a[0];
long long sum1;
for (int i = 1; i < n; i++) {
sum1 = sum0 + a[i];
if (sum1 * sum0 < 0) {
} else if (sum1 * sum0 > 0) {
ans += abs(sum1) + 1;
sum1 = -1 * sum0 / abs(sum0);
} else {
ans++;
sum1 = -1 * sum0 / abs(sum0);
}
sum0 = sum1;
}
cout << ans << endl;
return;
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
solve();
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
prv_total =0
cnt = 0
for i in range(n-1):
total = prv_total + a[i]
nxt_total = total+a[i+1]
while total*nxt_total >= 0:
if total == nxt_total:
if total > 0:
a[i+1] -= 1
nxt_total -=1
else:
a[i+1] += 1
nxt_total +=1
elif total-nxt_total == 1:
a[i+1] -= 1
nxt_total -=1
elif nxt_total-total == 1:
a[i+1] += 1
nxt_total += 1
elif (total<nxt_total and total >= 0) or (nxt_total<total and nxt_total >= 0):
total -= 1
nxt_total -= 1
elif (total<nxt_total and nxt_total <= 0) or (nxt_total<total and total <= 0):
total += 1
nxt_total += 1
cnt += 1
prv_total = total
total = prv_total + a[-1]
if total == 0:
cnt += 1
print(cnt) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <bits/stdc++.h>
int main(void) {
long long n, ans = 0;
scanf("%lld", &n);
long long a[n], sum[n];
for (long long i = 0; i < n; i++) {
scanf("%lld", &a[i]);
}
sum[0] = a[0];
for (long long i = 1; i < n; i++) {
sum[i] = a[i] + sum[i - 1];
if (sum[i - 1] < 0) {
if (sum[i] <= 0) {
ans += (llabs(sum[i]) + 1);
sum[i] += (llabs(sum[i]) + 1);
}
} else {
if (sum[i] >= 0) {
ans += (llabs(sum[i]) + 1);
sum[i] -= (llabs(sum[i]) + 1);
}
}
}
printf("%lld\n", ans);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void) {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
int tmp1 = 0, tmp2 = 0;
int ans1 = 0, ans2 = 0;
for (int i = 0; i < n; ++i) {
tmp1 += a[i];
if (i % 2 == 0) {
if (tmp1 >= 0) {
ans1 += 1 + tmp1;
tmp1 = -1;
} else {
}
} else {
if (tmp1 <= 0) {
ans1 += 1 - tmp1;
tmp1 = 1;
} else {
}
}
}
for (int i = 0; i < n; ++i) {
tmp2 += a[i];
if (i % 2 == 0) {
if (tmp2 <= 0) {
ans2 += 1 - tmp2;
tmp2 = 1;
} else {
}
} else {
if (tmp2 >= 0) {
ans2 += 1 + tmp2;
tmp2 = -1;
} else {
}
}
}
cout << min(ans1, ans2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.io.*;
import static java.lang.Math.*;
import static java.lang.Math.min;
import java.util.*;
import java.util.stream.*;
/**
* @author baito
*/
class P implements Comparable<P> {
int x, y;
P(int a, int b) {
x = a;
y = b;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof P)) return false;
P p = (P) o;
return x == p.x && y == p.y;
}
@Override
public int hashCode() {
return Objects.hash(x, y);
}
@Override
public int compareTo(P p) {
return x == p.x ? y - p.y : x - p.x; //xで昇順にソート
//return (x == p.x ? y - p.y : x - p.x) * -1; //xで降順にソート
//return y == p.y ? x - p.x : y - p.y;//yで昇順にソート
//return (y == p.y ? x - p.x : y - p.y)*-1;//yで降順にソート
}
}
@SuppressWarnings("unchecked")
public class Main {
static StringBuilder sb = new StringBuilder();
static int INF = 1234567890;
static int MINF = -1234567890;
static long LINF = 123456789123456789L;
static long MLINF = -123456789123456789L;
static long MOD = 1000000007;
static double EPS = 1e-10;
static int[] y4 = {0, 1, 0, -1};
static int[] x4 = {1, 0, -1, 0};
static int[] y8 = {0, 1, 0, -1, -1, 1, 1, -1};
static int[] x8 = {1, 0, -1, 0, 1, -1, 1, -1};
static ArrayList<Long> Fa;
static boolean[] isPrime;
static int[] primes;
static char[][] ban;
static long maxRes = MLINF;
static long minRes = LINF;
static boolean DEBUG = true;
static int N;
static long[] A;
public static void solve() throws Exception {
//longを忘れるなオーバーフローするぞ
N = ni();
A = nla(N);
long[] B = A.clone();
long sum = A[0];
long cou = 0;
boolean plus = A[0] >= 0 ? false : true;
if (A[0] == 0) {
A[0]++;
cou++;
sum++;
}
for (int i = 1; i < N; i++) {
if (plus) {
long now = sum + A[i];
if (now < 0) {
cou += (-now) + 1;
A[i] += (-now) + 1;
} else if (now == 0) {
cou++;
A[i]++;
}
sum += A[i];
plus = false;
} else {
long now = sum + A[i];
if (now > 0) {
cou += (now) + 1;
A[i] -= (now) + 1;
} else if (now == 0) {
cou++;
A[i]--;
}
sum += A[i];
plus = true;
}
}
chMax(cou);
if (B[0] == 0) {
B[0]--;
long sum2 = B[0] ;
long cou2 = 1;
boolean plu2 = true;
for (int i = 1; i < N; i++) {
if (plu2) {
long now = sum2 + B[i];
if (now < 0) {
cou2 += (-now) + 1;
B[i] += (-now) + 1;
} else if (now == 0) {
cou2++;
B[i]++;
}
sum2 += B[i];
plu2 = false;
} else {
long now = sum2 + B[i];
if (now > 0) {
cou2 += (now) + 1;
B[i] -= (now) + 1;
} else if (now == 0) {
cou2++;
B[i]--;
}
sum2 += B[i];
plu2 = true;
}
}chMax(cou2);
}
System.out.println(maxRes);
}
public static boolean calc(long va) {
//貪欲にギリギリセーフを選んでいく。
int v = (int) va;
return true;
}
//条件を満たす最大値、あるいは最小値を求める
static int mgr(long ok, long ng, long w) {
//int ok = 0; //解が存在する
//int ng = N; //解が存在しない
while (Math.abs(ok - ng) > 1) {
long mid;
if (ok < 0 && ng > 0 || ok > 0 && ng < 0) mid = (ok + ng) / 2;
else mid = ok + (ng - ok) / 2;
if (calc(mid)) {
ok = mid;
} else {
ng = mid;
}
}
if (calc(ok)) return (int) ok;
else return -1;
}
boolean equal(double a, double b) {
return a == 0 ? abs(b) < EPS : abs((a - b) / a) < EPS;
}
public static void matPrint(long[][] a) {
for (int hi = 0; hi < a.length; hi++) {
for (int wi = 0; wi < a[0].length; wi++) {
System.out.print(a[hi][wi] + " ");
}
System.out.println("");
}
}
//rにlを掛ける l * r
public static long[][] matMul(long[][] l, long[][] r) throws IOException {
int lh = l.length;
int lw = l[0].length;
int rh = r.length;
int rw = r[0].length;
//lwとrhが,同じである必要がある
if (lw != rh) throw new IOException();
long[][] res = new long[lh][rw];
for (int i = 0; i < lh; i++) {
for (int j = 0; j < rw; j++) {
for (int k = 0; k < lw; k++) {
res[i][j] = modSum(res[i][j], modMul(l[i][k], r[k][j]));
}
}
}
return res;
}
public static long[][] matPow(long[][] a, int n) throws IOException {
int h = a.length;
int w = a[0].length;
if (h != w) throw new IOException();
long[][] res = new long[h][h];
for (int i = 0; i < h; i++) {
res[i][i] = 1;
}
long[][] pow = a.clone();
while (n > 0) {
if (bitGet(n, 0)) res = matMul(pow, res);
pow = matMul(pow, pow);
n >>= 1;
}
return res;
}
public static void chMax(long v) {
maxRes = Math.max(maxRes, v);
}
public static void chMin(long v) {
minRes = Math.min(minRes, v);
}
//2点間の行き先を配列に持たせる
static int[][] packE(int n, int[] from, int[] to) {
int[][] g = new int[n][];
int[] p = new int[n];
for (int f : from)
p[f]++;
for (int t : to)
p[t]++;
for (int i = 0; i < n; i++)
g[i] = new int[p[i]];
for (int i = 0; i < from.length; i++) {
g[from[i]][--p[from[i]]] = to[i];
g[to[i]][--p[to[i]]] = from[i];
}
return g;
}
public static boolean bitGet(BitSet bit, int keta) {
return bit.nextSetBit(keta) == keta;
}
public static boolean bitGet(long bit, int keta) {
return ((bit >> keta) & 1) == 1;
}
public static int restoreHashA(long key) {
return (int) (key >> 32);
}
public static int restoreHashB(long key) {
return (int) (key & -1);
}
//正の数のみ
public static long getHashKey(int a, int b) {
return (long) a << 32 | b;
}
public static long sqrt(long v) {
long res = (long) Math.sqrt(v);
while (res * res > v) res--;
return res;
}
public static int u0(int a) {
if (a < 0) return 0;
return a;
}
public static long u0(long a) {
if (a < 0) return 0;
return a;
}
public static int[] toIntArray(int a) {
int[] res = new int[keta(a)];
for (int i = res.length - 1; i >= 0; i--) {
res[i] = a % 10;
a /= 10;
}
return res;
}
public static Integer[] toIntegerArray(int[] ar) {
Integer[] res = new Integer[ar.length];
for (int i = 0; i < ar.length; i++) {
res[i] = ar[i];
}
return res;
}
public static long bitGetCombSizeK(int k) {
return (1 << k) - 1;
}
//k個の次の組み合わせをビットで返す 大きさに上限はない 110110 -> 111001
public static long bitNextComb(long comb) {
long x = comb & -comb; //最下位の1
long y = comb + x; //連続した下の1を繰り上がらせる
return ((comb & ~y) / x >> 1) | y;
}
public static int keta(long num) {
int res = 0;
while (num > 0) {
num /= 10;
res++;
}
return res;
}
public static boolean isOutofIndex(int x, int y, int w, int h) {
if (x < 0 || y < 0) return true;
if (w <= x || h <= y) return true;
return false;
}
public static boolean isOutofIndex(int x, int y, char[][] ban) {
if (x < 0 || y < 0) return true;
if (ban[0].length <= x || ban.length <= y) return true;
return false;
}
public static int arrayCount(int[] a, int v) {
int res = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] == v) res++;
}
return res;
}
public static int arrayCount(long[] a, int v) {
int res = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] == v) res++;
}
return res;
}
public static int arrayCount(int[][] a, int v) {
int res = 0;
for (int hi = 0; hi < a.length; hi++) {
for (int wi = 0; wi < a[0].length; wi++) {
if (a[hi][wi] == v) res++;
}
}
return res;
}
public static int arrayCount(long[][] a, int v) {
int res = 0;
for (int hi = 0; hi < a.length; hi++) {
for (int wi = 0; wi < a[0].length; wi++) {
if (a[hi][wi] == v) res++;
}
}
return res;
}
public static int arrayCount(char[][] a, char v) {
int res = 0;
for (int hi = 0; hi < a.length; hi++) {
for (int wi = 0; wi < a[0].length; wi++) {
if (a[hi][wi] == v) res++;
}
}
return res;
}
public static void setPrimes() {
int n = 100001;
isPrime = new boolean[n];
List<Integer> prs = new ArrayList<>();
Arrays.fill(isPrime, true);
isPrime[0] = isPrime[1] = false;
for (int i = 2; i * i <= n; i++) {
if (!isPrime[i]) continue;
prs.add(i);
for (int j = i * 2; j < n; j += i) {
isPrime[j] = false;
}
}
primes = new int[prs.size()];
for (int i = 0; i < prs.size(); i++)
primes[i] = prs.get(i);
}
public static void revSort(int[] a) {
Arrays.sort(a);
reverse(a);
}
public static void revSort(long[] a) {
Arrays.sort(a);
reverse(a);
}
public static int[][] copy(int[][] ar) {
int[][] nr = new int[ar.length][ar[0].length];
for (int i = 0; i < ar.length; i++)
for (int j = 0; j < ar[0].length; j++)
nr[i][j] = ar[i][j];
return nr;
}
/**
* <h1>指定した値以上の先頭のインデクスを返す</h1>
* <p>配列要素が0のときは、0が返る。</p>
*
* @return<b>int</b> : 探索した値以上で、先頭になるインデクス
* 値が無ければ、挿入できる最小のインデックス
*/
public static <T extends Number> int lowerBound(final List<T> lis, final T value) {
int low = 0;
int high = lis.size();
int mid;
while (low < high) {
mid = ((high - low) >>> 1) + low; //(low + high) / 2 (オーバーフロー対策)
if (lis.get(mid).doubleValue() < value.doubleValue()) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
/**
* <h1>指定した値より大きい先頭のインデクスを返す</h1>
* <p>配列要素が0のときは、0が返る。</p>
*
* @return<b>int</b> : 探索した値より上で、先頭になるインデクス
* 値が無ければ、挿入できる最小のインデックス
*/
public static <T extends Number> int upperBound(final List<T> lis, final T value) {
int low = 0;
int high = lis.size();
int mid;
while (low < high) {
mid = ((high - low) >>> 1) + low; //(low + high) / 2 (オーバーフロー対策)
if (lis.get(mid).doubleValue() < value.doubleValue()) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
/**
* <h1>指定した値以上の先頭のインデクスを返す</h1>
* <p>配列要素が0のときは、0が返る。</p>
*
* @return<b>int</b> : 探索した値以上で、先頭になるインデクス
* 値が無ければ、挿入できる最小のインデックス
*/
public static int lowerBound(final int[] arr, final int value) {
int low = 0;
int high = arr.length;
int mid;
while (low < high) {
mid = ((high - low) >>> 1) + low; //(low + high) / 2 (オーバーフロー対策)
if (arr[mid] < value) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
/**
* <h1>指定した値より大きい先頭のインデクスを返す</h1>
* <p>配列要素が0のときは、0が返る。</p>
*
* @return<b>int</b> : 探索した値より上で、先頭になるインデクス
* 値が無ければ、挿入できる最小のインデックス
*/
public static int upperBound(final int[] arr, final int value) {
int low = 0;
int high = arr.length;
int mid;
while (low < high) {
mid = ((high - low) >>> 1) + low; //(low + high) / 2 (オーバーフロー対策)
if (arr[mid] <= value) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
/**
* <h1>指定した値以上の先頭のインデクスを返す</h1>
* <p>配列要素が0のときは、0が返る。</p>
*
* @return<b>int</b> : 探索した値以上で、先頭になるインデクス
* 値がなければ挿入できる最小のインデックス
*/
public static long lowerBound(final long[] arr, final long value) {
int low = 0;
int high = arr.length;
int mid;
while (low < high) {
mid = ((high - low) >>> 1) + low; //(low + high) / 2 (オーバーフロー対策)
if (arr[mid] < value) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
/**
* <h1>指定した値より大きい先頭のインデクスを返す</h1>
* <p>配列要素が0のときは、0が返る。</p>
*
* @return<b>int</b> : 探索した値より上で、先頭になるインデクス
* 値がなければ挿入できる最小のインデックス
*/
public static long upperBound(final long[] arr, final long value) {
int low = 0;
int high = arr.length;
int mid;
while (low < high) {
mid = ((high - low) >>> 1) + low; //(low + high) / 2 (オーバーフロー対策)
if (arr[mid] <= value) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
//次の順列に書き換える、最大値ならfalseを返す
public static boolean nextPermutation(int A[]) {
int len = A.length;
int pos = len - 2;
for (; pos >= 0; pos--) {
if (A[pos] < A[pos + 1]) break;
}
if (pos == -1) return false;
//posより大きい最小の数を二分探索
int ok = pos + 1;
int ng = len;
while (Math.abs(ng - ok) > 1) {
int mid = (ok + ng) / 2;
if (A[mid] > A[pos]) ok = mid;
else ng = mid;
}
swap(A, pos, ok);
reverse(A, pos + 1, len - 1);
return true;
}
//次の順列に書き換える、最小値ならfalseを返す
public static boolean prevPermutation(int A[]) {
int len = A.length;
int pos = len - 2;
for (; pos >= 0; pos--) {
if (A[pos] > A[pos + 1]) break;
}
if (pos == -1) return false;
//posより小さい最大の数を二分探索
int ok = pos + 1;
int ng = len;
while (Math.abs(ng - ok) > 1) {
int mid = (ok + ng) / 2;
if (A[mid] < A[pos]) ok = mid;
else ng = mid;
}
swap(A, pos, ok);
reverse(A, pos + 1, len - 1);
return true;
}
static long ncr2(int a, int b) {
if (b == 0) return 1;
else if (a < b) return 0;
long res = 1;
for (int i = 0; i < b; i++) {
res *= a - i;
res /= i + 1;
}
return res;
}
static long ncrdp(int n, int r) {
if (n < r) return 0;
long[][] dp = new long[n + 1][r + 1];
for (int ni = 0; ni < n + 1; ni++) {
dp[ni][0] = dp[ni][ni] = 1;
for (int ri = 1; ri < ni; ri++) {
dp[ni][ri] = dp[ni - 1][ri - 1] + dp[ni - 1][ri];
}
}
return dp[n][r];
}
public static int mod(int a, int m) {
return a >= 0 ? a % m : (int) (a + ceil(-a * 1.0 / m) * m) % m;
}
static long modNcr(int n, int r) {
if (n < 0 || r < 0 || n < r) return 0;
if (Fa == null || Fa.size() <= n) factorial(n);
long result = Fa.get(n);
result = modMul(result, modInv(Fa.get(n - r)));
result = modMul(result, modInv(Fa.get(r)));
return result;
}
public static long modSum(long... lar) {
long res = 0;
for (long l : lar)
res = (res + l % MOD) % MOD;
if (res < 0) res += MOD;
res %= MOD;
return res;
}
public static long modDiff(long a, long b) {
long res = a % MOD - b % MOD;
if (res < 0) res += MOD;
res %= MOD;
return res;
}
public static long modMul(long... lar) {
long res = 1;
for (long l : lar)
res = (res * l % MOD) % MOD;
if (res < 0) res += MOD;
res %= MOD;
return res;
}
public static long modDiv(long a, long b) {
long x = a % MOD;
long y = b % MOD;
long res = (x * modInv(y)) % MOD;
return res;
}
static long modInv(long n) {
return modPow(n, MOD - 2);
}
static void factorial(int n) {
if (Fa == null) {
Fa = new ArrayList<>();
Fa.add(1L);
Fa.add(1L);
}
for (int i = Fa.size(); i <= n; i++) {
Fa.add((Fa.get(i - 1) * i) % MOD);
}
}
static long modPow(long x, long n) {
long res = 1L;
while (n > 0) {
if ((n & 1) == 1) {
res = res * x % MOD;
}
x = x * x % MOD;
n >>= 1;
}
return res;
}
//↑nCrをmod計算するために必要
static long lcm(long n, long r) {
return n / gcd(n, r) * r;
}
static int gcd(int n, int r) {
return r == 0 ? n : gcd(r, n % r);
}
static long gcd(long n, long r) {
return r == 0 ? n : gcd(r, n % r);
}
static <T> void swap(T[] x, int i, int j) {
T t = x[i];
x[i] = x[j];
x[j] = t;
}
static void swap(int[] x, int i, int j) {
int t = x[i];
x[i] = x[j];
x[j] = t;
}
public static void reverse(int[] x) {
int l = 0;
int r = x.length - 1;
while (l < r) {
int temp = x[l];
x[l] = x[r];
x[r] = temp;
l++;
r--;
}
}
public static void reverse(long[] x) {
int l = 0;
int r = x.length - 1;
while (l < r) {
long temp = x[l];
x[l] = x[r];
x[r] = temp;
l++;
r--;
}
}
public static void reverse(char[] x) {
int l = 0;
int r = x.length - 1;
while (l < r) {
char temp = x[l];
x[l] = x[r];
x[r] = temp;
l++;
r--;
}
}
public static void reverse(int[] x, int s, int e) {
int l = s;
int r = e;
while (l < r) {
int temp = x[l];
x[l] = x[r];
x[r] = temp;
l++;
r--;
}
}
static int length(int a) {
int cou = 0;
while (a != 0) {
a /= 10;
cou++;
}
return cou;
}
static int length(long a) {
int cou = 0;
while (a != 0) {
a /= 10;
cou++;
}
return cou;
}
static int cou(boolean[] a) {
int res = 0;
for (boolean b : a) {
if (b) res++;
}
return res;
}
static int cou(String s, char c) {
int res = 0;
for (char ci : s.toCharArray()) {
if (ci == c) res++;
}
return res;
}
static int countC2(char[][] a, char c) {
int co = 0;
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[0].length; j++)
if (a[i][j] == c) co++;
return co;
}
static int countI(int[] a, int key) {
int co = 0;
for (int i = 0; i < a.length; i++)
if (a[i] == key) co++;
return co;
}
static int countI(int[][] a, int key) {
int co = 0;
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[0].length; j++)
if (a[i][j] == key) co++;
return co;
}
static void fill(int[][] a, int v) {
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[0].length; j++)
a[i][j] = v;
}
static void fill(char[][] a, char c) {
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[0].length; j++)
a[i][j] = c;
}
static void fill(long[][] a, long v) {
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[0].length; j++)
a[i][j] = v;
}
static void fill(int[][][] a, int v) {
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[0].length; j++)
for (int k = 0; k < a[0][0].length; k++)
a[i][j][k] = v;
}
static int max(int... a) {
int res = Integer.MIN_VALUE;
for (int i : a) {
res = Math.max(res, i);
}
return res;
}
static long max(long... a) {
long res = Integer.MIN_VALUE;
for (long i : a) {
res = Math.max(res, i);
}
return res;
}
static long min(long... a) {
long res = Long.MAX_VALUE;
for (long i : a) {
res = Math.min(res, i);
}
return res;
}
static int max(int[][] ar) {
int res = Integer.MIN_VALUE;
for (int i[] : ar)
res = Math.max(res, max(i));
return res;
}
static long max(long[][] ar) {
long res = Integer.MIN_VALUE;
for (long i[] : ar)
res = Math.max(res, max(i));
return res;
}
static int min(int... a) {
int res = Integer.MAX_VALUE;
for (int i : a) {
res = Math.min(res, i);
}
return res;
}
static int min(int[][] ar) {
int res = Integer.MAX_VALUE;
for (int i[] : ar)
res = Math.min(res, min(i));
return res;
}
static int sum(int[] a) {
int cou = 0;
for (int i : a)
cou += i;
return cou;
}
static long sum(long[] a) {
long cou = 0;
for (long i : a)
cou += i;
return cou;
}
//FastScanner
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer tokenizer = null;
public static String next() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
/*public String nextChar(){
return (char)next()[0];
}*/
public static String nextLine() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
return reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken("\n");
}
public static long nl() {
return Long.parseLong(next());
}
public static String n() {
return next();
}
public static int ni() {
return Integer.parseInt(next());
}
public static double nd() {
return Double.parseDouble(next());
}
public static int[] nia(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = ni();
}
return a;
}
//1-index
public static int[] niao(int n) {
int[] a = new int[n + 1];
for (int i = 1; i < n + 1; i++) {
a[i] = ni();
}
return a;
}
public static int[] niad(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = ni() - 1;
}
return a;
}
public static P[] npa(int n) {
P[] p = new P[n];
for (int i = 0; i < n; i++) {
p[i] = new P(ni(), ni());
}
return p;
}
public static P[] npad(int n) {
P[] p = new P[n];
for (int i = 0; i < n; i++) {
p[i] = new P(ni() - 1, ni() - 1);
}
return p;
}
public static int[][] nit(int h, int w) {
int[][] a = new int[h][w];
for (int hi = 0; hi < h; hi++) {
for (int wi = 0; wi < w; wi++) {
a[hi][wi] = ni();
}
}
return a;
}
public static int[][] nitd(int h, int w) {
int[][] a = new int[h][w];
for (int hi = 0; hi < h; hi++) {
for (int wi = 0; wi < w; wi++) {
a[hi][wi] = ni() - 1;
}
}
return a;
}
static int[][] S_ARRAY;
static long[][] S_LARRAY;
static int S_INDEX;
static int S_LINDEX;
//複数の配列を受け取る
public static int[] niah(int n, int w) {
if (S_ARRAY == null) {
S_ARRAY = new int[w][n];
for (int i = 0; i < n; i++) {
for (int ty = 0; ty < w; ty++) {
S_ARRAY[ty][i] = ni();
}
}
}
return S_ARRAY[S_INDEX++];
}
public static long[] nlah(int n, int w) {
if (S_LARRAY == null) {
S_LARRAY = new long[w][n];
for (int i = 0; i < n; i++) {
for (int ty = 0; ty < w; ty++) {
S_LARRAY[ty][i] = ni();
}
}
}
return S_LARRAY[S_LINDEX++];
}
public static char[] nca() {
char[] a = next().toCharArray();
return a;
}
public static char[][] nct(int h, int w) {
char[][] a = new char[h][w];
for (int i = 0; i < h; i++) {
a[i] = next().toCharArray();
}
return a;
}
//スペースが入っている場合
public static char[][] ncts(int h, int w) {
char[][] a = new char[h][w];
for (int i = 0; i < h; i++) {
a[i] = nextLine().replace(" ", "").toCharArray();
}
return a;
}
public static char[][] nctp(int h, int w, char c) {
char[][] a = new char[h + 2][w + 2];
//char c = '*';
int i;
for (i = 0; i < w + 2; i++)
a[0][i] = c;
for (i = 1; i < h + 1; i++) {
a[i] = (c + next() + c).toCharArray();
}
for (i = 0; i < w + 2; i++)
a[h + 1][i] = c;
return a;
}
//スペースが入ってる時用
public static char[][] nctsp(int h, int w, char c) {
char[][] a = new char[h + 2][w + 2];
//char c = '*';
int i;
for (i = 0; i < w + 2; i++)
a[0][i] = c;
for (i = 1; i < h + 1; i++) {
a[i] = (c + nextLine().replace(" ", "") + c).toCharArray();
}
for (i = 0; i < w + 2; i++)
a[h + 1][i] = c;
return a;
}
public static long[] nla(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = nl();
}
return a;
}
public static long[][] nlt(int h, int w) {
long[][] a = new long[h][w];
for (int hi = 0; hi < h; hi++) {
for (int wi = 0; wi < w; wi++) {
a[hi][wi] = nl();
}
}
return a;
}
public static void main(String[] args) throws Exception {
long startTime = System.currentTimeMillis();
solve();
System.out.flush();
long endTime = System.currentTimeMillis();
if (DEBUG) System.err.println(endTime - startTime);
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import sys
input = sys.stdin.readline
def main():
n = int(input())
a_list = list(map(int, input().split()))
a_sum = a_list[0]
if a_list[0] > 0:
sign = "plus"
else:
sign = "minus"
ans = 0
for i in range(1, n):
if sign == "plus":
sign = "minus"
if a_sum + a_list[i] == 0:
ans += 1
a_sum = -1
elif a_sum + a_list[i] > 0:
ans += a_sum + 1 + a_list[i]
a_sum = -1
else:
a_sum += a_list[i]
elif sign == "minus":
sign = "plus"
if a_sum + a_list[i] == 0:
ans += 1
a_sum = 1
elif a_sum + a_list[i] < 0:
ans += -1 * a_sum + 1 + -1 * a_list[i]
a_sum = 1
else:
a_sum += a_list[i]
print(ans)
if __name__ == '__main__':
main()
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.