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 = [int(x) for x in input().split()] # 数列の各項
# print(a)
cnt = 0 # 操作回数
while True:
sum = a[0] # 1列目までの和
# 1~N列目までの和を求める
# print ('和 → ' + str(sum), end = ' ')
# print (str(sum), end = ' ')
for i in range(1, len(a)):
pre = sum
sum += a[i]
# print (str(sum), end = ' ')
# 条件を満たしてない場合は和を求めるのを中断する
if (pre > 0 and sum > 0) or (pre < 0 and sum < 0) or (sum == 0):
break
# print('')
# print('i = ' + str(i))
# print('pre = ' + str(pre) + ', sum = ', str(sum))
if (pre > 0 and sum < 0) or (pre < 0 and sum > 0):
# 条件を満たした場合の処理
# print(a)
print(str(cnt))
break
else:
# 操作が必要な場合の処理
if pre < 0:
j = 1
elif pre > 0:
j = -1
if pre < -1 or pre > 1:
# preの絶対値が1でない場合(i - 1番目を操作する)
if pre < 0 and i != len(a) - 1:
j = -(pre + 1)
elif pre > 0 and i != len(a) - 1:
j = -(pre - 1)
a[i - 1] += j
else:
# preの絶対値が1の場合(i番目を操作する)
if pre < 0:
j = -(sum - 1)
elif pre > 0:
j = -(sum + 1)
a[i] += j
# 操作回数の更新
if j < 0:
j = -j
cnt += j
# print ('cnt = ' + str(cnt))
# print (a) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
A = list(map(int, input().split()))
S = [0] * (N+1)
for i in range(N):
S[i+1] = S[i] + A[i]
print(S)
#S[1] > 0
ans1 = 0
work = 0
for i in range(1,N+1):
if i%2 == 1:
# pos is required
if S[i] + work <= 0:
temp = 1 - (S[i] + work)
ans1 += temp
work += temp
else:
# neg is required
if S[i] + work >= 0:
temp = (S[i] + work) + 1
ans1 += temp
work -= temp
#S[1] < 0
ans2 = 0
work = 0
for i in range(1,N+1):
if i%2 == 1:
# neg is required
if S[i] + work >= 0:
temp = (S[i] + work) + 1
ans2 += temp
work -= temp
else:
# neg is required
if S[i] + work <= 0:
temp = 1 - (S[i] + work)
ans2 += temp
work += temp
print(min(ans1, ans2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
long long sum = a[0];
long long cnt = 0;
if (sum == 0) {
sum = (a[1] > 0 ? -1 : 1);
cnt++;
}
for (int i = 1; i < n; i++) {
long long nsum = sum + a[i];
if (sum > 0 && nsum < 0 || sum < 0 && nsum > 0) {
sum = nsum;
continue;
}
sum = (sum > 0 ? -1 : 1);
cnt += (nsum == 0 ? 1 : abs(nsum) + 1);
}
cout << cnt << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <bits/stdc++.h>
int main(void) {
int n, i, check = 0;
long long int a, count = 0, sum = 0;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%lld", &a);
if (i == 0 && a == 0) {
count = 1;
sum = 1;
check = 1;
continue;
}
sum += a;
if (check == 1 && sum >= 0) {
count += (1 + sum);
sum = -1;
} else if (check == -1 && sum <= 0) {
count += (1 - sum);
sum = 1;
}
if (sum >= 0) {
check = 1;
} else {
check = -1;
}
}
printf("%lld", count);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
int main() {
int N;
cin >> N;
long long ans = 0;
long long sum[N];
for (int i = 0; i < N; i++) {
long long t;
cin >> t;
if (i == 0) {
sum[i] = t;
continue;
}
sum[i] = sum[i - 1] + t;
if (sum[i - 1] > 0) {
if (sum[i] > 0) {
ans += sum[i] + 1;
sum[i] = -1;
}
} else {
if (sum[i] < 0) {
ans += -sum[i] + 1;
sum[i] = 1;
}
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int A[n];
for (int i = 0; i < n; i++) {
cin >> A[i];
}
int countA = 0;
int countB = 0;
int part = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0 && A[i] + part <= 0) {
countA += 1 - (A[i] + part);
part = 1;
} else if (i % 2 == 1 && A[i] + part >= 0) {
countA += A[i] + part + 1;
part = -1;
} else
part += A[i];
}
for (int i = 0; i < n; i++) {
if (i % 2 == 0 && A[i] + part >= 0) {
countB += A[i] + part + 1;
part = -1;
} else if (i % 2 == 1 && A[i] + part <= 0) {
countB += 1 - (A[i] + part);
part = 1;
} else
part += A[i];
}
cout << countA << endl;
cout << countB << 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()))
sum = a[0]
s = [a[0]]
cnt = 0
for i in range(1,n):
sum = sum + a[i]
if sum*s[i-1] < 0:
s.append(sum)
else:
if sum > 0:
cnt = cnt + (sum+1)
sum = -1
elif sum < 0:
cnt = cnt + (-1)*(sum-1)
sum = 1
elif sum == 0:
if s[i-1]<0:
cnt = cnt + 1
sum = 1
elif s[i-1]>0:
cnt = cnt + 1
sum = -1
s.append(sum)
print(cnt) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
L = list(map(int, input().split()))
#print(L)
sum = L[0]
count = 0
for i in range(1, N):
if sum * (sum + L[i]) >= 0:
count = count + (abs(sum + L[i]) + 1)
if sum > 0 and sum + L[i] >= 0:
sum = -1
else:
sum = 1
else:
sum = sum + L[i]
#print(sum)
print(count)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
A = list(map(int, input().split()))
ans = 1e9
for sign in (1, -1):
s = sign
res, acc = 0, 0
for a in A:
acc += a
if acc * s <= 0:
res += abs(acc-s)
acc = s
s *= -1
ans = min(ans, res)
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n + 1];
for (int i = 1; i <= n; i++) cin >> a[i];
int GS = 0;
int GC = 0;
int KS = 0;
int KC = 0;
for (int i = 1; i <= n; i++) {
GS += a[i];
if (i % 2 == 1) {
if (GS >= 1) {
} else {
GC += 1 - GS;
GS = 1;
}
} else {
if (GS >= 1) {
GC += 1 + GS;
GS = -1;
}
}
KS += a[i];
if (i % 2 != 1) {
if (KS >= 1) {
} else {
KC += 1 - KS;
KS = 1;
}
} else {
if (KS >= 1) {
KC += 1 + KS;
KS = -1;
}
}
}
int Ans = min(KC, GC);
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 | java | import java.util.Scanner;
/**
* https://abc059.contest.atcoder.jp/tasks/arc072_a
*/
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = Integer.parseInt(sc.next());
long[] a = new long[N];
for(int i=0; i<N; i++) a[i] = sc.nextLong();
sc.close();
long sum = a[0];
long ans = 0;
for(int i=1; i<N; i++){
if(sum>0 && sum+a[i]>=0){
ans += Math.abs(a[i]+sum)+1;
sum = -1;
}else if(sum<0 && sum+a[i]<=0){
ans += Math.abs(a[i]+sum)+1;
sum = 1;
}else{
sum = sum + a[i];
}
}
System.out.println(ans);
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | import java.util.Scanner
object Main extends App {
val sc = new Scanner(System.in)
val n = sc.nextInt()
val a = new Array[Int](n)
for(i <- 0 until n)
a(i) = sc.nextInt()
var cnt = 0
var ans = 0
for(i <- 0 until n){
cnt += a(i)
if(i % 2 == 0){
if(cnt < 1){
ans += 1 - cnt
cnt = 1
}
}
else{
if(cnt > -1){
ans += cnt - (-1)
cnt = -1
}
}
}
cnt = 0
var ans2 = 0
for(i <- 0 until n){
cnt += a(i)
if(i % 2 != 0){
if(cnt < 1){
ans2 += 1 - cnt
cnt = 1
}
}
else{
if(cnt > -1){
ans2 += cnt - (-1)
cnt = -1
}
}
}
println(Math.min(ans, 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() {
long long n;
cin >> n;
long long l1[n + 1];
long long x = 0, s = 0;
for (int i = 1; i <= n; i++) {
cin >> l1[i];
x += l1[i];
if (i == 1 && l1[i] == 0 && l1[i + 1] <= 0)
x++, s++, l1[i] = 1;
else if (i == 1 && l1[i] == 0 && l1[i + 1] > 0)
x--, s++, l1[i] = -1;
if (i >= 2) {
if (x - l1[i] <= 0 && x <= 0) {
s += (long long)abs(-(x - l1[i]) + 1 - l1[i]);
x = 1;
} else if (x - l1[i] >= 0 && x >= 0) {
s += (long long)abs(-(x - l1[i]) - 1 - l1[i]);
x = -1;
}
}
}
cout << s << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
long long a[100005], dp[100005];
cin >> n;
long long sum = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
dp[i] = sum;
}
long long diff = 0, ans = 0;
if (dp[0] == 0) diff--, ans++;
for (int i = 1; i < n; i++) {
if (dp[i] + diff == 0) {
if (dp[i - 1] + diff < 0) diff++, ans++;
if (dp[i - 1] + diff > 0) diff--, ans++;
continue;
}
if ((dp[i - 1] + diff) / llabs(dp[i - 1] + diff) ==
(dp[i] + diff) / llabs(dp[i] + diff)) {
if (dp[i] + diff >= 0) {
ans += llabs(dp[i] + diff) + 1;
diff -= llabs(dp[i] + diff) + 1;
} else {
ans += llabs(dp[i] + diff) + 1;
diff += llabs(dp[i] + diff) + 1;
}
}
}
long long tans = ans;
diff = 0, ans = 0;
if (dp[0] == 0) diff++, ans++;
for (int i = 1; i < n; i++) {
if (dp[i] + diff == 0) {
if (dp[i - 1] + diff < 0) diff++, ans++;
if (dp[i - 1] + diff > 0) diff--, ans++;
continue;
}
if ((dp[i - 1] + diff) / llabs(dp[i - 1] + diff) ==
(dp[i] + diff) / llabs(dp[i] + diff)) {
if (dp[i] + diff >= 0) {
ans += llabs(dp[i] + diff) + 1;
diff -= llabs(dp[i] + diff) + 1;
} else {
ans += llabs(dp[i] + diff) + 1;
diff += llabs(dp[i] + diff) + 1;
}
}
}
cout << min(tans, 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 | n = gets.chomp.to_i
a = gets.chomp.split(" ").map(&:to_i)
ans = 0
cumulative_sum = [0]
n.times do |i|
tmp = cumulative_sum[i] + a[i]
if tmp == 0 then
ans += 1
if cumulative_sum[i] == 0 then
if a[i+1] < 0 then
cumulative_sum << -1
else
cumulative_sum << 1
end
else
if 0 < cumulative_sum[i] then
cumulative_sum << -1
elsif 0 > cumulative_sum[i] then
cumulative_sum << 1
end
end
else
if 0 < tmp && 0 < cumulative_sum[i] then
ans += tmp + 1
cumulative_sum << -1
elsif 0 > tmp && 0 > cumulative_sum[i] then
ans += 1 - tmp
cumulative_sum << 1
else
cumulative_sum << tmp
end
end
end
puts ans |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
int count = 0;
cin >> N;
int a[N];
for (int i = 0; i < N; i++) {
cin >> a[i];
}
for (int k = 0; k < N; k++) {
int num = 0;
int sum = 0;
for (int i = 0; i < N; i++) {
num += a[i];
}
for (int i = 0; i < N - 1; i++) {
sum += a[i];
}
if (num <= 0) {
a[0] += 1;
count++;
}
if ((sum >= 0) && (num >= 0)) {
a[N] -= 1;
count++;
} else if ((sum <= 0) && (num <= 0)) {
a[N] += 1;
count++;
}
if (((sum > 0) && (num < 0)) || ((sum < 0) && (num > 0))) break;
}
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;
std::vector<int64_t> data(n);
for (int i = 0; i < n; i++) {
cin >> data.at(i);
}
int answer = 0;
int sum_a = data.at(0);
for (int i = 1; i < n; i++) {
sum_a += data.at(i);
if (data.at(0) > 0) {
if (i % 2 != 0 && sum_a >= 0) {
while (sum_a >= 0) {
sum_a--;
answer++;
}
}
if (i % 2 == 0 && sum_a <= 0) {
while (sum_a <= 0) {
sum_a++;
answer++;
}
}
}
if (data.at(0) < 0) {
if (i % 2 != 0 && sum_a <= 0) {
while (sum_a <= 0) {
sum_a++;
answer++;
}
}
if (i % 2 == 0 && sum_a >= 0) {
while (sum_a >= 0) {
sum_a--;
answer++;
}
}
}
}
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 | UNKNOWN | using System;
using System.Text;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using static System.Console;
using static System.Math;
namespace AtCorder
{
public class Program
{
public static void Main(string[] args)
{
new Program().Solve(new ConsoleInput(Console.In, ' '));
}
public void Solve(ConsoleInput cin)
{
var n = cin.ReadInt;
var a = cin.ReadLongArray(n);
var ans = 0L;
var pre = a[0];
for(int i = 1; i < n; i++)
{
var now = pre + a[i];
if(pre * now < 0)
{
pre += a[i];
continue;
}
if(now > 0)
{
ans += now + 1;
a[i] -= now + 1;
}
else if(now < 0)
{
ans += 1 - now;
a[i] += 1 - now;
}
else
{
if(i < n - 1)
{
if(a[i + 1] < 0)
{
a[i]++;
}
else
{
a[i]--;
}
}
else
{
a[i]++;
}
ans++;
}
pre += a[i];
}
WriteLine(ans);
}
public long C(int X, int Y)
{
if (Y == 0 || Y == X)
{
return 1;
}
if (X < Y)
{
return 0;
}
var Pascal = new long[X + 1, X + 1];
for (int i = 0; i <= X; i++)
{
Pascal[i, 0] = 1L;
Pascal[i, i] = 1L;
}
for (int i = 2; i <= X; i++)
{
for (int j = 1; j < i; j++)
{
Pascal[i, j] = Pascal[i - 1, j] + Pascal[i - 1, j - 1];
}
}
return Pascal[X, Y];
}
public class ConsoleInput
{
private readonly System.IO.TextReader _stream;
private char _separator = ' ';
private Queue<string> inputStream;
public ConsoleInput(System.IO.TextReader stream, char separator = ' ')
{
this._separator = separator;
this._stream = stream;
inputStream = new Queue<string>();
}
public string Read
{
get
{
if (inputStream.Count != 0) return inputStream.Dequeue();
string[] tmp = _stream.ReadLine().Split(_separator);
for (int i = 0; i < tmp.Length; ++i)
inputStream.Enqueue(tmp[i]);
return inputStream.Dequeue();
}
}
public string ReadLine { get { return _stream.ReadLine(); } }
public int ReadInt { get { return int.Parse(Read); } }
public long ReadLong { get { return long.Parse(Read); } }
public double ReadDouble { get { return double.Parse(Read); } }
public string[] ReadStrArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret; }
public int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret; }
public long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret; }
}
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #!/usr/bin/env ruby
STDIN.gets.chomp.to_i
array = STDIN.gets.chomp.split(' ').map(&:to_i)
def get_answer(first, array)
ans = 0
sum = first
array.each do |a|
if sum >= 0
if sum + a < 0
sum += a
else
ans += (-1 - (sum + a)).abs
sum = -1
end
else # sumがマイナス
if sum + a > 0
sum += a
else
ans += (1 - (sum + a)).abs
sum = 1
end
end
#puts "#{i}: sum = #{sum}, ans = #{ans}"
end
return ans
end
first = array.shift
if first == 0
ans = [get_answer(1, array), get_answer(-1, array)].min
else
ans = get_answer(first, array)
end
puts ans
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import numpy
n=int(input())
a=[int(i) for i in input().split()]
ans=0
sum=0
if a[0]==0:
a[0]=1
ans=1
sum=1
for j in a[1:]:
if numpy.sign(sum)==numpy.sign(sum+j) or numpy.sign(sum+j)==0:
ans+=abs(sum+j)+1
sum=-numpy.sign(sum)
else:
sum+=j
pans=ans
a[0]=-1
ans=1
sum=-1
for j in a[1:]:
if numpy.sign(sum)==numpy.sign(sum+j) or numpy.sign(sum+j)==0:
ans+=abs(sum+j)+1
sum=-numpy.sign(sum)
else:
sum+=j
mans=ans
ans=min(pans,mans)
else:
for j in a:
if numpy.sign(sum)==numpy.sign(sum+j) or numpy.sign(sum+j)==0:
ans+=abs(sum+j)+1
sum=-numpy.sign(sum)
else:
sum+=j
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
inline long long toLongLong(string s) {
long long v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T>
inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
inline vector<char> toVC(string s) {
vector<char> data(s.begin(), s.end());
return data;
}
template <typename List>
void SPRIT(const std::string &s, const std::string &delim, List &result) {
result.clear();
string::size_type pos = 0;
while (pos != string::npos) {
string::size_type p = s.find(delim, pos);
if (p == string::npos) {
result.push_back(s.substr(pos));
break;
} else {
result.push_back(s.substr(pos, p - pos));
}
pos = p + delim.size();
}
}
string TRIM(const string &str, const char *trimCharacterList = " \t\v\r\n") {
string result;
string::size_type left = str.find_first_not_of(trimCharacterList);
if (left != string::npos) {
string::size_type right = str.find_last_not_of(trimCharacterList);
result = str.substr(left, right - left + 1);
}
return result;
}
template <typename T>
bool VECTOR_EXISTS(vector<T> vec, T data) {
auto itr = std::find(vec.begin(), vec.end(), data);
size_t index = distance(vec.begin(), itr);
if (index != vec.size()) {
return true;
} else {
return 0;
}
}
double ceil_n(double dIn, int nLen) {
double dOut;
dOut = dIn * pow(10.0, nLen);
dOut = (double)(int)(dOut + 0.9);
return dOut * pow(10.0, -nLen);
}
double floor_n(double dIn, int nLen) {
double dOut;
dOut = dIn * pow(10.0, nLen);
dOut = (double)(int)(dOut);
return dOut * pow(10.0, -nLen);
}
double round_n(double dIn, int nLen) {
double dOut;
dOut = dIn * pow(10.0, nLen);
dOut = (double)(int)(dOut + 0.5);
return dOut * pow(10.0, -nLen);
}
int take_a_n(int num, int n) {
string str = toString(num);
return str[str.length() - n] - '0';
}
int strbase_2to10(const std::string &s) {
int out = 0;
for (int i = 0, size = s.size(); i < size; ++i) {
out *= 2;
out += ((int)s[i] == 49) ? 1 : 0;
}
return out;
}
int strbase_10to2(const std::string &s) {
int binary = toInt(s);
int out = 0;
for (int i = 0; binary > 0; i++) {
out = out + (binary % 2) * pow(static_cast<int>(10), i);
binary = binary / 2;
}
return out;
}
int strbase_16to10(const std::string &s) {
int out = stoi(s, 0, 16);
return out;
}
int intbase_2to10(int in) {
string str = toString(in);
return strbase_2to10(str);
}
int intbase_10to2(int in) {
string str = toString(in);
return strbase_10to2(str);
}
int intbase_16to10(int in) {
string str = toString(in);
return strbase_16to10(str);
}
string intbase_10to16(unsigned int val, bool lower = true) {
if (!val) return std::string("0");
std::string str;
const char hc = lower ? 'a' : 'A';
while (val != 0) {
int d = val & 15;
if (d < 10)
str.insert(str.begin(), d + '0');
else
str.insert(str.begin(), d - 10 + hc);
val >>= 4;
}
return str;
}
long long bitcount64(long long bits) {
bits = (bits & 0x5555555555555555) + (bits >> 1 & 0x5555555555555555);
bits = (bits & 0x3333333333333333) + (bits >> 2 & 0x3333333333333333);
bits = (bits & 0x0f0f0f0f0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f0f0f0f0f);
bits = (bits & 0x00ff00ff00ff00ff) + (bits >> 8 & 0x00ff00ff00ff00ff);
bits = (bits & 0x0000ffff0000ffff) + (bits >> 16 & 0x0000ffff0000ffff);
return (bits & 0x00000000ffffffff) + (bits >> 32 & 0x00000000ffffffff);
}
const double EPS = 1e-10;
const double PI = acos(-1.0);
template <typename T>
inline bool BETWEEN(const T aim, const T min, const T max) {
if (min <= aim && aim <= max) {
return true;
} else {
return false;
}
}
template <class T>
inline T SQR(const T x) {
return x * x;
}
template <class T1, class T2>
inline T1 POW(const T1 x, const T2 y) {
if (!y)
return 1;
else if ((y & 1) == 0) {
return SQR(POW(x, y >> 1));
} else
return POW(x, y ^ 1) * x;
}
template <typename T>
constexpr T ABS(T x) {
static_assert(is_signed<T>::value, "ABS(): argument must be signed");
return x < 0 ? -x : x;
}
template <class BidirectionalIterator>
bool next_partial_permutation(BidirectionalIterator first,
BidirectionalIterator middle,
BidirectionalIterator last) {
reverse(middle, last);
return next_permutation(first, last);
}
template <class BidirectionalIterator>
bool next_combination(BidirectionalIterator first1, BidirectionalIterator last1,
BidirectionalIterator first2,
BidirectionalIterator last2) {
if ((first1 == last1) || (first2 == last2)) {
return false;
}
BidirectionalIterator m1 = last1;
BidirectionalIterator m2 = last2;
--m2;
while (--m1 != first1 && !(*m1 < *m2)) {
}
bool result = (m1 == first1) && !(*first1 < *m2);
if (!result) {
while (first2 != m2 && !(*m1 < *first2)) {
++first2;
}
first1 = m1;
std::iter_swap(first1, first2);
++first1;
++first2;
}
if ((first1 != last1) && (first2 != last2)) {
m1 = last1;
m2 = first2;
while ((m1 != first1) && (m2 != last2)) {
std::iter_swap(--m1, m2);
++m2;
}
std::reverse(first1, m1);
std::reverse(first1, last1);
std::reverse(m2, last2);
std::reverse(first2, last2);
}
return !result;
}
template <typename T>
constexpr bool ODD(T x) {
return x % 2 != 0;
}
template <typename T>
constexpr bool EVEN(T x) {
return x % 2 == 0;
}
template <class T>
inline T GCD(const T x, const T y) {
if (x < 0) return GCD(-x, y);
if (y < 0) return GCD(x, -y);
return (!y) ? x : GCD(y, x % y);
}
template <class T>
inline T LCM(const T x, const T y) {
if (x < 0) return LCM(-x, y);
if (y < 0) return LCM(x, -y);
return x * (y / GCD(x, y));
}
template <class T>
inline T EXTGCD(const T a, const T b, T &x, T &y) {
if (a < 0) {
T d = EXTGCD(-a, b, x, y);
x = -x;
return d;
}
if (b < 0) {
T d = EXTGCD(a, -b, x, y);
y = -y;
return d;
}
if (!b) {
x = 1;
y = 0;
return a;
} else {
T d = EXTGCD(b, a % b, x, y);
T t = x;
x = y;
y = t - (a / b) * y;
return d;
}
}
template <class T>
inline bool ISPRIME(const T x) {
if (x <= 1) return false;
for (T i = 2; SQR(i) <= x; i++)
if (x % i == 0) return false;
return true;
}
template <class T>
vector<bool> ERATOSTHENES(const T n) {
vector<bool> arr(n, true);
for (int i = 2; i < SQR(n); i++) {
if (arr[i]) {
for (int j = 0; i * (j + 2) < n; j++) {
arr[i * (j + 2)] = false;
}
}
}
return arr;
}
template <typename T>
vector<bool> ERATOSTHENES(const T a, const T b) {
vector<bool> small = ERATOSTHENES(b);
vector<bool> prime(b - a, true);
for (int i = 2; (T)(SQR(i)) < b; i++) {
if (small[i]) {
for (T j = max(2, (a + i - 1) / i) * i; j < b; j += i) {
prime[j - a] = false;
}
}
}
return prime;
}
template <class T>
vector<T> DIVISOR(T n) {
vector<T> v;
for (int i = 1; i * i <= n; ++i) {
if (n % i == 0) {
v.push_back(i);
if (i != n / i) {
v.push_back(n / i);
}
}
}
sort(v.begin(), v.end());
return v;
}
template <typename T>
T NCR(T n, T r) {
T ans = 1;
for (T i = n; i > n - r; --i) {
ans = ans * i;
}
for (T i = 1; i < r + 1; ++i) {
ans = ans / i;
}
return ans;
}
int MATRIZ_CHAIN(vector<int> &p, vector<vector<int> > &s) {
const static int INF = 1 << 20;
const int n = p.size() - 1;
vector<vector<int> > X(n, vector<int>(n, INF));
s.resize(n, vector<int>(n));
for (int i = 0; i < n; ++i) X[i][i] = 0;
for (int w = 1; w < n; ++w)
for (int i = 0, j; j = i + w, j < n; ++i)
for (int k = i; k < j; ++k) {
int f = p[i] * p[k + 1] * p[j + 1];
if (X[i][k] + X[k + 1][j] + f < X[i][j]) {
X[i][j] = X[i][k] + X[k + 1][j] + f;
s[i][j] = k;
}
}
return X[0][n - 1];
}
vector<int> LIS(const vector<int> &a) {
const static int INF = 99999999;
const int n = a.size();
vector<int> A(n, INF);
vector<int> id(n);
for (int i = 0; i < n; ++i) {
id[i] = distance(A.begin(), lower_bound(A.begin(), A.end(), a[i]));
A[id[i]] = a[i];
}
int m = *max_element(id.begin(), id.end());
vector<int> b(m + 1);
for (int i = n - 1; i >= 0; --i)
if (id[i] == m) b[m--] = a[i];
return b;
}
template <typename T>
vector<T> LCS(const vector<T> &a, const vector<T> &b) {
const int n = a.size(), m = b.size();
vector<vector<int> > X(n + 1, vector<int>(m + 1));
vector<vector<int> > Y(n + 1, vector<int>(m + 1));
for (int i = (0); i < (n); ++i) {
for (int j = (0); j < (m); ++j) {
if (a[i] == b[j]) {
X[i + 1][j + 1] = X[i][j] + 1;
Y[i + 1][j + 1] = 0;
} else if (X[i + 1][j] < X[i][j + 1]) {
X[i + 1][j + 1] = X[i][j + 1];
Y[i + 1][j + 1] = +1;
} else {
X[i + 1][j + 1] = X[i + 1][j];
Y[i + 1][j + 1] = -1;
}
}
}
vector<T> c;
for (int i = n, j = m; i > 0 && j > 0;) {
if (Y[i][j] > 0)
--i;
else if (Y[i][j] < 0)
--j;
else {
c.push_back(a[i - 1]);
--i;
--j;
}
}
reverse((c).begin(), (c).end());
return c;
}
vector<int> money_change(int C, vector<int> &cs) {
const int INF = 99999999;
int n = cs.size();
vector<int> xs(C + 1, INF);
vector<int> ys(C + 1);
xs[0] = 0;
for (int i = 0; i < n; ++i) {
for (int c = 0; c + cs[i] <= C; ++c) {
if (xs[c + cs[i]] > xs[c] + 1) {
xs[c + cs[i]] = xs[c] + 1;
ys[c + cs[i]] = c;
}
}
}
vector<int> zs;
for (int c = C; c > 0; c = ys[c]) {
zs.push_back(c - ys[c]);
}
return zs;
}
int main() {
int N;
cin >> N;
vector<long long> a(N);
for (int i = (0); i < (N); ++i) {
cin >> a[i];
}
long long cost = 0;
long long sum = a[0];
for (int i = (0); i < (N - 1); ++i) {
if (sum > 0) {
if (sum + a[i + 1] < 0) {
sum += a[i + 1];
} else {
cost += (1 + (sum + a[i + 1]));
sum = -1;
}
} else {
if (sum + a[i + 1] > 0) {
sum += a[i + 1];
} else {
cost += (1 + ABS(sum + a[i + 1]));
sum = 1;
}
}
}
std::cout << (cost) << endl;
;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace kyoupuro {
class MainClass {
public static void Main() {
var N = Input.NextLong();
var list = Input.LongList();
long count = 0;
// 最初を非ゼロにする
if (list[0] == 0) {
var index = list.FindIndex(x => x != 0);
if (index == -1) list[0] = 1;
else if ((index % 2 == 1) == (list[index] < 0)) list[0] = 1;
else list[0] = -1;
count++;
}
long sum = list[0];
for (int i = 1; i < N; i++) {
var nextSum = sum + list[i];
if (sum > 0 && nextSum >= 0) {
count += nextSum + 1;
sum = -1;
} else if (sum < 0 && nextSum <= 0) {
count += -nextSum + 1;
sum = 1;
} else {
sum = nextSum;
}
}
Console.WriteLine(count);
}
}
class Input {
static IEnumerator<string> enumerator = new string[] { }.AsEnumerable().GetEnumerator();
public static string Line() {
return Console.ReadLine();
}
public static int NextInt() {
while (!enumerator.MoveNext()) {
enumerator = StrArr().AsEnumerable().GetEnumerator();
}
return int.Parse(enumerator.Current);
}
public static long NextLong() {
while (!enumerator.MoveNext()) {
enumerator = StrArr().AsEnumerable().GetEnumerator();
}
return long.Parse(enumerator.Current);
}
public static string[] StrArr() {
return Line().Split(' ');
}
public static List<int> IntList() {
return StrArr().Select(int.Parse).ToList();
}
public static List<long> LongList() {
return StrArr().Select(long.Parse).ToList();
}
public static void Skip(int line = 1) {
enumerator.Reset();
for (int i = 0; i < line; i++) Console.ReadLine();
}
public static void Reset() {
enumerator.Reset();
}
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
long body(std::vector<long>& a) {
long ans = 0;
std::vector<long> s(a.size());
s.at(0) = a.at(0);
for (unsigned long i = 1; i < a.size(); i++) {
s.at(i) = s.at(i - 1) + a.at(i);
}
long diff = 0;
for (unsigned long i = 1; i < s.size(); i++) {
s.at(i) += diff;
long n = 0;
if (s.at(i - 1) > 0 && s.at(i) >= 0) {
n = s.at(i) + 1;
ans += n;
diff -= n;
s.at(i) += diff;
} else if (s.at(i - 1) < 0 && s.at(i) <= 0) {
n = -s.at(i) + 1;
ans += n;
diff += n;
s.at(i) += diff;
}
}
return ans;
}
int main(int argc, char** argv) {
long n;
std::cin >> n;
std::vector<long> a(n);
for (long i = 0; i < n; i++) {
std::cin >> a.at(i);
}
int ans;
if (a.at(0) != 0) {
ans = body(a);
} else {
a.at(0) = -1;
long ans_a = body(a) + 1;
a.at(0) = 1;
long ans_b = body(a) + 1;
ans = std::min(ans_a, ans_b);
}
std::cout << ans << std::endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <bits/stdc++.h>
int main(void) {
int i, a, n, num;
int sum = 0, bsum = 0, ans = 0, m = 0;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &a);
bsum = sum;
sum += a;
if (bsum > 0) {
if (sum > 0) {
num = sum;
do {
num--;
ans++;
m++;
} while (num >= 0);
sum -= m;
m = 0;
}
if (sum = 0) {
ans++;
sum -= 1;
}
}
if (bsum < 0) {
if (sum < 0) {
num = sum;
do {
num++;
ans++;
m++;
} while (num <= 0);
sum += m;
m = 0;
}
if (sum = 0) {
ans++;
sum += 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 = list(map(int, input().split()))
ans = 0
prev_sm = A[0] # total to i - 1
for i in range(1, N):
# if prev_sum is plus and a is more minus than prev_sum.
if prev_sm > 0 and prev_sm + A[i] < 0:
prev_sm += A[i]
continue
# if prev_sum is plus and a is larger than or equal to prev_sum.
elif prev_sm > 0 and prev_sm + A[i] >= 0:
diff = prev_sm + A[i] + 1
prev_sm += A[i]
ans += diff
A[i] -= diff
# if prev_sum is minus and a is more plus than prev_sum.
elif prev_sm < 0 and prev_sm + A[i] > 0:
prev_sm += A[i]
continue
# if prev_sum is minus and a is more smaller than or equal to prev_sum.
elif prev_sm < 0 and prev_sm + A[i] <= 0:
diff = -(prev_sm + A[i] - 1)
prev_sm += A[i]
ans += diff
A[i] += diff
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;
scanf("%d", &n);
vector<int> a;
for (int i = 0; i < n; i++) {
int an;
scanf("%d", &an);
a.push_back(an);
}
long long op_count = 0;
long long now_sum = 0;
if (a[0] == 0) {
a[0] = 1;
op_count++;
}
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 n;
int main() {
cin >> n;
int a[n + 1];
for (int i = 0; i < n; i++) cin >> a[i];
bool flag;
long long sum;
long long ans1 = 0;
sum = a[0];
flag = true;
for (int i = 1; i < n; i++) {
sum += a[i];
if (flag && sum <= 0) {
ans1 += sum * -1 + 1;
sum = 1;
} else if (!flag && sum >= 0) {
ans1 += sum + 1;
sum = -1;
}
if (flag)
flag = false;
else
flag = true;
}
long long ans2 = 0;
sum = a[0];
flag = false;
for (int i = 1; i < n; i++) {
sum += a[i];
if (flag && sum <= 0) {
ans2 += sum * -1 + 1;
sum = 1;
} else if (!flag && sum >= 0) {
ans2 += sum + 1;
sum = -1;
}
if (flag)
flag = false;
else
flag = true;
}
cout << min(ans1, ans2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
num_list = list(map(int, input().split()))
count = 0
sum_ = num_list[0]
if sum_ > 0:
for i in range(1, n):
sum_ += num_list[i]
if i%2 == 0:
if sum_ <= 0:
sum_ = 1
count += abs(sum_) + 1
else:
if sum_ >= 0:
sum_ = -1
count += abs(sum_) + 1
print(count)
elif sum_ < 0:
for i in range(1, n):
sum_ += num_list[i]
if i%2 == 1:
if sum_ <= 0:
sum_ = 1
count += abs(sum_) + 1
else:
if sum_ >= 0:
sum_ = -1
count += abs(sum_) + 1
print(count)
else:
sum_ = 1
for i in range(1, n):
sum_ += num_list[i]
if i%2 == 0:
if sum_ <= 0:
sum_ = 1
count += abs(sum_) + 1
else:
if sum_ >= 0:
sum_ = -1
count += abs(sum_) + 1
count1 = count
sum_ = -1
for i in range(1, n):
sum_ += num_list[i]
if i%2 == 1:
if sum_ <= 0:
sum_ = 1
count += abs(sum_) + 1
else:
if sum_ >= 0:
sum_ = -1
count += abs(sum_) + 1
count2 = count
print(min(count1, count2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
long long a[n];
long long res = 0;
for (long long i = 0; i < n; ++i) {
cin >> a[i];
}
bool can1 = false;
if (a[0] == 0) {
for (long long i = 1; i < n; ++i) {
if (a[i] > 0 && i % 2 == 0) {
a[0] = -1;
res++;
can1 = true;
break;
}
if (a[i] > 0 && i % 2 != 0) {
a[0] = -1;
res++;
can1 = true;
break;
} else if (a[i] < 0 && i % 2 == 0) {
a[0] = -1;
res++;
can1 = true;
break;
} else if (a[i] < 0 && i % 2 != 0) {
a[0] = 1;
res++;
can1 = true;
break;
}
}
} else
can1 = true;
long long total = a[0];
for (long long i = 1; i < n; ++i) {
if (total < 0 && total + a[i] >= 0) {
total += a[i];
if (total == 0) {
total++;
res++;
}
} else if (total > 0 && total + a[i] <= 0) {
total += a[i];
if (total == 0) {
total--;
res++;
}
} else if (total < 0 && total + a[i] <= 0) {
res += 1 - total - a[i];
a[i] = 1 - total;
total += a[i];
if (total == 0) {
total++;
res++;
}
} else if (total > 0 && total + a[i] >= 0) {
res += abs(-1 - total - a[i]);
a[i] = -1 - total;
total += a[i];
if (total == 0) {
total--;
res++;
}
}
}
if (can1 == false)
cout << 2 * n + 1 << endl;
else
cout << res << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n, ci, counter = 0;
bool isPositive;
cin >> n;
vector<int> a(n);
for (int(i) = (0); (i) <= (n - 1); ++(i)) cin >> a[i];
ci = a[0];
if (ci == 0) {
long long counter1, counter2;
counter1 = counter2 = 0;
isPositive = true;
for (int(i) = (1); (i) <= (n - 1); ++(i)) {
ci += a[i];
if (isPositive && ci > 0) {
counter1 += abs(ci) + 1;
ci -= abs(ci) + 1;
} else if (!isPositive && ci < 0) {
counter1 += abs(ci) + 1;
ci += abs(ci) + 1;
} else if (ci == 0) {
if (isPositive) {
--ci;
++counter1;
} else {
++ci;
++counter1;
}
}
isPositive = !isPositive;
}
isPositive = false;
for (int(i) = (1); (i) <= (n - 1); ++(i)) {
ci += a[i];
if (isPositive && ci > 0) {
counter2 += abs(ci) + 1;
ci -= abs(ci) + 1;
} else if (!isPositive && ci < 0) {
counter2 += abs(ci) + 1;
ci += abs(ci) + 1;
} else if (ci == 0) {
if (isPositive) {
--ci;
++counter2;
} else {
++ci;
++counter2;
}
}
isPositive = !isPositive;
}
counter = min(counter1, counter2);
cout << counter << endl;
return 0;
}
if (ci > 0) isPositive = true;
if (ci < 0) isPositive = false;
for (int(i) = (1); (i) <= (n - 1); ++(i)) {
ci += a[i];
if (isPositive && ci > 0) {
counter += abs(ci) + 1;
ci -= abs(ci) + 1;
} else if (!isPositive && ci < 0) {
counter += abs(ci) + 1;
ci += abs(ci) + 1;
} else if (ci == 0) {
if (isPositive) {
--ci;
++counter;
} else {
++ci;
++counter;
}
}
isPositive = !isPositive;
}
cout << counter << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
using static System.Console;
using static System.Math;
public class Hello{
public static void Main(){
int kazu = int.Parse(ReadLine());
string[] number = ReadLine().Split(' ');
int wa = 0;
int wa2 = 0;
int num0 = int.Parse(number[0]);
long count = 0;
long ans =0;
if(num0 == 0){
wa = 1;
wa2 = -1;
count++;
}else{
wa = num0;
}
for(int i=1;i<kazu;i++){
int numi = int.Parse(number[i]);
int temp = wa + numi;
if(wa > 0){
if(temp < 0){
wa = temp;
}else{
count += Abs(numi + wa) + 1;
wa = -1;
}
}else if(wa < 0){
if(temp > 0){
wa = temp;
}else{
count += Abs(numi + wa) + 1;
wa = 1;
}
}
}
ans = count;
if(wa2 != 0){
count = 1;
for(int i=1;i<kazu;i++){
int numi = int.Parse(number[i]);
int temp = wa2 + numi;
if(wa2 > 0){
if(temp < 0){
wa2 = temp;
}else{
count += Abs(numi + wa2) + 1;
wa2 = -1;
}
}else if(wa2 < 0){
if(temp > 0){
wa2 = temp;
}else{
count += Abs(numi + wa2) + 1;
wa2 = 1;
}
}
}
}
WriteLine(Min(count,ans));
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n + 10];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int firstOperation;
int operation = 0, sum = a[0];
if (sum <= 0) {
operation += abs(sum) + 1;
sum = 1;
}
for (int i = 1; i < n; i++) {
int tmp = sum + a[i];
if (sum < 0) {
if (tmp <= 0) {
operation += abs(tmp) + 1;
sum = 1;
} else
sum = tmp;
} else {
if (tmp >= 0) {
operation += tmp + 1;
sum = -1;
} else
sum = tmp;
}
}
firstOperation = operation;
operation = 0, sum = a[0];
if (sum >= 0) {
operation += abs(sum) + 1;
sum = -1;
}
for (int i = 1; i < n; i++) {
int tmp = sum + a[i];
if (sum < 0) {
if (tmp <= 0) {
operation += abs(tmp) + 1;
sum = 1;
} else
sum = tmp;
} else {
if (tmp >= 0) {
operation += tmp + 1;
sum = -1;
} else
sum = tmp;
}
}
cout << min(firstOperation, operation) << 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 = input().split()
a = [int(m) for m in a]
q = []
k = 0
kco = 0
#+-
for i in range(n):
if i == 0:
if a[i] > 0:
q.append(a[i])
else:
q.append(1)
kco += 1 - a[i]
k += q[i]
else:
k += a[i]
if i % 2 != 0:
if k < 0:
q.append(a[i])
else:
q.append(a[i]-k-1)
kco += k + 1
k += q[i] - a[i]
if i % 2 == 0:
if k > 0:
q.append(a[i])
else:
q.append(a[i]-k+1)
kco += -k + 1
k += q[i] - a[i]
xco = kco
q = []
k = 0
kco = 0
#-+
for i in range(n):
if i == 0:
if a[i] < 0:
q.append(a[i])
else:
q.append(1)
kco += 1 - a[i]
k += q[i]
else:
k += a[i]
if i % 2 == 0:
if k < 0:
q.append(a[i])
else:
q.append(a[i]-k-1)
kco += k + 1
k += q[i] - a[i]
if i % 2 != 0:
if k > 0:
q.append(a[i])
else:
q.append(a[i]-k+1)
kco += -k + 1
k += q[i] - a[i]
yco = kco
print(min(xco, yco)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import sys
sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7
def resolve():
n = int(input())
A = list(map(int, input().split()))
res = 0
R = [0]
for i in range(n):
if i == 0:
if A[i] == 0:
res += 1
if A[i + 1] >= 0:
R.append(-1)
else:
R.append(1)
else:
R.append(R[-1] + A[i])
else:
if R[-1] >= 0 and R[-1] + A[i] >= 0:
res += R[-1] + A[i] + 1
R.append(-1)
elif R[-1] <= 0 and R[-1] + A[i] <= 0:
res += abs(R[-1] + A[i]) + 1
R.append(1)
else:
R.append(R[-1] + A[i])
print(res)
if __name__ == '__main__':
resolve()
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
count = 0
sum_i = a[0]
if sum_i < 0:
sum_i = 1
pos_count = 1 - sum_i
for i in range(1, n):
if sum_i < 0:
sum_i += a[i]
if sum_i <= 0:
pos_count += 1 - sum_i
sum_i = 1
elif sum_i > 0:
sum_i += a[i]
if sum_i >= 0:
pos_count += sum_i + 1
sum_i = -1
sum_i = a[0]
neg_count = 0
for i in range(1, n):
if sum_i < 0:
sum_i += a[i]
if sum_i <= 0:
neg_count += 1 - sum_i
sum_i = 1
elif sum_i > 0:
sum_i += a[i]
if sum_i >= 0:
neg_count += sum_i + 1
sum_i = -1
if neg_count < pos_count:
count = neg_count
else:
count = pos_count
elif sum_i > 0:
sum_i = a[0]
pos_count = 0
for i in range(1, n):
if sum_i < 0:
sum_i += a[i]
if sum_i <= 0:
pos_count += 1 - sum_i
sum_i = 1
elif sum_i > 0:
sum_i += a[i]
if sum_i >= 0:
pos_count += sum_i + 1
sum_i = -1
sum_i = -1
neg_count = sum_i + 1
for i in range(1, n):
if sum_i < 0:
sum_i += a[i]
if sum_i <= 0:
neg_count += 1 - sum_i
sum_i = 1
elif sum_i > 0:
sum_i += a[i]
if sum_i >= 0:
neg_count += sum_i + 1
sum_i = -1
if neg_count < pos_count:
count = neg_count
else:
count = pos_count
else:
sum_i = 1
pos_count = 1
for i in range(1, n):
if sum_i < 0:
sum_i += a[i]
if sum_i <= 0:
pos_count += 1 - sum_i
sum_i = 1
elif sum_i > 0:
sum_i += a[i]
if sum_i >= 0:
pos_count += sum_i + 1
sum_i = -1
sum_i = -1
neg_count = 1
for i in range(1, n):
if sum_i < 0:
sum_i += a[i]
if sum_i <= 0:
neg_count += 1 - sum_i
sum_i = 1
elif sum_i > 0:
sum_i += a[i]
if sum_i >= 0:
neg_count += sum_i + 1
sum_i = -1
if neg_count < pos_count:
count = neg_count
else:
count = pos_count
print(count)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
int a[n];
long long int s = 0;
long long int ans = INT_MAX;
int i;
for (i = 0; i < n; i++) cin >> a[i];
s = a[0];
long long int p = 0;
if (s > 0) {
for (i = 1; i < n; i++) {
if (i % 2) {
if (s + a[i] < 0) {
s += a[i];
} else {
p += 1 + s + a[i];
s = -1;
}
} else {
if (s + a[i] > 0)
s += a[i];
else {
p += 1 - s - a[i];
s = 1;
}
}
}
s = -1;
ans = min(ans, p);
cout << ans << endl;
} else if (s < 0) {
for (i = 1; i < n; i++) {
if (i % 2 == 0) {
if (s + a[i] < 0) {
s += a[i];
} else {
p += 1 + s + a[i];
s = -1;
}
} else {
if (s + a[i] > 0)
s += a[i];
else {
p += 1 - s - a[i];
s = 1;
}
}
}
s = 1;
ans = min(ans, p);
cout << ans << endl;
} else {
p = 1;
s = 1;
for (i = 1; i < n; i++) {
if (i % 2) {
if (s + a[i] < 0) {
s += a[i];
} else {
p += 1 + s + a[i];
s = -1;
}
} else {
if (s + a[i] > 0)
s += a[i];
else {
p += 1 - s - a[i];
s = 1;
}
}
}
s = -1;
ans = min(ans, p);
p = 1;
for (i = 1; i < n; i++) {
if (i % 2 == 0) {
if (s + a[i] < 0) {
s += a[i];
} else {
p += 1 + s + a[i];
s = -1;
}
} else {
if (s + a[i] > 0)
s += a[i];
else {
p += 1 - s - a[i];
s = 1;
}
}
}
ans = min(ans, p);
cout << ans << endl;
}
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> data(N);
for (int i = 0; i < N; i++) cin >> data[i];
int count = 0;
int ans = data[0];
int saisyo;
for (int i = 1; i < N; i++) {
ans += data[i];
if (i % 2 == 0) {
while (ans <= 0) {
ans++;
count++;
}
} else {
while (ans >= 0) {
ans--;
count++;
}
}
}
saisyo = count;
count = 0;
ans = data[0];
while (ans >= 0) {
ans--;
count++;
}
for (int i = 1; i < N; i++) {
ans += data[i];
if (i % 2 != 0) {
while (ans <= 0) {
ans++;
count++;
}
} else {
while (ans >= 0) {
ans--;
count++;
}
}
}
saisyo = min(saisyo, count);
cout << saisyo << 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 long> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long Sum = a[0], ans = 0;
if (Sum <= 0) {
ans += -Sum + 1;
Sum = 1;
}
for (int i = 0; i < n - 1; i++) {
if (Sum < 0 && Sum + a[i + 1] <= 0) {
ans += -(Sum + a[i + 1]) + 1;
Sum = 1;
} else if (Sum > 0 && Sum + a[i + 1] >= 0) {
ans += Sum + a[i + 1] + 1;
Sum = -1;
} else {
Sum += a[i + 1];
}
}
long long S1 = a[0], t1 = 0;
if (S1 >= 0) {
ans += S1 + 1;
Sum = -1;
}
for (int i = 0; i < n - 1; i++) {
if (S1 < 0 && S1 + a[i + 1] <= 0) {
t1 += -(S1 + a[i + 1]) + 1;
S1 = 1;
} else if (S1 > 0 && S1 + a[i + 1] >= 0) {
t1 += S1 + a[i + 1] + 1;
S1 = -1;
} else {
S1 += a[i + 1];
}
}
ans = min(ans, t1);
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[100000];
for (int i = 0; i < (int)(n); i++) cin >> a[i];
bool zeroFlag = false;
int costA = 0, costB = 0;
int sum = 0, prev = 0;
for (int i = 0; i < (int)(n); i++) {
if (a[0] == 0) {
a[0] = 1;
costA++;
zeroFlag = true;
}
prev = sum;
sum += a[i];
if (i == 0) continue;
if ((prev * sum) < 0) continue;
if (prev < 0) {
for (; (prev * sum) >= 0; sum++) costA++;
} else {
for (; (prev * sum) >= 0; sum--) costA++;
}
}
if (zeroFlag) {
a[0] = -1;
costB++;
} else {
a[0] = -1 * a[0] / abs(a[0]);
}
sum = 0;
prev = 0;
for (int i = 0; i < (int)(n); i++) {
prev = sum;
sum += a[i];
if (i == 0) continue;
if ((prev * sum) < 0) continue;
if (prev < 0) {
for (; (prev * sum) >= 0; sum++) costB++;
} else {
for (; (prev * sum) >= 0; sum--) costB++;
}
}
cout << min(costA, costB) << 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()]
count = 0
sum_before = A[0]
for i in range(n):
if i == 0:
continue
sum_for_i = sum_before + A[i]
#print('[',i,']: before',sum_before,'after',sum_for_i, A)
if sum_for_i == 0 and sum_before > 0:
#print("case 1")
A[i] -= 1
count += 1
elif sum_for_i == 0 and sum_before <0:
#print("case 2")
A[i] += 1
count += 1
elif sum_before >0 and sum_for_i>0:
#print("case 3")
count += (abs(sum_for_i)+1)
A[i] -= (abs(sum_for_i)+1)
elif sum_before <0 and sum_for_i<0:
#print("case 4")
count += (abs(sum_for_i)+1)
A[i] += (abs(sum_for_i)+1)
#print('[',i,']: ',A, 'count', count)
sum_before += A[i]
print(count) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
long long ans = 0;
long long num = 0;
int offset = 0;
for (int i = 0; i < N; i++) {
long long a;
cin >> a;
num += a;
if (i == 0 && a < 0) offset = 1;
if ((i + offset) % 2 == 0 && num <= 0) {
ans += 1 - num;
num = 1;
} else if ((i + offset) % 2 == 1 && num >= 0) {
ans += num + 1;
num = -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()))
sum = a[0]
ans = 0
if sum > 0:
flg = 1
else:
flg = -1
for i in range(1, n):
sum += a[i]
if flg == 1:
if sum < 0:
flg = -1
else:
ans += sum + 1
sum = -1
flg = -1
else:
if sum > 0:
flg = 1
else:
ans += (sum) * (-1) + 1
sum = 1
flg = 1
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | from functools import reduce
N =int(input())
A = list(map(int, input().split()))
def check(A):
arr1 = [0]*N
ans1 = 0
for i in range(len(A)):
if i ==0:
arr1[0] = A[0]
else:
arr1[i] += arr1[i-1] + A[i]
if arr1[i]*arr1[i-1] >0:
if arr1[i-1] >=0:
arr1[i] = -1
elif arr1[i-1] <=0:
arr1[i] = 1
ans1 += abs(arr1[i])+1
ans2 = 0
arr2 = [0]*N
#change head.
for i in range(len(A)):
if i ==1:
if A[0]>=0:
arr2[0] = -1
elif A[0]<=0:
arr2[0] = 1
ans2 += (abs(A[0])+1)
else:
arr2[i] += arr2[i-1] + A[i]
if arr2[i]*arr2[i-1] >=0:
if arr2[i-1]>=0:
arr2[i] = -1
elif arr2[i-1]<=0:
arr2[i] = 1
ans2 += abs(arr2[i])+1
return min(ans1,ans2)
print(check(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;
template <typename T>
T gcd(T a, T b) {
if (a < b) gcd(b, a);
if (b == 1) return 1;
T r;
while ((r = a % b)) {
a = b;
b = r;
}
return b;
}
bool comp(pair<long long int, long long int> a,
pair<long long int, long long int> b) {
return a.second < b.second;
}
struct UnionFind {
vector<int> par;
vector<int> rank;
UnionFind(int n = 1) { init(n); }
void init(int n = 1) {
par.resize(n);
rank.resize(n);
for (int i = 0; i < n; ++i) {
par[i] = i;
rank[i] = 0;
}
}
int root(int x) {
if (par[x] == x) {
return x;
} else {
int r = root(par[x]);
return par[x] = r;
}
}
bool issame(int x, int y) { return root(x) == root(y); }
bool merge(int x, int y) {
x = root(x);
y = root(y);
if (x == y) return false;
if (rank[x] < rank[y]) swap(x, y);
if (rank[x] == rank[y]) ++rank[x];
par[y] = x;
return true;
}
};
class SegmentTree {
public:
int N, n;
vector<long long int> value;
SegmentTree(int n) {
this->n = n;
this->N = 1;
while (N < n) N *= 2;
this->value = vector<long long int>(2 * N - 1, (long long int(1e18)));
}
void update(int i, long long int x) {
i += N - 1;
value[i] = x;
while (i > 0) {
i = (i - 1) / 2;
value[i] = min(value[i * 2 + 1], value[i * 2 + 2]);
}
}
long long int query(int l, int r) { return _query(l, r, 0, 0, N); }
long long int _query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l) return (long long int(1e18));
if (a <= l && r <= b)
return value[k];
else {
long long int c1 = _query(a, b, 2 * k + 1, l, (l + r) / 2);
long long int c2 = _query(a, b, 2 * k + 2, (l + r) / 2, r);
return min(c1, c2);
}
}
};
int msb(long long int x) {
int n = 0;
while (x > 0) {
x /= 2;
n++;
}
return n;
}
pair<long long int, long long int> merge(long long int a, long long int b) {
long long int cnt = 1;
while (a + b >= 10) {
long long int c = a + b;
a = c / 10;
b = c % 10;
cnt++;
}
return make_pair(a + b, cnt);
}
vector<long long int> decomp(long long int d, long long int c,
long long int *cost) {
vector<long long int> x;
if (c % 2 == 1) {
x.push_back(d);
}
if (c >= 2) {
auto r = merge(d, d);
*cost = *cost + r.second * (c / 2);
auto y = decomp(r.first, c / 2, cost);
x.insert(x.end(), y.begin(), y.end());
}
return x;
}
int main(int argc, const char *argv[]) {
cin.tie(0);
ios::sync_with_stdio(false);
long long int n;
cin >> n;
vector<long long int> a(n);
for (long long int i = 0, i_len = (n); i < i_len; ++i) {
cin >> a[i];
}
vector<long long int> sp(n), sn(n);
long long int p = 0, q = 0;
if (a[0] > 0) {
q += a[0] + 1;
sn[0] = -1;
sp[0] = a[0];
} else if (a[0] < 0) {
p += 1 - a[0];
sp[0] = 1;
sn[0] = a[0];
} else {
p = 1;
sp[0] = 1;
sn[0] = -1;
}
for (long long int i = (1), i_len = (n); i < i_len; ++i) {
sp[i] = sp[i - 1] + a[i];
if (sp[i] >= 0 && i % 2 == 1) {
p += sp[i] + 1;
sp[i] = -1;
} else if (sp[i] <= 0 && i % 2 == 0) {
p += 1 - sp[i];
sp[i] = 1;
}
sn[i] = sn[i - 1] + a[i];
if (sn[i] >= 0 && i % 2 == 0) {
q += sn[i] + 1;
sn[i] = -1;
} else if (sn[i] <= 0 && i % 2 == 1) {
q += 1 - sn[i];
sn[i] = 1;
}
}
cout << min(q, p) << 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 t;
int answer = 0;
int sumi;
bool flag;
cin >> t;
vector<int> A(t);
cin >> A[0];
sumi = A[0];
if (sumi > 0) {
flag = true;
} else if (sumi < 0) {
flag = false;
} else {
answer += 1;
A[0] += 1;
sumi += 1;
}
for (int i = 1; i < t; i++) {
cin >> A[i];
sumi += A[i];
if (sumi == 0) {
answer += 1;
if (flag) {
sumi = -1;
} else {
sumi = 1;
}
} else if (sumi > 0 == flag) {
answer += sumi + 1;
if (sumi > 0) {
sumi = -1;
} else {
sumi = 1;
}
}
flag = !flag;
}
cout << answer << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int a[100005];
int N;
int solve() {
int Num = 0, Sum = 0;
for (int i = 0; i < N; i++) {
if (i % 2 == 0) {
if (Sum + a[i] <= 0) {
Num += 1 - (Sum + a[i]);
Sum = 1;
} else
Sum += a[i];
} else {
if (Sum + a[i] >= 0) {
Num += 1 + Sum + a[i];
Sum = -1;
} else
Sum += a[i];
}
}
return Num;
}
int main() {
cin >> N;
for (int i = 0; i < N; i++) {
cin >> a[i];
}
int ans1, ans2;
ans1 = solve();
for (int i = 0; i < N; i++) a[i] *= -1;
ans2 = solve();
printf("%d\n", min(ans1, ans2));
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | # -*- coding: utf-8 -*-
"""
Created on Sat Sep 8 15:51:53 2018
@author: maezawa
"""
n = int(input())
a = list(map(int, input().split()))
sa = 0
cnt = 0
for i in range(n-1):
sa += a[i]
if sa == 0:
sa += 1
cnt += 1
na = -sa//abs(sa)*(abs(sa)+1)
if abs(a[i+1]) > abs(na) and a[i+1]*na > 0:
continue
else:
cnt += abs(na-a[i+1])
a[i+1] = na
print(cnt)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
bool value(int y, int x, int R, int C) {
return 0 <= y && y < R && 0 <= x && x < C;
}
double pie = acos(-1);
int INF = 1000000007;
int dx[4] = {0, -1, 0, 1};
int dy[4] = {-1, 0, 1, 0};
int main() {
int n;
long long a[100005], ans = 0, b;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
b = a[0];
if (b > 0) {
for (int i = 1; i < n; i++) {
if (i % 2 == 1) {
if (a[i] + b < 0) {
b += a[i];
} else {
ans += abs(b + a[i]) + 1;
a[i] = a[i] - abs(b + a[i]) - 1;
b += a[i];
}
} else {
if (a[i] + b > 0) {
b += a[i];
} else {
ans += abs(b + a[i]) + 1;
a[i] = a[i] + abs(b + a[i]) + 1;
b += a[i];
}
}
}
} else {
for (int i = 1; i < n; i++) {
if (i % 2 == 0) {
if (a[i] + b < 0) {
b += a[i];
} else {
ans += abs(b + a[i]) + 1;
a[i] = a[i] - abs(b + a[i]) - 1;
b += a[i];
}
} else {
if (a[i] + b > 0) {
b += a[i];
} else {
ans += abs(b + a[i]) + 1;
a[i] = a[i] + abs(b + a[i]) + 1;
b += a[i];
}
}
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9 + 7;
const long long N = 100003;
long long n, a[N], ans = 0;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (long long i = (0); i < (n); ++i) {
cin >> a[i];
}
long long w = a[0];
if (a[0] <= 0) {
ans += -a[0] + 1;
w = 1;
}
for (long long i = (1); i < (n); ++i) {
if (i % 2 == 1) {
if (w + a[i] >= 0) {
ans += (w + a[i]) + 1;
w = -1;
} else {
w += a[i];
}
} else {
if (w + a[i] <= 0) {
ans += -(w + a[i]) + 1;
w = 1;
} else {
w += a[i];
}
}
}
long long ans2 = 0;
w = a[0];
if (a[0] >= 0) {
ans2 += a[0] + 1;
w = 1;
}
for (long long i = (1); i < (n); ++i) {
if (i % 2 == 0) {
if (w + a[i] >= 0) {
ans2 += (w + a[i]) + 1;
w = -1;
} else {
w += a[i];
}
} else {
if (w + a[i] <= 0) {
ans2 += -(w + a[i]) + 1;
w = 1;
} else {
w += a[i];
}
}
}
cout << min(ans, ans2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
numbers = list(map(int, input().split()))
counter = 0
sum_i_n = 0
sum_i_n_1 = numbers[0]
for i in range(len(numbers) - 1):
# sum_i_n = sum(numbers[:i + 1])
# sum_i_n_1 = sum(numbers[:i + 2])
sum_i_n += numbers[i]
sum_i_n_1 += numbers[i + 1]
if sum_i_n == 0:
numbers[i] += 1
sum_i_n += 1
sum_i_n_1 += 1
counter += 1
if sum_i_n * sum_i_n_1 > 0:
sub = abs(sum_i_n_1) + 1
counter += sub
if sum_i_n_1 > 0:
numbers[i + 1] -= sub
sum_i_n_1 -= sub
else:
numbers[i + 1] += sub
sum_i_n_1 += sub
if sum_i_n_1 == 0:
counter += 1
print(counter)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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 itn = int;
using ld = long double;
template <class T>
using vec = vector<T>;
int main() {
int n;
cin >> n;
vec<int> a(n);
for (int i = 0; i < (int)(n); i++) cin >> a[i];
int ans = 0;
vec<int> s(n, 0);
int ii = 0;
while (true) {
s[ii] = a[ii];
if (s[ii] != 0) break;
++ii;
}
ans += ii;
for (int i = ((int)(ii + 1)); i < ((int)(n)); i++) {
s[i] = a[i] + s[i - 1];
if (s[i] * s[i - 1] > 0) {
if (s[i] < 0) {
ans += 1 - s[i];
s[i] = 1;
} else if (s[i] > 0) {
ans += s[i] + 1;
s[i] = -1;
}
} else if (s[i] == 0) {
if (s[i - 1] < 0) {
++ans;
s[i] = 1;
} else if (s[i - 1] > 0) {
++ans;
s[i] = -1;
}
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n, *A = map(int, open(0).read().split())
def sgn(n):
return 0 if n==0 else 1 if n>0 else -1
C = [0, 0]
S = [1, -1]
for a in A:
for i, s in enumerate(S):
sgn_sum = sgn(s)
if sgn(s+a) != sgn_sum:
S[i] += a
else:
C[i] += abs(s+a+sgn_sum)
S[i] = -sgn_sum
print(min(C)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n;
long long func(vector<long long> a, int fugo) {
long long ans = 0;
long long offset = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == fugo) {
if (a[i] <= offset) {
ans += offset - (a[i] - 1);
offset = a[i] - 1;
}
} else {
if (a[i] >= offset) {
ans += (a[i] + 1) - offset;
offset = a[i] + 1;
}
}
printf("[%d]", offset);
}
return ans;
}
int main() {
cin >> n;
vector<long long> a;
long long sum_tmp = 0;
for (int i = 0; i < n; i++) {
int tmp;
cin >> tmp;
sum_tmp += tmp;
a.push_back(sum_tmp);
}
long long ans = min(func(a, 0), func(a, 1));
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
template <class T = int>
T in() {
T x;
cin >> x;
return x;
}
int calc_sum(int N, vector<int> a, int sum, int start) {
int prev = start;
for (int i = 1; i < N; i++) {
int curr = prev + a[i];
;
;
if (prev < 0 && curr < 0) {
sum += abs(curr) + 1;
curr = 1;
} else if (prev > 0 && curr > 0) {
sum += abs(curr) + 1;
curr = -1;
} else if (prev > 0 && curr == 0) {
curr -= 1;
sum++;
} else if (prev < 0 && curr == 0) {
curr += 1;
sum++;
}
prev = curr;
}
return sum;
}
int main() {
int N = in();
vector<int> a(N);
for (int i = 0; i < (N); i++) a[i] = in();
int start1;
int start2;
int sum1 = 0;
int sum2 = 0;
if (a[0] == 0) {
start1 = 1;
start2 = -1;
} else {
start1 = a[0];
if (a[0] > 0) {
start2 = -1;
} else {
start2 = 1;
}
sum2 += abs(a[0]) + 1;
}
int res1 = calc_sum(N, a, sum1, start1);
int res2 = calc_sum(N, a, sum2, start2);
cout << min(res1, res2) << '\n';
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> List(n);
for (int i = 0; i < n; i++) {
cin >> List.at(i);
}
int cnt = 0;
long long Sign = 0;
for (int i = 0; i < n; i++) {
if (Sign == 0) {
if (List.at(i) > 0) {
Sign = List.at(i);
} else if (List.at(i) < 0) {
Sign = List.at(i);
}
continue;
}
if (Sign > 0) {
if (Sign + List.at(i) >= 0) {
cnt += abs(Sign + List.at(i)) + 1;
Sign = -1;
} else {
Sign += List.at(i);
}
continue;
}
if (Sign < 0) {
if (List.at(i) + Sign <= 0) {
cnt += abs(Sign + List.at(i)) + 1;
Sign = 1;
} else {
Sign += List.at(i);
}
continue;
}
}
cout << cnt << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
int A[N];
for (int i = 0; i < N; i++) cin >> A[i];
int numSign = 1, sum = 0, actNum = 0;
for (int i = 0; i < N; i++) {
if (A[i] > 0)
break;
else if (A[i] < 0) {
numSign *= -1;
break;
} else
numSign *= -1;
}
for (int i = 0; i < N; i++) {
sum += A[i];
if (numSign == 1) {
if (sum <= 0) {
actNum += 1 - sum;
sum = 1;
}
} else {
if (sum >= 0) {
actNum += sum - -1;
sum = -1;
}
}
numSign *= -1;
}
cout << actNum << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | package main
import (
"fmt"
"math"
)
func nextInt() int {
var x int
fmt.Scan(&x)
return x
}
func main() {
n := nextInt()
a := make([]int, n)
for i := range a {
a[i] = nextInt()
}
ans := math.MaxInt64
for j := 0; j <= 1; j++ {
miniAns := 0
now := 0
for i, v := range a {
k := pow(-1, i+j)
now += v
if sign(now) != k {
miniAns += abs(k - now)
now = k
}
}
ans = min(ans, miniAns)
}
fmt.Println(ans)
}
func abs(a int) int {
if a > 0 {
return a
}
return -a
}
func sign(a int) int {
if a != 0 {
return a / abs(a)
}
return 0
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func pow(a, b int) int {
res := 1
for range make([]struct{}, b) {
res *= a
}
return res
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int counter(int N, vector<int> A, int sum, int ans) {
for (int i = 1; i < N; i++) {
if (sum > 0 && sum + A.at(i) >= 0) {
ans += sum + A.at(i) + 1;
sum = -1;
} else if (sum < 0 && sum + A.at(i) <= 0) {
ans += -(sum + A.at(i)) + 1;
sum = 1;
} else
sum += A.at(i);
}
return ans;
}
int main() {
int N;
cin >> N;
vector<int> A(N);
for (int i = 0; i < N; i++) cin >> A.at(i);
int sum = A.at(0);
int ans = 0;
if (sum != 0) {
cout << counter(N, A, sum, ans) << endl;
return 0;
}
ans = min(counter(N, A, sum + 1, ans + 1), counter(N, A, sum - 1, ans + 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(void) {
long long n, i, j, sw, sw2, count = 0, add = 0, adda = 0;
cin >> n;
vector<long long> a(n);
for (i = 0; i < n; i++) {
cin >> a[i];
adda += a[i];
}
if (a[0] > 0)
sw = 1;
else
sw = -1;
add += a[0];
if ((adda > 0 && n % 2 == 1) || (adda < 0 && n % 2 == 0)) {
} else {
if (a[0] < 0) {
while (a[0] != 1) {
add++;
a[0]++;
count++;
}
} else {
while (a[0] != -1) {
add--;
a[0]--;
count++;
}
}
}
if (a[0] > 0)
sw = 1;
else
sw = -1;
for (i = 1; i < n; i++) {
add += a[i];
if (sw == 1) {
if (add < 0) {
} else {
while (add != -1) {
a[i]--;
add--;
count++;
}
}
} else {
if (add > 0) {
} else {
while (add != 1) {
a[i]++;
add++;
count++;
}
}
}
if (a[i] > 0)
sw = 1;
else
sw = -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>
int main() {
int n;
scanf("%d", &n);
int data[100001];
for (int i = 0; i < n; i++) {
scanf("%d", data + i);
}
int sum = data[0];
int change = 0;
int lastsum = sum;
if (lastsum == 0) {
if (data[1] > 0) {
lastsum = -1;
} else {
lastsum = 1;
}
change = 1;
}
for (int i = 1; i < n; i++) {
sum += data[i];
if (lastsum * sum < 0) {
lastsum = sum;
continue;
} else {
if (lastsum > 0) {
change += sum + 1;
lastsum = -1;
sum = -1;
} else {
change += (-sum + 1);
lastsum = 1;
sum = 1;
}
}
}
printf("%d", change);
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
using System.Text;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using static System.Console;
using static System.Math;
namespace AtCorder
{
public class Program
{
public static void Main(string[] args)
{
new Program().Solve(new ConsoleInput(Console.In, ' '));
}
public void Solve(ConsoleInput cin)
{
var n = cin.ReadInt;
var a = cin.ReadLongArray(n);
var ans = 0L;
var pre = a[0];
for(int i = 1; i < n; i++)
{
var now = pre + a[i];
if(pre * now < 0)
{
pre += a[i];
continue;
}
if(now > 0)
{
ans += (now + 1);
a[i] -= (now + 1);
}
else if(now < 0)
{
ans += 1 - now;
a[i] += 1 - now;
}
else if(now == 0)
{
if(pre < 0)
{
a[i]++;
}
else
{
a[i]--;
}
ans++;
}
pre += a[i];
}
WriteLine(ans);
}
public long C(int X, int Y)
{
if (Y == 0 || Y == X)
{
return 1;
}
if (X < Y)
{
return 0;
}
var Pascal = new long[X + 1, X + 1];
for (int i = 0; i <= X; i++)
{
Pascal[i, 0] = 1L;
Pascal[i, i] = 1L;
}
for (int i = 2; i <= X; i++)
{
for (int j = 1; j < i; j++)
{
Pascal[i, j] = Pascal[i - 1, j] + Pascal[i - 1, j - 1];
}
}
return Pascal[X, Y];
}
public class ConsoleInput
{
private readonly System.IO.TextReader _stream;
private char _separator = ' ';
private Queue<string> inputStream;
public ConsoleInput(System.IO.TextReader stream, char separator = ' ')
{
this._separator = separator;
this._stream = stream;
inputStream = new Queue<string>();
}
public string Read
{
get
{
if (inputStream.Count != 0) return inputStream.Dequeue();
string[] tmp = _stream.ReadLine().Split(_separator);
for (int i = 0; i < tmp.Length; ++i)
inputStream.Enqueue(tmp[i]);
return inputStream.Dequeue();
}
}
public string ReadLine { get { return _stream.ReadLine(); } }
public int ReadInt { get { return int.Parse(Read); } }
public long ReadLong { get { return long.Parse(Read); } }
public double ReadDouble { get { return double.Parse(Read); } }
public string[] ReadStrArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret; }
public int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret; }
public long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret; }
}
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
long long sum = a[0];
long long cnt = 0;
if (sum == 0) {
int ind = 1;
for (int i = 0; i < n; i++) {
if (a[i] != 0) ind = i;
break;
}
if (a[ind] > 0)
sum = (ind % 2 == 0 ? -1 : 1);
else
sum = (ind % 2 == 0 ? 1 : -1);
cnt++;
}
for (int i = 1; i < n; i++) {
long long nsum = sum + a[i];
if (sum > 0 && nsum < 0 || sum < 0 && nsum > 0) {
sum = nsum;
continue;
}
sum = (sum > 0 ? -1 : 1);
cnt += (nsum == 0 ? 1 : abs(nsum) + 1);
}
cout << cnt << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using Int = long long;
Int INF = 1 << 30;
int main() {
Int n;
std::cin >> n;
std::vector<Int> a(n);
for (Int i = 0; i < n; i++) std::cin >> a[i];
Int ans = 0;
Int sum = a[0];
if (a[0] > 0) {
for (Int i = 1; i < n; i++) {
sum += a[i];
if (i % 2 == 1 && sum >= 0) {
ans += sum + 1;
sum = -1;
}
if (i % 2 == 0 && sum <= 0) {
ans += 1 - sum;
sum = 1;
}
}
}
if (a[0] < 0) {
for (Int i = 1; i < n; i++) {
sum += a[i];
if (i % 2 == 1 && sum <= 0) {
ans += 1 - sum;
sum = 1;
}
if (i % 2 == 0 && sum >= 0) {
ans += sum + 1;
sum = -1;
}
}
}
std::cout << ans << std::endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
signed main() {
int n;
cin >> n;
vector<int> A(n), S(n);
for (int i = 0; i < n; ++i) {
cin >> A[i];
}
int posC = 0, negC = 0;
if (A[0] <= 0) {
posC += abs(A[0]) + 1;
S[0] = 1;
} else
S[0] = A[0];
for (int i = 1; i < n; ++i) {
S[i] = S[i - 1] + A[i];
if (S[i] * S[i - 1] < 0) continue;
posC += abs(S[i]) + 1;
if (S[i - 1] < 0)
S[i] = 1;
else
S[i] = -1;
}
if (A[0] >= 0) {
negC += A[0] + 1;
S[0] = -1;
} else
S[0] = A[0];
for (int i = 1; i < n; ++i) {
S[i] = S[i - 1] + A[i];
if (S[i] * S[i - 1] < 0) continue;
negC += abs(S[i]) + 1;
if (S[i - 1] < 0)
S[i] = 1;
else
S[i] = -1;
}
cout << min(negC, posC) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int a[100005];
int main() {
int n, sum;
long long num = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sum = a[0];
bool flag;
if (a[0] < 0)
flag = true;
else
flag = false;
for (int i = 1; i < n; i++) {
sum += a[i];
if (flag) {
flag = !flag;
if (sum > 0)
continue;
else if (sum == 0)
num += 1, sum = 1;
else
num += 1 - sum, sum = 1;
} else {
flag = !flag;
if (sum < 0)
continue;
else if (sum == 0)
num += 1, sum = -1;
else
num += sum + 1, sum = -1;
}
}
cout << num << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < (n); i++) cin >> a[i];
vector<long long> sums(n);
long long ans = 0;
sums[0] = a[0];
for (int i = 1; i < n; i++) {
sums[i] = sums[i - 1] + a[i];
if (sums[i - 1] > 0 && sums[i] >= 0) {
ans += a[i] + sums[i - 1] + 1;
a[i] = -(sums[i - 1] + 1);
sums[i] = sums[i - 1] + a[i];
} else if (sums[i - 1] < 0 && sums[i] <= 0) {
ans += (-a[i]) + (-sums[i - 1] + 1);
a[i] = (-sums[i - 1] + 1);
sums[i] = sums[i - 1] + a[i];
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
long long ans = 0, cumsum = a[0];
if (a[0] == 0) {
int i = 1;
while (a[i] == 0 && i < n) i++;
if (i == n || (a[i] < 0 && i % 2 == 1) || (a[i] > 0 && i % 2 == 0))
cumsum = 1;
else
cumsum = -1;
ans += 1;
}
for (int i = 1; i < n; i++) {
if (cumsum > 0) {
if (cumsum + a[i] >= 0) {
ans += cumsum + a[i] + 1;
cumsum = -1;
} else
cumsum += a[i];
} else {
if (cumsum + a[i] <= 0) {
ans += 1 - cumsum - a[i];
cumsum = 1;
} else
cumsum += a[i];
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = std::vector<int>;
using vc = std::vector<char>;
using vll = std::vector<long long>;
using vs = std::vector<string>;
using Mi = map<int, int>;
using Mll = map<ll, ll>;
using UMi = unordered_map<int, int>;
using UMll = unordered_map<ll, ll>;
using Pi = pair<int, int>;
using Pll = pair<ll, ll>;
using vPi = vector<Pi>;
using vPll = vector<Pll>;
using vvi = vector<vector<int>>;
using vvll = vector<vector<ll>>;
using vvc = vector<vector<char>>;
using vvs = vector<vector<string>>;
using pqgi = priority_queue<int, vector<int>, greater<int>>;
using pqsi = priority_queue<int, vector<int>, less<int>>;
using pqgll = priority_queue<int, vector<int>, greater<int>>;
using pssll = priority_queue<int, vector<int>, less<int>>;
template <class T>
using vec = vector<T>;
class Cpmath {
public:
template <typename T>
static T gcd(T a, T b) {
if (a == 0) return b;
return gcd(b % a, a);
}
template <typename T>
static T findGCD(vector<T>& arr, size_t n) {
T result = arr[0];
for (size_t i = 1; i < n; i++) result = gcd(arr[i], result);
return result;
}
template <typename T>
static T findLCM(vector<T>& arr, size_t n) {
T lcm = arr[0];
for (size_t i = 1; i < n; i++) {
lcm = (lcm / gcd(arr[i], lcm)) * arr[i];
}
return lcm;
}
template <typename T>
static bool is_prime(T n) {
if (n == 1) {
return false;
}
for (size_t i = 2; i <= pow(n, 0.5); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
static ll fact(ll n) {
if (n == 0) {
return 1LL;
} else {
return n * fact(n - 1);
}
}
static ll permutation(int n, int r) {
assert(n >= r);
ll ret = 1;
for (int i = n; i > n - r; i--) {
ret *= i;
}
return ret;
}
};
class NCR {
private:
static const int MAX = 210000;
static const int MOD = 998244353;
ll fac[MAX], finv[MAX], inv[MAX];
public:
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
ll COM(int n, int k) {
if (n < k) return 0;
if (n < 0 || k < 0) {
return 0;
}
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
};
struct BipartiteMatching {
vector<vector<int>> E;
int n, m;
vector<int> match, dist;
void init(int _n, int _m) {
n = _n, m = _m;
E.resize(n + m + 2);
match.resize(n + m + 2);
dist.resize(n + m + 2);
}
bool bfs() {
queue<int> que;
for (int i = (1); i < (n + 1); i++) {
if (!match[i])
dist[i] = 0, que.push(i);
else
dist[i] = 1e9;
}
dist[0] = 1e9;
while (!que.empty()) {
int u = que.front();
que.pop();
if (u)
for (auto& v : E[u])
if (dist[match[v]] == 1e9) {
dist[match[v]] = dist[u] + 1;
que.push(match[v]);
}
}
return (dist[0] != 1e9);
}
bool dfs(int u) {
if (u) {
for (auto& v : E[u])
if (dist[match[v]] == dist[u] + 1)
if (dfs(match[v])) {
match[v] = u;
match[u] = v;
return true;
}
dist[u] = 1e9;
return false;
}
return true;
}
void add(int a, int b) {
b += n;
E[a + 1].push_back(b + 1);
E[b + 1].push_back(a + 1);
}
int whois(int x) { return match[x + 1] - 1; }
int solve() {
for (int i = (0); i < (n + m + 1); i++) match[i] = 0;
int res = 0;
while (bfs())
for (int i = (1); i < (n + 1); i++)
if (!match[i] && dfs(i)) res++;
return res;
}
};
struct SegmentTree {
private:
int n;
vector<int> node;
public:
SegmentTree(vector<int> v) {
int sz = v.size();
n = 1;
while (n < sz) n *= 2;
node.resize(2 * n - 1, 1e9);
for (int i = 0; i < sz; i++) {
node[i + n - 1] = v[i];
}
for (int i = n - 2; i >= 0; i--) {
node[i] = min(node[2 * i + 1], node[2 * i + 2]);
}
}
void update(int x, int val) {
x += (n - 1);
node[x] = val;
while (x > 0) {
x = (x - 1) / 2;
node[x] = min(node[2 * x + 1], node[2 * x + 2]);
}
}
int getmin(int a, int b, int k = 0, int l = 0, int r = -1) {
if (r < 0) {
r = n;
}
if (r <= a || b <= l) {
return 1e9;
}
if (a <= l && r <= b) {
return node[k];
}
int vl = getmin(a, b, 2 * k + 1, l, (l + r) / 2);
int vr = getmin(a, b, 2 * k + 2, (l + r) / 2, r);
return min(vl, vr);
}
};
template <class Abel>
struct WUnionFind {
vector<int> par;
vector<int> rank;
vector<Abel> diff_weight;
WUnionFind(int n = 1, Abel SUM_UNITY = 0) { init(n, SUM_UNITY); }
void init(int n = 1, Abel SUM_UNITY = 0) {
par.resize(n);
rank.resize(n);
diff_weight.resize(n);
for (int i = 0; i < n; ++i)
par[i] = i, rank[i] = 0, diff_weight[i] = SUM_UNITY;
}
int root(int x) {
if (par[x] == x) {
return x;
} else {
int r = root(par[x]);
diff_weight[x] += diff_weight[par[x]];
return par[x] = r;
}
}
Abel weight(int x) {
root(x);
return diff_weight[x];
}
bool issame(int x, int y) { return root(x) == root(y); }
bool merge(int x, int y, Abel w) {
w += weight(x);
w -= weight(y);
x = root(x);
y = root(y);
if (x == y) return false;
if (rank[x] < rank[y]) swap(x, y), w = -w;
if (rank[x] == rank[y]) ++rank[x];
par[y] = x;
diff_weight[y] = w;
return true;
}
Abel diff(int x, int y) { return weight(y) - weight(x); }
};
struct UnionFind {
vector<int> par;
UnionFind(int n) : par(n, -1) {}
void init(int n) { par.assign(n, -1); }
int root(int x) {
if (par[x] < 0)
return x;
else
return par[x] = root(par[x]);
}
bool issame(int x, int y) { return root(x) == root(y); }
bool merge(int x, int y) {
x = root(x);
y = root(y);
if (x == y) return false;
if (par[x] > par[y]) {
swap(x, y);
}
par[x] += par[y];
par[y] = x;
return true;
}
int size(int x) { return -par[root(x)]; }
};
void YN(bool a) { cout << (a ? "YES" : "NO") << "\n"; }
void Yn(bool a) { cout << (a ? "Yes" : "No") << "\n"; }
void yn(bool a) { cout << (a ? "yes" : "no") << "\n"; }
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;
}
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
template <typename T>
inline int digitsum(T num) {
int ret = 0;
while (num) {
ret += num % 10;
num /= 10;
}
return ret;
}
template <typename InputIt, typename T>
inline bool argexist(InputIt first, InputIt last, const T& x) {
if (std::find(first, last, x) != last) {
return true;
} else {
return false;
}
}
template <typename InputIt, typename T>
inline int argfind(InputIt first, InputIt last, const T& x) {
auto it = find(first, last, x);
return distance(first, it);
}
template <typename InputIt>
inline int argmax(InputIt first, InputIt last) {
auto it = max_element(first, last);
return distance(first, it);
}
template <typename InputIt>
inline int argmin(InputIt first, InputIt last) {
auto it = min_element(first, last);
return distance(first, it);
}
template <typename T>
inline void erasebv(vector<T>& c, T v) {
c.erase(remove(begin(c), end(c), v), end(c));
}
template <typename T>
inline void uniq(T& c) {
c.erase(unique(begin(c), end(c)), end(c));
}
template <typename T>
inline T POP_BACK(vector<T>& que) {
T x = que.back();
que.pop_back();
return x;
}
template <typename T>
inline T POP_BACK(deque<T>& que) {
T x = que.back();
que.pop_back();
return x;
}
template <typename T>
inline T POP_FRONT(deque<T>& que) {
T x = que.front();
que.pop_front();
return x;
}
template <typename T, typename C>
inline T POP(stack<T, C>& stk) {
T x = stk.top();
stk.pop();
return x;
}
template <typename T, typename C>
inline T POP(queue<T, C>& que) {
T x = que.front();
que.pop();
return x;
}
template <typename T, typename Cont, typename Cmp>
inline T POP(priority_queue<T, Cont, Cmp>& que) {
T x = que.top();
que.pop();
return x;
}
template <typename T1, typename T2>
ostream& operator<<(ostream& s, const pair<T1, T2>& p) {
return s << "(" << p.first << ", " << p.second << ")";
}
template <typename T>
ostream& operator<<(ostream& s, const vector<T>& v) {
int len = v.size();
for (int i = 0; i < len; ++i) {
s << v[i];
if (i < len - 1) s << "\t";
}
return s;
}
template <typename T>
ostream& operator<<(ostream& s, const vector<vector<T>>& vv) {
int len = vv.size();
for (int i = 0; i < len; ++i) {
s << vv[i] << "\n";
}
return s;
}
namespace GraphLib {
using Weight = int;
struct Edge {
int src, dst;
Weight weight;
Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {}
};
bool operator<(const Edge& e, const Edge& f) {
return e.weight != f.weight ? e.weight > f.weight
: e.src != f.src ? e.src < f.src
: e.dst < f.dst;
}
using Edges = vector<Edge>;
using Graph = vector<Edges>;
using Array = vector<Weight>;
using Matrix = vector<Array>;
void DijkstraShortestPath(const Graph& g, int s, vector<Weight>& dist,
vector<int>& prev) {
int n = g.size();
dist.assign(n, 1e9);
dist[s] = 0;
prev.assign(n, -1);
priority_queue<Edge> Q;
for (Q.push(Edge(-2, s, 0)); !Q.empty();) {
Edge e = Q.top();
Q.pop();
if (prev[e.dst] != -1) continue;
prev[e.dst] = e.src;
for (auto& f : g[e.dst]) {
if (dist[f.dst] > e.weight + f.weight) {
dist[f.dst] = e.weight + f.weight;
Q.push(Edge(f.src, f.dst, e.weight + f.weight));
}
}
}
}
vector<int> DijkstraBuildPath(const vector<int>& prev, int t) {
vector<int> path;
for (int u = t; u >= 0; u = prev[u]) path.push_back(u);
reverse(path.begin(), path.end());
return path;
}
bool BellmandFordshortestPath(const Graph g, int s, vector<Weight>& dist,
vector<int>& prev) {
int n = g.size();
dist.assign(n, 1e9 + 1e9);
dist[s] = 0;
prev.assign(n, -2);
bool negative_cycle = false;
for (int k = 0; k < (int)(n); k++)
for (int i = 0; i < (int)(n); i++)
for (auto& e : g[i]) {
if (dist[e.dst] > dist[e.src] + e.weight) {
dist[e.dst] = dist[e.src] + e.weight;
prev[e.dst] = e.src;
if (k == n - 1) {
dist[e.dst] = -1e9;
negative_cycle = true;
}
}
}
return !negative_cycle;
}
vector<int> BellmanFordbuildPath(const vector<int>& prev, int t) {
vector<int> path;
for (int u = t; u >= 0; u = prev[u]) path.push_back(u);
reverse(path.begin(), path.end());
return path;
}
void visit(const Graph& g, int v, int u, Edges& brdg,
vector<vector<int>>& tecomp, stack<int>& roots, stack<int>& S,
vector<bool>& inS, vector<int>& num, int& time) {
num[v] = ++time;
S.push(v);
inS[v] = true;
roots.push(v);
for (auto& e : g[v]) {
int w = e.dst;
if (num[w] == 0)
visit(g, w, v, brdg, tecomp, roots, S, inS, num, time);
else if (u != w && inS[w])
while (num[roots.top()] > num[w]) roots.pop();
}
if (v == roots.top()) {
brdg.push_back(Edge(u, v, 1));
tecomp.push_back(vector<int>());
while (1) {
int w = S.top();
S.pop();
inS[w] = false;
tecomp.back().push_back(w);
if (v == w) break;
}
roots.pop();
}
}
void bridge(const Graph& g, Edges& brdg, vector<vector<int>>& tecomp) {
const int n = g.size();
vector<int> num(n);
vector<bool> inS(n);
stack<int> roots, S;
int time = 0;
for (int u = 0; u < (int)(n); u++)
if (num[u] == 0) {
visit(g, u, n, brdg, tecomp, roots, S, inS, num, time);
brdg.pop_back();
}
}
pair<Weight, Edges> minimumSpanningTree(const Graph& g, int r = 0) {
int n = g.size();
Edges T;
Weight total = 0;
vector<bool> visited(n);
priority_queue<Edge> Q;
Q.push(Edge(-1, r, 0));
while (!Q.empty()) {
Edge e = Q.top();
Q.pop();
if (visited[e.dst]) continue;
T.push_back(e);
total += e.weight;
visited[e.dst] = true;
for (auto& f : g[e.dst])
if (!visited[f.dst]) Q.push(f);
}
return pair<Weight, Edges>(total, T);
}
} // namespace GraphLib
ll N;
vll a;
vll s;
signed main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
cin >> N;
a.resize(N);
vll s(N + 1, 0);
for (int i = 0; i < (int)(N); i++) {
cin >> a[i];
}
ll ans = (1LL << 60);
for (int j = 0; j < (int)(2); j++) {
ll ret = 0;
bool prev_sign;
s[0] = 0;
s[1] = a[0];
if (a[0] > 0) {
prev_sign = 1;
if (j == 0) {
prev_sign = 1;
ret = ret + abs(a[0] + 1);
s[1] = -1;
}
} else {
prev_sign = 0;
if (j == 0) {
prev_sign = 1;
ret = ret + abs(a[0] - 1);
s[1] = 1;
}
}
for (int i = (1); i < (N); i++) {
if (prev_sign) {
ll cur_s = s[i] + a[i];
if (cur_s < 0) {
s[i + 1] = s[i] + a[i];
} else {
s[i + 1] = -1;
ret += abs(cur_s + 1);
}
prev_sign = 0;
} else {
ll cur_s = s[i] + a[i];
if (cur_s > 0) {
s[i + 1] = s[i] + a[i];
} else {
s[i + 1] = 1;
ret += abs(cur_s - 1);
}
prev_sign = 1;
}
}
ans = min(ret, ans);
}
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 | python3 | n = int(input())
a = list(map(int, input().split()))
count = 0
sum_ = 0
for i in range(n):
if sum_ * (sum_+a[i]) >0:
if sum_ > 0:
count += sum_+a[i]+1
a[i] = -sum_-1
if sum_ < 0:
count += abs(sum_+a[i])+1
a[i] = -sum_+1
sum_ += a[i]
print(count) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
list_a = list(map(int,input().split()))
i = 0
k = 0
count = 0
ans = 0
if list_a[0] > 0:
for i in range(0,n):
ans += list_a[i]
if ans / ((-1) ** i) <= 0:
count += abs(ans - (-1) ** i)
ans = (-1) ** i
else:
for i in range(0,n):
ans += list_a[i]
if ans / ((-1) ** (i+1)) <= 0:
count += abs(ans - (-1) ** (i+1))
ans = (-1) ** (i+1)
count1 = count
count = 0
if list_a[0] <= 0:
for i in range(0,n):
ans += list_a[i]
if ans / ((-1) ** i) <= 0:
count += abs(ans - (-1) ** i)
ans = (-1) ** i
else:
for i in range(0,n):
ans += list_a[i]
if ans / ((-1) ** (i+1)) <= 0:
count += abs(ans - (-1) ** (i+1))
ans = (-1) ** (i+1)
count2 = count
print(min(count1, count2))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | n = gets.to_i
as = gets.split(' ').map { |e| e.to_i }
x = 0
bs = []
as.each { |e|
x += e
bs << x
}
# p bs
ans = 0
for i in (1..(n - 1))
a, b = bs[i - 1], bs[i]
if a >= 0 && b >= 0
d = b + 1
for j in (i..(n-1))
bs[j] = bs[j] - d
end
# p bs
ans += d
elsif a <= 0 && b <= 0
d = -1 * b + 1
for j in (i..(n-1))
bs[j] = bs[j] + d
end
# p bs
ans += d
end
end
ans += 1 if bs[n-1] == 0
puts ans |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <bits/stdc++.h>
int main(int argc, char *argv[]) {
int i, n, t = 0, a, sign, c = 0;
scanf("%d", &n);
scanf("%d", &t);
sign = t > 0 ? -1 : 1;
for (i = 1; i < n; i++) {
scanf("%d", &a);
if (sign < 0) {
if (t + a >= 0) {
c += abs(-1 - t - a);
t = -1;
} else {
t += a;
}
} else {
if (t + a <= 0) {
c += abs(1 - t - a);
t = 1;
} else {
t += a;
}
}
sign *= -1;
}
printf("%d\n", c);
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 解():
iN = int(input())
aA = [int(_) for _ in input().split()]
iL = len(aA)
iStart = 0
if sum(aA[0::2]) < sum(aA[2::2]):
iStart = 1
iC = 0
aD = [0]*iL
if 0 % 2 == iStart :
if aA[0] <= 0:
aA[0] = 1
iC += -1 * aA[0] + 1
else:
if 0 <= aA[0] :
aA[0] = -1
iC += aA[0] + 1
aD[0] = aA[0]
for i in range(1,iL):
aD[i] = aD[i-1]+aA[i]
if i % 2 == iStart:
if aD[i] <= 0:
iC += -1*aD[i] +1
aD[i] = 1
else:
if aD[i] >= 0:
iC += aD[i] +1
aD[i] = -1
print(iC)
解()
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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 stdout
printn = lambda x: stdout.write(str(x))
inn = lambda : int(input())
inl = lambda: list(map(int, input().split()))
inm = lambda: map(int, input().split())
ins = lambda : input().strip()
DBG = True # and False
BIG = 999999999
R = 10**9 + 7
def ddprint(x):
if DBG:
print(x)
n = inn()
a = inl()
ne = sum(a[::2])
no = sum(a[1::2])
p0 = (ne>no)
cnt = 0
acc = 0
#ddprint("ne {} no {} p0 {} a {}".format(ne,no,p0,a))
for i in range(n):
if (p0 if i%2==0 else not p0):
x = max(0, 1-a[i]-acc)
cnt += x
acc += a[i]+x
else:
x = max(0, a[i]+acc+1)
cnt += x
acc += a[i]-x
#ddprint("cnt {} acc {}".format(cnt,acc))
print(cnt)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
template <typename T1, typename T2>
inline void chmin(T1& a, T2 b) {
if (a > b) a = b;
}
template <typename T1, typename T2>
inline void chmax(T1& a, T2 b) {
if (a < b) a = b;
}
using namespace std;
std::mt19937 mt((long long)time(0));
long long dx[4] = {0, 1, 0, -1};
long long dy[4] = {1, 0, -1, 0};
using Weight = long long;
using Flow = long long;
struct Edge {
long long src, dst;
Weight weight;
Flow cap;
Edge() : src(0), dst(0), weight(0) {}
Edge(long long s, long long d, Weight w) : src(s), dst(d), weight(w) {}
};
using Edges = std::vector<Edge>;
using Graph = std::vector<Edges>;
using Array = std::vector<Weight>;
using Matrix = std::vector<Array>;
void add_edge(Graph& g, long long a, long long b, Weight w = 1) {
g[a].emplace_back(a, b, w);
g[b].emplace_back(b, a, w);
}
void add_arc(Graph& g, long long a, long long b, Weight w = 1) {
g[a].emplace_back(a, b, w);
}
struct uf_tree {
std::vector<long long> parent;
long long __size;
uf_tree(long long size_) : parent(size_, -1), __size(size_) {}
void unite(long long x, long long y) {
if ((x = find(x)) != (y = find(y))) {
if (parent[y] < parent[x]) std::swap(x, y);
parent[x] += parent[y];
parent[y] = x;
__size--;
}
}
bool is_same(long long x, long long y) { return find(x) == find(y); }
long long find(long long x) {
return parent[x] < 0 ? x : parent[x] = find(parent[x]);
}
long long size(long long x) { return -parent[find(x)]; }
long long size() { return __size; }
};
template <signed M, unsigned T>
struct mod_int {
constexpr static signed MODULO = M;
constexpr static unsigned TABLE_SIZE = T;
signed x;
mod_int() : x(0) {}
mod_int(long long y)
: x(static_cast<signed>(y >= 0 ? y % MODULO : MODULO - (-y) % MODULO)) {}
mod_int(signed y) : x(y >= 0 ? y % MODULO : MODULO - (-y) % MODULO) {}
mod_int& operator+=(const mod_int& rhs) {
if ((x += rhs.x) >= MODULO) x -= MODULO;
return *this;
}
mod_int& operator-=(const mod_int& rhs) {
if ((x += MODULO - rhs.x) >= MODULO) x -= MODULO;
return *this;
}
mod_int& operator*=(const mod_int& rhs) {
x = static_cast<signed>(1LL * x * rhs.x % MODULO);
return *this;
}
mod_int& operator/=(const mod_int& rhs) {
x = static_cast<signed>((1LL * x * rhs.inv().x) % MODULO);
return *this;
}
mod_int operator-() const { return mod_int(-x); }
mod_int operator+(const mod_int& rhs) const { return mod_int(*this) += rhs; }
mod_int operator-(const mod_int& rhs) const { return mod_int(*this) -= rhs; }
mod_int operator*(const mod_int& rhs) const { return mod_int(*this) *= rhs; }
mod_int operator/(const mod_int& rhs) const { return mod_int(*this) /= rhs; }
bool operator<(const mod_int& rhs) const { return x < rhs.x; }
mod_int inv() const {
assert(x != 0);
if (x <= static_cast<signed>(TABLE_SIZE)) {
if (_inv[1].x == 0) prepare();
return _inv[x];
} else {
signed a = x, b = MODULO, u = 1, v = 0, t;
while (b) {
t = a / b;
a -= t * b;
std::swap(a, b);
u -= t * v;
std::swap(u, v);
}
return mod_int(u);
}
}
mod_int pow(long long t) const {
assert(!(x == 0 && t == 0));
mod_int e = *this, res = mod_int(1);
for (; t; e *= e, t >>= 1)
if (t & 1) res *= e;
return res;
}
mod_int fact() {
if (_fact[0].x == 0) prepare();
return _fact[x];
}
mod_int inv_fact() {
if (_fact[0].x == 0) prepare();
return _inv_fact[x];
}
mod_int choose(mod_int y) {
assert(y.x <= x);
return this->fact() * y.inv_fact() * mod_int(x - y.x).inv_fact();
}
static mod_int _inv[TABLE_SIZE + 1];
static mod_int _fact[TABLE_SIZE + 1];
static mod_int _inv_fact[TABLE_SIZE + 1];
static void prepare() {
_inv[1] = 1;
for (long long i = 2; i <= (long long)TABLE_SIZE; ++i) {
_inv[i] = 1LL * _inv[MODULO % i].x * (MODULO - MODULO / i) % MODULO;
}
_fact[0] = 1;
for (unsigned i = 1; i <= TABLE_SIZE; ++i) {
_fact[i] = _fact[i - 1] * signed(i);
}
_inv_fact[TABLE_SIZE] = _fact[TABLE_SIZE].inv();
for (long long i = (long long)TABLE_SIZE - 1; i >= 0; --i) {
_inv_fact[i] = _inv_fact[i + 1] * (i + 1);
}
}
};
template <signed M, unsigned F>
std::ostream& operator<<(std::ostream& os, const mod_int<M, F>& rhs) {
return os << rhs.x;
}
template <signed M, unsigned F>
std::istream& operator>>(std::istream& is, mod_int<M, F>& rhs) {
long long s;
is >> s;
rhs = mod_int<M, F>(s);
return is;
}
template <signed M, unsigned F>
mod_int<M, F> mod_int<M, F>::_inv[TABLE_SIZE + 1];
template <signed M, unsigned F>
mod_int<M, F> mod_int<M, F>::_fact[TABLE_SIZE + 1];
template <signed M, unsigned F>
mod_int<M, F> mod_int<M, F>::_inv_fact[TABLE_SIZE + 1];
template <signed M, unsigned F>
bool operator==(const mod_int<M, F>& lhs, const mod_int<M, F>& rhs) {
return lhs.x == rhs.x;
}
template <long long M, unsigned F>
bool operator!=(const mod_int<M, F>& lhs, const mod_int<M, F>& rhs) {
return !(lhs == rhs);
}
const signed MF = 1000010;
const signed MOD = 1000000007;
using mint = mod_int<MOD, MF>;
mint binom(long long n, long long r) {
return (r < 0 || r > n || n < 0) ? 0 : mint(n).choose(r);
}
mint fact(long long n) { return mint(n).fact(); }
mint inv_fact(long long n) { return mint(n).inv_fact(); }
template <typename T, typename E>
struct SegmentTree {
typedef function<T(T, T)> F;
typedef function<T(T, E)> G;
typedef function<E(E, E)> H;
typedef function<E(E, long long)> P;
long long n;
F f;
G g;
H h;
P p;
T d1;
E d0;
vector<T> dat;
vector<E> laz;
SegmentTree(
long long n_, F f, G g, H h, T d1, E d0, vector<T> v = vector<T>(),
P p = [](E a, long long b) { return a; })
: f(f), g(g), h(h), d1(d1), d0(d0), p(p) {
init(n_);
if (n_ == (long long)v.size()) build(n_, v);
}
void init(long long n_) {
n = 1;
while (n < n_) n *= 2;
dat.clear();
dat.resize(2 * n - 1, d1);
laz.clear();
laz.resize(2 * n - 1, d0);
}
void build(long long n_, vector<T> v) {
for (long long i = 0; i < n_; i++) dat[i + n - 1] = v[i];
for (long long i = n - 2; i >= 0; i--)
dat[i] = f(dat[i * 2 + 1], dat[i * 2 + 2]);
}
inline void eval(long long len, long long k) {
if (laz[k] == d0) return;
if (k * 2 + 1 < n * 2 - 1) {
laz[k * 2 + 1] = h(laz[k * 2 + 1], laz[k]);
laz[k * 2 + 2] = h(laz[k * 2 + 2], laz[k]);
}
dat[k] = g(dat[k], p(laz[k], len));
laz[k] = d0;
}
T update(long long a, long long b, E x, long long k, long long l,
long long r) {
eval(r - l, k);
if (r <= a || b <= l) return dat[k];
if (a <= l && r <= b) {
laz[k] = h(laz[k], x);
return g(dat[k], p(laz[k], r - l));
}
return dat[k] = f(update(a, b, x, k * 2 + 1, l, (l + r) / 2),
update(a, b, x, k * 2 + 2, (l + r) / 2, r));
}
T update(long long a, long long b, E x) { return update(a, b, x, 0, 0, n); }
T query(long long a, long long b, long long k, long long l, long long r) {
eval(r - l, k);
if (r <= a || b <= l) return d1;
if (a <= l && r <= b) return dat[k];
T vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
return f(vl, vr);
}
T query(long long a, long long b) { return query(a, b, 0, 0, n); }
};
class compress {
public:
static const long long MAP = 10000000;
map<long long, long long> zip;
long long unzip[MAP];
compress(vector<long long>& x) {
sort(x.begin(), x.end());
x.erase(unique(x.begin(), x.end()), x.end());
for (long long i = 0; i < x.size(); i++) {
zip[x[i]] = i;
unzip[i] = x[i];
}
}
};
unsigned euclidean_gcd(unsigned a, unsigned b) {
while (1) {
if (a < b) swap(a, b);
if (!b) break;
a %= b;
}
return a;
}
template <class T>
struct CumulativeSum2D {
vector<vector<T>> data;
CumulativeSum2D(long long W, long long H)
: data(W + 1, vector<long long>(H + 1, 0)) {}
void add(long long x, long long y, T z) {
++x, ++y;
if (x >= data.size() || y >= data[0].size()) return;
data[x][y] += z;
}
void build() {
for (long long i = 1; i < data.size(); i++) {
for (long long j = 1; j < data[i].size(); j++) {
data[i][j] += data[i][j - 1] + data[i - 1][j] - data[i - 1][j - 1];
}
}
}
T query(long long sx, long long sy, long long gx, long long gy) {
return (data[gx][gy] - data[sx][gy] - data[gx][sy] + data[sx][sy]);
}
};
long long nC2(long long n) { return n * (n - 1) / 2; }
class node {
public:
long long depth;
long long num;
node(long long d, long long n) {
depth = d;
num = n;
}
};
CumulativeSum2D<long long> sumB(4001, 4001);
template <class T>
struct CumulativeSum {
vector<T> data;
CumulativeSum(long long sz) : data(sz, 0){};
void add(long long k, T x) { data[k] += x; }
void build() {
for (long long i = 1; i < data.size(); i++) {
data[i] += data[i - 1];
}
}
T query(long long k) {
if (k < 0) return (0);
return (data[min(k, (long long)data.size() - 1)]);
}
T query(long long left, long long right) {
return query(right) - query(left - 1);
}
};
std::vector<bool> IsPrime;
void sieve(size_t max) {
if (max + 1 > IsPrime.size()) {
IsPrime.resize(max + 1, true);
}
IsPrime[0] = false;
IsPrime[1] = false;
for (size_t i = 2; i * i <= max; ++i)
if (IsPrime[i])
for (size_t j = 2; i * j <= max; ++j) IsPrime[i * j] = false;
}
vector<int64_t> divisor(int64_t n) {
vector<int64_t> ret;
for (int64_t i = 1; i * i <= n; i++) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n) ret.push_back(n / i);
}
}
sort(begin(ret), end(ret));
return (ret);
}
long long binary_search(function<bool(long long)> isOk, long long ng,
long long ok) {
while (abs(ok - ng) > 1) {
long long mid = (ok + ng) / 2;
if (isOk(mid))
ok = mid;
else
ng = mid;
}
return ok;
}
std::pair<std::vector<Weight>, bool> bellmanFord(const Graph& g, long long s) {
long long n = g.size();
const Weight inf = std::numeric_limits<Weight>::max() / 8;
Edges es;
for (long long i = 0; i < n; i++)
for (auto& e : g[i]) es.emplace_back(e);
std::vector<Weight> dist(n, inf);
dist[s] = 0;
bool negCycle = false;
for (long long i = 0;; i++) {
bool update = false;
for (auto& e : es) {
if (dist[e.src] != inf && dist[e.dst] > dist[e.src] + e.weight) {
dist[e.dst] = dist[e.src] + e.weight;
update = true;
}
}
if (!update) break;
if (i > n) {
negCycle = true;
break;
}
}
return std::make_pair(dist, !negCycle);
}
std::pair<std::vector<Weight>, bool> bellmanFord(const Graph& g, long long s,
long long d) {
long long n = g.size();
const Weight inf = std::numeric_limits<Weight>::max() / 8;
Edges es;
for (long long i = 0; i < n; i++)
for (auto& e : g[i]) es.emplace_back(e);
std::vector<Weight> dist(n, inf);
dist[s] = 0;
bool negCycle = false;
for (long long i = 0; i < n * 2; i++) {
bool update = false;
for (auto& e : es) {
if (dist[e.src] != inf && dist[e.dst] > dist[e.src] + e.weight) {
dist[e.dst] = dist[e.src] + e.weight;
update = true;
if (e.dst == d && i == n * 2 - 1) negCycle = true;
}
}
if (!update) break;
}
return std::make_pair(dist, !negCycle);
}
vector<long long> Manachar(string S) {
long long len = S.length();
vector<long long> R(len);
long long i = 0, j = 0;
while (i < S.size()) {
while (i - j >= 0 && i + j < S.size() && S[i - j] == S[i + j]) ++j;
R[i] = j;
long long k = 1;
while (i - k >= 0 && i + k < S.size() && k + R[i - k] < j)
R[i + k] = R[i - k], ++k;
i += k;
j -= k;
}
return R;
}
std::vector<long long> tsort(const Graph& g) {
long long n = g.size(), k = 0;
std::vector<long long> ord(n), in(n);
for (auto& es : g)
for (auto& e : es) in[e.dst]++;
std::queue<long long> q;
for (long long i = 0; i < n; ++i)
if (in[i] == 0) q.push(i);
while (q.size()) {
long long v = q.front();
q.pop();
ord[k++] = v;
for (auto& e : g[v]) {
if (--in[e.dst] == 0) {
q.push(e.dst);
}
}
}
return *std::max_element(in.begin(), in.end()) == 0
? ord
: std::vector<long long>();
}
std::vector<Weight> dijkstra(const Graph& g, long long s) {
const Weight INF = std::numeric_limits<Weight>::max() / 8;
using state = std::tuple<Weight, long long>;
std::priority_queue<state> q;
std::vector<Weight> dist(g.size(), INF);
dist[s] = 0;
q.emplace(0, s);
while (q.size()) {
Weight d;
long long v;
std::tie(d, v) = q.top();
q.pop();
d *= -1;
if (dist[v] < d) continue;
for (auto& e : g[v]) {
if (dist[e.dst] > dist[v] + e.weight) {
dist[e.dst] = dist[v] + e.weight;
q.emplace(-dist[e.dst], e.dst);
}
}
}
return dist;
}
Matrix WarshallFloyd(const Graph& g) {
auto const INF = std::numeric_limits<Weight>::max() / 8;
long long n = g.size();
Matrix d(n, Array(n, INF));
for (long long i = (0); i < (long long)(n); i++) d[i][i] = 0;
for (long long i = (0); i < (long long)(n); i++)
for (auto& e : g[i]) d[e.src][e.dst] = std::min(d[e.src][e.dst], e.weight);
for (long long k = (0); k < (long long)(n); k++)
for (long long i = (0); i < (long long)(n); i++)
for (long long j = (0); j < (long long)(n); j++) {
if (d[i][k] != INF && d[k][j] != INF) {
d[i][j] = std::min(d[i][j], d[i][k] + d[k][j]);
}
}
return d;
}
const long long BLACK = 1, WHITE = 0;
bool isValid(vector<vector<long long>>& mapData, long long gyo,
long long retu) {
bool f = true;
for (long long i = (0); i < (long long)(gyo); i++) {
for (long long j = (0); j < (long long)(retu); j++) {
long long colorCnt = 0;
if (j > 0 && mapData[i][j] == mapData[i][j - 1]) {
colorCnt++;
}
if (i > 0 && mapData[i][j] == mapData[i - 1][j]) {
colorCnt++;
}
if (i < gyo - 1 && mapData[i][j] == mapData[i + 1][j]) {
colorCnt++;
}
if (j < retu - 1 && mapData[i][j] == mapData[i][j + 1]) {
colorCnt++;
}
if (colorCnt > 1) {
f = false;
}
}
}
return f;
}
void getNext(long long nowX, long long nowY, long long* pOutX, long long* pOutY,
long long gyo, long long retu) {
if (nowX == retu - 1) {
*pOutY = nowY + 1;
*pOutX = 0;
return;
}
*pOutX = nowX + 1;
*pOutY = nowY;
}
void dfs(vector<vector<long long>> mapData, long long nowX, long long nowY,
long long gyo, long long retu, long long* outCnt) {
if (nowX == retu - 1 && nowY == gyo - 1) {
mapData[nowY][nowX] = BLACK;
if (isValid(mapData, gyo, retu)) {
*outCnt = *outCnt + 1;
}
mapData[nowY][nowX] = WHITE;
if (isValid(mapData, gyo, retu)) {
*outCnt = *outCnt + 1;
}
return;
}
mapData[nowY][nowX] = BLACK;
long long nextX, nextY;
getNext(nowX, nowY, &nextX, &nextY, gyo, retu);
dfs(mapData, nextX, nextY, gyo, retu, outCnt);
mapData[nowY][nowX] = WHITE;
getNext(nowX, nowY, &nextX, &nextY, gyo, retu);
dfs(mapData, nextX, nextY, gyo, retu, outCnt);
}
void dec(map<long long, long long>& ma, long long a) {
ma[a]--;
if (ma[a] == 0) {
ma.erase(a);
}
}
long long N;
long long solve(long long ans, vector<long long> A, vector<long long> cu) {
for (long long i = (0); i < (long long)(N); i++) {
if (cu[i] == 0) {
ans++;
if (i == 0) {
if (cu[i + 1] < 0) {
cu[i] = 1;
} else {
cu[i] = -1;
}
} else {
if (cu[i - 1] < 0) {
cu[i] = 1;
} else {
cu[i] = -1;
}
}
}
if (cu[i] < 0 == cu[i + 1] < 0) {
if (cu[i + 1] > 0) {
ans += cu[i + 1] + 1;
cu[i + 1] -= cu[i + 1] + 1;
} else {
ans += -cu[i + 1] + 1;
cu[i + 1] += -cu[i + 1] + 1;
}
}
cu[i + 2] = cu[i + 1] + A[i + 2];
}
return ans;
}
signed main() {
cin >> N;
vector<long long> A(N + 2), A2;
vector<long long> cu(N + 2);
long long su = 0;
for (long long i = (0); i < (long long)(N); i++) {
cin >> A[i];
su += A[i];
cu[i] = su;
}
A2 = A;
long long ans1 = 0, ans2 = 0;
ans1 = solve(ans1, A, cu);
if (A2[0] < 0) {
ans2 = -A2[0] + 1;
A2[0] = 1;
} else {
ans2 = A2[0] + 1;
A2[0] = -1;
}
su = 0;
for (long long i = (0); i < (long long)(N); i++) {
su += A2[i];
cu[i] = su;
}
ans2 = solve(ans2, A2, cu);
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 | #k = int(input())
#s = input()
#a, b = map(int, input().split())
#s, t = map(str, input().split())
#l = list(map(int, input().split()))
#l = [list(map(int,input().split())) for i in range(n)]
#a = [input() for _ in range(n)]
import copy
n = int(input())
a = list(map(int, input().split()))
b = copy.copy(a)
lastSum = a[0]
for i in range(1, n):
nowSum = sum(a[:i+1])
#print(nowSum)
if lastSum > 0:
if nowSum >=0:
a[i] = a[i] - (nowSum+1)
else: #lastSum < 0
if nowSum <= 0:
a[i] = a[i] - (nowSum-1)
lastSum = sum(a[:i+1])
ans = 0
for i in range(n):
ans += abs(a[i]-b[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 | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
long long l1[n + 1];
long long x = 0, s = 0;
for (int i = 1; i <= n; i++) {
cin >> l1[i];
x += l1[i];
if (i == 0 && l1[i] == 0 && l1[i + 1] <= 0)
x++, s++, l1[i] = 1;
else if (i == 0 && l1[i] == 0 && l1[i + 1] > 0)
x--, s++, l1[i] = -1;
if (i >= 2) {
if (x - l1[i] <= 0 && x <= 0) {
s += abs((-x + l1[i] + 1) - l1[i]);
l1[i] = l1[i] - x + 1;
x = 1;
} else if (x - l1[i] >= 0 && x >= 0) {
s += abs(-(x - l1[i] + 1) - l1[i]);
l1[i] = -(x - l1[i] + 1);
x = -1;
}
}
}
cout << s << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < (n); ++i) {
cin >> a[i];
}
if (a[0] != 0) {
long long now = 0;
long long ans = 0;
now += a[0];
for (int i = 1; i < n; i++) {
long long nex = now + a[i];
if (now > 0) {
if (nex >= 0) {
now = -1;
ans += nex + 1;
} else {
now = nex;
}
} else {
if (nex <= 0) {
now = 1;
ans += 1 - nex;
} else {
now = nex;
}
}
}
cout << ans << endl;
} else {
long long now = 0;
long long ans1 = 1;
now = 1;
for (int i = 1; i < n; i++) {
long long nex = now + a[i];
if (now > 0) {
if (nex >= 0) {
now = -1;
ans1 += nex + 1;
} else {
now = nex;
}
} else {
if (nex <= 0) {
now = 1;
ans1 += 1 - nex;
} else {
now = nex;
}
}
}
now = -1;
long long ans2 = 1;
for (int i = 1; i < n; i++) {
long long nex = now + a[i];
if (now > 0) {
if (nex >= 0) {
now = -1;
ans2 += nex + 1;
} else {
now = nex;
}
} else {
if (nex <= 0) {
now = 1;
ans2 += 1 - nex;
} else {
now = nex;
}
}
}
cout << min(ans1, ans2) << endl;
}
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
#define rep(i, a) for (int i = 0; i < (int) (a); i++)
#define reps(i, a, b) for (int i = (int) (a); i < (int) (b); i++)
#define rrep(i, a) for (int i = (int) a-1; i >= 0; i--)
#define rreps(i, a, b) for (int i = (int) (a)-1; i >= (int) (b); i--)
#define MP(a, b) make_pair((a), (b))
#define PB(a) push_back((a))
#define all(v) (v).begin(), (v).end()
#define PRINT(f) if((f)){cout << (TRUE__) << endl;}else{cout << FALSE__ << endl;}
#define TRUE__ "Yes"
#define FALSE__ "No"
#define PERM(v) next_permutation(all(v))
#define UNIQUE(v) sort(all(v));(v).erase(unique(all(v)), v.end())
#define CIN(type, x) type x;cin >> x
#ifdef LOCAL
#define lcout(a) cout << a;
#define lcoutln(a) cout << a << endl;
#define lcerr(a) cerr << a;
#define lcerrln(a) cerr << a << endl;
#else
#define lcout(a)
#define lcoutln(a)
#define lcerr(a)
#define lcerrln(a)
#endif
vector<int> a;
int N;
signed main()
{
cin >> N;
rep(i, N) {
CIN(int, t);
a.PB(t);
}
int res = 0;
int cnt = 0;
int sum = 0;
rep(i, N) {
sum += a[i];
if (sum <= 0) {
cnt += -sum + 1;
sum = 1;
}
i++;
if (i == N) break;
sum += a[i];
if (sum >= 0) {
cnt += sum + 1;
sum = -1;
}
}
res = cnt;
sum = 0;
cnt = 0;
rep (i, N) {
sum += a[i];
if (sum >= 0) {
cnt += sum + 1;
sum = -1;
}
i++;
if (i == N) break;
sum += a[i];
if (sum <= 0) {
cnt += -sum + 1;
sum = 1;
}
}
res = min(res, cnt);
cout << res << endl;
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
const int INF = 1001001001;
const int MOD = 1000000007;
const long long INFL = (1LL << 60);
const double EPS = 1e-9;
bool meet(vi a) {
bool ret = true;
bool pos = (a[0] > 0);
int sum = a[0];
for (int i = (1); i < (int)(a.size()); i++) {
sum += a[i];
if ((pos && i % 2 && sum >= 0) || (pos && !(i % 2) && sum <= 0) ||
(!pos && i % 2 && sum <= 0) || (!pos && !(i % 2) && sum >= 0)) {
ret = false;
break;
}
}
return ret;
}
int solve(vi a, int res) {
int sum = a[0];
bool pos = (a[0] > 0);
for (int i = (1); i < (int)(a.size()); i++) {
if ((pos && i % 2 && sum + a[i] >= 0) ||
(!pos && !(i % 2) && sum + a[i] >= 0)) {
while (true) {
a[i]--;
res++;
if (sum + a[i] == -1) break;
}
} else if ((pos && !(i % 2) && sum + a[i] <= 0) ||
(!pos && i % 2 && sum + a[i] <= 0)) {
while (true) {
a[i]++;
res++;
if (sum + a[i] == 1) break;
}
}
sum += a[i];
}
return res;
}
int main() {
int N;
cin >> N;
vi a(N);
for (int i = 0; i < (int)(N); i++) cin >> a[i];
bool flg = meet(a);
bool pos = (a[0] > 0);
int res = 0;
int res1 = solve(a, res);
res = 0;
if (pos) {
while (a[0] != -1) {
a[0]--;
res++;
}
} else {
while (a[0] != 1) {
a[0]++;
res++;
}
}
int res2 = solve(a, res);
res = min(res1, res2);
if (flg) res = 0;
cout << res << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <iostream>
int a[1000000010];
int main(){
std::cin.tie(0);
std::ios::sync_with_stdio(false);
int n, sum, count, i;
std::cin >> n;
for (i = 0; i < n; i++) std::cin >> a[i];
for (i = 0; i < n; i++){
sum += a[i];
if(sum>0){
while(sum>=-1) count++;
}
else if (sum<0){
while(sum <= 1) count++;
}
std::cout << count << '\n';
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 |
import bisect
import collections
import itertools
def getint(): return int(input())
def getints(): return list(map(int, input().split()))
def getint2d(rows): return [getints() for _ in range(rows)]
def getgrid(rows): return [input() for _ in range(rows)]
def array1d(n, value): return [value for _ in range(n)]
def array2d(n, m, value): return [array1d(m, value) for _ in range(n)]
n = getint()
values = getints()
def solve(values, prev):
res = 0
for v in values:
s = prev + v
add = 0
if prev > 0 and s >= 0:
add = -s - 1
if prev < 0 and s <= 0:
add = -s + 1
res += abs(add)
prev = s + add
return res
res1, res2 = 0, 0
if values[0] <= 0:
add = abs(values[0]) + 1
res1 = solve(values[1:], values[0] + add) + add
res2 = solve(values[1:], values[0])
else:
res1 = solve(values[1:], values[0])
add = -(values[0] + 1)
res2 = solve(values[1:], values[0] + add) + abs(add)
print(min(res1, res2))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
int *a;
int ans = 0;
cin >> n;
a = new int[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int sum = 0;
int opr1 = 0, opr2 = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 1 && sum <= 0) {
int add = 1 - sum;
sum += add;
opr1 += add;
} else if (i % 2 == 0 && sum >= 0) {
int add = -1 - sum;
sum += add;
opr1 += (add * -1);
}
}
sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 0 && sum <= 0) {
int add = 1 - sum;
sum += add;
opr2 += add;
} else if (i % 2 == 1 && sum >= 0) {
int add = -1 - sum;
sum += add;
opr2 += (add * -1);
}
}
ans = min(opr1, opr2);
cout << ans << endl;
delete (a);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll MOD = 1000000007;
const long long INF = numeric_limits<long long>::max();
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() {
ll N;
cin >> N;
vector<ll> vec(N);
for (ll i = 0; i < (N); i++) {
cin >> vec[i];
}
ll count = INF;
{
ll c = 0;
ll sum = 0;
for (ll i = 0; i < N; ++i) {
bool isSign = (i % 2) == 0;
ll tmp = sum + vec[i];
if (isSign != (tmp > 0)) {
ll next = isSign ? 1 : -1;
c += abs(tmp - next);
sum = next;
} else {
sum = tmp;
}
}
chmin(count, c);
}
{
ll c = 0;
ll sum = 0;
for (ll i = 0; i < N; ++i) {
bool isSign = (i % 2) != 0;
ll tmp = sum + vec[i];
if (isSign != (tmp > 0)) {
ll next = isSign ? 1 : -1;
c += abs(tmp - next);
sum = next;
} else {
sum = tmp;
}
}
chmin(count, c);
}
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() {
long long n, i, j, ans = 0, sum = 0, flag;
cin >> n;
vector<long long> a(n);
for (i = 0; i < n; i++) {
cin >> a[i];
}
sum += a[0];
if (sum == 0) {
ans++;
for (i = 0; i < n; i++) {
if (a[i] > 0) {
if (i % 2 == 0) {
sum = 1;
} else {
sum = -1;
}
break;
} else if (a[i] < 0) {
if (i % 2 != 0) {
sum = 1;
} else {
sum = -1;
}
break;
}
}
}
for (i = 1; i < n; i++) {
if (sum > 0) {
flag = 1;
} else {
flag = 0;
}
if (flag == 1) {
sum += a[i];
if (sum >= 0) {
ans += (sum + 1);
sum = -1;
}
} else {
sum += a[i];
if (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 | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long a[n], s[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
if (i == 0) {
s[0] = a[0];
} else {
s[i] = s[i - 1] + a[i];
}
}
long long cng = 0;
long long ans = 0;
if (a[0] == 0) {
cng++;
ans++;
}
for (int i = 1; i < n; i++) {
if (((s[i] + cng < 0) && (0 < s[i - 1] + cng)) ||
((s[i] + cng > 0) && (0 > s[i - 1] + cng))) {
continue;
}
assert(s[i - 1] + cng != 0);
if (s[i] + cng >= 0) {
ans += s[i] + cng + 1;
cng -= s[i] + cng + 1;
} else {
ans += -(s[i] + cng) + 1;
cng += -(s[i] + cng) + 1;
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import sys
input = sys.stdin.readline
n = int(input())
a_list = list(map(int, input().split()))
cnt = 0
a_sum = a_list[0]
if a_sum > 0:
flag = 1
elif a_sum < 0:
flag = -1
else:
flag = 0
cnt += 1
for a in a_list[1:]:
if flag == 0:
if a > 0:
flag = 1
a_sum = a - 1
if a == 1:
cnt += 1
elif a < 0:
flag = -1
a_sum = a + 1
if a == -1:
cnt += 1
else:
flag = 0
cnt += 2
else:
a_sum += a
if flag == 1:
if a_sum >= 0:
cnt += a_sum+1
a_sum = -1
flag = -1
else:
if a_sum <= 0:
cnt += abs(a_sum)+1
a_sum = 1
flag = 1
print(cnt)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int INF = 1001001001;
const int MOD = (int)1e9 + 7;
const long long INFLL = 1001001001001001001;
const long long MODLL = (long long)1e9 + 7;
const double EPS = 1e-9;
int sign(int n) { return (n > 0) - (n < 0); }
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < (n); ++i) cin >> a[i];
int count1 = 0;
int cum1 = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
if (cum1 + a[i] <= 0) {
count1 += abs(cum1 + a[i]) + 1;
cum1 = 1;
} else {
cum1 += a[i];
}
} else {
if (cum1 + a[i] >= 0) {
count1 += abs(cum1 + a[i]) + 1;
cum1 = -1;
} else {
cum1 += a[i];
}
}
}
int count2 = 0;
int cum2 = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
if (cum2 + a[i] >= 0) {
count2 += abs(cum2 + a[i]) + 1;
cum2 = -1;
} else {
cum2 += a[i];
}
} else {
if (cum2 + a[i] <= 0) {
count2 += abs(cum2 + a[i]) + 1;
cum2 = 1;
} else {
cum2 += a[i];
}
}
}
cout << min(count1, count2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | 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 = a[0];
int cnt = 0;
int index = 1;
bool toggle = true;
while (index<n) {
if (sum + a[index] == 0) {
a[index] += sum < 0 ? 1 : -1;
cnt++;
}else if ((sum < 0 && sum + a[index] < 0) || (sum > 0 && sum + a[index] > 0)) {
if (toggle && Math.Abs(a[index - 1]) > 2) {
int dir = a[index - 1] < 0 ? 1 : -1;
a[index - 1] += dir;
sum += dir;
toggle = true;
} else {
int dir = a[index] < 0 ? 1 : -1;
a[index] += dir;
toggle = false;
}
cnt++;
} else {
sum += a[index];
index++;
}
}
cnt.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 | java | import java.util.*;
// ABC 6-C
// http://abc006.contest.atcoder.jp/tasks/abc006_3
public class Main {
public static void main (String[] args) throws java.lang.Exception {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = in.nextInt();
}
int answer = 0;
if (nums[0] == 0) {
answer = solve(nums, 0, 0);
} else {
answer = solve(nums, nums[0], 1);
}
System.out.println(answer);
}
public static int solve(int[] nums, int sum, int index) {
if (index == nums.length) {
return 0;
}
if (sum < 0 && sum + nums[index] < 0) {
return 1 + Math.abs(sum + nums[index]) + solve(nums, 1, index + 1);
} else if (sum > 0 && sum + nums[index] > 0) {
return 1 + sum + nums[index] + solve(nums, -1, index + 1);
} else if (sum + nums[index] == 0) {
return 1 + Math.min(solve(nums, 1, index + 1), solve(nums, -1, index + 1));
} else {
return solve(nums, sum + nums[index], index + 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 | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using pi = pair<ll, ll>;
using vi = vector<ll>;
using ld = long double;
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
os << "(" << p.first << "," << p.second << ")";
return os;
}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
os << "{";
for (ll i = ll(0); i < ll((ll)v.size()); i++) {
if (i) os << ",";
os << v[i];
}
os << "}";
return os;
}
ll read() {
ll i;
scanf("%" SCNd64, &i);
return i;
}
void printSpace() { printf(" "); }
void printEoln() { printf("\n"); }
void print(ll x, ll suc = 1) {
printf("%" PRId64, x);
if (suc == 1) printEoln();
if (suc == 2) printSpace();
}
string readString() {
static char buf[3341000];
scanf("%s", buf);
return string(buf);
}
char *readCharArray() {
static char buf[3341000];
static ll bufUsed = 0;
char *ret = buf + bufUsed;
scanf("%s", ret);
bufUsed += strlen(ret) + 1;
return ret;
}
template <class T, class U>
void chmax(T &a, U b) {
if (a < b) a = b;
}
template <class T, class U>
void chmin(T &a, U b) {
if (b < a) a = b;
}
template <class T>
T Sq(const T &t) {
return t * t;
}
const ll mod = 1e9 + 7;
signed main() {
ll n = read(), sum = 0, ans = 0;
ll a = read();
bool isPlus = (a > 0);
sum = a;
for (ll i = ll(0); i < ll(n - 1); i++) {
sum += read();
if (isPlus && sum >= 0) {
ans += sum + 1;
sum = -1;
} else if (!isPlus && sum <= 0) {
ans += -1 * sum + 1;
sum = 1;
}
isPlus ^= 1;
}
print(ans);
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9 + 7;
const int MAX = 1e6;
int arr[MAX], arr2[MAX], n;
int status(int a) {
if (a < 0)
return 1;
else if (a > 0)
return 0;
else
return 2;
}
long long int solve() {
long long int cnt = 0;
arr2[0] = arr[0];
for (int i = 1; i < n; i++) {
arr2[i] = arr[i];
if (status(arr2[i - 1]) == status(arr2[i])) {
cnt += abs(arr2[i] * 2);
arr2[i] *= -1;
}
}
long long int sum = arr2[0], f = 0;
if (arr2[0] < 0)
f = 1;
else
f = 0;
for (int i = 1; i < n; i++) {
f ^= 1;
sum += arr2[i];
if (status(sum) != f) {
cnt += abs(sum) + 1;
if (f)
sum = -1;
else
sum = 1;
}
}
return cnt;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
if (!arr[0]) {
arr[0] = 1;
long long int x = solve();
arr[0] = -1;
long long int y = solve();
cout << min(x, y) << endl;
} else
cout << solve() << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n;
cin >> n;
long long a[n];
long long sum = 0, cnt = 0;
for (int i = 0; i < n; i++) cin >> a[i];
int flag = 2;
sum = a[0];
if (sum == 0) {
cnt++;
flag = 2;
} else if (sum < 0) {
flag = -2;
}
for (int i = 1; i < n; i++) {
sum = sum + a[i];
if (flag == 2) {
if (sum > 0) {
cnt += sum + 1;
sum = -1;
} else if (sum == 0) {
cnt++;
}
flag = 0 - flag;
} else {
if (sum < 0) {
cnt += 1 - sum;
sum = 1;
} else if (sum == 0)
cnt++;
flag = 0 - flag;
}
}
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 | java | import java.util.Scanner;
class Main{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int N=sc.nextInt();
long[] k=new long[N];
for(int i=0; i<N; i++) {
k[i]=sc.nextLong();
}
for(int i=1; i<N; i++) {
k[i]=k[i-1]+k[i];
}
long counter=0;
if(k[0]>0) {
counter=1;
}
else {
counter=-1;
}
long[] tasu=new long[N];
long kaz=0;
for(int i=0; i<N; i++) {
tasu[i]=0;
}
for(int i=0; i<N; i++) {
long tmp=counter*(k[i]+tasu[i]);
if(tmp>0) {
//条件を満たすのでOK
}
else if(tmp<0){
if((k[i]+tasu[i])>0) {
long tt=(k[i]+tasu[i])+1;
kaz+=tt;
tasu[i]-=tt;
}
else if((k[i]+tasu[i])<0) {
long tt=((k[i]+tasu[i])-1)*-1;
kaz+=tt;
tasu[i]+=tt;
}
}
if(tmp==0) {
if(counter==-1) {
tasu[i]--;
}
else if(counter==1) {
tasu[i]++;
}
kaz++;
}
if(i!=N-1) {
tasu[i+1]=tasu[i];
}
counter*=-1;
}
System.out.println(kaz);
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
struct edge {
int to, cost;
};
const int INF = 100000000;
int main() {
long long int n, a[100010], sum = 0, ans = 0;
cin >> n;
for (int i = 0; i < (n); i++) {
cin >> a[i];
}
if (a[0] >= 0) {
sum += a[0];
for (int i = 1; i < n; i++) {
sum += a[i];
if (i % 2 != 0) {
if (sum > 0) {
ans += abs(sum) + 1;
sum = -1;
}
} else {
if (sum < 0) {
ans += abs(sum) + 1;
sum = 1;
}
}
}
if (sum == 0) {
ans++;
}
} else {
sum += a[0];
for (int i = 1; i < n; i++) {
sum += a[i];
if (i % 2 != 0) {
if (sum < 0) {
ans += abs(sum) + 1;
sum = 1;
}
} else {
if (sum > 0) {
ans += abs(sum) + 1;
sum = -1;
}
}
}
if (sum == 0) {
ans++;
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
int cnt1, cnt2;
cnt1 = cnt2 = 0;
int sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 0 && sum <= 0) {
cnt1 += abs(sum - 1);
sum = 1;
} else if (i % 2 != 0 && sum >= 0) {
cnt1 += sum + 1;
sum = -1;
}
}
sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 0 && sum >= 0) {
cnt2 += sum + 1;
sum = -1;
}
if (i % 2 != 0 && sum <= 0) {
cnt2 += abs(sum - 1);
sum = 1;
}
}
int ans = min(cnt1, cnt2);
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int N;
int64_t cont = 0;
long long dfs(bool odd, int64_t sum, int i, vector<int64_t> &a) {
long long ans;
if (i == N) {
ans = cont;
return ans;
}
if (odd) {
if (a[i] + sum <= 0) {
cont += abs(1 - sum - a[i]);
sum = 1;
} else {
sum += a[i];
}
ans = dfs(false, sum, i + 1, a);
} else {
if (a[i] + sum >= 0) {
cont += abs(-1 - sum - a[i]);
sum = -1;
} else {
sum += a[i];
}
ans = dfs(true, sum, i + 1, a);
}
return ans;
}
int main() {
long long ans;
cin >> N;
vector<int64_t> a(N);
for (int i = 0; i < N; i++) {
cin >> a.at(i);
}
ans = min(dfs(true, 0, 0, a), dfs(false, 0, 0, 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;
const long long mod = 1e9 + 7;
int main() {
int n;
cin >> n;
long long a[120000];
for (int i = 0; i < (n); i++) cin >> a[i];
long long total = 0;
long long count1 = 0;
long long count2 = 0;
for (int i = 0; i < (n - 2) / 2; i++) {
total += a[2 * i];
if (total >= 0) {
count1 += (abs(total) + 1);
total = -1;
}
if (total <= 0) {
count2 += (abs(total) + 1);
total = 1;
}
total += a[2 * i + 1];
if (total <= 0) {
count1 += (abs(total) + 1);
total = 1;
}
if (total >= 0) {
count2 += (abs(total) + 1);
total = -1;
}
}
cout << min(count1, count2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
long long a[n];
for (long long i = 0; i < n; i++) {
cin >> a[i];
}
long long left = 0;
long long right = 0;
long long now = 0;
for (long long i = 0; i < n; i++) {
if (i == 0) {
if (a[i] <= 0) {
now = 1;
left = 1 - a[i];
} else {
now = a[i];
left = 0;
}
} else {
if (now > 0) {
if (now + a[i] < 0) {
now = now + a[i];
} else {
left += now + a[i] + 1;
now = -1;
}
} else {
if (now + a[i] > 0) {
now = now + a[i];
} else {
left += -a[i] - now + 1;
now = 1;
}
}
}
}
now = 0;
for (long long i = 0; i < n; i++) {
if (i == 0) {
if (a[i] >= 0) {
now = 1;
right = 1 + a[i];
} else {
now = a[i];
right = 0;
}
} else {
if (now > 0) {
if (now + a[i] < 0) {
now = now + a[i];
} else {
right += now + a[i] + 1;
now = -1;
}
} else {
if (now + a[i] > 0) {
now = now + a[i];
} else {
right += -a[i] - now + 1;
now = 1;
}
}
}
}
cout << ((left > right) ? right : left) << 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;
void sum(int* N, int* S, int n);
void add(int* S, int n, int del, int k);
int main() {
int *N, *S;
int count_eve = 0, count_odd = 0, n;
int j = 0, k = 0;
cin >> n;
N = new int[n];
S = new int[n];
for (int i = 0; i < n; i++) {
cin >> N[i];
}
sum(N, S, n);
int delta1 = 0, delta2 = 0;
while (j != n) {
if (j % 2 == 0 && S[j] <= 0) {
count_eve += abs(S[j]) + 1;
add(S, n, abs(S[j]) + 1, j);
} else if (j % 2 == 1 && S[j] >= 0) {
count_eve += abs(S[j]) + 1;
add(S, n, -abs(S[j]) - 1, j);
}
j++;
}
sum(N, S, n);
while (k != n) {
if (k % 2 == 0 && S[k] >= 0) {
count_odd += abs(S[k]) + 1;
add(S, n, -abs(S[k]) - 1, k + 1);
} else if (k % 2 == 1 && S[k] <= 0) {
count_odd += abs(S[k]) + 1;
add(S, n, abs(S[k]) + 1, k);
}
k++;
}
cout << min(count_eve, count_odd) << endl;
return 0;
}
void sum(int* N, int* S, int n) {
S[0] = N[0];
for (int i = 1; i < n; i++) S[i] = S[i - 1] + N[i];
for (int i = 0; i < n; i++) {
}
}
void add(int* S, int n, int del, int k) {
for (int i = k; i < n + 1; i++) S[i] += del;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.