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(_) for _ in input().split()]
def counter(dp):
count = 0
is_positive = dp > 0
for i in range(1, N):
is_positive *= -1
dp += A[i]
if dp * is_positive <= 0:
count += abs(dp)+1
dp = is_positive
return count
print(min(1+abs(A[0])+counter(-1), 1+abs(A[0])+counter(1), counter(A[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 <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <stdlib.h>
#include <list>
#include <queue>
#include <map>
#include <stack>
#include <set>
#define inf 0x3f3f3f3f
#define PI acos(double(-1))
using namespace std;
typedef long long ll;
const int maxn=1e5+10;
int a[maxn];
int main()
{
int n;
ll sum,num=1e18;
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
sum=a[0];
ll cnt=0;
if(sum==0)
{
int i=1;
while(a[i]==0&&i<n)
i++;
if(i==n)
{
cnt=(n-1)*2+1;
printf("%lld\n",cnt);
return 0;
}
for(int i=1;i<n;i++)
{
sum=1;
// scanf("%d",&a[i]);
if(sum>0)
{
ll t=sum+a[i];
if(t<0)
sum=t;
else
{
ll b=abs(t+1);
cnt+=b;
sum=-1;
}
}
else if(sum<0)
{
ll t=sum+a[i];
if(t>0)
sum=t;
else
{
ll b=abs(1-t);
cnt+=b;
sum=1;
}
}
num=min(num,cnt);
}
for(int i=1;i<n;i++)
{
sum=-1;
// scanf("%d",&a[i]);
if(sum>0)
{
ll t=sum+a[i];
if(t<0)
sum=t;
else
{
ll b=abs(t+1);
cnt+=b;
sum=-1;
}
}
else if(sum<0)
{
ll t=sum+a[i];
if(t>0)
sum=t;
else
{
ll b=abs(1-t);
cnt+=b;
sum=1;
}
}
num=min(num,cnt)
}
printf("%lld\n",num);
return 0;
}
for(int i=1;i<n;i++)
{
// scanf("%d",&a[i]);
if(sum>0)
{
ll t=sum+a[i];
if(t<0)
sum=t;
else
{
ll b=abs(t+1);
cnt+=b;
sum=-1;
}
}
else if(sum<0)
{
ll t=sum+a[i];
if(t>0)
sum=t;
else
{
ll b=abs(1-t);
cnt+=b;
sum=1;
}
}
}
printf("%lld\n",cnt);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int N;
long long a[100001];
int main() {
cin >> N;
for (int i = 0; i < N; i++) {
cin >> a[i];
}
long long ans = 0;
long long ans2 = 0;
long long R = a[0];
if (a[0] <= 0) {
ans = 1 - a[0];
R = 1;
}
for (int i = 1; i < N; i++) {
if (R > 0) {
R += a[i];
if (R >= 0) {
ans += R + 1;
R = -1;
}
} else {
R += a[i];
if (R <= 0) {
ans += 1 - R;
R = 1;
}
}
}
if (a[0] >= 0) {
ans2 = a[0] - 1;
R = -1;
}
for (int i = 1; i < N; i++) {
if (R < 0) {
R += a[i];
if (R <= 0) {
ans2 += 1 - R;
R = 1;
}
} else {
R += a[i];
if (R >= 0) {
ans2 += R + 1;
R = -1;
}
}
}
long long ans3 = min(ans, ans2);
cout << ans3 << 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 itertools
def main():
N = int(input())
A = tuple(map(int, input().split()))
a = list(itertools.accumulate(A))
even, odd = [], []
e_apnd, o_apnd = even.append, odd.append
for i, v in enumerate(a):
if i % 2:
o_apnd(v)
else:
e_apnd(v)
if sum(even) > sum(odd):
func = (plus, minus)
elif sum(even) < sum(odd):
func = (minus, plus)
elif max(even) > max(odd):
func = (plus, minus)
else:
func = (minus, plus)
cnt = 0
add_ = 0
for i, v in enumerate(a):
ret = func[i%2](v + add_)
add_ += ret
cnt += abs(ret)
print(cnt)
def plus(n):
if n <= 0:
x = 1 - n
return x
else:
return 0
def minus(n):
if n >= 0:
x = -1 - n
return x
else:
return 0
main()
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
int inf = 1e9 + 1000;
long long infi = 1e18 + 100;
long long n;
long long a[100005];
int main() {
cin >> n;
for (int i = 0; i <= (int)(n - 1); i++) cin >> a[i];
a[n] = 0;
long long sum;
long long ans = 0;
for (int i = 0; i <= (int)(n - 1); i++) {
long long p = sum;
sum += a[i];
if (p < 0 && sum < 0) {
ans += (1 - sum);
sum = 1;
} else if (p > 0 && sum > 0) {
ans += (sum + 1);
sum = -1;
} else if (sum == 0) {
if (a[i + 1] > 0) {
ans += 1;
sum = -1;
} else {
ans += 1;
sum = 1;
}
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.*;
class Main{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] sum = new int[n];
int ans = 0;
sum[0] = sc.nextInt();
for(int i = 1; i < n; i++){
int a = sc.nextInt();
sum[i] = sum[i-1] + a;
if(sum[i-1] < 0 && sum[i] <= 0){
ans += -sum[i] + 1;
sum[i] += ans;
}else if(sum[i-1] > 0 && sum[i] >= 0){
ans += sum[i] + 1;
sum[i] -= ans;
}
}
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 | cpp | #include <bits/stdc++.h>
using namespace std;
const long long INFF = 0x3f3f3f3f3f3f3f3f;
long long a[1000010];
int n;
long long solve() {
long long sum = 0;
long long oo = 0, flag;
if (a[0] > 0)
flag = -1;
else if (a[0] < 0)
flag = 1;
for (int i = 0; i < n; i++) {
oo += a[i];
if (flag == 1) {
if (oo >= 0) {
sum += oo + 1;
oo = -1;
}
}
if (flag == -1) {
if (oo <= 0) {
sum += 0 - oo + 1;
oo = 1;
}
}
flag = -flag;
}
return sum;
}
int main() {
while (scanf("%d", &n) != EOF) {
long long sum = INFF;
for (int i = 0; i < n; i++) {
scanf("%lld", &a[i]);
}
if (a[0] == 0) {
a[0] = 1;
long long sum1 = solve();
a[0] = -1;
long long sum2 = solve();
sum = min(sum1, sum2) + 1;
} else {
long long sum0 = solve();
a[0] = 1;
long long sum1 = solve() + abs(a[0] - 1);
a[0] = -1;
long long sum2 = solve() + abs(a[0] + 1);
sum = min(sum0, min(sum1, sum2));
}
printf("%lld\n", sum);
}
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Main main = new Main();
main.run();
}
public void run() {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int a[]=new int[n];
a[0]=sc.nextInt();
int cnt=0;
for(int i=1; i<n; i++) {
int v=sc.nextInt();
if(a[i-1]>0) {
if(a[i-1]+v>=0) {
while(a[i-1]+v>=0) {
v--;
cnt++;
}
}
} else {
if(a[i-1]+v<=0) {
while(a[i-1]+v<=0) {
v++;
cnt++;
}
}
}
a[i]=a[i-1]+v;
}
System.out.println(cnt);
sc.close();
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n, a[];
long count = 0, sum = 0, presum;
n = sc.nextInt();
a = new int[n];
for(int i = 0; i < n; ++i)a[i] = sc.nextInt();
sc.close();
presum = -a[0];
for(int i: a) {
sum += (long)i;
if(sum * presum > 0) {
long tmp = Math.abs(sum) + 1;
if(presum > 0)sum -= tmp;
else sum += tmp;
count += tmp;
}
if(sum == 0) {
if(presum > 0)sum--;
else sum++;
++count;
}
presum = sum;
}
System.out.println(count);
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | def sign(x):
if x > 0:
return 1
elif x < 0:
return -1
else:
return 0
n = int(input())
a = list(map(int, input().split()))
ac=0
ans1 = 0
for i in range(n):
ac += a[i]
if ac * (-1) ** i > 0:
continue
else:
ans1 += abs(ac) + 1
ac = -sign(ac)
ac=0
ans2 = 0
for i in range(n):
ac += a[i]
if ac * (-1) ** i < 0:
continue
else:
ans2 += abs(ac) + 1
ac = -sign(ac)
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;
long long a[n], suma[n];
long long eplus = 0, oplus = 0, ans;
cin >> a[0];
suma[0] = a[0];
for (int i = 1; i < n; i++) {
cin >> a[i];
suma[i] += suma[i - 1] + a[i];
}
for (int i = 0; i < n; i++) {
if (suma[i] == 0) {
eplus++;
oplus++;
} else {
if (i % 2 == 0) {
if (suma[i] < 0) {
eplus += 1 - suma[i];
} else {
oplus += 1 + suma[i];
}
} else {
if (0 < suma[i]) {
eplus += 1 + suma[i];
} else {
oplus += 1 - suma[i];
}
}
}
}
ans = min(eplus, oplus);
cout << ans;
cout << 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 = 0;
int sum[2] = {0, 0}, ans[2] = {0, 0};
int hoge, foo;
int flag[2] = {0, 1};
scanf("%d", &n);
int in[n];
for (int i = 0; i < n; i++) {
scanf("%d", &in[i]);
}
for (int i = 0; i < n; i++) {
int tmp[2];
tmp[0] = in[i];
tmp[1] = tmp[0];
if (i == 0) {
sum[0] = tmp[0];
sum[1] = tmp[1];
continue;
}
int foo[2] = {tmp[0], tmp[0]};
for (int j = 0; j < 2; j++) {
if (sum[j] + tmp[j] <= 0 && !flag[j]) {
tmp[j] = abs(sum[j]) + 1;
ans[j] += abs(sum[j]) - abs(foo[j]) + 1;
sum[j] += tmp[j];
flag[j] = 1;
} else if (sum[j] + tmp[j] > 0 && !flag[j]) {
flag[j] = 1;
sum[j] += tmp[j];
} else if (sum[j] + tmp[j] < 0 && flag[j]) {
sum[j] += tmp[j];
flag[j] = 0;
} else if (sum[j] + tmp[j] >= 0 && flag[j]) {
tmp[j] = -1 * (abs(sum[j]) + 1);
ans[j] += abs(sum[j]) + abs(foo[j]) + 1;
sum[j] += tmp[j];
flag[j] = 0;
}
}
}
printf("%d\n", ans[0] < ans[1] ? ans[0] : ans[1]);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int32_t main() {
uint64_t N;
cin >> N;
long long total, sign;
unsigned long long count;
cin >> total;
if (total == 0) {
total = 1;
sign = 1;
count = 1;
} else {
sign = total / abs(total);
count = 0;
}
for (uint64_t i = 1; i < N; i++) {
sign *= -1;
long long val;
cin >> val;
total += val;
if ((total == 0) || (sign * total < 0)) {
count += abs(sign - total);
total = sign;
}
}
cout << count << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
long long N;
cin >> N;
long long a[N];
long long sum = 0, cnt = 0;
for (long long i = 0; i < N; i++) cin >> a[i];
for (long long i = 0; i < N; i++) {
if (i == 0) {
sum += a[i];
if (a[i] == 0) {
if (a[i + 1] < 0) {
sum++;
cnt++;
} else {
sum-- + cnt++;
}
}
continue;
}
if (sum > 0 && sum + a[i] > 0) {
cnt += abs(sum) + abs(a[i]) + 1;
sum = -1;
} else if (sum < 0 && sum + a[i] < 0) {
cnt += abs(sum) + abs(a[i]) + 1;
sum = 1;
} else if (sum + a[i] == 0) {
if (a[i] >= 0) {
sum++;
cnt++;
} else {
sum--;
cnt++;
}
} else
sum += a[i];
}
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 dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const int MAX_N = int(1e5);
long long n, a[MAX_N], dp[MAX_N];
void solve() {
long long sum_diff = 0, ans = 0;
if (dp[0] == 0) {
if (dp[1] < 0) {
dp[0] = 1;
sum_diff++, ans++;
} else {
dp[0] = -1;
sum_diff--, ans++;
}
}
for (long long i = 0; i < (long long)(n - 1); i++) {
long long diff = 0;
dp[i + 1] += sum_diff;
if (dp[i] * dp[i + 1] > 0) {
if (dp[i + 1] > 0) {
diff = -1 - dp[i + 1];
sum_diff += diff;
dp[i + 1] = -1;
} else {
diff = 1 - dp[i + 1];
sum_diff += diff;
dp[i + 1] = 1;
}
}
if (dp[i + 1] == 0) {
if (dp[i] > 0) {
sum_diff--, diff = -1;
dp[i + 1] = -1;
} else {
sum_diff++, diff = 1;
dp[i + 1] = 1;
}
}
ans += abs(diff);
}
cout << ans << endl;
}
int main() {
cin >> n;
for (long long i = 0; i < (long long)(n); i++) {
cin >> a[i];
if (i == 0)
dp[0] = a[0];
else
dp[i] = dp[i - 1] + a[i];
}
solve();
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.Scanner;
class Main {
public static void main(String[] args) {
new Main().compute();
}
void compute() {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int sum = sc.nextInt();
long ans = 0;
for (int i = 0; i < N - 1; i++) {
int cur = sc.nextInt();
if (Math.signum(sum) == Math.signum(sum + cur) || Math.signum(sum + cur) == 0) {
int tmp = -(int) Math.signum(sum);
ans += Math.abs(tmp - sum - cur);
sum = tmp;
} else {
sum += cur;
}
}
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 | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
long n;
cin >> n;
vector<long> a(n);
for (long i = 0; i < n; i++) cin >> a[i];
long sum = a[0];
long long j = 0;
for (long i = 1; i < n; i++) {
if (sum * (sum + a[i]) < 0)
sum += a[i];
else {
j += abs(sum + a[i]) + 1;
if (sum < 0)
sum = 1;
else if (sum > 0)
sum = -1;
}
}
cout << j << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
A = [int(x) for x in input().split()]
count = 0
for i in range(n):
if i == 0:
continue
sum_for_i = sum(A[:i])
sum_for_next = sum(A[:i+1])
if (sum_for_i != 0 and ((sum_for_i > 0 and sum_for_next <0) or (sum_for_i < 0 and sum_for_next >0))):
continue
else:
#print("needs to be changed: A[{}] ({})".format(i, A[i]))
while not (sum_for_i != 0 and ((sum_for_i > 0 and sum_for_next <0) or (sum_for_i < 0 and sum_for_next >0))):
if A[i-1] < 0:
A[i] += 1
count += 1
else:
A[i] -= 1
count += 1
sum_for_i = sum(A[:i])
sum_for_next = sum(A[:i+1])
print(count) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long a[n], b[n];
cin >> a[0];
b[0] = a[0];
for (int i = 1; i < n; i++) {
cin >> a[i];
b[i] = b[i - 1] + a[i];
}
if (count(a, a + n, 0) == n) {
cout << 2 * n - 1 << endl;
return 0;
}
int sgn = 1;
long long ans = 0;
long long shift = 0;
for (int i = 0; i < n; i++) {
if ((b[i] + shift) * sgn <= 0) {
ans += abs(b[i] + shift) + 1;
shift += -b[i] + (b[i] == 0 ? 0 : -b[i] / abs(b[i]));
}
sgn *= -1;
}
sgn = -1;
shift = 0;
long long ans2 = 0;
for (int i = 0; i < n; i++) {
if ((b[i] + shift) * sgn <= 0) {
ans2 += abs(b[i] + shift) + 1;
shift += -b[i] + (b[i] == 0 ? 0 : -b[i] / abs(b[i]));
}
sgn *= -1;
}
cout << min(ans, ans2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long INF = 1e18;
const double pi = acos(-1.0);
int main(void) {
long long n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < (n); ++i) cin >> a[i];
long long ans = 0, sum = 0;
for (int i = 0; i < (n); ++i) {
sum += a[i];
if (i % 2 == 0) {
if (sum <= 0) {
ans += abs(1 - sum);
sum = 1;
} else {
if (sum >= 0) {
ans += abs(1 - sum);
sum = -1;
}
}
}
}
long long tmp = 0;
sum = 0;
for (int i = 0; i < (n); ++i) {
sum += a[i];
if (i % 2 == 1) {
if (sum <= 0) {
tmp += abs(1 - sum);
sum = 1;
} else {
if (sum >= 0) {
tmp += abs(1 - sum);
sum = -1;
}
}
}
}
ans = min(ans, tmp);
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using lli = long long int;
using ulli = unsigned long long int;
vector<lli> N, rN;
lli in, n, d = 0, dp;
ulli ans = 0;
int main() {
cin >> n;
for (lli l = 0; l < n; l++) {
cin >> in;
if (l == 0) {
N.push_back(in);
} else {
N.push_back(N[l - 1] + in);
}
{};
}
{};
for (int l = 1; l < (int)N.size(); l++) {
{};
dp = d;
if (N[l - 1] + d < 0) {
if (N[l] + d < 0) {
d += 1 - N[l] - dp;
ans += 1 - N[l] - dp;
{} {};
{};
{};
{};
} else if (N[l] + dp == 0) {
d += 1;
ans += 1;
}
} else if (N[l - 1] + dp > 0) {
if (N[l] + dp > 0) {
d -= N[l] + dp + 1;
ans += N[l] + dp + 1;
{} {};
{};
{};
{};
} else if (N[l] + dp == 0) {
d -= 1;
ans += 1;
}
}
{};
{};
{};
{};
}
cout << ans;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | n = gets.to_i
arr = gets.split(' ').map(&:to_i)
min = 1000000001
(0..1).each do |i|
plus = i == 0
sum = 0
modify = 0
(0...n).each do |j|
sum += arr[j]
if plus && sum <= 0
modify += (1 - sum).abs
sum = 1
elsif !plus && sum >= 0
modify += (-1 - sum).abs
sum = -1
end
plus = !plus
end
min = [min, modify].min
end
puts min |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long INF = 2e18;
const long long MOD = 1e9 + 7;
long long N;
long long a[100010];
int main() {
cin >> N;
for (long long i = 0; i < N; i++) cin >> a[i];
long long ans = 0;
long long preSum = 0;
if (a[0] == 0) {
a[0] = 1;
long long sum = a[0];
for (long long i = 1; i < N; i++) {
preSum = sum;
sum += a[i];
if ((preSum < 0) ^ (sum > 0)) {
if (preSum > 0) {
ans += abs(-1 - sum);
sum = -1;
} else {
ans += abs(1 - sum);
sum = 1;
}
}
}
a[0] = -1;
}
long long sum = a[0];
for (long long i = 1; i < N; i++) {
preSum = sum;
sum += a[i];
if ((preSum < 0) ^ (sum > 0)) {
if (preSum > 0) {
ans += abs(-1 - sum);
sum = -1;
} else {
ans += abs(1 - sum);
sum = 1;
}
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
int count = 0;
vector<int> a(100000);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<int> sum(100000);
if (a[0] == 0) {
if (a[1] > 0) {
a[0]--;
count++;
} else {
a[0]++;
count++;
}
}
sum[0] = a[0];
for (int i = 1; i < n; i++) {
sum[i] = sum[i - 1] + a[i];
if (sum[i] * sum[i - 1] >= 0) {
if (sum[i - 1] < 0) {
while (sum[i] * sum[i - 1] >= 0) {
sum[i]++;
count++;
}
} else {
while (sum[i] * sum[i - 1] >= 0) {
sum[i]--;
count++;
}
}
}
}
cout << count;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int MAX_N = 300000;
const unsigned long long mod = 1000000000 + 7;
int main() {
long long N;
cin >> N;
vector<long long> A(N);
for (int i = 0; i < (int)N; ++i) {
cin >> A[i];
}
long long S = A[0];
long long S_pre = A[0];
long long res = 0;
for (int i = 1; i < N; i++) {
S += A[i];
if (S * S_pre >= 0) {
if (S_pre > 0) {
res += (S + 1);
S = -1;
} else {
res += (1 - S);
S = 1;
}
}
S_pre = S;
}
cout << res << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void) {
int n;
cin >> n;
int* a = new int[n];
int flg = 0;
long long sum = 0;
long long cnt = 0;
for (int i = 0; i < n; ++i) {
cin >> a[i];
if (i == 0) {
if (a[0] < 0)
flg = 1;
else if (a[0] > 0)
flg = 2;
}
sum += a[i];
if (flg == 0 && sum != 0) {
if (i % 2 == 0 && sum > 0) {
flg = 2;
cnt = 2 * i - 1;
} else if (i % 2 == 0 && sum < 0) {
flg = 1;
cnt = 2 * i - 1;
} else if (i % 2 == 1 && sum > 0) {
flg = 1;
cnt = 2 * i - 1;
} else if (i % 2 == 1 && sum < 0) {
flg = 2;
cnt = 2 * i - 1;
}
}
if (flg == 1 && i % 2 == 0 && sum >= 0) {
while (sum >= 0) {
--sum;
++cnt;
}
} else if (flg == 1 && i % 2 == 1 && sum <= 0) {
while (sum <= 0) {
++sum;
++cnt;
}
} else if (flg == 2 && i % 2 == 0 && sum <= 0) {
while (sum <= 0) {
++sum;
++cnt;
}
} else if (flg == 2 && i % 2 == 1 && sum >= 0) {
while (sum >= 0) {
--sum;
++cnt;
}
}
}
cout << cnt << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 1000000007;
int main() {
int n;
cin >> n;
vector<ll> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
ll t = 0;
ll sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 0) {
if (sum <= 0) {
t += 1 - sum;
sum += t;
}
} else {
if (sum >= 0) {
t += sum + 1;
sum -= t;
}
}
}
ll u = 0;
sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 1) {
if (sum <= 0) {
u += 1 - sum;
sum += u;
}
} else {
if (sum >= 0) {
u += sum + 1;
sum -= u;
}
}
}
cout << min(t, u) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using ll = long long;
using ull = unsigned long long;
using uint = unsigned int;
static ull tenq = 1000000000;
static ull mod = tenq + 7;
using namespace std;
int main() {
ll N;
cin >> N;
ll sum = 0;
ll res = 0;
for (auto i = 0; i < N; i++) {
ll a;
cin >> a;
if (sum == 0) {
sum = a;
continue;
}
if (sum * (sum + a) >= 0) {
res += abs(sum + a) + 1;
if (sum > 0)
sum = -1;
else
sum = 1;
} else {
sum += a;
}
}
cout << res << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | #!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')
#A
def A():
a = input().split()
a = list(map(lambda x: x.capitalize(), a))
a,b,c = a
print(a[0]+b[0]+c[0])
return
#B
def B():
a = II()
b = II()
if a > b:
print("GREATER")
if a < b:
print("LESS")
if a == b:
print("EQUAL")
return
#C
def C():
II()
a = LI()
def f(suma, b, tern):
for i in a[1:]:
#print((suma + i < 0) ^ tern, i, suma + i < 0 , tern, b, suma)
if not ((suma + i <= 0) ^ tern):
suma = suma + i
tern ^= 1
continue
#print(b,i)
b += abs(suma + i) + 1
suma = -1 * tern or 1
tern ^= 1
#print(b)
return b
if a[0] == 0:
ans = min(f(1, 1, 1), f(-1, 1, 0))
else:
ans = min(f(a[0], 0, a[0] > 0), f(-a[0], 2 * abs(a[0]), a[0] > 0))
print(ans)
return
#D
def D():
s = S()
for i in range(len(s) - 1):
if s[i] == s[i+1]:
print(i + 1, i + 2)
return
for i in range(len(s) - 2):
if s[i] == s[i + 2]:
print(i + 1, i + 3)
return
print(-1, -1)
return
#Solve
if __name__ == '__main__':
C()
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int execOne(const vector<int> &a) {
int sum = 0;
int ans = 0;
for (int i = 0; i < a.size();) {
sum += a[i];
if (0 <= sum) {
ans += (abs(sum) + 1);
sum = -1;
}
i++;
if (i == a.size()) {
break;
}
sum += a[i];
if (0 >= sum) {
ans += (abs(sum) + 1);
sum = 1;
}
i++;
}
return ans;
}
int execOneRev(const vector<int> &a) {
int sum = 0;
int ans = 0;
for (int i = 0; i < a.size();) {
sum += a[i];
if (0 >= sum) {
ans += (abs(sum) + 1);
sum = 1;
}
i++;
if (i == a.size()) {
break;
}
sum += a[i];
if (0 <= sum) {
ans += (abs(sum) + 1);
sum = -1;
}
i++;
}
return ans;
}
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int(i) = 0; (i) < (int)(n); ++(i)) {
cin >> a[i];
}
int ans1 = execOne(a);
int ans2 = execOneRev(a);
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;
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;
}
signed main() {
int n;
cin >> n;
long long ans = 0;
long long sum = 0;
bool fl = 0;
for (int i = 0; i < (n); i++) {
int a;
cin >> a;
if (i == 0) {
if (a == 0) {
fl = 1;
break;
}
sum += a;
continue;
}
if (0 < sum) {
if (0 < sum + a) {
ans += sum + a + 1;
sum = -1;
} else if (sum + a == 0) {
ans++;
sum = -1;
} else {
sum += a;
}
} else {
if (0 > sum + a) {
ans += abs(sum + a) + 1;
sum = 1;
} else if (sum + a == 0) {
ans++;
sum = 1;
} else {
sum += a;
}
}
}
if (fl) {
long long sum1 = 1, sum2 = -1;
ans++;
int ans2 = 1;
for (int i = 0; i < (n - 1); i++) {
int a;
cin >> a;
if (0 < sum1) {
if (0 < sum1 + a) {
ans += sum1 + a + 1;
sum1 = -1;
} else if (sum1 + a == 0) {
ans++;
sum1 = -1;
} else {
sum1 += a;
}
} else {
if (0 < sum1 + a) {
ans += abs(sum1 + a) + 1;
sum1 = 1;
} else if (sum1 + a == 0) {
ans++;
sum1 = 1;
} else {
sum1 += a;
}
}
if (0 < sum2) {
if (0 < sum2 + a) {
ans2 += sum2 + a + 1;
sum2 = -1;
} else if (sum2 + a == 0) {
ans2++;
sum2 = -1;
} else {
sum2 += a;
}
} else {
if (0 < sum2 + a) {
ans2 += abs(sum2 + a) + 1;
sum2 = 1;
} else if (sum2 + a == 0) {
ans2++;
sum2 = 1;
} else {
sum2 += a;
}
}
}
chmax(ans, ans2);
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int,input().split()))
ttl = a[0]
cst = 0
if a[0]>0:
flg = 1
elif a[0]<0:
flg = -1
for i in range(1,n):
ttl += a[i]
if ttl*flg < 0:
flg *= -1
else:
if flg > 0:
memo = abs(ttl)+1
ttl -= memo
cst += memo
elif flg < 0:
memo = abs(ttl)+1
ttl += memo
cst += memo
flg *= -1
ttl = a[0]
cst2 = 0
if a[0]>0:
flg = -1
cst2 += abs(ttl)+1
ttl += 0-ttl-1
elif a[0]<0:
flg = 1
cst2 += abs(ttl)+1
ttl += 0-ttl+1
for i in range(1,n):
ttl += a[i]
if ttl*flg < 0:
flg *= -1
else:
if flg > 0:
memo = abs(ttl)+1
ttl -= memo
cst2 += memo
elif flg < 0:
memo = abs(ttl)+1
ttl += memo
cst2 += memo
flg *= -1
print(cst2," ",ttl)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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, cost1 = 0, cost2 = 0;
int sum1 = 0, sum2 = 0;
cin >> n;
vector<int> data(n);
for (int i = 0; i < n; i++) cin >> data[i];
for (int i = 0; i < n; i++) {
if (i % 2 == 1) {
sum1 += data[i];
if (sum1 >= 0) {
cost1 += 1 + sum1;
sum1 = -1;
}
} else {
sum1 += data[i];
if (sum1 <= 0) {
cost1 += 1 - sum1;
sum1 = 1;
}
}
}
for (int i = 0; i < n; i++) {
if (i % 2 == 1) {
sum2 += data[i];
if (sum2 <= 0) {
cost2 += 1 - sum2;
sum2 = 1;
}
} else {
sum2 += data[i];
if (sum2 >= 0) {
cost2 += 1 + sum2;
sum2 = -1;
}
}
}
cout << min(cost1, cost2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
int main(int argc, char const* argv[]) {
uint64_t n;
std::cin >> n;
int64_t sum, current;
std::cin >> current;
sum = current;
uint64_t result = 0;
if (current == 0) {
sum = -1;
result++;
}
bool is_sum_negative = sum < 0;
for (int i = 1; i < n; ++i) {
std::cin >> current;
sum += current;
auto tmp = std::abs(sum) + 1;
if (is_sum_negative) {
if (sum <= 0) {
sum += tmp;
result += tmp;
assert(sum == 1);
}
} else {
if (sum >= 0) {
sum -= tmp;
result += tmp;
assert(sum == -1);
}
}
is_sum_negative = !is_sum_negative;
}
std::cout << result << std::endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
template <class T>
using VEC = std::vector<T>;
template <class T>
using MAT = std::vector<std::vector<T>>;
void DUMP() { cerr << endl; }
template <class Head, class... Tail>
void DUMP(Head &&head, Tail &&...tail) {
cerr << head << ", ";
DUMP(std::move(tail)...);
}
template <typename T>
ostream &operator<<(ostream &os, vector<T> &vec) {
os << "{";
for (auto v : vec) os << v << ",";
os << "}";
return os;
}
int sign(LL n) { return n == 0 ? 0 : n / abs(n); }
int main() {
int n;
cin >> n;
VEC<LL> a(n);
for (int i = (0); i < (int)(n); ++i) {
cin >> a[i];
}
auto make = [&](LL tgt) -> LL {
VEC<LL> cur(n, 0);
cur[0] = a[0];
LL cnt = 0;
if (sign(cur[0]) == sign(tgt)) {
cnt = 0;
} else {
cnt += abs(a[0] - tgt);
}
for (int i = (1); i < (int)(n); ++i) {
tgt = -tgt;
cur[i] = cur[i - 1] + a[i];
if (sign(cur[i]) != sign(tgt)) {
cnt += abs(cur[i] - tgt);
cur[i] = tgt;
}
}
return cnt;
};
cout << min(make(1), make(-1)) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
a_lst = [int(x) for x in input().split()]
def my_sign(num):
return (num > 0) - (num < 0)
sum = 0
cnt = 0
sum_lst = []
for i in range(N):
if i == 0:
if a_lst[i] == 0:
a_lst[i] = 1
sum_lst.append(a_lst[i])
else:
sum_lst.append(a_lst[i] + sum_lst[i - 1])
if my_sign(sum_lst[i]) == my_sign(sum_lst[i - 1]) or my_sign(sum_lst[i]) == 0:
cnt += max(-my_sign(sum_lst[i - 1]), sum_lst[i]) - min(-my_sign(sum_lst[i - 1]), sum_lst[i])
a_lst[i] += -my_sign(sum_lst[i - 1]) - sum_lst[i]
sum_lst[i] = -my_sign(sum_lst[i - 1])
#print(a_lst)
#print(sum_lst)
print(cnt)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <ctype.h>
#include <stdint.h>
#include <string.h>
#include <wchar.h>
#include <math.h>
#define N_MAX (100)
#define P_MAX (100)
#define DP_ARRAY_SIZE (N_MAX * P_MAX / 32 + 1)
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define ABS(a) ((a) < 0 ? -(a) : (a))
#define ABSS(a, b) ((a) > (b) ? (a) - (b) : (b) - (a))
int compare_sz_asc(const void* a, const void* b) {
return *((size_t*)a) < *((size_t*)b) ? -1 : 1;
}
int compare_sz_desc(const void* a, const void* b) {
return *((size_t*)a) > * ((size_t*)b) ? -1 : 1;
}
int compare_i64_asc(const void* a, const void* b) {
return *((int64_t*)a) < *((int64_t*)b) ? -1 : 1;
}
int compare_i64_desc(const void* a, const void* b) {
return *((int64_t*)a) > * ((int64_t*)b) ? -1 : 1;
}
int compare_c_asc(const void* a, const void* b) {
return *((char*)a) < *((char*)b) ? -1 : 1;
}
int compare_c_desc(const void* a, const void* b) {
return *((char*)a) > * ((char*)b) ? -1 : 1;
}
static size_t powSz(const size_t base, const size_t exp) {
if (exp == 0) {
return 1;
}
if (exp == 1) {
return base;
}
if (exp % 2 == 0) {
return powSz(base * base, exp / 2);
}
else {
return base * powSz(base, exp - 1);
}
}
static size_t comb(const size_t n, const size_t r) {
size_t result = 1;
for (size_t i = 0; i < r; i++) {
result *= n - i;
result /= i + 1;
}
return result;
}
static uint64_t combU64(const uint64_t n, const uint64_t r) {
uint64_t result = 1;
for (uint64_t i = 0; i < r; i++) {
result *= n - i;
result /= i + 1;
}
return result;
}
static size_t gcdZu(size_t m, size_t n) {
size_t temp;
while (m % n != 0) {
temp = n;
n = m % n;
m = temp;
}
return n;
}
static uint64_t gcdU64(uint64_t m, uint64_t n)
{
uint64_t temp;
while (m % n != 0) {
temp = n;
n = m % n;
m = temp;
}
return n;
}
static int64_t a[100000];
int main(void) {
size_t n;
scanf("%zu\n", &n);
for (size_t i = 0; i < n; i++) {
scanf("%"PRId64, &a[i]);
}
size_t cnt[2] = { 0,0 };
int64_t base[2] = { 1,-1 };
int64_t sum = 0;
for (size_t i = 0; i < n; i++) {
sum += a[i];
cnt[0] += (size_t)ABSS(base[0], sum);
cnt[1] += (size_t)ABSS(base[1], sum);
base[0] = -base[0];
base[1] = -base[1];
}
printf("%zu", MIN(cnt[0], cnt[1]));
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
A = list(map(int, input().split()))
sum = A[0]
ans = 0
if sum == 0:
sum += 1
ans += 1
for i in range(1, len(A)):
if sum > 0:
if sum + A[i] >= 0:
ans += abs(sum + A[i]) + 1
sum = -1
else:
sum += A[i]
continue
elif sum < 0:
if sum + A[i] <= 0:
ans += abs(sum + A[i]) + 1
sum = 1
else:
sum += A[i]
continue
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
long long sum = a[0];
long long ans = 0;
for (int i = 1; i < n; i++) {
if (sum + a[i] == 0) {
ans++;
if (sum > 0) {
sum = 1;
} else {
sum = -1;
}
} else if (sum > 0 && sum + a[i] > 0) {
ans += abs(a[i] + sum + 1);
sum = -1;
} else if (sum < 0 && sum + a[i] < 0) {
ans += abs(1 - sum - a[i]);
sum = 1;
} else {
sum += a[i];
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int sum = 0, prev = 0;
long long int ans = 0;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
sum += a;
if (prev > 0) {
if (sum >= 0) {
ans += abs(sum) + 1;
sum = -1;
}
} else if (prev < 0) {
if (sum <= 0) {
ans += abs(sum) + 1;
sum = 1;
}
}
prev = sum;
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
const long long MAX_N = 1E5;
long long N;
long long A[MAX_N], S[MAX_N];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N;
for (int i = 0; i < N; i++) {
cin >> A[i];
}
S[0] = A[0];
for (int i = 1; i < N; i++) {
S[i] = S[i - 1] + A[i];
}
int cnt1 = 0;
int d = 0;
for (int i = 0; i < N; i++) {
if (i & 1) {
if (S[i] + d >= 0) {
int cost = S[i] + d + 1;
d -= cost;
cnt1 += cost;
}
} else {
if (S[i] + d <= 0) {
int cost = -(S[i] + d) + 1;
d += cost;
cnt1 += cost;
}
}
}
int cnt2 = 0;
d = 0;
for (int i = 0; i < N; i++) {
if (!(i & 1)) {
if (S[i] + d >= 0) {
int cost = S[i] + d + 1;
d -= cost;
cnt2 += cost;
}
} else {
if (S[i] + d <= 0) {
int cost = -(S[i] + d) + 1;
d += cost;
cnt2 += cost;
}
}
}
cout << min(cnt1, cnt2) << "\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 | # -*- coding: utf-8 -*-
"""
https://abc059.contest.atcoder.jp/tasks/arc072_a
WA
"""
import sys
from sys import stdin
input = stdin.readline
def check_sign(n):
if n > 0:
return 1
elif n < 0:
return -1
else:
return 0
def solve(A):
p_ans = 0
n_ans = 0
# 最初をプラス側に振った場合の解
total = A[0]
prev_sign = check_sign(total)
if prev_sign == 0:
p_ans += 1
total += 1
prev_sign = 1
for a in A[1:]:
total += a
sign = check_sign(total)
if sign == 0:
total -= prev_sign
p_ans += 1
elif prev_sign != sign:
prev_sign = sign
else:
p_ans += (abs(total) + 1)
if prev_sign < 0:
total = 1
prev_sign = 1
else:
total = -1
prev_sign = -1
# 最初をマイナス側に振った場合の解
total = A[0]
prev_sign = check_sign(total)
if prev_sign == 0:
n_ans += 1
total -= 1
prev_sign = -1
for a in A[1:]:
total += a
sign = check_sign(total)
if sign == 0:
total -= prev_sign
n_ans += 1
elif prev_sign != sign:
prev_sign = sign
else:
n_ans += (abs(total) + 1)
if prev_sign < 0:
total = 1
prev_sign = 1
else:
total = -1
prev_sign = -1
return min(p_ans, n_ans)
def main(args):
n = int(input())
A = [int(x) for x in input().split()]
ans = solve(A)
print(ans)
if __name__ == '__main__':
main(sys.argv[1:])
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <bits/stdc++.h>
int main(void) {
int iNum[100000] = {0};
int iNumber = 0;
int iLoop;
int iSum = 0;
int iFlg = 0;
int iResult = 0;
scanf("%d", &iNumber);
for (iLoop = 0; iLoop < iNumber; iLoop++) {
scanf("%d", &iNum[iLoop]);
}
for (iLoop = 0; iLoop < iNumber; iLoop++) {
if (iFlg == 0 && iNum[iLoop] == 0) {
if (iLoop == 0) {
iResult++;
} else {
iResult += 2;
}
continue;
}
iSum += iNum[iLoop];
if (iFlg == 0) {
if (iNum[iLoop] > 0) {
iFlg = 1;
} else {
iFlg = -1;
}
continue;
}
if (iFlg * iSum >= 0) {
iResult += abs(iSum) + 1;
iSum = iFlg * -1;
}
iFlg *= -1;
}
printf("%d\n", iResult);
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()))
cnt=0
for i in range(1,n):
# 条件満たすまでループ
while True:
print(a)
now_tmp = sum(a[:i])
next_tmp = sum(a[:i+1])
print(i, now_tmp, next_tmp)
# 符号が逆転していればOK かつ 現在までの総和が0でない
# 異なる符号を掛けるとマイナスになる
if now_tmp * next_tmp <0 and now_tmp !=0:
break
else:
# 現在の合計がマイナスの場合
if now_tmp < 0:
a[i] += next_tmp+1
cnt +=abs(next_tmp+1)
# 現在の合計がプラスの場合
elif now_tmp > 0 :
a[i] += -next_tmp-1
cnt +=abs(next_tmp+1)
# 現在の合計が0の場合
elif now_tmp == 0 :
# 1個前がプラスの場合、
if sum(a[:i]) > 0:
a[i] += -next_tmp+1
cnt +=abs(next_tmp+1)
# 1個前がマイナスの場合
else:
a[i] += next_tmp+1
cnt +=abs(next_tmp+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 | python3 | n=int(input())
a=[int(i) for i in input().split()]
check=a[0]
ans=0
if check==0:
for i in range(1,n):
if check+a[i]==0:
continue
elif check+a[i]>0:
if i%2==0:
check=1
ans=1
break
else:
check=-1
ans=1
break
else:
if i%2==0:
check=-1
ans=1
break
else:
check=1
ans=1
break
else:
check=1
ans=1
for i in range(1,n):
check2=check+a[i]
if check<0:
if check2>0:
check=check2
else:
ans+=(abs(check2)+1)
check=1
else:
if check2<0:
check=check2
else:
ans+=(abs(check2)+1)
check=-1
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
int odd = 0, even = 0, sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 0 && sum <= 0) {
odd += abs(sum) + 1;
sum = +1;
} else if (i % 2 == 1 && sum >= 0) {
odd += abs(sum) + 1;
sum = -1;
}
}
sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 1 && sum <= 0) {
even += abs(sum) + 1;
sum = +1;
} else if (i % 2 == 0 && sum >= 0) {
even += abs(sum) + 1;
sum = -1;
}
}
cout << min(odd, even) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
const long long INF = 1LL << 60;
using namespace std;
template <class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
int main(void) {
long long n, i, a[100001], sum, ans = 0;
cin >> n;
for (i = 1; i <= n; i++) {
cin >> a[i];
}
if (a[1] == 0) {
if (a[2] > 0) {
a[1] = -1;
ans++;
} else {
a[1] = 1;
ans++;
}
}
sum = a[1];
for (i = 2; i <= n; i++) {
if (sum > 0) {
if (sum + a[i] < 0) {
sum += a[i];
} else {
ans += abs(sum + a[i] + 1);
sum = -1;
}
} else {
if (sum + a[i] > 0) {
sum += a[i];
} else {
ans += abs(sum + a[i] - 1);
sum = 1;
}
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.*;
public class Main {
private static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int n = sc.nextInt();
long[] a = new long[n];
for (int i = 0;i < n;i++) a[i] = sc.nextLong();
long ret = calc(a,true);
ret = Math.min(calc(a,false),ret);
System.out.println(ret);
}
private static long calc(long[] a, boolean b) {
long sum = a[0];
if (b) {
sum *= -1;
}
long ret = 0;
long tmp = 0;
for (int i = 1;i < a.length;i++) {
long num = a[i];
tmp = sum;
sum += num;
if ((tmp<0&&sum>=0)||(tmp>=0&&sum<0)) continue;
long l = Math.abs(sum)+1;
if (sum>=0) {
sum -= l;
} else {
sum += l;
}
ret += l;
}
if (sum==0) ret++;
return ret;
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int,input().split()))
cnt=0
for i in range(n-1):
sum_a = sum(a[0:i+1])
if abs(sum_a) >= abs(a[i+1]) or sum_a*a[i+1]>=0:
if sum_a<0:
cnt += abs(-sum_a+1 -a[i+1])
a[i+1]=-sum_a+1
elif sum_a>=0:
cnt+=abs(-sum_a-1-a[i+1])
a[i+1]=-sum_a-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;
using long long = long long;
template <class T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
const long long INF = 1e9;
long long n;
vector<long long> a;
int main() {
cin >> n;
a.resize(n);
for (long long i = 0; i < (n); ++i) cin >> a[i];
long long sum;
long long ans = 0;
sum = -1;
ans += abs(a[0] - sum);
for (long long i = 0; i < (n - 1); ++i) {
if (sum < 0) {
if (sum + a[i + 1] > 0) {
sum += a[i + 1];
continue;
} else {
ans += 1 - sum - a[i + 1];
sum = 1;
}
} else {
if (sum + a[i + 1] < 0) {
sum += a[i + 1];
continue;
} else {
ans += a[i + 1] + sum + 1;
sum = -1;
}
}
}
long long ans2 = 0;
sum = 1;
ans2 += abs(a[0] - sum);
for (long long i = 0; i < (n - 1); ++i) {
if (sum < 0) {
if (sum + a[i + 1] > 0) {
sum += a[i + 1];
continue;
} else {
ans2 += 1 - sum - a[i + 1];
sum = 1;
}
} else {
if (sum + a[i + 1] < 0) {
sum += a[i + 1];
continue;
} else {
ans2 += a[i + 1] + sum + 1;
sum = -1;
}
}
}
chmin(ans, ans2);
long long ans3 = 0;
sum = a[0];
for (long long i = 0; i < (n - 1); ++i) {
if (sum == 0) {
ans3 = INF;
continue;
}
if (sum < 0) {
if (sum + a[i + 1] > 0) {
sum += a[i + 1];
continue;
} else {
ans3 += 1 - sum - a[i + 1];
sum = 1;
}
} else {
if (sum + a[i + 1] < 0) {
sum += a[i + 1];
continue;
} else {
ans3 += a[i + 1] + sum + 1;
sum = -1;
}
}
}
chmin(ans, ans3);
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
using System.Linq;
using System.Collections.Generic;
using System.Numerics;
using static System.Console;
class Program {
static Scanner sc = new Scanner();
internal static void Main(string[] args) {
var N = sc.nextInt();
var a = sc.ArrayInt(N);
var sum = a[0];
var ans = 0;
bool bef = a[0] >= 0;
for (int i = 1; i < N; i++) {
sum += a[i];
if (sum == 0 || (sum > 0) == bef ) {
if (bef) {
while (sum >= 0) {
sum--;
ans++;
}
} else {
while (sum < 1) {
sum++;
ans++;
}
}
}
bef = !bef;
}
WriteLine(ans);
}
}
class Scanner {
string[] s;
int i;
char[] cs = new char[] { ' ' };
public Scanner() {
s = new string[0];
i = 0;
}
public string next() {
if (i < s.Length) return s[i++];
string st = Console.ReadLine();
while (st == "") st = Console.ReadLine();
s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
if (s.Length == 0) return next();
i = 0;
return s[i++];
}
public int nextInt() {
return int.Parse(next());
}
public int[] ArrayInt(int N, int add = 0) {
int[] Array = new int[N];
for (int i = 0; i < N; i++) {
Array[i] = nextInt() + add;
}
return Array;
}
public long nextLong() {
return long.Parse(next());
}
public long[] ArrayLong(int N, long add = 0) {
long[] Array = new long[N];
for (int i = 0; i < N; i++) {
Array[i] = nextLong() + add;
}
return Array;
}
public double nextDouble() {
return double.Parse(next());
}
public double[] ArrayDouble(int N, double add = 0) {
double[] Array = new double[N];
for (int i = 0; i < N; i++) {
Array[i] = nextDouble() + add;
}
return Array;
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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 = 1000000007;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (long long(i) = 0; (i) < (n); (i)++) cin >> a[i];
long long ans = LLONG_MAX;
long long total = 0;
long long tmp = 0;
for (int i = 0; i < n; ++i) {
tmp += a[i];
if (i % 2) {
if (tmp > 0) {
total += abs(tmp) + 1;
tmp = -1;
}
} else {
if (tmp < 0) {
total += abs(tmp) + 1;
tmp = 1;
}
}
}
ans = total;
total = 0;
tmp = 0;
for (int i = 0; i < n; ++i) {
tmp += a[i];
if (i % 2 == 0) {
if (tmp > 0) {
total += abs(tmp) + 1;
tmp = -1;
}
} else {
if (tmp < 0) {
total += abs(tmp) + 1;
tmp = 1;
}
}
}
ans = min(total, ans);
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = [int(i) for i in input().split()]
s = []
sign = []
debug_total = [0 for i in range(n)]
ans = 0
if a[0]==0:
if a[1]<0:
a[0]=1
else:
a[0]=-1
ans+=1
if a[0]<0:
sign.append(-1)
else:
sign.append(1)
total = a[0]
debug_total[0]=total
op = 0
#print('total_in: ',total)
#print('ans_in: ',ans)
#print('a : ', a)
for i in range(1,n):
total+=a[i]
if total==0:
sign.append(sign[i-1]*-1)
total += sign[i-1]*-1
a[i] += sign[i-1]*-1
ans += 1
elif total<0:
sign.append(-1)
else:
sign.append(1)
if sign[i]==sign[i-1]:
op = abs(total)+1
if total+op==0 or total-op==0:
op+=1
if sign[i]==1:
total -= op
a[i] -= op
else:
total += op
a[i] += op
ans += op
sign[i] *= -1
debug_total[i] = total
#print('==========================')
#print('i: ',i)
#print('total: ',total)
#print('ans: ',ans)
#print('a: ',a)
#print('total_list: ', debug_total)
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using 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;
}
uint64_t solve(vi a, uint64_t 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)) {
res += abs(sum + a[i] - (-1));
a[i] = -1 - sum;
} else if ((pos && !(i % 2) && sum + a[i] <= 0) ||
(!pos && i % 2 && sum + a[i] <= 0)) {
res += abs(sum + a[i] - 1);
a[i] = 1 - sum;
}
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);
uint64_t res = 0;
uint64_t res1 = solve(a, res);
res = 0;
if (pos) {
res += abs(a[0] - (-1));
a[0] = -1;
} else {
res += abs(a[0] - 1);
a[0] = 1;
}
uint64_t 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 | python3 | n = int(input())
as_ = list(map(int, input().split()))
ans = 0
sum_ = as_[0]
if sum_ == 0 and as_[1] <= 0:
sum_ += 1
ans += 1
elif sum_ == 0 and as_[1] < 0:
sum_ += 1
ans += 1
else:
pass
for i in range(1, n):
new_sum_ = sum_+as_[i]
if sum_ > 0 and new_sum_ >= 0:
as_[i] -= (1+new_sum_)
ans += 1+new_sum_
sum_ = -1
elif sum_ < 0 and new_sum_ <= 0:
as_[i] += (1-new_sum_)
ans += 1-new_sum_
sum_ = 1
else:
sum_ = new_sum_ |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
signed main() {
long long n;
cin >> n;
long long a[n + 1];
a[0] = 0;
long long cnta = 0;
long long cntb = 0;
long long sum = 0;
for (long long i = 0; i < n; i++) {
cin >> a[i + 1];
a[i + 1] += a[i];
}
for (long long i = 1; i < n + 1; i++) {
if (i % 2 == 0) {
if (0 <= a[i] + sum) {
cnta += (a[i] + sum + 1);
sum -= (a[i] + sum + 1);
}
} else {
if (a[i] + sum <= 0) {
cnta += (1 - (a[i] + sum));
sum += (1 - (a[i] - sum));
}
}
}
sum = 0;
for (long long i = 1; i < n + 1; i++) {
if (i % 2 == 0) {
if (a[i] + sum <= 0) {
cntb += (1 - (a[i] + sum));
sum += (1 - (a[i] - sum));
}
} else {
if (0 <= a[i] + sum) {
cntb += (a[i] + sum + 1);
sum -= (a[i] + sum + 1);
}
}
}
cout << min(cnta, cntb) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
void solve() {
long long n;
cin >> n;
long long a[n];
for (long long i = 0; i < n; i++) cin >> a[i];
long long sum = 0, cnt = 0, sum1 = 0, cnt1 = 0;
if (a[0] > 0) {
sum = a[0];
sum1 = -1;
cnt1 += (abs(a[0]) + 1);
} else if (a[0] < 0) {
sum = 1;
cnt = a[0] + 1;
sum1 = a[0];
} else {
sum = 1, sum1 = -1;
cnt = 1, cnt1 = 1;
}
for (long long i = 1; i < n; i++) {
if (sum * (sum + a[i]) < 0)
sum += a[i];
else {
cnt += (abs(sum + a[i]) + 1);
sum = (sum < 0 ? 1 : -1);
}
if (sum1 * (sum1 + a[i]) < 0)
sum1 += a[i];
else {
cnt1 += (abs(sum1 + a[i]) + 1);
sum1 = (sum1 < 0 ? 1 : -1);
}
}
cout << min(cnt1, cnt) << endl;
}
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cin.exceptions(cin.failbit);
solve();
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < (int)(n); i++) cin >> a.at(i);
long long cnt = 0;
long long wa = 0;
long long wa2 = 0;
wa = a.at(0);
for (int i = 0; i < n - 1;) {
wa2 = wa + a.at(i + 1);
if (wa > 0) {
if (wa2 < 0) {
i++;
wa = wa2;
} else if (wa2 > 0) {
cnt += abs(-1 - wa2);
a.at(i + 1) -= abs(-1 - wa2);
} else if (wa2 == 0) {
cnt += abs(-1 - wa2);
a.at(i + 1) -= abs(-1 - wa2);
}
} else if (wa < 0) {
if (wa2 < 0) {
cnt += abs(1 - wa2);
a.at(i + 1) += abs(1 - wa2);
} else if (wa2 > 0) {
i++;
wa = wa2;
} else if (wa2 == 0) {
cnt += abs(1 - wa2);
a.at(i + 1) += abs(1 - wa2);
}
}
}
cout << cnt << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | def count_loop(n, old_state, count, a_list, flag):
for i in range(1, int(n)):
num_state = old_state + int(a_list[i])
if flag == 1:
if num_state >= 0:
count += num_state + 1
old_state = -1
elif num_state < 0:
old_state = num_state
flag = -1
elif flag == -1:
if num_state <= 0:
count += abs(num_state) + 1
old_state = 1
elif num_state > 0:
old_state = num_state
flag = 1
return count
if __name__ == "__main__":
n = input()
a = input()
a_list = a.split(" ")
old_state = int(a_list[0])
count = 0
if old_state == 0:
c1 = count_loop(n,1,1,a_list,1)
c2 = count_loop(n,-1,1,a_list,-1)
else:
c1 = count_loop(n,old_state,count,a_list,1)
c2 = count_loop(n,old_state,count,a_list,-1)
print(min(c1,c2))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.OutputStream;
import java.io.IOException;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Arrays;
import java.util.Collections;
import java.util.ArrayList;
import java.util.List;
import java.util.HashSet;
import java.util.Comparator;
import java.util.Set;
import java.util.HashMap;
import java.util.Map;
public class Main {
// 標準入力
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// 標準入力数値配列用 int
static int[] inputval() throws Exception {
String[] strarray = br.readLine().trim().split(" ");
int[] intarray = new int[strarray.length];
for (int i = 0; i < intarray.length; i++) {
intarray[i] = Integer.parseInt(strarray[i]);
}
return intarray;
}
/* 標準入力数値配列用 long */
static long[] inputLongArr() throws Exception {
String[] strarray = br.readLine().trim().split(" ");
long[] longarray = new long[strarray.length];
for (int i = 0; i < longarray.length; i++) {
longarray[i] = Long.parseLong(strarray[i]);
}
return longarray;
}
// 標準入力数値リスト用 int
static List<Integer> inputIntList() throws Exception {
List<String> strList = Arrays.asList(br.readLine().trim().split(" "));
List<Integer> intList = new ArrayList<Integer>();
for (String elem : strList){
intList.add(Integer.parseInt(elem));
}
return intList;
}
// 標準入力数値配列用 integer 降順ソート用
static Integer[] inputvalInteger() throws Exception {
String[] strarray = br.readLine().trim().split(" ");
Integer[] intarray = new Integer[strarray.length];
for (int i = 0; i < intarray.length; i++) {
intarray[i] = Integer.parseInt(strarray[i]);
}
return intarray;
}
/*標準入力long*/
static long inputLong() throws Exception {
return Long.parseLong(br.readLine());
}
/*標準入力long*/
static int inputInt() throws Exception {
return Integer.parseInt(br.readLine());
}
public static void main(String[] args) throws Exception {
// write your code here
int n = inputInt();
long [] al = inputLongArr();
boolean nextPlusF = al[0] < 0;
long sum2;
long ans2;
if (nextPlusF){
sum2 = 1;
ans2 = 1 - (al[0]);
}else{
sum2 = -1;
ans2 = al[0] + 1;
}
long ans = 0;
long sum = al[0];
for(int i=1;i<n;i++){
sum += al[i];
if(nextPlusF && sum <=0){
ans += 1-sum;
sum += ans;
}else if ((! nextPlusF) && sum >= 0){
ans += sum +1;
sum -= ans;
}
nextPlusF = !nextPlusF;
}
nextPlusF = !(al[0] < 0);
for(int i=1;i<n;i++){
sum2 += al[i];
if(nextPlusF && sum2 <=0){
ans2 += 1-sum2;
sum2 += ans2;
}else if ((! nextPlusF) && sum2 >= 0){
ans2 += sum2 +1;
sum2 -= ans2;
}
nextPlusF = !nextPlusF;
}
System.out.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 | python3 | import numpy as np
import copy
N = int(input())
a = list(map(int,input().split()))
a = np.cumsum(a)
ans1 = 0
ans2 = 0
b = copy.copy(a)
for i in range(N):
if i % 2 == 0:
if a[i] > 0:
pass
else:
ans1 += abs(a[i]) + 1
a[i+1:] = list(map(lambda n:n+abs(a[i])+1, a[i+1:]))
else:
if a[i] < 0:
pass
else:
ans1 += abs(a[i]) + 1
a[i+1:] = list(map(lambda n:n-(abs(a[i])+1), a[i+1:]))
for i in range(N):
if i % 2 == 1:
if b[i] > 0:
pass
else:
ans2 += abs(b[i]) + 1
b[i+1:] = list(map(lambda n:n+abs(b[i])+1, b[i+1:]))
else:
if b[i] < 0:
pass
else:
ans2 += abs(b[i]) + 1
b[i+1:] = list(map(lambda n:n-(abs(b[i])+1), b[i+1:]))
print(min(ans1,ans2))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
vector<int> vec(N);
vector<int> sum(N);
int error = 0;
int ans1 = 0;
int ans2 = 0;
for (int i = 0; i < N; i++) {
cin >> vec[i];
sum[i] = vec[i];
if (i) sum[i] += sum[i - 1];
}
for (int i = 0; i < N; i++) {
if (i % 2 == 0) {
if (sum[i] + error <= 0) {
ans1 += abs(sum[i] + error) + 1;
error += abs(sum[i] + error) + 1;
}
} else {
if (sum[i] + error >= 0) {
ans1 += abs(sum[i] + error) + 1;
error -= abs(sum[i] + error) + 1;
}
}
}
error = 0;
for (int i = 0; i < N; i++) {
if (i % 2 == 1) {
if (sum[i] + error <= 0) {
ans2 += abs(sum[i] + error) + 1;
error += abs(sum[i] + error) + 1;
}
} else {
if (sum[i] + error >= 0) {
ans2 += abs(sum[i] + error) + 1;
error -= abs(sum[i] + error) + 1;
}
}
}
cout << min(ans1, ans2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ABC59C
{
class Program
{
static void Main(string[] args)
{
int n = int.Parse(Console.ReadLine());
string[] str = Console.ReadLine().Split(' ');
int[] a = new int[n];
for(int i = 0; i < n; i++)
{
a[i] = int.Parse(str[i]);
}
int x = 0;
int f = 0;
int sum = 0;
if (a[0] < 0) f = 1;
if (a[0] == 0)
{
if (a[1] >= 0)
{
a[0] = -1;
}else
{
a[0] = 1;
}
x++;
}
for(int i = 0; i < n; i++)
{
sum += a[i];
if (f == 1 && sum >= 0)
{
x += (sum + 1);
f = 0;
sum = -1;
}else if (f == 0 && sum <= 0)
{
x += (1 - sum);
f = 1;
sum = 1;
}else
{
f = 1 - f;
}
}
Console.WriteLine(x);
}
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long MOD = pow(10, 9) + 7;
long long mod(long long A, long long M) { return (A % M + M) % M; }
const long long INF = 1LL << 60;
template <class T>
bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T>
bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
long long divCeil(long long A, long long B) { return (A + (B - 1)) / B; }
long long myctoi(char C) { return C - '0'; }
char myitoc(long long N) { return '0' + N; }
signed main() {
long long N;
cin >> N;
vector<long long> A(N);
for (long long i = 0; i < N; i++) {
cin >> A.at(i);
}
long long ans0 = 0, sum = 1;
for (long long i = 0; i < N; i++) {
long long s = sum;
sum += A.at(i);
if (s > 0 && sum >= 0) {
ans0 += 1 + sum;
sum = -1;
} else if (s < 0 && sum <= 0) {
ans0 += 1 - sum;
sum = 1;
}
}
long long ans1 = 0;
sum = -1;
for (long long i = 0; i < N; i++) {
long long s = sum;
sum += A.at(i);
if (s > 0 && sum >= 0) {
ans1 += 1 + sum;
} else if (s < 0 && sum <= 0) {
ans1 += 1 - sum;
}
}
cout << min(ans0, ans1) - 1 << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void) {
int n;
cin >> n;
int* a = new int[n];
int flg = 0;
long long sum = 0;
long long cnt = 0;
for (int i = 0; i < n; ++i) {
cin >> a[i];
if (i == 0) {
if (a[0] < 0)
flg = 1;
else
flg = 2;
}
sum += a[i];
if (flg == 0 && sum != 0) {
if (i % 2 == 0 && sum > 0) {
flg = 2;
cnt = 2 * i - 1;
} else if (i % 2 == 0 && sum < 0) {
flg = 1;
cnt = 2 * i - 1;
} else if (i % 2 == 1 && sum > 0) {
flg = 1;
cnt = 2 * i - 1;
} else if (i % 2 == 1 && sum < 0) {
flg = 2;
cnt = 2 * i - 1;
}
}
if (flg == 1 && i % 2 == 0 && sum >= 0) {
while (sum >= 0) {
--sum;
++cnt;
}
} else if (flg == 1 && i % 2 == 1 && sum <= 0) {
while (sum <= 0) {
++sum;
++cnt;
}
} else if (flg == 2 && i % 2 == 0 && sum <= 0) {
while (sum <= 0) {
++sum;
++cnt;
}
} else if (flg == 2 && i % 2 == 1 && sum >= 0) {
while (sum >= 0) {
--sum;
++cnt;
}
}
}
cout << cnt << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int,input().split()))
ans = 0
cnt = [a[0]]
for i in range(1,n):
cnt.append(cnt[-1]+a[i])
cnt_o = cnt[::2]
cnt_e = cnt[1::2]
o = sum(cnt_o)
e = sum(cnt_e)
ans = 0
c = 0
if o >= e:
for i in range(n):
cnt[i] += c
if (i+1) % 2 != 0:
if cnt[i] > 0:
pass
else:
ans += abs(cnt[i])+1
c += abs(cnt[i])+1
else:
if cnt[i] < 0:
pass
else:
ans += cnt[i]+1
c -= a[i]+1
else:
for i in range(n):
cnt[i] += c
if (i+1) % 2 != 0:
if cnt[i] < 0:
pass
else:
ans += cnt[i]+1
c -= cnt[i]+1
else:
if cnt[i] > 0:
pass
else:
ans += abs(cnt[i])+1
c += abs(a[i])+1
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | # input
n = int(input())
A = list(map(int, input().split()))
# 偶数番目が+
A_even = A
S_even = 0
ans_even = 0
for i in range(n):
if (S_even + A_even[i]) * ((-1) ** (i - 1)) > 0:
S_even += A_even[i]
continue
ans_even += S_even + A_even[i] + 1
A_even[i] = (-1) ** (i - 1) - S_even
S_even = (-1) ** (i - 1)
# 奇数番目が+
A_odd = A
S_odd = 0
ans_odd = 0
for i in range(n):
if (S_odd + A_odd[i]) * ((-1) ** i) > 0:
S_odd += A_odd[i]
continue
ans_odd += S_odd + A_odd[i] + 1
A_odd[i] = (-1) ** i - S_odd
S_odd = (-1) ** i
ans = min(ans_even, ans_odd)
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
int main() {
int n, sum;
std::cin >> n >> sum;
int a, count;
std::cin >> a;
sum += a;
count = 0;
for (int i = 0; i < n - 2; i++) {
std::cin >> a;
int tmp = sum + a;
if (sum * tmp < 0) {
sum = tmp;
continue;
}
int next_sum = (-1) * sum / abs(sum);
count += abs(next_sum - tmp);
sum = next_sum;
}
std::cout << count << std::endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
res = -max(0, ~a[0])
sm = 1
if a[0] > 1: sm = a[0]
for i in range(1, N):
if sm > 0:
res += max(0, a[i] + sm + 1)
sm = min(-1, a[i])
else:
res += max(0, sm - a[i] + 1)
sm = max(1, a[i])
res2 = max(0, a[0] + 1)
sm = -1
if a[0] < -1: sm = a[0]
for i in range(1, N):
if sm > 0:
res2 += max(0, a[i] + sm + 1)
sm = min(-1, a[i])
else:
res2 += max(0, sm - a[i] + 1)
sm = max(1, a[i])
print(min(res, 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;
using ll = long long;
using vi = vector<int>;
constexpr int sign(int n) { return n < 0 ? -1 : n > 0; }
int main() {
int N;
cin >> N;
vi A(N);
for (int i = 0, _i = (N); i < _i; ++i) cin >> A[i];
ll sum = A[0], ans = 0;
for (int i = 0, _i = (N - 1); i < _i; ++i) {
if (sum == 0) {
sum = sign(A[i + 1]) * (abs(A[i + 1]) - 1);
++ans;
continue;
}
if (sign(sum) == sign(sum + A[i + 1])) {
ans += abs(-sign(sum) - (sum + A[i + 1]));
sum = -sign(sum);
} else
sum += A[i + 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;
long int check(long int sum, long int ans, vector<int> T, int N, bool pre_pm) {
for (int i = 0; i < N; i++) {
if (pre_pm) {
sum += T.at(i);
while (0 <= sum) {
sum--;
ans++;
}
pre_pm = false;
} else {
sum += T.at(i);
while (sum <= 0) {
sum++;
ans++;
}
pre_pm = true;
}
}
return ans;
}
int main() {
int N;
vector<int> T;
cin >> N;
for (int i = 0; i < N; i++) {
int tmp;
cin >> tmp;
T.push_back(tmp);
}
long int ans = 0;
long int sum = 0;
bool pre_pm;
cout << check(sum, ans, T, N, true) << endl;
cout << check(sum, ans, T, N, false) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long seq(int a, vector<long long> b) {
long long count = 0;
if (0 <= b[0] * a) {
if (b[0] == 0) {
b[0] = a;
count++;
}
long long s = b[0];
for (int i = 1; i < b.size(); i++) {
int k = (1 - 2 * (i % 2)) * a;
s += b[i];
if (s * k <= 0) {
count += 1 - k * s;
s = k;
}
}
}
return count;
}
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
if (a.at(0) == 0)
cout << min(seq(1, a), seq(-1, a)) << endl;
else if (a.at(0) > 0)
cout << seq(1, a) << endl;
else
cout << seq(-1, a) << 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 argc, char *argv[]) { return 0; }
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
static const int INF = 0x3f3f3f3f;
static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;
template <typename T, typename U>
inline void amin(T &x, U y) {
if (y < x) x = y;
}
template <typename T, typename U>
inline void amax(T &x, U y) {
if (x < y) x = y;
}
signed main() {
long long n;
cin >> n;
vector<long long> a(n);
for (long long(i) = 0; (i) < (long long)(n); (i)++) cin >> a[i];
long long sum = 0;
long long prev = 0;
sum += a[0];
long long ans = 0;
if (sum != 0) {
for (long long(i) = (long long)(1); (i) < (long long)(n); (i)++) {
prev = sum;
sum += a[i];
if (prev * sum < 0) {
continue;
} else {
if (sum > 0) {
ans += sum + 1;
sum = -1;
} else if (sum < 0) {
ans += abs(sum) + 1;
sum = 1;
} else {
ans++;
sum = (prev < 0 ? 1 : -1);
}
}
}
} else {
ans = INF;
}
sum = 0;
prev = 0;
long long ans2 = 0;
sum += a[0];
if (sum > 0) {
ans2 += sum + 1;
sum = -1;
} else if (sum < 0) {
ans2 += abs(sum) + 1;
sum = 1;
} else {
ans2++;
sum = 1;
}
for (long long(i) = (long long)(1); (i) < (long long)(n); (i)++) {
prev = sum;
sum += a[i];
if (prev * sum < 0) {
continue;
} else {
if (sum > 0) {
ans2 += sum + 1;
sum = -1;
} else if (sum < 0) {
ans2 += abs(sum) + 1;
sum = 1;
} else {
ans2++;
sum = (prev < 0 ? 1 : -1);
}
}
}
sum = 0;
prev = 0;
long long ans3 = 0;
sum += a[0];
if (sum > 0) {
ans3 += sum + 1;
sum = -1;
} else if (sum < 0) {
ans3 += abs(sum) + 1;
sum = 1;
} else {
ans3++;
sum = -1;
}
for (long long(i) = (long long)(1); (i) < (long long)(n); (i)++) {
prev = sum;
sum += a[i];
if (prev * sum < 0) {
continue;
} else {
if (sum > 0) {
ans3 += sum + 1;
sum = -1;
} else if (sum < 0) {
ans3 += abs(sum) + 1;
sum = 1;
} else {
ans3++;
sum = (prev < 0 ? 1 : -1);
}
}
}
cout << min(ans, min(ans3, ans2)) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <bits/stdc++.h>
int main(void) {
int num_integer;
if (scanf("%d", &num_integer) != 1) {
puts("num_integer input error.");
return 1;
}
int integer, sum_1 = 0, sum_2 = 0, num_operation_1 = 0, num_operation_2 = 0;
for (int i = 0; i < num_integer; i++) {
if (scanf("%d", &integer) != 1) {
puts("integer input error.");
}
if (sum_1 > 0 && (sum_1 + integer) >= 0) {
num_operation_1 += sum_1 + integer + 1;
sum_1 = -1;
} else if (sum_1 < 0 && (sum_1 + integer) <= 0) {
num_operation_1 += -(sum_1 + integer) + 1;
sum_1 = 1;
} else {
sum_1 += integer;
}
if (sum_2 > 0 && (sum_2 + integer) >= 0) {
num_operation_2 += sum_2 + integer + 1;
sum_2 = -1;
} else if (sum_2 < 0 && (sum_2 + integer) <= 0) {
num_operation_2 += -(sum_2 + integer) + 1;
sum_2 = 1;
} else if (i == 0) {
num_operation_2 = (integer > 0) ? integer + 1 : -integer + 1;
sum_2 = (integer > 0) ? -1 : 1;
} else {
sum_2 += integer;
}
}
printf("%d", (num_operation_1 < num_operation_2) ? num_operation_1
: num_operation_2);
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];
long long cnt1 = 0, cnt2 = 0;
long long sumv = 0;
for (int i = 0; i < n; i++) {
sumv += a[i];
if (i % 2 == 0 && sumv < 0) {
sumv = 1;
cnt1 += abs(sumv) + 1;
} else if (i % 2 == 1 && sumv > 0) {
sumv = -1;
cnt1 += abs(sumv) + 1;
}
}
sumv = 0;
for (int i = 0; i < n; i++) {
sumv += a[i];
if (i % 2 == 0 && sumv > 0) {
sumv = -1;
cnt2 += abs(sumv) + 1;
} else if (i % 2 == 1 && sumv < 0) {
sumv = 1;
cnt2 += abs(sumv) + 1;
}
}
cout << min(cnt1, cnt2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | n = gets.to_i
a = gets.chomp.split.map(&:to_i)
sum1 = 0
sum2 = 0
ans = 0
for i in 0..n-2
sum1 += a[i]
if sum1 == 0
sum1 += 1
ans += 1
end
sum2 = sum1 + a[i+1]
if sum2 * sum1 >= 0
if sum1 < 0
a[i+1] += 1 - sum2
ans += 1 - sum2
else
a[i+1] -= sum2 + 1
ans += sum2 + 1
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>
int number[100001];
int N;
int sum[100001];
int main() {
int mul = 0;
long long int answer = 0;
int fugou = 0;
scanf("%d", &N);
for (int i = 0; i < N; i++) {
scanf("%d", &(number[i]));
mul += number[i];
sum[i] = mul;
if (i > 0) {
if (sum[i - 1] * mul >= 0) {
answer += (long long int)abs(mul) + 1;
}
}
}
printf("%lld\n", answer);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
scanf("%d", &n);
int a[n];
for (int i = 0; i < n; i++) scanf(" %d", &a[i]);
int S = a[0];
int j = 0;
for (int i = 1; i < n; i++) {
if (S * (S + a[i]) < 0) {
S += a[i];
} else {
if (S < 0) {
j += 1 - S - a[i];
S = 1;
} else {
j += S + a[i] + 1;
S = -1;
}
}
}
printf("%d\n", j);
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"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;
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 = 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;
const long long N = 123456;
long long a[N];
long long b[N];
long long gh(long long n) {
long long h = 0;
long long s = a[0];
long long q = 0;
if (s > 0)
q = 1;
else
q = 0;
for (long long i = 1; i < n; i++) {
s += a[i];
if (s == 0) {
if (q == 0) {
h++;
a[i]++;
q = 1;
} else {
h++;
a[i]--;
q = 0;
}
} else if (s < 0) {
long long foo = abs(s);
if (q == 0) {
h += (foo + 1);
a[i] += (foo + 1);
s += (foo + 1);
q = 1;
} else {
q = 0;
}
} else {
long long foo = (s);
if (q == 1) {
h += (foo + 1);
a[i] -= (foo + 1);
s -= (foo + 1);
q = 0;
} else {
q = 1;
}
}
}
for (long long i = 0; i < n; i++) {
a[i] = b[i];
}
return h;
}
signed main() {
long long n;
scanf("%lld", &n);
for (long long i = 0; i < n; i++) {
scanf("%lld", &a[i]);
b[i] = a[i];
}
long long ap1 = gh(n);
long long u1 = 123456789;
long long u2 = 123456789;
long long u3 = 123456789;
if (a[0] == 0) {
ap1 = 123456789;
long long ap2 = 0;
a[0] = 1;
ap2 += 1;
ap2 += gh(n);
long long ap3 = 0;
a[0] = -1;
ap3 += 1;
ap3 += gh(n);
u1 = min(ap2, ap3);
} else if (a[0] < 0) {
long long uoo = a[0];
u2 = 0;
a[0] = 1;
u2 += (1 - uoo);
u2 += gh(n);
} else if (a[0] > 0) {
long long uoo = a[0];
u3 = 0;
a[0] = -1;
u3 += (uoo + 1);
u3 += gh(n);
}
printf("%lld\n", min(min(u1, u2), min(u3, ap1)));
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long n_ = 1e5 + 1000;
const long double PI = acos(-1.0);
long long gcd(long long a, long long b) { return (a ? gcd(b % a, a) : b); }
long long power(long long a, long long n) {
long long p = 1;
while (n > 0) {
if (n % 2) {
p = p * a;
}
n >>= 1;
a *= a;
}
return p;
}
long long power(long long a, long long n, long long mod) {
long long p = 1;
while (n > 0) {
if (n % 2) {
p = p * a;
p %= mod;
}
n >>= 1;
a *= a;
a %= mod;
}
return p % mod;
}
bool vowel(char ch) {
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
return true;
else
return false;
}
class UnionFind {
private:
vector<long long> p, rank, setSize;
long long numSets;
public:
UnionFind(long long N) {
setSize.assign(N, 1);
numSets = N;
rank.assign(N, 0);
p.assign(N, 0);
for (long long i = 0; i < N; i++) p[i] = i;
}
long long findSet(long long i) {
return (p[i] == i) ? i : (p[i] = findSet(p[i]));
}
bool isSameSet(long long i, long long j) { return findSet(i) == findSet(j); }
void unionSet(long long i, long long j) {
if (!isSameSet(i, j)) {
numSets--;
long long x = findSet(i), y = findSet(j);
if (rank[x] > rank[y]) {
p[y] = x;
setSize[x] += setSize[y];
} else {
p[x] = y;
setSize[y] += setSize[x];
if (rank[x] == rank[y]) rank[y]++;
}
}
}
long long numDisjointSets() { return numSets; }
long long sizeOfSet(long long i) { return setSize[findSet(i)]; }
};
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n;
cin >> n;
long long arr[n];
for (long long i = 0; i < n; i++) cin >> arr[i];
;
long long sum = 0, ans = 0, sign = 1;
if (arr[0] < 0) sign = -1;
for (long long i = 0; i < n; i++) {
sum += arr[i];
if (sign * sum <= 0) {
if (sign == 1) {
ans += (1 - sum);
sum = 1;
} else {
ans += (sum + 1);
sum = -1;
}
}
sign *= -1;
}
cout << ans;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
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 (i == N - 1) {
break;
}
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 | UNKNOWN | #pragma warning disable
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Numerics;
using System.Collections;
static class MainClass
{
public static void Main(string[] args)
{
var n = Console.ReadLine().ToInt32();
var an = Console.ReadLine().SplittedStringToInt32List();
var sum = 0;
var ruisekiwa = new long[n];
ruisekiwa[0] = an[0];
var count = 0l;
for (int i = 1; i < n; i++)
{
var before = ruisekiwa[i - 1] + an[i];
if ((before < 0 && ruisekiwa[i - 1] < 0))
{
count += Math.Abs(before) + 1;
before = 1;
}
else if (before > 0 && ruisekiwa[i - 1] > 0)
{
count += before + 1;
before = -1;
}
ruisekiwa[i] = before;
}
if (ruisekiwa[n - 1] == 0)
count++;
Console.WriteLine(count);
Console.ReadLine();
//var nm = Console.ReadLine().SplittedStringToInt32List();
//var n = nm[0];
//var m = nm[1];
//InitializeUnionFind(n);
//var abs = new List<KeyValuePair<int,int>>();
//for (int i = 0; i < m; i++)
//{
// var ab = Console.ReadLine().SplittedStringToInt32List();
// abs.Add(new KeyValuePair<int, int>(ab[0] - 1, ab[1] - 1));
//}
//abs.Reverse();
//var ls = new List<int>();
//foreach (var item in abs)
//{
// Unite(item.Key, item.Value);
// ls.Add(ParentCount);
//}
//ls.Reverse();
//var before = -1;
//var befores = 0;
//for (int i = 0; i < m; i++)
//{
// if (before != -1)
// {
// var num = ls[i] - befores;
// Console.WriteLine((num * (num - 1))/2 + before);
// before = (num * (num - 1)) / 2 + before;
// }
// else
// {
// var num = ls[i] - befores;
// Console.WriteLine((num * ( num - 1)) / 2);
// before = (num * (num - 1)) / 2;
// }
// befores = ls[i];
//}
//Console.ReadLine();
}
public static BigInteger[] factors;
public static BigInteger[] revFactors;
public static void SetFactor(int N)
{
factors = new BigInteger[N];
factors[0] = 1;
revFactors = new BigInteger[N];
for (int i = 1; i < N; i++)
{
factors[i] = factors[i - 1] * i;
factors[i] %= Mod1e9;
}
revFactors[N - 1] = BigInteger.ModPow(factors[N - 1], Mod1e9 - 2, Mod1e9);
for (int i = N - 2; i >= 0; i--)
{
revFactors[i] = revFactors[i + 1] * (i + 1);
revFactors[i] %= Mod1e9;
}
}
public static BigInteger GetCombination(int a, int b)
{
return a >= b ? (((factors[a] * revFactors[b]) % Mod1e9) * revFactors[a - b]) % Mod1e9 : 0;
}
public static int ParentCount;
public static int[] Parents;
public static int[] IfParentsChildrenCount;
public static int[] Ranks;
public static void InitializeUnionFind(int N)
{
Parents = new int[N];
Ranks = new int[N];
IfParentsChildrenCount = new int[N];
for (int i = 0; i < N; i++)
{
Parents[i] = i;
Ranks[i] = 0;
IfParentsChildrenCount[i] = 1;
}
ParentCount = N + 1;
}
public static int Root(int a)
{
if (Parents[a] == a)
{
return a;
}
else
{
Parents[a] = Root(Parents[a]);
return Parents[a];
}
}
public static bool Same(int a, int b)
{
return Root(a) == Root(b);
}
public static void Unite(int a, int b)
{
var parentA = Root(a);
var parentB = Root(b);
if (parentA == parentB)
return;
if (Ranks[parentA] < Ranks[parentB])
{
Parents[parentA] = parentB;
IfParentsChildrenCount[parentB] += IfParentsChildrenCount[parentA];
ParentCount -= IfParentsChildrenCount[parentA];
IfParentsChildrenCount[parentA] = 0;
}
else
{
Parents[parentB] = parentA;
if (Ranks[parentA] == Ranks[parentB])
{
Ranks[parentA]++;
}
IfParentsChildrenCount[parentA] += IfParentsChildrenCount[parentB];
ParentCount -= IfParentsChildrenCount[parentB];
IfParentsChildrenCount[parentB] = 0;
}
}
#region ライブラリ
public static long ToInt64(this string str) => long.Parse(str);
public static int ToInt32(this string str) => int.Parse(str);
public static BigInteger ToBigInteger(this string str) => BigInteger.Parse(str);
public static List<string> SplittedStringToList(this string str) => str.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList();
public static List<int> SplittedStringToInt32List(this string str) => str.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(x => int.Parse(x)).ToList();
public static List<long> SplittedStringToInt64List(this string str) => str.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(x => long.Parse(x)).ToList();
public static List<BigInteger> SplittedStringToBigInteger(this string str) => str.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(x => BigInteger.Parse(x)).ToList();
public const int INF = int.MaxValue / 4;
public const long LONGINF = long.MaxValue / 2;
public const int Mod1e9 = 1000000007;
public static void PrintArray(bool[,] array)
{
for (int i = 0; i < array.GetLength(0); i++)
{
var sb = new StringBuilder();
for (int j = 0; j < array.GetLength(1); j++)
{
sb.Append(array[i, j]).Append(" ");
}
Console.WriteLine(sb.ToString());
}
}
public static int Manhattan(int x1, int x2, int y1, int y2)
{
return Math.Abs(x1 - x2) + Math.Abs(y1 - y2);
}
public static Dictionary<T, int> ToCountedDictionary<T>(this IEnumerable<T> items)
{
var dic = new Dictionary<T, int>();
foreach (var item in items)
{
if (dic.ContainsKey(item))
dic[item]++;
else
dic.Add(item, 1);
}
return dic;
}
public static long Sums(IEnumerable<int> list)
{
var sum = 0l;
foreach (var item in list)
{
sum += item;
}
return sum;
#endregion
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
cin.sync_with_stdio(false);
int n;
cin >> n;
vector<ll> a(n);
ll sum = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
ll count = 0;
sum = a[0];
for (int i = 1; i < n; i++) {
if (sum < 0 && sum + a[i] <= 0) {
count += abs(sum) - a[i] + 1;
sum = 1;
} else if (sum > 0 && sum + a[i] >= 0) {
count += abs(sum + a[i] + 1);
sum = -1;
} else {
sum += a[i];
}
if (sum == 0) {
count += 1;
}
}
cout << count << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
#define forx(i,a,b) for(int i=(a);i<(b);i++)
#define rep(j,n) for(int j=0;j<(n);j++)
typedef long long ll;
int main()
{
int n,ansa=0,ansb=0,sum=0;
cin>>n;
bool plus=true;
vector<int>t(n);
rep(i,n){
int a;
cin>>t(i);
a=t(i);
while(plus&&sum+a<=0){
a++;
ansa++;
}
while(!plus&&sum+a>=0){
a--;
ansa++;
}
sum+=a;
plus=!plus;
}
plus=false;
sum=0;
rep(i,n){
int a;
a=t(i);
while(plus&&sum+a>=0){
a++;
ansb++;
}
while(!plus&&sum+a<=0){
a--;
ansb++;
}
sum+=a;
plus=!plus;
}
cout<<min(ansa,ansb)<<endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9 + 7;
const int INF = 100100100;
const double PI = 3.14159265358979323846;
int main() {
int n, a[100001] = {0};
int res = 0;
long long sum = 0;
cin >> n;
for (int i = 0; i < (n); ++i) cin >> a[i];
int notZ = 0;
for (int i = 0; i < (n); ++i) {
if (a[i] != 0) {
notZ = i;
break;
}
}
if (notZ > 0) {
if (notZ % 2 == 1) {
a[0] = -1;
++res;
} else if (notZ % 2 == 0) {
a[0] = 1;
++res;
}
}
sum = a[0];
for (int i = (1); i < (n); ++i) {
int prod = sum * (sum + a[i]);
if (prod >= 0) {
int newSum = -sum / abs(sum);
res += abs(newSum - sum - a[i]);
sum = newSum;
} else if (prod < 0) {
sum += a[i];
}
}
cout << res << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
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 = a[0];
for (int i = 1; i < n; i++) {
int tmp = sum + a[i];
if (sum < 0 && tmp <= 0) {
int add = 1 - tmp;
sum = tmp + add;
ans += add;
} else if (sum > 0 && tmp >= 0) {
int add = -1 - tmp;
sum = tmp + add;
add *= -1;
ans += add;
} else {
sum = tmp;
}
}
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;
int main() {
const int N = 100000;
int n;
long long a[N];
long long sum = 0, ans = 0;
bool sign1 = true;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sum = a[0];
if (sum >= 0)
sign1 = true;
else
sign1 = false;
for (int i = 1; i < n; i++) {
bool sign2 = true;
sum += a[i];
if (sum >= 0)
sign2 = true;
else
sign2 = false;
if (sign1 != sign2) {
sign1 = sign2;
if (sum == 0) {
ans++;
if (sign1)
sum = -1;
else
sum = 1;
}
} else {
sign1 = !sign2;
ans += abs(sum) + 1;
if (sign2)
sum = -1;
else
sum = 1;
}
}
cout << ans << endl;
return (0);
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
int main(int argc, char const *argv[]) {
int n;
std::cin >> n;
std::vector<int> v(n);
std::vector<int> sums(2, 0);
for (size_t i = 0; i < n; i++) {
std::cin >> v[i];
sums[i % 2] += v[i];
}
ull ans = 0;
if (sums[0] > sums[1] && v[0] <= 0) {
ans = ans + abs(v[0]) + 1;
v[0] = 1;
} else if (sums[0] < sums[1] && v[0] >= 0) {
ans = ans + abs(v[0]) + 1;
v[0] = -1;
} else if (v[0] == 0) {
ans += 1;
v[0] = 1;
}
ll now, pre;
now = pre = v[0];
for (size_t i = 1; i < n; i++) {
now = v[i] + pre;
if (pre * now >= 0) {
if (pre > 0) {
ans = ans + abs(now) + 1;
now = -1;
} else if (pre < 0) {
ans = ans + abs(now) + 1;
now = 1;
}
}
pre = now;
}
std::cout << ans << '\n';
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
template <class T>
bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
template <class T>
int former(const vector<T> &v, T x) {
return upper_bound(v.begin(), v.end(), x) - v.begin() - 1;
}
template <class T>
int latter(const vector<T> &v, T x) {
return lower_bound(v.begin(), v.end(), x) - v.begin();
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
const long long LLINF = 1LL << 60;
const int INTINF = 1 << 30;
const int MAX = 510000;
const int MOD = 1000000007;
long long fac[MAX], finv[MAX], inv[MAX];
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;
}
}
long long 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 UnionFind {
vector<long long> par;
UnionFind(long long n) : par(n, -1) {}
void init(long long n) { par.assign(n, -1); }
long long root(long long x) {
if (par[x] < 0)
return x;
else
return par[x] = root(par[x]);
}
bool issame(long long x, long long y) { return root(x) == root(y); }
bool merge(long long x, long long 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;
}
long long size(long long x) { return -par[root(x)]; }
};
template <typename T>
vector<T> dijkstra(int s, vector<vector<pair<int, T> > > &G) {
const T INF = numeric_limits<T>::max();
using P = pair<T, int>;
int n = G.size();
vector<T> d(n, INF);
vector<int> b(n, -1);
priority_queue<P, vector<P>, greater<P> > q;
d[s] = 0;
q.emplace(d[s], s);
while (!q.empty()) {
P p = q.top();
q.pop();
int v = p.second;
if (d[v] < p.first) continue;
for (auto &e : G[v]) {
int u = e.first;
T c = e.second;
if (d[u] > d[v] + c) {
d[u] = d[v] + c;
b[u] = v;
q.emplace(d[u], u);
}
}
}
return d;
}
vector<vector<int> > bfs(vector<string> &s, int sy, int sx, char wall,
int dir) {
int h = s.size(), w = s.front().size();
vector<vector<int> > dp(h, vector<int>(w, -1));
using P = pair<int, int>;
queue<P> q;
dp[sy][sx] = 0;
q.emplace(sy, sx);
int dy[] = {1, -1, 0, 0, 1, 1, -1, -1};
int dx[] = {0, 0, 1, -1, 1, -1, 1, -1};
auto in = [&](int y, int x) { return 0 <= y && y < h && 0 <= x && x < w; };
while (!q.empty()) {
int y, x;
tie(y, x) = q.front();
q.pop();
for (int k = 0; k < dir; k++) {
int ny = y + dy[k], nx = x + dx[k];
if (!in(ny, nx) || s[ny][nx] == wall) continue;
if (~dp[ny][nx]) continue;
dp[ny][nx] = dp[y][x] + 1;
q.emplace(ny, nx);
}
}
return dp;
}
int64_t power(int64_t x, int64_t n, int64_t mod) {
int64_t ret = 1;
while (n > 0) {
if (n & 1) (ret *= x) %= mod;
(x *= x) %= mod;
n >>= 1;
}
return ret;
}
vector<int> sieve_of_eratosthenes(int n) {
vector<int> primes(n);
for (int i = 2; i < n; ++i) primes[i] = i;
for (int i = 2; i * i < n; ++i)
if (primes[i])
for (int j = i * i; j < n; j += i) primes[j] = 0;
return primes;
}
std::vector<long long> divisor(long long n) {
std::vector<long long> ret;
for (long long i = 1; i * i <= n; ++i) {
if (n % i == 0) {
ret.push_back(i);
if (i != 1 && i * i != n) {
ret.push_back(n / i);
}
}
}
return ret;
}
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int main(void) {
long long n;
cin >> n;
vector<long long> a(n);
for (long long i = 0, i_len = (n); i < i_len; ++i) cin >> a[i];
long long sum = 0;
long long count = 0;
long long ans = 0;
for (long long i = 0, i_len = (n); i < i_len; ++i) {
sum += a[i];
if (i % 2 == 0)
if (sum < 0) ans += 1 - sum;
if (i % 2 != 0)
if (sum > 0) ans += sum + 1;
}
sum = 0;
for (long long i = 0, i_len = (n); i < i_len; ++i) {
sum += a[i];
if (i % 2 == 0)
if (sum > 0) count += sum + 1;
if (i % 2 != 0)
if (sum < 0) count += 1 - sum;
}
chmin(ans, count);
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
int sum = 0;
for (long long i = 0; i < n; i++) {
int x;
cin >> x;
sum += x;
a[i] = sum;
}
int f = (a[0] > 0 ? 1 : -1);
int ans = 0;
int fix = 0;
for (long long i = 0; i < n; i++) {
if (f == 1) {
if (a[i] + fix <= 0) {
ans += 1 - (fix + a[i]);
fix += 1 - (fix + a[i]);
}
f = -1;
} else {
if (a[i] + fix >= 0) {
ans += (fix + a[i]) + 1;
fix -= ((fix + a[i]) + 1);
}
f = 1;
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
namespace ABC059C
{
class Program
{
static void Main(string[] args)
{
int n = Int32.Parse(Console.ReadLine());
string[] bufs = Console.ReadLine().Split(' ');
int res = 0;
int sum = Int32.Parse(bufs[0]);
if (sum == 0) { res++; }
for (int i = 1; i < n; i++)
{
int tmp = sum + Int32.Parse(bufs[i]);
if (sum == 0)
{
if (tmp == 0)
{
res += 2;
}
else if (tmp == 1 || tmp == -1)
{
sum = tmp;
res++;
}
else if (tmp > 1)
{
sum = tmp - 1;
}
else
{
sum = tmp + 1;
}
}
else if (sum > 0)
{
if (tmp >= 0)
{
res += tmp + 1;
sum = -1;
}
else
{
sum = tmp;
}
}
else
{
if (tmp <= 0)
{
res += -tmp + 1;
sum = 1;
}
else
{
sum = tmp;
}
}
}
Console.WriteLine(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 main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a.at(i);
}
long long sumi = 0, val1 = 0;
int ne = 0;
if (a.at(0) == 0) {
while (a.at(ne) == 0) {
if (ne == 0)
val1++;
else
val1 += 2;
ne++;
if (ne == n) break;
}
}
long long val2 = val1;
for (int i = ne; i < n; i++) {
if (a.at(0) == 0) {
if (ne % 2 == 0)
sumi = -1;
else
sumi = 1;
}
if (i == 0) {
sumi = a.at(i);
continue;
}
if (i % 2 == 1) {
if (sumi + a.at(i) < 0)
sumi += a.at(i);
else {
val1 += (sumi + a.at(i) + 1);
sumi = -1;
}
} else {
if (sumi + a.at(i) > 0)
sumi += a.at(i);
else {
val1 += (abs(sumi + a.at(i)) + 1);
sumi = 1;
}
}
}
for (int i = ne; i < n; i++) {
if (a.at(0) == 0) {
if (ne % 2 == 0)
sumi = 1;
else
sumi = -1;
}
if (i == 0) {
sumi = a.at(i);
continue;
}
if (i % 2 == 1) {
if (sumi + a.at(i) > 0)
sumi += a.at(i);
else {
val2 += (abs(sumi + a.at(i)) + 1);
sumi = 1;
}
} else {
if (sumi + a.at(i) < 0)
sumi += a.at(i);
else {
val2 += (sumi + a.at(i) + 1);
sumi = -1;
}
}
}
cout << min(val1, val2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <iostream>
#include <cstdio>
#define N 100005
using namespace std;
typedef long long ll;
ll n, b, a[N];
ll abs(ll p) {return p > 0 ? p : -p;}
ll f(ll p) {
ll i, s = 0;
for (i = 1; i < n; i++) {
if (p > 0) {
p += a[i];
if (p >= 0) s += p + 1, p = -1;
} else {
p += a[i];
if (p <= 0) s += -p + 1, p = 1;
}
}
return s;
}
int main()
{
ll i, t = 1e19;
cin >> n;
for (i = 0; i < n; i++) scanf("%lld", &a[i]);
if (a[0] > 0) t = f(-1) + a[0] - 1;
else t = f(1) - a[0] + 1;
cout << min(f(a[0]), t);
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() {
const int N = 100000;
int n;
long long a[N];
long long sum = 0, ans = 0;
bool sign1 = true;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sum = a[0];
if (sum >= 0)
sign1 = true;
else
sign1 = false;
for (int i = 1; i < n; i++) {
bool sign2 = true;
sum += a[i];
if (sum >= 0)
sign2 = true;
else
sign2 = false;
if (sign1 != sign2) {
if (sum == 0) {
ans++;
if (sign1)
sum = -1;
else
sum = 1;
}
sign1 = sign2;
} else {
ans += abs(sum) + 1;
if (sign2)
sum = -1;
else
sum = 1;
sign1 = !sign2;
}
}
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;
unsigned long long func(vector<long long int>& s, vector<int>& hugo,
long long int k) {
unsigned long long int ret = 0;
for (int i = 1; i < n; i++) {
if (s[i] == k) {
if (hugo[i - 1] == 0) {
hugo[i] = 1;
ret++;
k++;
} else {
hugo[i] = 0;
ret++;
k--;
}
} else if (s[i] > k) {
if (hugo[i - 1] == 0) {
hugo[i] = 1;
ret += s[i] - k + 1;
k += s[i] - k + 1;
} else {
hugo[i] = 0;
}
} else {
if (hugo[i - 1] == 0) {
hugo[i] = 1;
} else {
hugo[i] = 0;
ret += k - s[i] + 1;
k -= k - s[i] + 1;
}
}
}
return ret;
}
void solve() {
cin >> n;
vector<long long int> v(n), sum(n), sum2(n);
for (int i = 0; i < n; i++) {
cin >> v[i];
if (i == 0)
sum[i] = v[i];
else
sum[i] = sum[i - 1] + v[i];
}
vector<int> hugo(n), hugo2(n);
sum2 = sum;
unsigned long long int ans;
long long int k = 0;
if (sum[0] == 0) {
hugo[0] = 0;
hugo2[0] = 1;
ans = min(func(sum, hugo, -1), func(sum2, hugo2, 1));
} else if (sum[0] > 0) {
hugo[0] = 0;
hugo2[0] = 1;
ans = min(func(sum, hugo, 0), func(sum2, hugo2, sum2[0] + 1) + sum2[0] + 1);
} else {
hugo[0] = 1;
hugo2[0] = 0;
ans = min(func(sum, hugo, 0), func(sum2, hugo2, sum2[0] - 1) - sum2[0] + 1);
}
cout << ans << endl;
return;
}
int main() {
solve();
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int64_t> a(n);
for (int i = 0; i < n; i++) cin >> a.at(i);
int64_t ans = 0, s = 0;
for (int i = 0; i < n; i++) {
if (s > 0) {
if (s + a.at(i) >= 0) {
ans += (s + a.at(i) - (-1));
s = -1;
} else
s += a.at(i);
} else if (s < 0) {
if (s + a.at(i) <= 0) {
ans += (1 - (s + a.at(i)));
s = 1;
} else
s += a.at(i);
} else
s += a.at(i);
if (s == 0) {
ans++;
if (i < n - 1) {
for (int j = i + 1; j < n; j++) {
if (a.at(j) > 0) {
s = -1;
break;
} else if (a.at(j) < 0) {
s = 1;
break;
}
}
}
}
}
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;
long long a[100004], temp[100004];
long long solve() {
long long ans = 0;
for (int i = (2); i <= (int)(n); ++i) {
if (a[i - 1] > 0) {
if (a[i] + a[i - 1] < 0) {
a[i] += a[i - 1];
continue;
}
ans += abs(a[i] + 1 + a[i - 1]);
a[i] = -1;
} else {
if (a[i] + a[i - 1] > 0) {
a[i] += a[i - 1];
continue;
}
ans += abs(a[i] - 1 + a[i - 1]);
a[i] = 1;
}
}
return ans;
}
int main() {
scanf("%d", &n);
for (int i = (1); i <= (int)(n); ++i) scanf("%lld", &a[i]);
long long ans = 0;
if (!a[1]) {
a[1] = 1;
memcpy(temp, a, sizeof(a));
ans = solve() + 1;
memcpy(a, temp, sizeof(temp));
a[1] = -1;
ans = min(ans, solve() + 1);
} else {
memcpy(temp, a, sizeof(a));
ans = solve();
memcpy(a, temp, sizeof(temp));
long long t = abs(a[1]) - 1;
if (a[1] > 0)
a[1] = -1;
else
a[1] = 1;
ans = min(ans, solve() + t);
}
printf("%lld\n", ans);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | def solve(n, a)
(n-1).times{|i|a[i+1]+=a[i]}
sum=res=0
(n-1).times do |i|
a[i+1] += sum
next if a[i]*a[i+1]<0
if a[i+1]==0
res += 1
sum -= a[i]/a[i].abs
else
res += a[i+1].abs + 1
sum -= a[i+1] + a[i+1]/a[i+1].abs
end
a[i+1] = -a[i]/a[i].abs
end
return res
end
n=gets.to_i
a=gets.split.map &:to_i
if a[0]!=0
p solve(n, a)
else
a[0]=1
s1 = 1+solve(n, a.dup)
a[0]=-1
s2 = 1+solve(n, a.dup)
p [s1, s2].min
end
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.