Search is not available for this dataset
name
stringlengths 2
88
| description
stringlengths 31
8.62k
| public_tests
dict | private_tests
dict | solution_type
stringclasses 2
values | programming_language
stringclasses 5
values | solution
stringlengths 1
983k
|
---|---|---|---|---|---|---|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
l = len(a)
b = []
for i in range(l):
b.append(a[i])
ans = 0
Ans = 0
summary = a[0]
if(summary == 0):
a[0] = 1
ans+= 1
b[0] = -1
Ans+= 1
else:
b[0] = int(-a[0]/ abs(a[0]))
Ans+= abs(a[0]- b[0])
Summary = b[0]
for i in range(1, l):
if(summary* (summary+ a[i])>= 0):
if(summary > 0):
ans+= a[i]+ summary+ 1
a[i] = -summary- 1
summary= -1
else:
ans+= -summary+ 1- a[i]
a[i] = -summary+ 1
summary= 1
else:
summary+= a[i]
for i in range(1, l):
if(Summary* (Summary+ b[i])>= 0):
if(Summary > 0):
Ans+= b[i]+ Summary+ 1
b[i] = -Summary- 1
Summary= -1
else:
Ans+= -Summary+ 1- b[i]
b[i] = -Summary+ 1
Summary= 1
else:
Summary+= b[i]
print(min(ans, Ans))
print(a, b) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import copy
n = int(input())
a = [int(ai) for ai in input().split()]
def search(a, flip=False):
if not flip:
count = 0
else:
count = abs(a[0]) + 1
a[0] = 1 if a[0] < 0 else -1
a_sum = a[0]
for ai in a[1:]:
tmp_sum = a_sum + ai
if tmp_sum < 0 and a_sum < 0:
c = abs(tmp_sum) + 1
elif tmp_sum > 0 and a_sum > 0:
c = -abs(tmp_sum) - 1
elif tmp_sum == 0 and a_sum < 0:
c = 1
elif tmp_sum == 0 and a_sum > 0:
c = -1
else:
c = 0
count += abs(c)
a_sum = tmp_sum + c
return count
print(min(search(a, False), search(a, True))) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
vector<int> b(n);
vector<int> c(n);
for (int i = 0; i < n; i++) {
cin >> a.at(i);
}
int ans_1 = 0;
int ans_2 = 0;
b.at(0) = a.at(0);
if (b.at(0) <= 0) {
ans_1 += 1 - b.at(0);
b.at(0) = 1;
}
for (int i = 1; i < n; i++) {
b.at(i) = b.at(i - 1) + a.at(i);
if ((i % 2) == 1) {
if (b.at(i) >= 0) {
ans_1 += 1 + b.at(i);
b.at(i) = -1;
}
} else {
if (b.at(i) <= 0) {
ans_1 += 1 - b.at(i);
b.at(i) = 1;
}
}
}
b.at(0) = a.at(0);
if (b.at(0) >= 0) {
ans_2 += 1 + b.at(0);
b.at(0) = -1;
}
for (int i = 1; i < n; i++) {
b.at(i) = b.at(i - 1) + a.at(i);
if ((i % 2) == 1) {
if (b.at(i) <= 0) {
ans_2 += 1 - b.at(i);
b.at(i) = 1;
}
} else {
if (b.at(i) >= 0) {
ans_2 += 1 + b.at(i);
b.at(i) = -1;
}
}
}
cout << min(ans_1, ans_2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n=int(input())
a=[int(i) for i in input().split()]
check=a[0]
ans=0
if check==0:
for i in range(1,n):
if check+a[i]==0:
continue
elif check+a[i]>0:
check=-1
ans=1
break
else:
check=1
ans=1
break
for i in range(1,n):
check2=check+a[i]
if check<0:
if check2>0:
check=check2
else:
ans+=abs(check2)+1
check=1
else:
if check2<0:
check=check2
else:
ans+=abs(check2)+1
check=-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;
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
int64_t n;
cin >> n;
vector<int64_t> a(n);
for (int64_t i = 0; i < n; i++) cin >> a[i];
int64_t sum1 = 0, cost1 = 0;
for (int64_t i = 0; i < n; i++) {
sum1 += a[i];
if (i % 2 == 0 && sum1 < 0) sum1 += abs(sum1) + 1, cost1 += abs(sum1) + 1;
if (i % 2 == 1 && sum1 > 0) sum1 -= abs(sum1) - 1, cost1 += abs(sum1) + 1;
}
int64_t sum2 = 0, cost2 = 0;
for (int64_t i = 0; i < n; i++) {
sum2 += a[i];
if (i % 2 == 0 && sum1 > 0) sum2 -= abs(sum2) - 1, cost2 += abs(sum2) + 1;
if (i % 2 == 1 && sum1 < 0) sum2 += abs(sum2) + 1, cost1 += abs(sum2) + 1;
}
if (sum1 == 0) cost1++;
if (sum2 == 0) cost2++;
cout << min(cost1, cost2) << 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 f(int a[], int N, bool positive) {
int cnt = 0;
int sum = a[0];
for (int i = 1; i < N; i++) {
int n = a[i];
if (positive) {
if (i % 2 == 0) {
while (sum + n <= 0) n++, cnt++;
} else {
while (0 <= sum + n) n--, cnt++;
}
} else {
if (i % 2 == 0) {
while (0 <= sum + n) n--, cnt++;
} else {
while (sum + n <= 0) n++, cnt++;
}
}
sum += n;
}
return cnt;
}
int main() {
int N;
cin >> N;
int a[N];
for (int i = 0; i < N; i++) cin >> a[i];
int cnt = min(f(a, N, 1), f(a, N, 0));
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 dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int n;
cin >> n;
int a[100000];
for (int i = 0; i < (n); i++) {
cin >> a[i];
}
long long int sm = 0;
for (int i = 1; i < n; i++) {
long long int b = 0;
for (int j = 0; j < (i); j++) b += a[j];
cout << (b) << "\n";
if (b * a[i] < 0 && abs(a[i]) > abs(b)) continue;
long long int t = (b > 0) ? -b - 1 : -b + 1;
sm += abs(t - a[i]);
a[i] = t;
}
cout << (sm) << "\n";
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
a = [int(i) for i in input().split()]
sam = a[0]
num = 0
if sam == 0 and a[1] > 0:
sam -= 1
num += 1
elif sam == 0:
sam += 1
num += 1
old = sam
for i in range(1, len(a)):
sam += a[i]
if sam >= 0 and old > 0:
num += (abs(sam) + 1)
sam -= (sam + 1)
elif sam <= 0 and old < 0:
num += (abs(sam) + 1)
sam -= (sam - 1)
old = sam
print(num)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | package main
import (
"bufio"
"fmt"
"os"
"strconv"
)
func solve(a []int64, ra int) int64 {
s := int64(0) // S_i = \sum_i a[i]
ans := int64(0)
for i, e := range a {
if i%2 == ra {
if s+e >= 0 {
ans += abs(-1 - s - e)
s = -1
continue
}
} else {
if s+e <= 0 {
ans += abs(1 - s - e)
s = 1
continue
}
}
s += e
}
return ans
}
func abs(x int64) int64 {
if x < 0 {
return -x
}
return x
}
func min(a, b int64) int64 {
if a < b {
return a
}
return b
}
func main() {
var n int
fmt.Scan(&n)
sc := bufio.NewScanner(os.Stdin)
sc.Split(bufio.ScanWords)
a := make([]int64, n)
for i := 0; i < n; i++ {
sc.Scan()
x, _ := strconv.Atoi(sc.Text())
a[i] = int64(x)
}
ans := int64(0)
if a[0] == 0 {
a[0] = -1
ans = solve(a, 0)
a[0] = 1
ans = min(ans, solve(a, 1))
} else if a[0] < 0 {
ans = solve(a, 0)
} else { // a[0] > 0
ans = solve(a, 1)
}
fmt.Println(ans)
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | # -*- coding:utf-8 -*-
def solve():
N = int(input())
A = list(map(int, input().split()))
"""考え方
ruiseki[i] := i番目までの累積和
・ruiseki[i]のiが偶数番目を+にするパターン
・ruiseki[i]のiが偶数番目を-にするパターン
の2通りを求め、小さい方を答えにする
"""
# 偶数番目を+にするパターン
ans1 = 0
ruiseki = [0] * N
ruiseki[0] = A[0]
if ruiseki[0] == 0:
ruiseki[0] = 1
ans1 += 1
for i in range(1, N):
ruiseki[i] = ruiseki[i-1] + A[i]
if i%2 != 0:
# iが奇数
if ruiseki[i] >= 0:
diff = abs(ruiseki[i] - (-1))
ans1 += diff
ruiseki[i] = -1
else:
# iが偶数
if ruiseki[i] <= 0:
diff = abs(ruiseki[i] - 1)
ans1 += diff
ruiseki[i] = 1
# 偶数番目を-にするパターン
ans2 = 0
ruiseki = [0] * N
ruiseki[0] = A[0]
if ruiseki[0] == 0:
ruiseki[0] = -1
ans2 += 1
for i in range(1, N):
ruiseki[i] = ruiseki[i-1] + A[i]
if i%2 != 0:
# iが奇数
if ruiseki[i] <= 0:
diff = abs(ruiseki[i] - 1)
ans2 += diff
ruiseki[i] = 1
else:
# iが偶数
if ruiseki[i] >= 0:
diff = abs(ruiseki[i] - (-1))
ans2 += diff
ruiseki[i] = -1
print(min(ans1, ans2))
def solve2():
N = int(input())
A = list(map(int, input().split()))
ans = 0
"""
(1) A = [0, 0, 0, 0, 0, 100, ...] みたいなときは
A = [-1, 2, -2, 2, -2, 100, ...] にしたい
(2) A = [0, 0, 0, 0, 100, ...] みたいなときは
A = [1, -2, 2, -2, 100, ...] にしたい
"""
if A[0] == 0:
hugou = 1 # 最後の符号(1:+, -1:-)
for i in range(1, N):
if A[i] == 0:
continue
if A[i] > 0:
hugou = 1
break
else:
hugou = -1
break
for j in range(i-1, 0, -1):
if hugou == -1:
A[j] = 2
else:
A[j] = -2
hugou *= -1
ans += 2
if hugou > 0:
A[0] = -1
else:
A[0] = 1
ans += 1
# ruiseki[i] := i番目までの累積和
ruiseki = [0] * N
ruiseki[0] = A[0]
for i in range(1, N):
i_sum = A[i] + ruiseki[i-1]
if ruiseki[i-1] > 0:
# ruiseki[i]をマイナス値にする必要がある
if i_sum < 0:
ruiseki[i] = i_sum
else:
diff = abs(i_sum - (-1))
ans += diff
A[i] -= diff
i_sum -= diff
ruiseki[i] = i_sum
elif ruiseki[i-1] < 0:
# ruiseki[i]をプラス値にする必要がある
if i_sum > 0:
ruiseki[i] = i_sum
else:
diff = abs(i_sum - 1)
ans += diff
A[i] += diff
i_sum += diff
ruiseki[i] = i_sum
print(ans)
if __name__ == "__main__":
solve()
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | from functools import reduce
N =int(input())
A = map(int, input().split())
def check(A):
arr = [0]*(N+1)
for i in range(len(A)):
arr[i+1] += arr[i-1] + ai
if arr[i+1]*arr[i] <=0:
return 0
val = abs(min(arr))
return val
print(val) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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 min(int x, int y) { return x < y ? x : y; }
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < (n); i++) cin >> a[i];
int sum = 0;
int cost = 0;
for (int i = 0; i < n; ++i) {
sum += a[i];
if (i % 2 == 0) {
if (sum <= 0) {
cost += -sum + 1;
sum = 1;
}
} else {
if (sum >= 0) {
cost += sum + 1;
sum = -1;
}
}
}
long long sum2 = 0;
long long cost2 = 0;
for (int i = 0; i < n; ++i) {
sum2 += a[i];
if (i % 2 == 0) {
if (sum2 >= 0) {
cost2 += sum2 + 1;
sum2 = -1;
}
} else {
if (sum2 <= 0) {
cost2 += -sum2 + 1;
sum2 = 1;
}
}
}
long long ans = min(cost, cost2);
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>
unsigned solve(unsigned N, const std::vector<long> &a, int flag) {
unsigned c = 0;
long s = 0;
for (unsigned n = 0; n < N; ++n) {
if (n % 2 == flag) {
if (s + a[n] < 0) {
c += std::abs(1 - (s + a[n]));
s = 1;
} else {
s += a[n];
}
} else {
if (s + a[n] > 0) {
c += std::abs((-1) - (s + a[n]));
s = -1;
} else {
s += a[n];
}
}
}
return c;
}
int main() {
unsigned N;
std::cin >> N;
std::vector<long> a(N);
for (unsigned n = 0; n < N; ++n) {
std::cin >> a[n];
}
std::cout << std::min(solve(N, a, 0), solve(N, a, 1)) << std::endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n;
int data[10000000];
int main() {
int cnt = 0;
bool flag = true;
int work;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> data[i];
}
work = data[0];
if (work < 0) flag = false;
for (int i = 1; i < n; i++) {
work += data[i];
if (flag && work >= 0) {
while (work >= 0) {
cnt++;
work--;
}
flag = false;
} else if (!(flag) && work <= 0) {
while (work <= 0) {
cnt++;
work++;
}
flag = true;
} else {
if (work > 0)
flag = true;
else
flag = false;
}
}
cout << cnt << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #![allow(non_snake_case)]
#![allow(dead_code)]
#![allow(unused_macros)]
#![allow(unused_imports)]
use std::str::FromStr;
use std::io::*;
use std::collections::*;
use std::cmp::*;
struct Scanner<I: Iterator<Item = char>> {
iter: std::iter::Peekable<I>,
}
macro_rules! exit {
() => {{
exit!(0)
}};
($code:expr) => {{
if cfg!(local) {
writeln!(std::io::stderr(), "===== Terminated =====")
.expect("failed printing to stderr");
}
std::process::exit($code);
}}
}
impl<I: Iterator<Item = char>> Scanner<I> {
pub fn new(iter: I) -> Scanner<I> {
Scanner {
iter: iter.peekable(),
}
}
pub fn safe_get_token(&mut self) -> Option<String> {
let token = self.iter
.by_ref()
.skip_while(|c| c.is_whitespace())
.take_while(|c| !c.is_whitespace())
.collect::<String>();
if token.is_empty() {
None
} else {
Some(token)
}
}
pub fn token(&mut self) -> String {
self.safe_get_token().unwrap_or_else(|| exit!())
}
pub fn get<T: FromStr>(&mut self) -> T {
self.token().parse::<T>().unwrap_or_else(|_| exit!())
}
pub fn vec<T: FromStr>(&mut self, len: usize) -> Vec<T> {
(0..len).map(|_| self.get()).collect()
}
pub fn mat<T: FromStr>(&mut self, row: usize, col: usize) -> Vec<Vec<T>> {
(0..row).map(|_| self.vec(col)).collect()
}
pub fn char(&mut self) -> char {
self.iter.next().unwrap_or_else(|| exit!())
}
pub fn chars(&mut self) -> Vec<char> {
self.get::<String>().chars().collect()
}
pub fn mat_chars(&mut self, row: usize) -> Vec<Vec<char>> {
(0..row).map(|_| self.chars()).collect()
}
pub fn line(&mut self) -> String {
if self.peek().is_some() {
self.iter
.by_ref()
.take_while(|&c| !(c == '\n' || c == '\r'))
.collect::<String>()
} else {
exit!();
}
}
pub fn peek(&mut self) -> Option<&char> {
self.iter.peek()
}
}
fn main() {
let cin = stdin();
let cin = cin.lock();
let mut sc = Scanner::new(cin.bytes().map(|c| c.unwrap() as char));
let n: usize = sc.get();
let a: Vec<i64> = sc.vec(n);
let mut p = 0;
let mut ans1 = 0;
for i in 0..n {
let mut s = p + a[i];
if i == 0 && s > 0 {
ans1 += s.abs()+1;
s += -s - 1;
} else if s == 0 {
s += if p > 0 { 1 } else { -1 };
ans1 += 1;
} else if s * p > 0 {
ans1 += s.abs()+1;
s += if s > 0 { -s - 1 } else { s + 1 };
}
p = s;
}
let mut ans2 = 0;
for i in 0..n {
let mut s = p + a[i];
if i == 0 && s < 0 {
ans1 += s.abs()+1;
s += s + 1;
} else if s == 0 {
s += if p > 0 { 1 } else { -1 };
ans2 += 1;
} else if s * p > 0 {
ans2 += s.abs()+1;
s += if s > 0 { -s - 1 } else { s + 1 };
}
p = s;
}
println!("{}", min(ans1, ans2));
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int sign(int x) { return x < 0 ? -1 : 1; }
int main() {
int n;
cin >> n;
vector<int> A(n);
for (int i = 0; i < n; i++) {
cin >> A[i];
}
int cnt = 0;
int sum = A[0];
if (sum <= 0) {
cnt += abs(sum) + 1;
sum = 1;
}
for (int i = 1; i < n; i++) {
int a = A[i];
int newsum = sum + a;
if (sign(sum) == sign(newsum) || newsum == 0) {
cnt += abs(newsum) + 1;
newsum = -sign(sum);
}
sum = newsum;
}
int cnt2 = 0;
int sum2 = A[0];
if (sum2 >= 0) {
cnt2 += abs(sum2) + 1;
sum2 = -1;
}
for (int i = 1; i < n; i++) {
int a = A[i];
int newsum = sum2 + a;
if (sign(sum2) == sign(newsum) || newsum == 0) {
cnt2 += abs(newsum) + 1;
newsum = -sign(sum2);
}
sum2 = newsum;
}
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 | python3 | n = int(input())
a = [int(x) for x in input().split()]
def odd_positive(List, n):
sum_a = 0
cost = 0
for a in List:
nextsum = sum_a + a
if i & 1 and nextsum <= 0:
cost = - nextsum + 1
sum_a = 1
elif (not i & 1) and nextsum >= 0:
cost = nextsum + 1
sum_a = -1
return cost
def odd_negative(List, n):
sum_a = 0
cost = 0
for a in List:
nextsum = sum_a + a
if i & 1 and nextsum >= 0:
cost = nextsum + 1
sum_a = -1
elif (not i & 1) and nextsum <= 0:
cost = - nextsum + 1
sum_a = 1
return cost
ans = min(odd_positive(a,n), odd_negative(a,n))
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #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];
if (sum[0] == 0 && a[1] > 0) {
sum[0]--;
ans++;
} else if (sum[0] == 0 && a[1] <= 0) {
sum[0]++;
ans++;
}
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() {
int n;
vector<long long> S;
vector<long long> A;
int j;
bool is_plus;
int ans = 0;
long long sum = 0;
cin >> n;
S.push_back(0);
for (int i = 0; i < n; i++) {
long long a;
cin >> a;
A.push_back(a);
}
for (j = 0; j < n; j++) {
if (abs(A[j])) {
break;
}
}
if (j == n) {
cout << A.size() * 2 - 1 << endl;
return 0;
}
if (j) {
ans += (j + 1) * 2 - 1;
sum = (A[j] > 0) ? -1 : 1;
} else {
sum = 0;
ans = 0;
}
for (int i = j; i < n; i++) {
if (!i) {
sum = A[i];
continue;
}
bool is_plus = sum > 0;
sum += A[i];
if (sum == 0) {
ans += 1;
sum = is_plus ? -1 : 1;
} else if (is_plus == (sum > 0)) {
ans += abs(sum) + 1;
sum = is_plus ? -1 : 1;
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
int i;
for (i = 0; i < n; i++) {
cin >> a[i];
}
int sum = 0;
int cnt = 0;
if (a[0] > 0) {
for (i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 0) {
while (sum <= 0) {
sum++;
cnt++;
}
} else {
while (sum >= 0) {
sum--;
cnt++;
}
}
}
} else {
for (i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 0) {
while (sum >= 0) {
sum--;
cnt++;
}
} else {
while (sum <= 0) {
sum++;
cnt++;
}
}
}
}
cout << cnt << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
A = list(map(int, input().split()))
ans = 0
previous_sum = A[0] # total to i - 1
for i in range(1, N):
# if prev_sum is plus and A[i] is more minus than prev_sum, no action.
# if prev_sum is plus and minus of A[i] is larger than or equal to prev_sum.
if previous_sum > 0 and previous_sum + A[i] >= 0:
# the number that need to be subtracted to meet the requirements.
require_subtraction = previous_sum + A[i] + 1
# correcting the current number.
A[i] -= require_subtraction
ans += require_subtraction
# if prev_sum is minus and A[i] is more plus than prev_sum, no action.
# if prev_sum is minus and minus of A[i] is more smaller than or equal to prev_sum.
elif previous_sum < 0 and previous_sum + A[i] <= 0:
# the number that need to be added to meet the requirements.
require_addition = -(previous_sum + A[i] - 1)
# correcting the current number.
A[i] += require_addition
ans += require_addition
previous_sum += A[i]
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | use std::io::*;
use std::str::FromStr;
use std::collections::HashMap;
use std::collections::BTreeMap;
use std::collections::BTreeSet;
use std::collections::HashSet;
use std::cmp;
use std::f64::consts;
use std::cmp::Ordering;
use std::collections::VecDeque; //push_back, pop_front
fn main() {
let n: i64 = read();
let mut vec: Vec<i64> = (0..n).map(|_| read()).collect();
let mut count1 = 0;
// 偶数indexが+の場合
let mut sum = 0;
for (i, &e) in vec.iter().enumerate() {
sum += e;
if i % 2 == 0 {
if sum < 0 {
count1 += (1-sum).abs();
sum += (1-sum).abs();
}
} else {
if sum > 0 {
count1 += (-1-sum).abs();
sum -= (-1-sum).abs();
}
}
}
if sum == 0 {
count1+=1;
}
let mut count2 = 0;
// 奇数indexが+の場合
let mut sum = 0;
for (i, &e) in vec.iter().enumerate() {
sum += e;
if i % 2 == 0 {
if sum > 0 {
count2 += (-1-sum).abs();
sum -= (-1-sum).abs();
}
} else {
if sum < 0 {
count2 += (1-sum).abs();
sum += (1-sum).abs();
}
}
}
if sum == 0 {
count2+=1;
}
println!("{}", cmp::min(count1, count2));
}
fn calc_distance(xa: f64, ya: f64, xb: f64, yb: f64)->f64 {
(f64::powf(xb-xa, 2.0) + f64::powf(yb-ya, 2.0)).sqrt()
}
fn calc_triangle_area(x1: f64, y1: f64, x2: f64, y2: f64, x3: f64, y3: f64) -> f64 {
1.0/2.0 * ((x1-x3)*(y2-y3)-(x2-x3)*(y1-y3)).abs()
}
fn read<T: FromStr>() -> T {
let stdin = stdin();
let stdin = stdin.lock();
let token: String = stdin
.bytes()
.map(|c| c.expect("failed to read char") as char)
.skip_while(|c| c.is_whitespace())
.take_while(|c| !c.is_whitespace())
.collect();
token.parse().ok().expect("failed to parse token")
}
// 最大公約数
fn gcd(a: i64, b: i64) -> i64 {
match b {
0 => a,
_ => gcd(b, a % b)
}
}
// 最小公倍数
fn lcm(a: i64, b: i64) -> i64 {
let g = gcd(a ,b);
a / g * b
}
// 階乗-dp (overflow回避で1000000007の余りを使って計算)
fn kaijou_dp(n: i64) -> i64 {
let mut dp: Vec<i64> = vec![0; n as usize+1];
dp[0] = 1;
dp[1] = 1;
for i in 1..n {
dp[i as usize+1] = dp[i as usize] % 1000000007 * (i+1) % 1000000007;
}
return dp[n as usize];
}
// 階乗
fn kaijou(n: i64)->i64 {
if n == 1 {
return n;
}
return n * kaijou(n-1);
}
// 順列全列挙
pub trait LexicalPermutation {
/// Return `true` if the slice was permuted, `false` if it is already
/// at the last ordered permutation.
fn next_permutation(&mut self) -> bool;
}
impl<T> LexicalPermutation for [T] where T: Ord {
/// Original author in Rust: Thomas Backman <[email protected]>
/// let mut data = [1, 2, 3];
// let mut permutations = Vec::new();
//
// loop {
// permutations.push(data.to_vec());
// if !data.next_permutation() {
// break;
// }
// }
//
// println!("{:?}", permutations);
// // [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
fn next_permutation(&mut self) -> bool {
// These cases only have 1 permutation each, so we can't do anything.
if self.len() < 2 { return false; }
// Step 1: Identify the longest, rightmost weakly decreasing part of the vector
let mut i = self.len() - 1;
while i > 0 && self[i-1] >= self[i] {
i -= 1;
}
// If that is the entire vector, this is the last-ordered permutation.
if i == 0 {
return false;
}
// Step 2: Find the rightmost element larger than the pivot (i-1)
let mut j = self.len() - 1;
while j >= i && self[j] <= self[i-1] {
j -= 1;
}
// Step 3: Swap that element with the pivot
self.swap(j, i-1);
// Step 4: Reverse the (previously) weakly decreasing part
self[i..].reverse();
true
}
}
//iより小さい数字の数 = lower_bound(&a, i) as i64;
//iより大きい数字の数 = n - upper_bound(&c, i) as i64;
fn lower_bound<T: Ord>(vec: &Vec<T>, x: T) -> usize {
let (mut left, mut right): (i64, i64) = (-1, vec.len() as i64);
while (right - left) > 1 {
let mid = (right + left) / 2;
if x <= vec[mid as usize] {
right = mid;
} else {
left = mid;
}
}
return right as usize;
}
fn upper_bound<T: Ord>(vec: &Vec<T>, x: T) -> usize {
let (mut left, mut right): (i64, i64) = (-1, vec.len() as i64);
while (right - left) > 1 {
let mid = (right + left) / 2;
if x < vec[mid as usize] {
right = mid;
} else {
left = mid;
}
}
return right as usize;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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, ans, i = 0, 0, 0
while a[i] == 0 and i < n:
ans += 2
i += 1
ans = max(0, ans - 1)
if i == n:
print(ans)
exit()
if ans > 0:
if abs(a[i]) == 1:
ans += 1
s = a[i]
else:
s = abs(a[i]) // a[i] * (abs(a[i]) - 1)
else:
s = a[0]
##print('{} {} {}'.format(a[i], ans, s))
i += 1
for j in range(i, n):
if abs(a[j]) > abs(s) and (a[j] == abs(a[j])) != (s == abs(s)):
s += a[j]
else:
pre_s = s
s = -1 * s // abs(s)
ans += abs(a[j] - s + pre_s)
## print('{} {} {}'.format(a[j], ans, 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 | java | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
long[] A = new long[N];
for (int i = 0; i < N; i++) {
A[i] = sc.nextInt();
}
System.out.println( solve(N, A) );
}
private static long solve(int N, long[] A) {
long a0 = A[0];
if( a0 > 0 ) {
long p = solve1(N, A, a0, 0);
long m = solve1(N, A, -1, (int)a0 + 1);
return Math.min(p, m);
} else if( a0 < 0 ) {
long p = solve1(N, A, 1, (int)a0 + 1);
long m = solve1(N, A, a0, 0);
return Math.min(p, m);
} else {
long p = solve1(N, A, 1, 1);
long m = solve1(N, A, -1, 1);
return Math.min(p, m);
}
}
private static long solve1(int N, long[] A, long sum, long ans) {
for (int i = 1; i < N; i++) {
long a = A[i];
if( sum > 0 ) {
// 次はminusになるのを期待
if( a + sum >= 0 ) {
// sumが-1になるような値にまで変更する
// a + sum が 5 の場合、6 だけ操作すると -1 にできる
long diff = a + sum + 1;
ans += diff;
sum = -1;
} else {
sum += a;
}
} else {
if( a + sum <= 0 ) {
long diff = (a + sum) * -1 + 1;
ans += diff;
sum = 1;
} else {
sum += a;
}
}
}
return ans;
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int( input())
A = list( map( int, input().split()))
ans = 10**15
for i in [1, -1]:
ansi, sums = 0, 0
for a in A:
sums += A
if sums*i <= 0:
ansi += abs(sums-s)
sums = s
s *= -1
ans = min( ans, ansi)
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;
using ll = long long;
using ull = unsigned long long;
int main(int argc, char const *argv[]) {
int n;
std::cin >> n;
std::vector<int> v(n);
std::vector<int> sums(2, 0);
for (size_t i = 0; i < n; i++) {
std::cin >> v[i];
sums[i % 2] += v[i];
}
ull ans = 0;
if (sums[0] > sums[1] && v[0] <= 0) {
ans = ans + abs(v[0]) + 1;
} else if (sums[0] < sums[1] && v[0] >= 0) {
ans = ans + abs(v[0]) + 1;
}
ll now, pre;
now = pre = v[0];
for (size_t i = 1; i < n; i++) {
now += v[i];
if (pre * now >= 0) {
if (pre > 0) {
ans = ans + abs(now) + 1;
now = -1;
} else if (pre < 0) {
ans = ans + abs(now) + 1;
now = 1;
}
}
pre = now;
}
std::cout << ans << '\n';
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | n = gets.to_i
arr = gets.chomp.split(" ").map(&:to_i)
$count = [0,0]
def check(i,arr,t)
if i > arr.size - 1
arr[t] += 1
$count += 1
return
end
if arr[i] > 0
arr[t] -= 1
$count += 1
elsif arr[i] < 0
arr[t] += 1
$count += 1
else
check(i+1,arr,t)
end
end
flg = true
2.times do |j|
tmp_arr = Marshal.load(Marshal.dump(arr))
sum = tmp_arr[0] + tmp_arr[1]
if sum == 0
if flg
tmp_arr[1] -= 1
else
tmp_arr[1] += 1
end
$count[j] += 1
end
sum = tmp_arr[0] + tmp_arr[1]
(2...tmp_arr.size).each do |i|
diff = sum + tmp_arr[i]
# puts %(sum : #{sum})
# puts %(diff : #{diff})
if sum > 0
if diff > 0
tmp_arr[i] -= diff.abs+1
$count[j] += diff.abs+1
elsif diff == 0
tmp_arr[i] -= 1
$count[j] += 1
end
else
if diff < 0
tmp_arr[i] += diff.abs+1
$count[j] += diff.abs+1
elsif diff == 0
tmp_arr[i] += 1
$count[j] += 1
end
end
sum += tmp_arr[i]
# p tmp_arr
end
flg = false
end
#p $count
#p arr
puts $count.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;
template <class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
long long n;
cin >> n;
long long a[n];
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
long long cnt = a[0];
long long ans = 0ll;
long long p = -1ll;
if (a[0] <= 0ll) p = 1ll;
for (int i = 1; i < n; ++i) {
cnt += a[i];
long long g = 0;
if (cnt * p <= 0ll) {
g = cnt * -1ll + p;
ans += (g * p);
cnt = cnt + g;
}
g = 0;
p *= -1ll;
}
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 | java | import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.next());
int[] a1 = new int[n];
int[] a2 = new int[n];
for (int i = 0; i < n; i++) {
int temp = Integer.parseInt(sc.next());
a1[i] = temp;
a2[i] = temp;
}
int ans1 = 0;
int ans2 = 0;
long temp = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0 && temp + a1[i] >= 0) {
ans1 += temp + a1[i] + 1;
a1[i] -= temp + a1[i] + 1;
}
if (i % 2 != 0 && temp + a1[i] <= 0) {
ans1 += Math.abs(temp + a1[i] - 1);
a1[i] += Math.abs(temp + a1[i] - 1);
}
temp += a1[i];
}
if (Arrays.stream(a1).mapToLong(x -> x).sum() == 0) {
ans1++;
}
temp = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0 && temp + a2[i] <= 0) {
ans2 += Math.abs(temp + a2[i] - 1);
a2[i] += Math.abs(temp + a2[i] - 1);
}
if (i % 2 != 0 && temp + a2[i] >= 0) {
ans2 += temp + a2[i] + 1;
a1[i] -= temp + a2[i] + 1;
}
temp += a2[i];
}
if (Arrays.stream(a2).mapToLong(x -> x).sum() == 0) {
ans2++;
}
System.out.println(Math.min(ans1, ans2));
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
// 整数の入力
int n = sc.nextInt();
int a[] = new int[n];
for(int i = 0; i<n;i++){
int j = sc.nextInt();
a[i] = j;
}
int sum=a[0];
boolean plusFlag;
int ans=0;
if(a[0] > 0){
plusFlag = true;
}else{
plusFlag = false;
}
for(int i = 1;i<n;i++){
sum += a[i];
if(plusFlag){
if(sum >= 0){
ans += Math.abs(sum)+1;
sum = -1;
}
}else{
if(sum <= 0){
ans += Math.abs(sum)+1;
sum = 1;
}
}
plusFlag = !plusFlag;
}
System.out.println(ans);
sc.close();
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main(string[] args) {
new Calc().Solve();
}
public class Calc {
public Calc() { }
public void Solve() {
int n = Utils.ReadLine<int>();
var a = Utils.ReadLine<int>(' ');
int sum = 0;
int x = 0, y = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 0) {
if (sum > 0) continue;
x += Math.Abs(sum) + 1;
sum = 1;
} else {
if (sum < 0) continue;
x += Math.Abs(sum) + 1;
sum = -1;
}
}
sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 1) {
if (sum > 0) continue;
y += Math.Abs(sum) + 1;
sum = 1;
} else {
if (sum < 0) continue;
y += Math.Abs(sum) + 1;
sum = -1;
}
}
Math.Min(x, y).WriteLine();
return;
}
}
}
public static class Utils {
public static T ReadLine<T>() {
return (T)Convert.ChangeType(Console.ReadLine(), typeof(T));
}
public static T[] ReadLine<T>(params char[] separators) {
return Console.ReadLine()
.Split(separators)
.Where(_ => _.Length > 0)
.Select(_ => (T)Convert.ChangeType(_, typeof(T)))
.ToArray();
}
public static List<T> ReadLines<T>(int readCount) {
List<T> rt = new List<T>();
for (int i = 0; i < readCount; i++) {
rt.Add(ReadLine<T>());
}
return rt;
}
public static string Docking<T>(this IEnumerable<T> s, int sequenceRange, Func<T, string> filter = null) {
string str = "";
int c = 0;
foreach (var item in s) {
str += filter == null ? item.ToString() : filter(item);
c++;
if (c == sequenceRange) break;
}
return str;
}
public static string Docking<T>(this IEnumerable<T> s, Func<T, string> filter = null) {
return s.Docking(s.Count(), filter);
}
public static string RangeDocking<T>(this IEnumerable<T> s, int start, int end, Func<T, string> filter = null) {
string str = "";
end = end < s.Count() ? end : s.Count();
var items = s.ToArray();
for (int i = start; i < end; i++) {
str += filter == null ? items[i].ToString() : filter(items[i]);
}
return str;
}
public static int IntParse(this string n) {
return int.Parse(n);
}
public static void WriteLine(this object obj) {
Console.WriteLine(obj);
}
public static void AddTo<T>(this T obj,List<T> list) {
list.Add(obj);
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n][2];
for (int i = 0; i < n; i++) {
cin >> a[i][0];
a[i][1] = a[i][0];
}
int sum = 0;
int res[2];
for (int check = 0; check < 2; check++) {
sum = 0;
if (check) {
if (a[0][check] > 0) {
int temp = -1 - a[0][check];
a[0][check] += temp;
res[check] += temp * -1;
} else if (a[0][check] < 0) {
int temp = 1 - a[0][check];
a[0][check] += temp;
res[check] += temp;
}
}
if (a[0][check] == 0) {
if (!check) {
a[0][check]++;
} else {
a[0][check]--;
}
res[check]++;
}
for (int i = 0; i < n - 1; i++) {
sum += a[i][check];
if (sum * (sum + a[i + 1][check]) >= 0) {
if (sum > 0) {
int temp = -1 - sum - a[i + 1][check];
a[i + 1][check] += temp;
res[check] += temp * -1;
} else {
int temp = 1 - sum - a[i + 1][check];
a[i + 1][check] += temp;
res[check] += temp;
}
}
}
}
cout << min(res[0], res[1]) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
template <class T1, class T2>
bool chmin(T1 &a, T2 b) {
return b < a && (a = b, true);
}
template <class T1, class T2>
bool chmax(T1 &a, T2 b) {
return a < b && (a = b, true);
}
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using P = pair<ll, ll>;
using ld = long double;
using vld = vector<ld>;
using vi = vector<int>;
using vvi = vector<vi>;
vll conv(vi &v) {
vll r(v.size());
for (long long i = 0; i < (long long)(v.size()); i++) r[i] = v[i];
return r;
}
inline void input(int &v) {
v = 0;
char c = 0;
int p = 1;
while (c < '0' || c > '9') {
if (c == '-') p = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
v = (v << 3) + (v << 1) + c - '0';
c = getchar();
}
v *= p;
}
template <typename T, typename U>
ostream &operator<<(ostream &o, const pair<T, U> &v) {
o << "(" << v.first << ", " << v.second << ")";
return o;
}
template <size_t...>
struct seq {};
template <size_t N, size_t... Is>
struct gen_seq : gen_seq<N - 1, N - 1, Is...> {};
template <size_t... Is>
struct gen_seq<0, Is...> : seq<Is...> {};
template <class Ch, class Tr, class Tuple, size_t... Is>
void print_tuple(basic_ostream<Ch, Tr> &os, Tuple const &t, seq<Is...>) {
using s = int[];
(void)s{0, (void(os << (Is == 0 ? "" : ", ") << get<Is>(t)), 0)...};
}
template <class Ch, class Tr, class... Args>
auto operator<<(basic_ostream<Ch, Tr> &os, tuple<Args...> const &t)
-> basic_ostream<Ch, Tr> & {
os << "(";
print_tuple(os, t, gen_seq<sizeof...(Args)>());
return os << ")";
}
ostream &operator<<(ostream &o, const vvll &v) {
for (long long i = 0; i < (long long)(v.size()); i++) {
for (long long j = 0; j < (long long)(v[i].size()); j++)
o << v[i][j] << " ";
o << endl;
}
return o;
}
template <typename T>
ostream &operator<<(ostream &o, const vector<T> &v) {
o << '[';
for (long long i = 0; i < (long long)(v.size()); i++)
o << v[i] << (i != v.size() - 1 ? ", " : "");
o << "]";
return o;
}
template <typename T>
ostream &operator<<(ostream &o, const deque<T> &v) {
o << '[';
for (long long i = 0; i < (long long)(v.size()); i++)
o << v[i] << (i != v.size() - 1 ? ", " : "");
o << "]";
return o;
}
template <typename T>
ostream &operator<<(ostream &o, const set<T> &m) {
o << '[';
for (auto it = m.begin(); it != m.end(); it++)
o << *it << (next(it) != m.end() ? ", " : "");
o << "]";
return o;
}
template <typename T>
ostream &operator<<(ostream &o, const unordered_set<T> &m) {
o << '[';
for (auto it = m.begin(); it != m.end(); it++)
o << *it << (next(it) != m.end() ? ", " : "");
o << "]";
return o;
}
template <typename T, typename U>
ostream &operator<<(ostream &o, const map<T, U> &m) {
o << '[';
for (auto it = m.begin(); it != m.end(); it++)
o << *it << (next(it) != m.end() ? ", " : "");
o << "]";
return o;
}
template <typename T, typename U, typename V>
ostream &operator<<(ostream &o, const unordered_map<T, U, V> &m) {
o << '[';
for (auto it = m.begin(); it != m.end(); it++) o << *it;
o << "]";
return o;
}
vector<int> range(const int x, const int y) {
vector<int> v(y - x + 1);
iota(v.begin(), v.end(), x);
return v;
}
template <typename T>
istream &operator>>(istream &i, vector<T> &o) {
for (long long j = 0; j < (long long)(o.size()); j++) i >> o[j];
return i;
}
template <typename T, typename S, typename U>
ostream &operator<<(ostream &o, const priority_queue<T, S, U> &v) {
auto tmp = v;
while (tmp.size()) {
auto x = tmp.top();
tmp.pop();
o << x << " ";
}
return o;
}
template <typename T>
ostream &operator<<(ostream &o, const queue<T> &v) {
auto tmp = v;
while (tmp.size()) {
auto x = tmp.front();
tmp.pop();
o << x << " ";
}
return o;
}
template <typename T>
ostream &operator<<(ostream &o, const stack<T> &v) {
auto tmp = v;
while (tmp.size()) {
auto x = tmp.top();
tmp.pop();
o << x << " ";
}
return o;
}
template <typename T>
unordered_map<T, ll> counter(vector<T> vec) {
unordered_map<T, ll> ret;
for (auto &&x : vec) ret[x]++;
return ret;
};
string substr(string s, P x) { return s.substr(x.first, x.second - x.first); }
void vizGraph(vvll &g, int mode = 0, string filename = "out.png") {
ofstream ofs("./out.dot");
ofs << "digraph graph_name {" << endl;
set<P> memo;
for (long long i = 0; i < (long long)(g.size()); i++)
for (long long j = 0; j < (long long)(g[i].size()); j++) {
if (mode && (memo.count(P(i, g[i][j])) || memo.count(P(g[i][j], i))))
continue;
memo.insert(P(i, g[i][j]));
ofs << " " << i << " -> " << g[i][j]
<< (mode ? " [arrowhead = none]" : "") << endl;
}
ofs << "}" << endl;
ofs.close();
system(((string) "dot -T png out.dot >" + filename).c_str());
}
size_t random_seed;
namespace std {
using argument_type = P;
template <>
struct hash<argument_type> {
size_t operator()(argument_type const &x) const {
size_t seed = random_seed;
seed ^= hash<ll>{}(x.first);
seed ^= (hash<ll>{}(x.second) << 1);
return seed;
}
};
}; // namespace std
int main() {
int n, a[100100];
cin >> n;
int total = 0;
int count = 0;
for (int i = 0; i < n; ++i) {
cin >> a[i];
cerr << "i " << i << ", a[i] " << a[i] << " total: " << total << " count "
<< count << endl;
total += a[i];
if (i == 0) {
continue;
}
if (a[0] >= 0) {
cerr << "a[i] >= 0, so... " << endl;
if (i % 2 == 1) {
if (total >= 0) {
count += total + 1;
total = -1;
cerr << "aaaa should be minus!" << endl;
}
} else if (total <= 0) {
count += abs(total) + 1;
total = 1;
cerr << "bbbb should be plus!" << endl;
}
} else {
if (i % 2 == 0) {
if (total >= 0) {
count += total + 1;
total = -1;
cerr << "cccc should be minus!" << endl;
}
} else if (total <= 0) {
count += abs(total) + 1;
total = 1;
cerr << "dddd should be plus!" << endl;
}
}
cerr << "total " << total << " is after operation" << endl;
}
if (total == 0) {
cerr << "total is zero, increment total" << endl;
++count;
}
cout << count << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
sa = 0
count = [0, 0]
for i in range(n):
now = a[i]
sa += now
if sa <= 0 and i%2 == 0:
count[0] += 1 - sa
sa = 1
elif sa >= 0 and i%2 == 1:
count[0] += 1 + sa
sa = -1
sa = 0
for i in range(n):
now = a[i]
sa += now
if sa <= 0 and i%2 == 1:
count[1] += 1 - sa
sa = 1
elif sa >= 0 and i%2 == 0:
count[1] += 1 + sa
sa = -1
print(count) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void) {
int n;
int i;
int64_t sum;
bool default_flag;
bool plus_flag, minus_flag;
int64_t ope_count;
cin >> n;
vector<int64_t> a(n);
for (i = 0; i < n; i++) {
cin >> a.at(i);
}
sum = 0;
ope_count = 0;
default_flag = true;
plus_flag = false;
minus_flag = false;
for (i = 0; i < n; i++) {
sum += a.at(i);
if (default_flag == true) {
default_flag = false;
if (sum > 0) {
plus_flag = true;
} else if (sum < 0) {
minus_flag = true;
} else if (sum == 0) {
ope_count++;
if (a.at(i + 1) <= 0) {
sum++;
plus_flag = true;
} else if (a.at(i + 1) > 0) {
sum--;
minus_flag = true;
}
}
} else if (plus_flag == true) {
while (sum >= 0) {
ope_count++;
sum--;
}
plus_flag = false;
minus_flag = true;
} else if (minus_flag == true) {
while (sum <= 0) {
ope_count++;
sum++;
}
plus_flag = true;
minus_flag = false;
}
}
cout << ope_count << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | # -*- coding: utf-8 -*-
trial_num = int(input())
number_lists = list(map(int, input().split()))
def main():
sum_num = 0
change_num = 0
if(number_lists[0] < 0):
for i in range(trial_num):
number_lists[i] = -1 * number_lists[i]
for i in range(trial_num):
if(i == 0):
sum_num = number_lists[0]
continue
else:
if(i%2 == 1):
if(sum_num + number_lists[i] < 0):
sum_num += number_lists[i]
continue
else:
change_num += abs(sum_num + number_lists[i]) + 1
sum_num = -1
continue
else:
if (sum_num + number_lists[i] > 0):
sum_num += number_lists[i]
continue
else:
change_num += abs(sum_num + number_lists[i]) + 1
sum_num = 1
continue
print(change_num)
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 <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <tuple>
using ll = long long;
using namespace std;
#define modmod 1000000007
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,k,n) for(int i=k;i<n;i++)
int main(){
int N;
cin >> N;
ll a;
ll sum[N] = {0};
ll s = 0;
ll old_s = 0;
ll count = 0;
for(int i = 0; i < N; i++){
cin >> a;
if(i != 0 and old_s == 0 and a != 0){
if(a>0){
s = -1;
}else{
s = 1;
}
}
old_s = s;
s += a;
if(s==0){
if(old_s > 0){
s--;
count++;
}else if(old_s ==0){
count++;
}else{
s++;
count++;
}
}
if(s > 0 and old_s > 0){
count += s+1;
s = -1;
}else if(s < 0 and old_s < 0){
count += abs(s)+1;
s = 1;
}
}
cout << count << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
template <class T>
bool chmax(T& a, const T& b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
bool chmin(T& a, const T& b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
template <typename T1, typename T2>
pair<T1, T2> operator+(const pair<T1, T2>& l, const pair<T1, T2>& r) {
return make_pair(l.first + r.first, l.second + r.second);
}
template <typename T1, typename T2>
pair<T1, T2> operator-(const pair<T1, T2>& l, const pair<T1, T2>& r) {
return make_pair(l.first - r.first, l.second - r.second);
}
const long long int MOD = 1e9 + 7, INF = 1e18;
long long int N, arr[100000], sums[100000];
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
cin >> N;
for (long long int i = (0), i_end_ = (N); i < i_end_; i++) {
cin >> arr[i];
}
bool flag;
long long int sum = 0;
long long int ans = 0;
sums[0] = arr[0];
for (long long int i = (0), i_end_ = (N - 1); i < i_end_; i++) {
sums[i + 1] = arr[i + 1] + sums[i];
}
for (long long int i = (0), i_end_ = (N); i < i_end_; i++) {
cout << sums[i] << " ";
}
cout << endl;
flag = false;
for (long long int i = (0), i_end_ = (N); i < i_end_; i++) {
sums[i] += sum;
if (flag ^ ((i % 2) == 1)) {
if (sums[i] >= 0) {
sum -= (sums[i] + 1);
ans += abs(sums[i] + 1);
sums[i] -= (sums[i] + 1);
}
} else {
if (sums[i] <= 0) {
sum -= (sums[i] - 1);
ans += abs(sums[i] - 1);
sums[i] -= (sums[i] - 1);
}
}
}
for (long long int i = (0), i_end_ = (N); i < i_end_; i++) {
cout << sums[i] << " ";
}
cout << endl;
long long int tmp = ans;
sum = 0;
ans = 0;
sums[0] = arr[0];
for (long long int i = (0), i_end_ = (N - 1); i < i_end_; i++) {
sums[i + 1] = arr[i + 1] + sums[i];
}
flag = true;
for (long long int i = (0), i_end_ = (N); i < i_end_; i++) {
sums[i] += sum;
if (flag ^ ((i % 2) == 1)) {
if (sums[i] >= 0) {
sum -= (sums[i] + 1);
ans += abs(sums[i] + 1);
sums[i] -= (sums[i] + 1);
}
} else {
if (sums[i] <= 0) {
sum -= (sums[i] - 1);
ans += abs(sums[i] - 1);
sums[i] -= (sums[i] - 1);
}
}
}
for (long long int i = (0), i_end_ = (N); i < i_end_; i++) {
cout << sums[i] << " ";
}
cout << endl;
cout << min(tmp, ans) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n;
int d[100005];
int main(void) {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &d[i]);
int ans1 = 0, sum = 0;
for (int i = 1; i <= n; i++) {
if (i % 2 == 0) {
int a = 0;
if (sum + d[i] >= 0) {
a = abs(sum + d[i]) + 1;
ans1 += a;
sum = -1;
} else
sum += d[i];
} else {
int a = 0;
if (sum + d[i] <= 0) {
a = abs(sum + d[i]) + 1;
ans1 += a;
sum = 1;
} else
sum += d[i];
}
}
int ans2 = 0;
sum = 0;
for (int i = 1; i <= n; i++) {
if (i % 2 == 0) {
int a = 0;
if (sum + d[i] <= 0) {
a = abs(sum + d[i]) + 1;
ans2 += a;
sum = 1;
} else
sum += d[i];
} else {
int a = 0;
if (sum + d[i] >= 0) {
a = abs(sum + d[i]) + 1;
ans2 += a;
sum = -1;
} else
sum += d[i];
}
}
printf("%d\n", min(ans1, ans2));
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> a(N);
for (int i = 0; i < N; i++) cin >> a.at(i);
int64_t t = 0LL, res1 = 0LL, res2 = 0LL;
for (int i = 0; i < N; i++) {
t += a.at(i);
if (i % 2 == 0) {
if (t <= 0) {
res1 += (1 - t);
t = 1LL;
}
} else {
if (t >= 0) {
res1 += abs(-1 - t);
t = -1LL;
}
}
}
t = 0LL;
for (int i = 0; i < N; i++) {
t += a.at(i);
if (i % 2 == 1) {
if (t <= 0) {
res2 += (1 - t);
t = 1LL;
}
} else {
if (t >= 0) {
res2 += abs(-1 - t);
t = -1LL;
}
}
}
int res = min(res1, res2);
cout << res << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | 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
elif currentSum == 0 and restSum == 0 and A[i + 1] >= 0:
count += abs(currentSum) + 1
currentSum = -1
elif currentSum == 0 and restSum == 0 and A[i + 1] <= 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 | cpp | #include <bits/stdc++.h>
using ll = long long;
using namespace std;
const double pi = acos(-1.0);
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
void print(vector<ll> vec) {
for (int i = 0; i < ((ll)(vec).size()); i++) {
if (i) cout << " ";
cout << vec[i];
}
cout << "\n";
}
ll dp[100005];
int main() {
ll n;
cin >> n;
vector<ll> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
dp[0] = a[0];
ll ans = 0;
for (int i = 1; i < n; i++) {
if (dp[i - 1] < 0) {
if (dp[i - 1] + a[i] > 0) {
dp[i] = dp[i - 1] + a[i];
} else {
ll ai = 1 - dp[i - 1];
ans += abs(ai - a[i]);
dp[i] = 1;
}
}
if (dp[i - 1] > 0) {
if (dp[i - 1] + a[i] < 0) {
dp[i] = dp[i - 1] + a[i];
} else {
ll ai = -1 - dp[i - 1];
ans += abs(ai - a[i]);
dp[i] = -1;
}
}
}
ll cnt = 0;
if (a[0] > 0) {
dp[0] = -1;
cnt += abs(a[0] + 1);
} else {
dp[0] = 1;
cnt += abs(a[0] - 1);
}
for (int i = 1; i < n; i++) {
if (dp[i - 1] < 0) {
if (dp[i - 1] + a[i] > 0) {
dp[i] = dp[i - 1] + a[i];
} else {
ll ai = 1 - dp[i - 1];
cnt += abs(ai - a[i]);
dp[i] = 1;
}
}
if (dp[i - 1] > 0) {
if (dp[i - 1] + a[i] < 0) {
dp[i] = dp[i - 1] + a[i];
} else {
ll ai = -1 - dp[i - 1];
cnt += abs(ai - a[i]);
dp[i] = -1;
}
}
}
ans = min(ans, cnt);
cout << ans << "\n";
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int ans = 0;
int part = 0;
int a;
cin >> a;
part += a;
bool isP = part > 0 ? true : false;
for (int i = 1; i < n; i++) {
cin >> a;
part += a;
if (isP) {
if (part > 0) {
ans += part + 1;
part = -1;
} else if (part == 0) {
ans++;
part = -1;
}
isP = false;
} else {
if (part < 0) {
ans += 1 - part;
part = 1;
} else if (part == 0) {
ans++;
part = 1;
}
isP = 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>
using namespace std;
int main() {
long long n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < (n); ++i) cin >> a[i];
long long total = a[0];
long long total2 = a[0];
long long ans = 0;
for (int i = (1); i < (n); ++i) {
total = total2;
total += a[i];
if (total * total2 >= 0) {
ans += (abs(total) + 1);
if (total > 0) {
total = -1;
} else {
total = 1;
}
}
total2 = total;
}
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() {
long long n;
cin >> n;
vector<long long> a;
vector<long long> b;
for (long long i = 0; i < n; i++) {
long long ai;
cin >> ai;
a.push_back(ai);
b.push_back(ai);
}
long long countA = 0;
long long countB = 0;
if (a.at(0) == 0) {
a.at(0) = 1;
b.at(0) = -1;
countA = 1;
countB = 1;
}
long long sumA = a.at(0);
long long sumB = b.at(0);
for (long long i = 0; i < n - 1; i++) {
long long next_sumA = sumA + a.at(i + 1);
long long next_sumB = sumB + b.at(i + 1);
if (sumA > 0 && next_sumA >= 0) {
long long diff = 1 + next_sumA;
countA += diff;
a.at(i + 1) -= diff;
next_sumA -= diff;
} else if (sumA < 0 && next_sumA <= 0) {
long long diff = 1 - next_sumA;
countA += diff;
a.at(i + 1) += diff;
next_sumA += diff;
}
if (sumB > 0 && next_sumB >= 0) {
long long diff = 1 + next_sumB;
countB += diff;
b.at(i + 1) -= diff;
next_sumB -= diff;
} else if (sumB < 0 && next_sumB <= 0) {
long long diff = 1 - next_sumB;
countB += diff;
b.at(i + 1) += diff;
next_sumB += diff;
}
sumA = next_sumA;
sumB = next_sumB;
}
cout << min(countA, countB) << 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 array[n];
for (int i = 0; i < n; i++) {
cin >> array[i];
}
long long answer = 0;
long long sum = 0;
for (int i = 0; i < n; i++) {
if (sum == 0)
sum += array[i];
else if (sum < 0) {
if (sum + array[i] > 0) {
sum += array[0];
} else {
answer += abs((-1) * sum + 1 - array[i]);
sum = 1;
}
} else {
if (sum + array[i] < 0) {
sum += array[i];
} else {
answer += abs((-1) * sum - 1 - array[i]);
sum = -1;
}
}
}
cout << answer << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
vector<long long> v(n);
for (int i = 0; i < n; i++) cin >> v[i];
long long cnt = 0;
long long sum = 0;
for (int i = 0; i < n; i++) {
sum += v[i];
if (i % 2 == 0 && sum < 0) {
cnt += abs(sum) + 1;
sum = 1;
} else if (i % 2 == 1 && sum > 0) {
cnt += abs(sum) + 1;
sum = -1;
}
if (i % 2 == 0 && sum == 0) {
sum = 1;
cnt++;
} else if (i % 2 == 1 && sum == 0) {
sum = -1;
cnt++;
}
}
long long ans = cnt;
sum = 0;
cnt = 0;
for (int i = 0; i < n; i++) {
sum += v[i];
if (i % 2 == 0 && sum > 0) {
cnt += abs(sum) + 1;
sum = -1;
} else if (i % 2 == 1 && sum < 0) {
cnt += abs(sum) + 1;
sum = -1;
}
if (i % 2 == 0 && sum == 0) {
sum = -1;
cnt++;
} else if (i % 2 == 1 && sum == 0) {
sum = 1;
cnt++;
}
}
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 | UNKNOWN | #![allow(unused_imports, unused_macros, dead_code)]
macro_rules! min {
(.. $x:expr) => {{
let mut it = $x.iter();
it.next().map(|z| it.fold(z, |x, y| min!(x, y)))
}};
($x:expr) => ($x);
($x:expr, $($ys:expr),*) => {{
let t = min!($($ys),*);
if $x < t { $x } else { t }
}}
}
macro_rules! max {
(.. $x:expr) => {{
let mut it = $x.iter();
it.next().map(|z| it.fold(z, |x, y| max!(x, y)))
}};
($x:expr) => ($x);
($x:expr, $($ys:expr),*) => {{
let t = max!($($ys),*);
if $x > t { $x } else { t }
}}
}
macro_rules! ewriteln {
($($args:expr),*) => { let _ = writeln!(&mut std::io::stderr(), $($args),*); };
}
macro_rules! trace {
($x:expr) => { ewriteln!(">>> {} = {:?}", stringify!($x), $x) };
($($xs:expr),*) => { trace!(($($xs),*)) }
}
macro_rules! put {
(.. $x:expr) => {{
let mut it = $x.iter();
if let Some(x) = it.next() { print!("{}", x); }
for x in it { print!(" {}", x); }
println!("");
}};
($x:expr) => { println!("{}", $x) };
($x:expr, $($xs:expr),*) => { print!("{} ", $x); put!($($xs),*) }
}
const M: i64 = 1_000_000_007;
trait Group: std::ops::Add<Output=Self> + std::ops::Sub<Output=Self> + std::ops::Neg<Output=Self>
+ std::iter::Sum + Clone + Copy {
fn zero() -> Self;
}
impl Group for i32 { fn zero() -> Self { 0 }}
impl Group for i64 { fn zero() -> Self { 0 }}
impl Group for f32 { fn zero() -> Self { 0.0 }}
impl Group for f64 { fn zero() -> Self { 0.0 }}
trait Ring: Group + std::ops::Mul<Output=Self> + std::ops::Div<Output=Self> {
fn one() -> Self;
}
impl Ring for i32 { fn one() -> Self { 1 }}
impl Ring for i64 { fn one() -> Self { 1 }}
impl Ring for f32 { fn one() -> Self { 1.0 }}
impl Ring for f64 { fn one() -> Self { 1.0 }}
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug)]
enum Hyper<X> { NegInf, Real(X), Inf }
use Hyper::{Real, NegInf, Inf};
impl<X> Hyper<X> {
fn unwrap(self) -> X { if let Hyper::Real(x) = self { x } else { panic!() } }
}
impl<X: Ring> std::ops::Add for Hyper<X> {
type Output = Self;
fn add(self, rhs: Hyper<X>) -> Hyper<X> {
match (self, rhs) {
(Real(x), Real(y)) => Real(x + y),
(Inf, _) => Inf,
(_, Inf) => Inf,
_ => NegInf,
}
}
}
impl<X: Ring> std::ops::Sub for Hyper<X> {
type Output = Self;
fn sub(self, rhs: Hyper<X>) -> Hyper<X> { self + (-rhs) }
}
impl<X: Ring> std::ops::Neg for Hyper<X> {
type Output = Self;
fn neg(self) -> Hyper<X> {
match self {
Inf => NegInf,
NegInf => Inf,
Real(x) => Real(-x),
}
}
}
fn solve(d: i64, sign: bool, xs: &Vec<i64>) -> i64 {
let mut sign = sign;
let mut sum = 0;
let mut num = 0;
for &x in xs.iter() {
sum += x;
if sign {
if sum < 1 {
num += 1 - sum;
sum = 1;
}
else if sum > d {
num += sum - d;
sum = d;
}
sign = false;
} else {
if sum > -1 {
num += sum + 1;
sum = -1;
}
else if sum < -d {
num += -d - sum;
sum = -d;
}
sign = true;
}
}
num
}
fn main() {
let mut sc = Scanner::new();
let n: usize = sc.cin();
let xs: Vec<i64> = sc.vec(n);
put!(min!(solve(100000, true, &xs),
solve(100000, false, &xs)));
}
use std::io::{self, Write};
use std::str::FromStr;
use std::collections::VecDeque;
struct Scanner { stdin: io::Stdin, buffer: VecDeque<String>, }
impl Scanner {
fn new() -> Self { Scanner { stdin: io::stdin(), buffer: VecDeque::new() } }
fn cin<T: FromStr>(&mut self) -> T {
while self.buffer.len() == 0 {
let mut line = String::new();
let _ = self.stdin.read_line(&mut line);
for w in line.split_whitespace() {
self.buffer.push_back(String::from(w));
}
}
self.buffer.pop_front().unwrap().parse::<T>().ok().unwrap()
}
fn chars(&mut self) -> Vec<char> { self.cin::<String>().chars().collect() }
fn vec<T: FromStr>(&mut self, n: usize) -> Vec<T> { (0..n).map(|_| self.cin()).collect() }
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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 = 0;
if (a[0] >= 0) {
int sum = a[0];
for (int i = 1; i < n; ++i) {
if (i % 2 == 1) {
if (sum + a[i] < 0) {
sum += a[i];
} else {
ans += sum + a[i] + 1;
sum = -1;
}
} else {
if (sum + a[i] > 0) {
sum += a[i];
} else {
ans += abs(sum + a[i] - 1);
sum = 1;
}
}
}
} else {
int sum = a[0];
for (int i = 1; i < n; ++i) {
if (i % 2 == 1) {
if (sum + a[i] > 0) {
sum += a[i];
} else {
ans += abs(sum + a[i] - 1);
sum = 1;
}
} else {
if (sum + a[i] < 0) {
sum += a[i];
} else {
ans += sum + a[i] + 1;
sum = -1;
}
}
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = 1ll << 60;
ll N;
vector<ll> a;
int main(int argc, char **argv) {
cin >> N;
a.resize(N);
for (ll i = (0); i < (ll)(N); ++i) cin >> a[i];
for (ll i = (0); i < (ll)(N - 1); ++i) a[i + 1] += a[i];
ll res{INF};
ll pre;
ll cnt;
pre = 0;
cnt = 0;
for (ll i = (0); i < (ll)(N); ++i) {
ll t = a[i] + pre;
if (i & 1) {
if (t <= 0) {
cnt += abs(t) + 1;
pre += -t + 1;
}
} else {
if (t >= 0) {
cnt += a[i] + 1;
pre -= t + 1;
}
}
}
res = min(res, cnt);
cnt = 0;
pre = 0;
for (ll i = (0); i < (ll)(N); ++i) {
ll t = a[i] + pre;
if (~i & 1) {
if (t <= 0) {
cnt += abs(t) + 1;
pre += -t + 1;
}
} else {
if (t >= 0) {
cnt += t + 1;
pre -= t + 1;
}
}
}
res = min(res, cnt);
std::cout << res << std::endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.function.IntFunction;
import java.util.function.Supplier;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner();
int n=scanner.nextInt();
long[] a=new long[n+1];
for(int i=1;i<=n;i++){
a[i]=scanner.nextInt();
}
Arrays.parallelPrefix(a,(c,b)->c+b);
//put(Arrays.toString(a));
long ans=0;
long ruiseki=0;
for(int i=1;i<=n;i++){
//put(format("i=%d",i));
//put(format("ruiseki=%d",ruiseki));
long val=a[i]+ruiseki;
long val_=a[i-1]+ruiseki;
//put(format("val=%d",val));
//put(format("val_=%d",val_));
if(val==0){
int bit=Long.signum(val_);
ruiseki+=bit*1;
ans+=Math.abs(bit);
}else if(val>0&&val_>0){
ruiseki-=(val+1);
ans+=Math.abs(val+1);
}else if(val<0&&val_<0){
ruiseki+=Math.abs(val)+1;
ans+=Math.abs(val)+1;
}
//put(ans);
//put();
}
put(ans);
}
public static void print(Object object){
System.out.print(object);
}
public static void put(Object object) {
System.out.println(object);
}
public static void put(){
System.out.println();
}
public static String format(String string, Object... args) {
return String.format(string, args);
}
}
final class Scanner {
private final InputStream in = System.in;
private final byte[] buffer = new byte[1024];
private int ptr = 0;
private int buflen = 0;
private boolean hasNextByte() {
if (ptr < buflen) {
return true;
} else {
ptr = 0;
try {
buflen = in.read(buffer);
} catch (IOException e) {
e.printStackTrace();
}
if (buflen <= 0) {
return false;
}
}
return true;
}
private int readByte() {
if (hasNextByte())
return buffer[ptr++];
else
return -1;
}
private boolean isPrintableChar(int c) {
return 33 <= c && c <= 126;
}
public boolean hasNext() {
while (hasNextByte() && !isPrintableChar(buffer[ptr]))
ptr++;
return hasNextByte();
}
public String next() {
if (!hasNext())
throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while (isPrintableChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public long nextLong() {
if (!hasNext())
throw new NoSuchElementException();
long n = 0;
boolean minus = false;
int b = readByte();
if (b == '-') {
minus = true;
b = readByte();
}
if (b < '0' || '9' < b) {
throw new NumberFormatException();
}
while (true) {
if ('0' <= b && b <= '9') {
n *= 10;
n += b - '0';
} else if (b == -1 || !isPrintableChar(b)) {
return minus ? -n : n;
} else {
throw new NumberFormatException();
}
b = readByte();
}
}
public int nextInt() {
long nl = nextLong();
if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE)
throw new NumberFormatException();
return (int) nl;
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
final class Pair {
final public int x, y;
Pair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int hashCode() {
return x+y;
}
@Override
public boolean equals(Object obj) {
boolean result=super.equals(obj);
if(obj.getClass()!=this.getClass()){
return false;
}
Pair pair=(Pair)obj;
if(this.x==pair.x&&this.y==pair.y) return true;
return false;
}
@Override
public String toString() {
return String.format("(%d,%d)", x, y);
}
}
final class Tuple<T,V>{
//immutabl1でないことに注意(T,Vがmutableの場合変更可能)
final public T t;
final public V v;
Tuple(T t,V v){
this.t=t;
this.v=v;
}
@Override
public int hashCode() {
return (t.hashCode()+v.hashCode());
}
@Override
public boolean equals(Object obj) {
if(obj.getClass()!=this.getClass()){
return false;
}
Tuple<T,V> tuple=(Tuple)obj;
return tuple.t.equals(this.t)&&tuple.v.equals(this.v);
}
@Override
public String toString() {
return String.format("<Tuple>=<%s,%s>",t,v);
}
}
final class LowerBoundComparator<T extends Comparable<? super T>>
implements Comparator<T>
{
public int compare(T x, T y)
{
return (x.compareTo(y) >= 0) ? 1 : -1;
}
}
final class UpperBoundComparator<T extends Comparable<? super T>>
implements Comparator<T>
{
public int compare(T x, T y)
{
return (x.compareTo(y) > 0) ? 1 : -1;
}
}
final class Util {
static long gcd(long a,long b){
if(a%b==0)return b;
return gcd(b,a%b);
}
static long lcm(long a,long b){
long gcd=gcd(a,b);
long result=b/gcd;
return a*result;
}
static long kaijoMod(int n,int mod){
if(n<1) return -1;
long result=1;
for(int i=n;i>1;i--){
result*=i;
result%=mod;
}
return result;
}
static <T extends Comparable> Map<T,Integer> count(List<T> list){
//副作用
Collections.sort(list);
Map<T,Integer> result=new HashMap<>();
int l=0,r=0;
while(l<list.size()){
while(r<list.size()-1&&list.get(r).equals(r+1)){
r++;
}
result.put(list.get(r),r-l+1);
r++;
l=r;
}
return result;
}
static Map<Integer,Integer> count(int[] array){
//副作用
Arrays.sort(array);
Map<Integer,Integer> result=new HashMap<>();
int l=0,r=0;
while(l<array.length){
while(r<array.length-1&&array[r]==array[r+1]){
r++;
}
result.put(array[l],r-l+1);
r++;
l=r;
}
return result;
}
static String toStringBWS(Iterable iterable){
Iterator ite=iterable.iterator();
return toStringBWS(ite);
}
static String toStringBWS(Iterator ite){
StringBuilder sb=new StringBuilder();
sb.append(ite.next());
while(ite.hasNext()){
sb.append(" ");
sb.append(ite.next());
}
return sb.toString();
}
static String toStringBWS(int[] array){
StringBuilder sb=new StringBuilder();
for(int i=0;i<array.length-1;i++){
sb.append(array[i]);
sb.append(" ");
}
sb.append(array[array.length-1]);
return sb.toString();
}
static String toStringBWS(long[] array){
StringBuilder sb=new StringBuilder();
for(int i=0;i<array.length-1;i++){
sb.append(array[i]);
sb.append(" ");
}
sb.append(array[array.length-1]);
return sb.toString();
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> x;
int temp, ans = 0;
for (int i = 0; i != n; ++i) {
cin >> temp;
x.push_back(temp);
}
if (!x[0]) {
x[0] = -1;
++ans;
for (int i = 0; i != n; ++i) {
if (x[i] < 0) {
x[i] = 1;
break;
}
}
}
int sum = x[0];
for (int i = 1; i != n; ++i) {
int sum2 = sum + x[i];
if (sum * sum2 >= 0) {
ans += abs(sum2) + 1;
if (sum < 0)
sum2 = 1;
else
sum2 = -1;
}
sum = sum2;
}
cout << ans;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <bits/stdc++.h>
int abs(int num) { return (num > 0) ? num : -num; }
int main() {
int n;
scanf("%d", &n);
int A[n];
int i;
int sum = 0;
int ans = 0;
for (i = 0; i < n; i++) {
scanf("%d", &A[i]);
}
for (i = 0; i < n - 1; i++) {
sum += A[i];
if (sum > 0) {
if (sum >= -A[i + 1]) {
ans += abs(sum + A[i + 1] + 1);
A[i + 1] -= abs(sum + A[i + 1] + 1);
}
} else {
if (sum <= -A[i + 1]) {
ans += abs(sum + A[i + 1] - 1);
A[i + 1] += abs(sum + A[i + 1] + 1);
}
}
}
printf("%d\n", ans);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = [int(i) for i in input().split()]
a1 = a.copy()
cnt1 = 0
sum1 = 0
if a1[0] == 0:
a1[0] = 1
cnt1 +=1
a2 = a.copy()
cnt2 = 0
sum2 = 0
if a2[0] == 0:
a2[0] = -1
cnt2 +=1
for i in range(n-1):
sum1 = sum(a1[:i+1])
if sum1 > 0 and (sum1+a1[i+1]) >= 0:
cnt1 += abs(((sum1 * -1)-1) - a1[i+1])
a1[i+1] = ((sum1 * -1)-1)
elif sum1 < 0 and (sum1 + a1[i+1]) <= 0:
cnt1 += abs(abs(sum1)+1 - a1[i+1])
a1[i+1] = abs(sum1)+1
for i in range(n-1):
sum2 = sum(a2[:i+1])
if sum2 > 0 and (sum2+a2[i+1]) >= 0:
cnt2 += abs(((sum2 * -1)-1) - a2[i+1])
a2[i+1] = ((sum2 * -1)-1)
elif sum2 < 0 and (sum2+a2[i+1]) <= 0:
cnt2 += abs(abs(sum2)+1 - a2[i+1])
a2[i+1] = abs(sum2)+1
print(min(cnt1,cnt2))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
int a[100001];
for (int i = 0; i < N; i++) {
cin >> a[i];
}
int total = a[0];
int ops = 0;
if (total == 0) {
ops++;
if (a[1] > 0) {
total = -1;
} else {
total = 1;
}
}
for (int i = 1; i < N; i++) {
if (total > 0 && (total + a[i]) >= 0) {
ops += a[i] + total + 1;
total = -1;
} else if (total < 0 && (total + a[i]) <= 0) {
ops += -(a[i] + total) + 1;
total = 1;
} else {
total += a[i];
}
}
printf("%d\n", ops);
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n;
long long a[100000];
int main() {
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
long long sum = a[0];
long long count = 0;
bool f;
if (a[0] == 0) {
sum++;
count++;
}
if (sum > 0)
f = true;
else
f = false;
for (int i = 1; i < n; i++) {
if (f) {
if (sum + a[i] < 0) {
sum += a[i];
f = false;
continue;
}
count += (sum + a[i]) + 1;
sum = -1;
f = false;
} else {
if (sum + a[i] > 0) {
sum += a[i];
f = true;
continue;
}
count -= (sum + a[i]);
count++;
sum = 1;
f = true;
}
}
cout << count << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; i++) {
cin >> a.at(i);
}
long long cnt = 0;
long long tot = a.at(0);
long long k = 1;
if (a.at(0) == 0) {
k = -1;
for (int i = 1; i < n; i++) {
if (a.at(i) != 0) {
k = i;
break;
}
}
if (k == -1) {
cout << 1 + 2 * (n - 1) << endl;
return 0;
}
if (a.at(k) > 0) {
tot = -1;
cnt = 1 + 2 * (k - 1);
} else {
tot = 1;
cnt = 1 + 2 * (k - 1);
}
}
for (int i = k; i < n; i++) {
long long after;
if (tot < 0) {
after = max(1 - tot, a.at(i));
cnt += abs(after - a.at(i));
}
if (tot > 0) {
after = min(-1 - tot, a.at(i));
cnt += abs(after - a.at(i));
}
tot += after;
}
cout << cnt << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
l = len(a)
b = []
for i in range(l):
b.append(a[i])
ans = 0
Ans = 0
summary = a[0]
if(summary == 0):
a[0] = 1
ans+= 1
b[0] = -1
Ans+= 1
else:
b[0] = int(-a[0]/ abs(a[0]))
Ans+= abs(a[0]- b[0])
Summary = b[0]
for i in range(1, l):
if(summary* (summary+ a[i])>= 0):
if(summary > 0):
ans+= a[i]+ summary+ 1
a[i] = -summary- 1
summary= -1
else:
ans+= -summary+ 1- a[i]
a[i] = -summary+ 1
summary= 1
else:
summary+= a[i]
for i in range(1, l):
if(Summary* (Summary+ b[i])>= 0):
if(Summary > 0):
Ans+= b[i]+ Summary+ 1
b[i] = -Summary- 1
Summary= -1
else:
Ans+= -Summary+ 1- b[i]
b[i] = -Summary+ 1
Summary= 1
else:
Summary+= b[i]
print(min(ans, 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;
using ll = long long;
int gcd(int a, int b) { return (b == 0) ? a : gcd(b, a % b); }
int main() {
int n;
cin >> n;
;
vector<ll> a(n);
for (int i = 0; i < (n); i++) {
cin >> a[i];
};
ll ans = 0;
ll tempSum = a[0];
for (int i = 1; i < n; i++) {
if (tempSum > 0 && tempSum + a[i] >= 0) {
ans += fabs(tempSum + a[i]) + 1;
tempSum = -1;
} else if (tempSum < 0 && tempSum + a[i] <= 0) {
ans += fabs(tempSum + a[i]) + 1;
tempSum = 1;
} else {
tempSum += a[i];
}
}
cout << ans << "\n";
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long MOD = pow(10, 9) + 7;
long long mod(long long A, long long M) { return (A % M + M) % M; }
const long long INF = 1LL << 60;
template <class T>
bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T>
bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
long long divCeil(long long A, long long B) { return (A + (B - 1)) / B; }
long long myctoi(char C) { return C - '0'; }
char myitoc(long long N) { return '0' + N; }
signed main() {
long long N;
cin >> N;
vector<long long> A(N);
for (long long i = 0; i < N; i++) {
cin >> A.at(i);
}
long long ans = 0, sum = A.at(0);
for (long long i = 1; i < N; i++) {
long long s = sum;
sum += A.at(i);
if (s > 0 && sum >= 0) {
ans += 1 + sum;
sum = -1;
} else if (s < 0 && sum <= 0) {
ans += 1 - sum;
sum = 1;
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n=int(input())
l=list(map(int,input().split()))
l.append(0)
a1=0
b1=0
a2=0
b2=0
for i in range(n):
b1+=l[i]
b2+=l[i]
if i%2==0:
if b1>=0:
a1+=abs(-1-b1)
b1=-1-l[i+1]
if b2<=0:
a2+=abs(1-b2)
b2=1-l[i+1]
else:
if b1<=0:
a1+=abs(1-b1)
b1=1-l[i+1]
if b2>=0:
a2+=abs(-1-b2)
b2=-1-l[i+1]
print(min(a1,a2))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vll = vector<long long>;
using vp = vector<pair<int, int>>;
using vvi = vector<vector<int>>;
using vvc = vector<vector<char>>;
using vvll = vector<vector<long long>>;
template <class T>
inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
inline bool chmin(T &a, T b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vll a(n);
for (int i = 0; i < (int)(n); i++) cin >> a[i];
auto f = [&](ll x) {
ll sm = x;
ll res = 0;
for (int i = 1; i < n; ++i) {
if (sm > 0) {
if (!(a[i] < -sm)) {
res += a[i] - (-sm) + 1;
a[i] = -sm - 1;
}
} else {
if (!(a[i] > -sm)) {
res += -sm - a[i] + 1;
a[i] = -sm + 1;
}
}
sm += a[i];
}
return res;
};
ll ans = 0;
if (a[0] == 0) {
ll res1 = f(-1);
ll res2 = f(1);
ans = min(res1, res2);
} else {
ans = f(a[0]);
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long> as;
for (int i = 0; i < n; i++) {
long a;
cin >> a;
as.push_back(a);
}
long currentSum;
if (as[0] != 0) {
currentSum = as[0];
} else {
currentSum = 1;
}
int op = 0;
for (int i = 1; i < n; i++) {
if (currentSum > 0) {
if (currentSum + as[i] > 0) {
op += currentSum + as[i] + 1;
currentSum = -1;
} else if (currentSum + as[i] == 0) {
op += 1;
currentSum = -1;
} else {
currentSum = currentSum + as[i];
}
} else {
if (currentSum + as[i] < 0) {
op += (1 - currentSum - as[i]);
currentSum = 1;
} else if (currentSum + as[i] == 0) {
op += 1;
currentSum = 1;
} else {
currentSum = currentSum + as[i];
}
}
}
cout << op << "\n";
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | (defun read-num (&optional (stream *standard-input*))
(parse-integer (read-line stream)))
(defun read-list-from-string (str)
(read-from-string (concatenate 'string "(" str ")")))
(defun read-list (&optional (stream *standard-input*))
(read-list-from-string (read-line stream)))
(defun satisfy-num (acc val)
(if (> acc 0)
(if (< (+ acc val) 0)
0
(- (+ 1 val acc)))
(if (> (+ acc val) 0)
0
(- 1 val acc))))
(defun count-shift (list)
(let ((acc (car list))
(sum 0))
(dolist (e (cdr list) sum)
(let ((shift (satisfy-num acc e)))
(incf sum (abs shift))
(incf acc (+ e shift))))))
(defun solve (list)
(format t "~D~%" (count-shift list)))
(read-num)
(solve (read-list))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
int main(void) {
long long n, now = 0, ans[2] = {};
bool flag;
scanf("%ld", &n);
long long a[n];
for (int i = 0; i < n; i++) {
scanf("%ld", &a[i]);
printf("now :%ld \n", a[i]);
}
for (int j = 0; j < 2; j++) {
if (j == 0) {
flag = true;
} else {
flag = false;
}
now = a[0];
for (int i = 1; i < n; i++) {
now += a[i];
if (flag) {
if (now >= 0) {
ans[j] += (-1 - now) * (-1);
now = -1;
}
flag = false;
} else {
if (now <= 0) {
ans[j] += 1 - now;
now = 1;
}
flag = true;
}
}
}
if (ans[0] > ans[1]) {
printf("%ld", ans[1]);
} else {
printf("%ld", ans[0]);
}
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
ans = 0
tmp = a[0]
for i in range(n-1):
if (tmp > 0):
while not (-tmp > a[i+1]):
n = -tmp - a[i+1] - 1
a[i+1] += n
ans += -n
elif (tmp < 0):
while not (-tmp < a[i+1]):
n = -tmp - a[i+1] + 1
a[i+1] += n
ans += n
tmp += a[i+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;
vector<int> a(n);
for (int i = 0; i < (int)(n); i++) {
cin >> a[i];
}
long long cnt1 = 0;
long long cnt2 = 0;
long long now1 = a[0];
long long now2 = a[0];
for (int i = 1; i < (int)(n); i++) {
now1 += a[i];
if (i % 2 != 0) {
if (now1 >= 0) {
cnt1 += now1 + 1;
now1 = -1;
}
} else {
if (now1 <= 0) {
cnt1 += 1 - now1;
now1 = 1;
}
}
}
for (int i = 1; i < (int)(n); i++) {
now2 += a[i];
if (i % 2 != 0) {
if (now2 <= 0) {
cnt2 += 1 - now2;
now2 = 1;
}
} else {
if (now2 >= 0) {
cnt2 += 1 + now2;
now2 = -1;
}
}
}
long long ans = min(cnt1, cnt2);
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, *a;
cin >> n;
a = new int[n];
int ans = 0;
int sum = 0;
for (int i = 0; i < n; i++) cin >> a[i];
int flag = -1;
if (a[0] > 0)
flag = 1;
else if (a[0] == 0)
flag = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (flag > 0 || flag == 0) {
if (sum <= 0) {
while (sum <= 0) {
sum++;
ans++;
}
}
flag = -1;
} else if (flag < 0) {
if (sum >= 0) {
while (sum >= 0) {
sum--;
ans++;
}
}
flag = 1;
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
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;
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;
int a[n + 1];
for (int i = 0; i < n; i++) cin >> a[i];
int64_t sum = a[0];
int c = 1;
int64_t ans0 = 0, ans1 = 0;
for (int i = 1; i < n; i++) {
sum += a[i];
if (sum * c < 1) {
ans0 += 1 - sum * c;
sum = c;
}
c *= -1;
}
c = -1;
sum = a[0];
for (int i = 1; i < n; i++) {
sum += a[i];
if (sum * c < 1) {
ans1 += 1 - sum * c;
sum = c;
}
c *= -1;
}
if (ans0 < ans1)
cout << ans0 << endl;
else
cout << ans1 << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.*;
public class Main{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[] a = new int[n];
for(int i=0; i<n; i++){
a[i] = scan.nextInt();
}
int count = 0;
int sum = 0;
for(int i=0; i<n-1; i++){
sum = sum+a[i];
if(sum>0){
if(sum+a[i+1]<0){System.out.println(count); }
else{System.out.println(count + "t" + sum);
int p = -1-a[i+1]-sum;
count = count + Math.abs(p);
a[i+1] = a[i+1] + p;}
}
else if(sum<0){
if(sum+a[i+1]>0){System.out.println(count); }
else{System.out.println(count + "k" + sum);
int b = 1-a[i+1]-sum;
count = count + Math.abs(b);
a[i+1] = a[i+1] + b;}
}
}
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 | python3 | import numpy as np
n = input()
a_s = [int(x) for x in input().split()]
sum_i = 0
manipulate_num = 0
for a in a_s:
new_sum_i = sum_i + a
# print(f"a:{a}, sum_i:{sum_i}, new_sum_i:{new_sum_i}")
if new_sum_i == 0:
manipulate_num += 1
if new_sum_i * sum_i > 0:
manipulate_num += abs(new_sum_i)+1
new_sum_i -= np.sign(new_sum_i)*(abs(new_sum_i)+1)
sum_i = new_sum_i
# print(f"a:{a}, sum_i:{sum_i}")
# print()
print(manipulate_num) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
long long sum1 = 0, sum2 = 0;
long long ans1 = 0, ans2 = 0;
for (int i = 0; i < N; ++i) {
long long t;
cin >> t;
if (i == 0) {
if (t == 0) {
sum1 = 1;
ans1 = 1;
sum2 = -1;
ans2 = 1;
} else {
sum1 = t;
sum2 = t > 0 ? -1 : 1;
ans2 += abs(t) + 1;
}
} else {
if (sum1 < 0 && sum1 + t <= 0) {
ans1 += 1 - sum1 - t;
sum1 = 1;
} else if (sum1 > 0 && sum1 + t >= 0) {
ans1 += abs(-1 - sum1 - t);
sum1 = -1;
} else {
sum1 += t;
}
if (sum2 < 0 && sum2 + t <= 0) {
ans2 += 1 - sum2 - t;
sum2 = 1;
} else if (sum2 > 0 && sum2 + t >= 0) {
ans2 += abs(-1 - sum2 - t);
sum2 = -1;
} else {
sum2 += t;
}
}
}
printf("%ld,%ld\n", ans1, ans2);
cout << min(ans1, ans2) << "\n";
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | from sys import stdin
import sys
import math
n = int(input())
a = list(map(int, stdin.readline().rstrip().split()))
pair = 0
odd = 0
for i in range(0, len(a), 2):
pair += a[i]
for i in range(1, len(a), 2):
odd += a[i]
#print(odd)
#print(pair)
count = 0
current_sum = 0
for i in range(len(a)):
## odd
if i % 2 == 1 and odd >= pair:
if current_sum + a[i] < 1:
diff = 1 - (current_sum + a[i])
a[i] += diff
count += diff
## odd
elif i % 2 == 1 and odd < pair:
if current_sum + a[i] > -1:
diff = -1 - (current_sum + a[i])
a[i] += diff
count += -1 * diff
## pair
elif i % 2 == 0 and odd >= pair:
if current_sum + a[i] > -1:
diff = -1 - (current_sum + a[i])
a[i] += diff
count += -1 * diff
elif i % 2 == 0 and odd < pair:
if current_sum + a[i] < 1:
diff = 1 - (current_sum + a[i])
a[i] += diff
count += diff
else:
print("error")
current_sum += a[i]
print(count)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | # input
n = int(input())
A = list(map(int, input().split()))
# 偶数番目が+
A_even = A
S_even = 0
ans_even = 0
for i in range(n):
if (S + A_even[i]) * ((-1) ** (i - 1)) > 0:
continue
ans_even += S + A_even[i] + 1
A_even[i] = (-1) ** (i - 1) - S
S = (-1) ** (i - 1)
# 奇数番目が+
A_odd = A
S_odd = 0
ans_odd = 0
for i in range(n):
if (S + A_odd[i]) * ((-1) ** i) > 0:
continue
ans_odd += S + A_odd[i] + 1
A_odd[i] = (-1) ** i - S
S = (-1) ** i
ans = min(ans_even, ans_odd)
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
long long n;
cin >> n;
vector<long long> a(n);
for (long long i = (0); i < (n); i++) cin >> a[i];
long long sum = a[0], ans = 0;
if (a[0] == 0) sum++, ans++;
for (long long i = (1); i < (n); i++) {
long long nxt = sum + a[i];
if (nxt == 0) {
if (i == n - 1)
ans++;
else if (a[i + 1] > 0)
nxt--, ans++;
else
nxt++, ans++;
} else if (nxt * sum >= 0) {
if (nxt < 0) {
ans += 1 - nxt;
nxt = 1;
} else {
ans += nxt + 1;
nxt = -1;
}
}
sum = nxt;
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; n > i; i++) cin >> a[i];
int sh = 0;
int nw = 0;
for (int i = 0; n > i; i++) {
nw += a[i];
if (i % 2 == 0)
if (nw <= 0) sh += 1 - nw, nw = 1;
if (i % 2 == 1)
if (nw >= 0) sh += nw + 1, nw = -1;
}
int hs = 0;
nw = 0;
for (int i = 0; n > i; i++) {
nw += a[i];
if (i % 2 == 1)
if (nw <= 0) hs += 1 - nw, nw = 1;
if (i % 2 == 0)
if (nw >= 0) hs += nw + 1, nw = -1;
}
cout << min(hs, sh) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
int sum[n];
int count = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sum[0] = a[0];
for (int i = 0; i < n - 1; i++) {
if (a[0] == 0) {
int num = 0;
int j = 0;
while (a[j] == 0) {
num++;
j++;
}
if ((num % 2 == 1 && a[num] > 0) || (num % 2 == 0 && a[num] < 0)) {
sum[0]--;
}
if ((num % 2 == 1 && a[num] < 0) || (num % 2 == 0 && a[num] > 0)) {
sum[0]++;
}
}
sum[i + 1] = sum[i] + a[i + 1];
if (sum[i] > 0) {
while (sum[i + 1] >= 0) {
sum[i + 1]--;
count++;
}
}
if (sum[i] < 0) {
while (sum[i + 1] <= 0) {
sum[i + 1]++;
count++;
}
}
}
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 | UNKNOWN | package main
import (
"bufio"
"fmt"
"os"
"strconv"
)
func out(x ...interface{}) {
fmt.Println(x...)
}
var sc = bufio.NewScanner(os.Stdin)
func getInt() int {
sc.Scan()
i, e := strconv.Atoi(sc.Text())
if e != nil {
panic(e)
}
return i
}
func getString() string {
sc.Scan()
return sc.Text()
}
func f(a int) int {
if a > 0 {
return 1
} else if a < 0 {
return -1
}
return 0
}
func min(a, b int) int {
if a > b {
return b
}
return a
}
func main() {
sc.Split(bufio.ScanWords)
n := getInt()
a := make([]int, n)
for i := 0; i < n; i++ {
a[i] = getInt()
}
sum := 0
sign := -1
ans := [2]int{0, 0}
for k := 0; k < 2; k++ {
if k == 0 {
sign = -1
} else {
sign = 1
}
for i := 0; i < n; i++ {
sum += a[i]
if sign == 1 {
if sum >= 0 {
ans[k] += 1 + sum
sum = -1
}
} else {
if sum <= 0 {
ans[k] += 1 - sum
sum = 1
}
}
sign = -sign
}
}
fmt.Println(min(ans[0], ans[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 | python3 | def count_ops(a):
if a[0] == 0:
total_ops = 1
if a[1] < 0:
accum = 1
else:
accum = -1
else:
total_ops = 0
accum = a[0]
for i in range(1, len(a)):
new_accum = accum + a[i]
if (accum > 0 and new_accum > 0):
ops = ops_needed(new_accum, -1)
elif (accum > 0 and new_accum == 0):
ops = -1
elif (accum < 0 and new_accum < 0):
ops = ops_needed(new_accum, 1)
elif (accum < 0 and new_accum == 0):
ops = 1
else:
ops = 0
total_ops += abs(ops)
accum = new_accum + ops
return total_ops
if __name__ == '__main__':
_ = int(input())
a = list(map(int, input().split()))
print(count_ops(a))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long sum;
cin >> sum;
long long ans = 0;
for (int i = 1; i < n; i++) {
int a;
cin >> a;
if (sum > 0) {
if (sum + a >= 0) {
ans += 1 + a + sum;
sum = -1;
} else {
sum += a;
}
} else {
if (sum + a <= 0) {
ans += 1 - a - sum;
sum = 1;
} else {
sum += a;
}
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
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};
ll n;
ll solve(vector<ll> a) {
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];
}
return ans;
}
signed main() {
ios::sync_with_stdio(false);
cin >> n;
vector<ll> a;
for (ll i = 0; i < n; i++) {
ll x;
cin >> x;
a.push_back(x);
}
ll start = a[0];
auto ac = a;
ll fa1 = solve(a);
ac[0] = ac[0] *= -1;
ll fa2 = solve(ac);
fa2 += start + 1;
cout << min(fa1, fa2) << 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 | # coding: utf-8
# Here your code
N = int(input())
a = [int(i) for i in input().split()]
result_1 = 0
before_sum =a[0]
after_sum =a[0]
for i in range(1,N):
before_sum = after_sum
after_sum = before_sum + a[i]
if before_sum * after_sum > 0:
if after_sum < 0:
result_1 += 1 - after_sum
after_sum = 1
elif after_sum > 0:
result_1 += 1 + after_sum
after_sum = -1
elif before_sum * after_sum == 0:
result_1 += 1
if before_sum < 0:
after_sum = 1
else:
after_sum = -1
before_sum =int(-a[0]/abs(a[0]))
after_sum =before_sum
result_2 = 1 + abs(before_sum)
for i in range(1,N):
before_sum = after_sum
after_sum = before_sum + a[i]
if before_sum * after_sum > 0:
if after_sum < 0:
result_2 += 1 - after_sum
after_sum = 1
elif after_sum > 0:
result_2 += 1 + after_sum
after_sum = -1
elif before_sum * after_sum == 0:
result_2 += 1
if before_sum < 0:
after_sum = 1
else:
after_sum = -1
print(min(result_1,result_2))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.io.*;
import java.util.*;
public class Main{
static int n;
static long[] a;
static long count;
static long sum;
public static void main(String[] args) throws IOException{
MyReader r = new MyReader();
n = r.i();
a = r.ll();
sum = a[0];
count = 0;
if(a[0] == 0){
count = 1;
sum = 1;
solve();
long temp = count;
count = 1;
sum = -1;
solve();
count = Math.min(count, temp);
} else {
solve();
long temp = count;
sum = -a[1]-1;
count = Math.abs(a[0]+a[1]+1);
solve();
temp = Math.min(temp, count);
sum = -a[1]+1;
count = Math.abs(a[0]+a[1]-1);
solve();
count = Math.min(temp, count);
}
println(count);
}
static void solve(){
for(int i = 1; i < n; i++){
if(sum < 0){
if(a[i]+sum <= 0){
count += -(a[i]+sum)+1;
sum = 1;
} else
sum = a[i]+sum;
} else{
if(a[i]+sum>=0){
count += a[i]+sum+1;
sum = -1;
} else
sum = a[i]+sum;
}
}
}
static void print(Object o){
System.out.print(o.toString());
}
static void println(Object o){
System.out.println(o.toString());
}
static int Int(String s){
return Integer.parseInt(s);
}
static long Long(String s){
return Long.parseLong(s);
}
static class MyReader extends BufferedReader{
MyReader(){
super(new InputStreamReader(System.in));
}
String s() throws IOException{
return readLine();
}
String[] ss() throws IOException{
return s().split(" ");
}
int i() throws IOException{
return Int(s());
}
int[] ii() throws IOException{
String[] ss = ss();
int size = ss.length;
int[] ii = new int[size];
for(int j = 0; j < size; j++) ii[j] = Integer.parseInt(ss[j]);
return ii;
}
long l() throws IOException{
return Long(s());
}
long[] ll() throws IOException{
String[] ss = ss();
int size = ss.length;
long[] ll = new long[size];
for(int j = 0; j < size; j++) ll[j] = Long.parseLong(ss[j]);
return ll;
}
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int sign(int x) { return x < 0 ? -1 : 1; }
int main() {
int n;
cin >> n;
int cnt = 0;
int sum;
cin >> sum;
for (int i = 1; i < n; i++) {
int a;
cin >> a;
int newsum = sum + a;
if (sign(sum) == sign(newsum)) {
int m = min(abs(sum), abs(newsum));
newsum -= sign(sum) * (m + 1);
cnt += m + 1;
}
sum = newsum;
}
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>
#include<iostream>
#include<cmath>
#include<cstdio>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<utility>
#include<algorithm>
#include<cstring>
#include<sstream>
#include <iomanip>
using namespace std;
typedef long long ll ;
typedef double db;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector< pair<int,int> > vii;
const double pi = 2*acos(0) ;
#define pf printf
#define sf scanf
#define pb(a) push_back(a)
#define mp make_pair
#define fi first
#define se second
#define for0(i,n) for(int i=0;i<n;i++)
#define for1(i,n) for(int i=1;i<=n;i++)
#define forab(i,a,b) for(int i=a;i<=b;i++)
#define lcm(a, b) ((a)*((b)/gcd(a,b)))
#define sq(a) (a)*(a)
#define nw "\n"
#define abs(x) fabs(x)
#define pcase(z,x) cout<<"Case "<<z<<": "<<x<<"\n"
int main()
{
int n;
cin>>n;
vi a(n+1);
for1(i,n) cin>>a[i�];
int plus=0,s=0;
for1(i,n){
s += a[i];
if(i%2){
if(s<=0){
plus += 1-s;
s = 1;
}
}
else{
if(s>=0){
plus += s+1;
s = -1;
}
}
}
int minus=0;
s=0;
for1(i,n)�{
s += a[i];
if(i%2){
if(s>=0){
minus += s+1;
s = -1;
}
}
else{
if(s<=0){
minus += 1-s;
s=1;
}
}
}
//cout<<plus<<" "<<minus<<nw;
int ans = min(plus,minus);
cout<<ans<<nw;
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())
sum_b = 0
cnt_b = 0
sum_c = 0
cnt_c = 0
for i,a in enumerate(A):
sum_b += a
if (sum_b > 0) != ((i % 2) > 0):
cnt_b += abs(sum_b) + 1
sum_b = 1 if ((i % 2) > 0) else -1
sum_c += a
if (sum_c > 0) == ((i % 2) > 0):
cnt_c += abs(sum_c) + 1
sum_c = -1 if ((i % 2) > 0) else 1
print(min(cnt_b, cnt_c))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java |
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++)a[i]=sc.nextInt();
int sum=0;
int count=0;
for(int i=0;i<n-1;i++){
sum+=a[i];
if(sum>0){
while(sum+a[i+1]>=0){
a[i+1]--;
count++;
}
}else if(sum<0){
while(sum+a[i+1]<=0){
a[i+1]++;
count++;
}
}
}
System.out.println(count);
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
int main() {
int n;
cin >> n;
ll a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
ll sum = a[0], ans = 0;
for (int i = 1; i < n; i++) {
if (sum < 0) {
if (sum + a[i] > 0) {
sum += a[i];
} else {
ans += abs(1 - sum - a[i]);
sum = 1;
}
} else {
if (sum + a[i] < 0) {
sum += a[i];
} else {
ans += abs(-1 - sum - a[i]);
sum = -1;
}
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n=int(input())
a=list(map(int,input().split()))
b=a[:]
for i in range(n):
if i%2:b[i]=min(-sum(b[j] for j in range(i))-1,b[i])
else: b[i]=max(-sum(b[j] for j in range(i))+1,b[i])
s=sum(abs(b[i]-a[i]) for i in range(n))
c=a[:]
for i in range(n):
if i%2:c[i]=max(-sum(c[j] for j in range(i))+1,c[i])
else: c[i]=min(-sum(c[j] for j in range(i))-1,c[i])
t=sum(abs(c[i]-a[i]) for i in range(n))
print(min(s,t)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n), s(n);
for (int i = 0; i < n; i++) cin >> a[i];
s[0] = max(a[0], 1);
int retval_p = (a[0] <= 0) ? abs(a[0]) + 1 : 0;
for (int i = 1; i < n; i++) {
s[i] = a[i] + s[i - 1];
if (i % 2 == 1 && s[i] >= 0) {
retval_p += abs(s[i]) + 1;
s[i] = -1;
} else if (i % 2 == 0 && s[i] <= 0) {
retval_p += abs(s[i]) + 1;
s[i] = 1;
}
}
s[0] = min(a[0], -1);
int retval_m = (a[0] >= 0) ? abs(a[0]) + 1 : 0;
for (int i = 1; i < n; i++) {
s[i] = a[i] + s[i - 1];
if (i % 2 == 1 && s[i] <= 0) {
retval_m += abs(s[i]) + 1;
s[i] = 1;
} else if (i % 2 == 0 && s[i] >= 0) {
retval_m += abs(s[i]) + 1;
s[i] = -1;
}
}
cout << min(retval_p, retval_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 | cpp | #include <bits/stdc++.h>
using namespace std;
int n;
long long _count = 0;
long long sum, _sum;
bool _plus = false;
int main() {
cin >> n;
vector<int> a(n, 0);
for (int i = 0; i < n; i++) cin >> a[i];
sum = a[0];
if (sum > 0) _plus = true;
for (int i = 1; i < n; i++) {
_sum = sum;
sum = sum + a[i];
if (_plus) {
if (sum >= 0) {
_count = _count + abs(a[i] - (-_sum - 1));
a[i] = -_sum - 1;
sum = _sum + a[i];
}
_plus = false;
} else {
if (sum <= 0) {
_count = _count + abs(a[i] - (-_sum + 1));
a[i] = -_sum + 1;
sum = _sum + a[i];
}
_plus = true;
}
}
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 | UNKNOWN | using System;
using System.Collections.Generic;
using System.Linq;
namespace AtCoder
{
class Program
{
static int n;
static void Main(string[] args)
{
//[summary]C - Green Bin
n = int.Parse(Console.ReadLine());
var a = ReadLine();
long count = 0;
if (a[0] == 0)
{
var a1 = new List<int>(a);
a1[0] = 1;
long count1 = CountOperactions(a1);
var a2 = new List<int>(a);
a2[0] = -1;
long count2 = CountOperactions(a2);
if (count1 < count2)
{
count = count1 + 1;
}
else
{
count = count2 + 1;
}
}
else
{
count = CountOperactions(a);
}
Console.WriteLine(count);
}
static long CountOperactions(List<int> a)
{
long sum = a[0];
long next = 0;
long count = 0;
for (int i = 1; i < n; i++)
{
next = sum + a[i];
if ((sum > 0 && next < 0) | (sum < 0 && next > 0))
{
//何もしない
}
else if (sum > 0)
{
count += 1 + Math.Abs(next);
next = -1;
}
else
{
count += 1 + Math.Abs(next);
next = 1;
}
sum = next;
}
return count;
}
static List<int> ReadLine()
{
var line = Console.ReadLine();
var array = line.Split(' ');
return array.Select(x => int.Parse(x)).ToList();
}
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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 DIM = 1e5 + 5;
long long arr[DIM];
inline long long solve(int n) {
long long sum(arr[1]), ans(0);
for (int i = 2; i <= n; i++) {
if (sum < 0 && sum + arr[i] <= 0)
ans += 1 - sum - arr[i], sum = 1;
else if (sum > 0 && sum + arr[i] >= 0)
ans += 1 + sum + arr[i], sum = -1;
else
sum += arr[i];
}
return ans;
}
int main(void) {
int n;
cin >> n;
long long ans(1e18);
for (int i = 1; i <= n; i++) cin >> arr[i];
if (arr[1] != 0)
ans = solve(n);
else {
arr[1] = -1;
ans = min(ans, solve(n) + 1);
arr[1] = +1;
ans = min(ans, solve(n) + 1);
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> x;
int temp, ans = 0;
for (int i = 0; i != n; ++i) {
cin >> temp;
x.push_back(temp);
}
if (!x[0]) {
x[0] = 1;
++ans;
int val, ind;
for (int i = 0; i != n; ++i) {
if (!x[i]) {
val = x[i];
ind = i;
break;
}
}
if ((val > 0 && ind % 2) || (val < 0 && !(ind % 2))) x[0] = -1;
}
int sum = x[0];
for (int i = 1; i != n; ++i) {
int sum2 = sum + x[i];
if (sum * sum2 >= 0) {
ans += abs(sum2) + 1;
if (sum < 0)
sum2 = 1;
else
sum2 = -1;
}
sum = sum2;
}
cout << ans;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int min(int x, int y) { return x < y ? x : y; }
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < (n); i++) cin >> a[i];
int sum = 0;
int cost = 0;
for (int i = 0; i < n; ++i) {
sum += a[i];
if (i % 2 == 0) {
if (sum <= 0) {
cost += -sum + 1;
sum = 1;
}
} else {
if (sum >= 0) {
cost += sum + 1;
sum = -1;
}
}
}
long long sum2 = 0;
long long cost2 = 0;
for (int i = 0; i < n; ++i) {
sum2 += a[i];
if (i % 2 == 0) {
if (sum2 >= 0) {
cost2 += sum2 + 1;
sum2 = -1;
}
} else {
if (sum2 <= 0) {
cost2 += -sum2 + 1;
sum2 = 1;
}
}
}
long long ans = min(cost, cost2);
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 + 10;
int a[maxn];
int main() {
int n;
long long sum;
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
sum = a[0];
long long cnt = 0;
if (sum == 0) {
int i = 1;
while (a[i] == 0 && i < n) i++;
if (i == n) {
cnt = (n - 1) * 2 + 1;
printf("%lld\n", cnt);
return 0;
}
if (a[i] > 0 && i & 1 == 1) {
sum = -1;
cnt++;
} else if (a[i] > 0 && i & 1 == 0) {
sum = 1;
cnt++;
} else if (a[i] < 0 && i & 1 == 1) {
sum = 1;
cnt++;
} else if (a[i] < 0 && i & 1 == 0) {
sum = -1;
cnt++;
}
}
for (int i = 1; i < n; i++) {
if (sum > 0) {
int t = sum + a[i];
if (t < 0)
sum = t;
else {
int b = abs(t + 1);
cnt += b;
sum = -1;
a[i] -= b;
}
} else if (sum < 0) {
int t = sum + a[i];
if (t > 0)
sum = t;
else {
int b = abs(1 - t);
cnt += b;
sum = 1;
a[i] += b;
}
}
}
printf("%lld\n", cnt);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | def sign(x):
return (x > 0) - (x < 0)
def main():
n = int(input())
a = [int(an) for an in input().split()]
total = a[0]
sign_total = sign(a[0])
ans = 0
for i in range(1, n):
total += a[i]
if a[i] == 0:
total += sign_total * -1
ans += 1
sign_tmp = sign(total)
if total == 0 or sign_total == sign_tmp:
val = total + sign_total
ans += val * sign_total
total -= val
sign_total *= -1
print(ans)
if __name__ == "__main__":
main()
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long MOD = 1000000007;
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long sum = a[0];
long long ans = 0;
for (int i = 1; i < n; i++) {
long long t = sum + a[i];
if ((sum >= 0 && t < 0) || (sum < 0 && t >= 0)) {
sum = t;
if (sum == 0) {
ans++;
sum = 1;
}
continue;
}
long long at;
if (sum >= 0)
at = -1 - sum;
else
at = 1 - sum;
ans = ans + abs(a[i] - at);
a[i] = at;
sum = sum + a[i];
}
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() {
long long n;
scanf("%d", &n);
long long a[n];
for (int i = 0; i < n; i++) scanf(" %d", &a[i]);
int S = a[0];
int j = 0;
for (int i = 1; i < n; i++) {
if (S * (S + a[i]) < 0) {
S += a[i];
} else {
if (S < 0) {
j += 1 - S - a[i];
S = 1;
} else {
j += S + a[i] + 1;
S = -1;
}
}
}
printf("%d\n", j);
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.