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": []
} | CORRECT | cpp |
#include <iostream>
#include <string.h>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;
const ll INF=100000000000000LL;
ll a[100001];
ll r[2]={};
int main() {
int n;
cin>>n;
ll t1=0;
ll t2=0;
ll s1=0;
ll s2=0;
ll a1;
for(int i=0;i<n;++i){
cin>>a1;
t1+=a1;
t2+=a1;
if(i%2==0){
if(t1<=0){
s1+=-t1+1;
t1=1;
}
if(t2>=0){
s2+=t2+1;
t2=-1;
}
}else{
if(t1>=0){
s1+=t1+1;
t1=-1;
}
if(t2<=0){
s2+=-t2+1;
t2=1;
}
}
}
cout<<min(s1,s2)<<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": []
} | CORRECT | cpp | #include<iostream>
#include<string>
#include<algorithm>
#include<vector>
using ll = long long;
using namespace std;
ll n;
vector<ll> a;
ll solve(int x);
int main(){
cin >> n;
for(ll i=0;i<n;i++){
int x;
cin >> x;
a.push_back(x);
}
cout << min(solve(0),solve(1)) << endl;
return 0;
}
ll solve(int x){
ll ans = 0; ll now = 0;
for(ll i=0;i<n;i++){
now += a[i];
if((i%2^x)&&(now>=0)){
ans += now + 1;
now = -1;
}
if(!(i%2^x)&&(now<=0)){
ans -= now - 1;
now = 1;
}
}
return ans;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | python3 | n=int(input())
a=list(map(int,input().split()))
b=a[:]
x=0#pmpm
y=0
sum1=0
sum2=0
for i in range(n):
sum1+=a[i]
sum2+=b[i]
if(i%2==0):
if(sum1<=0):
x+=1-sum1
a[i]+=1-sum1
sum1+=1-sum1
if(sum2>=0):
y+=sum2+1
b[i]-=sum2+1
sum2-=sum2+1
else:
if(sum1>=0):
x+=sum1+1
a[i]-=1+sum1
sum1-=1+sum1
if(sum2<=0):
y+=1-sum2
b[i]+=1-sum2
sum2+=1-sum2
print(min(x,y)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | python3 | N = int(input())
A = [int(x) for x in input().split()]
c = [0, 0]
for f in [0, 1]:
sumA = 0
for i, a in enumerate(A):
sumA += a
if i % 2 == f:
if sumA < 1:
d = 1 - sumA
c[f] += d
sumA += d
else:
if sumA > -1:
d = 1 + sumA
c[f] += d
sumA -= d
print(min(c)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
vector<int> a(N);
for (int i = 0; i < N; ++i) cin >> a[i];
ll cnt = 0;
ll sum = 0;
for (int i = 0; i < N; ++i) {
ll t = sum + a[i];
if (i % 2 == 0 && t <= 0) cnt += 1 - t, t = 1;
if (i % 2 == 1 && t >= 0) cnt += t + 1, t = -1;
sum = t;
}
ll ans = cnt;
cnt = sum = 0;
for (int i = 0; i < N; ++i) {
ll t = sum + a[i];
if (i % 2 == 1 && t <= 0) cnt += 1 - t, t = 1;
if (i % 2 == 0 && t >= 0) cnt += t + 1, t = -1;
sum = t;
}
ans = min(ans, cnt);
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | python3 | n = int(input())
A = list(map(int,input().split()))
def seq(a,t):
ans = 0
x = 0
for i in a:
x+=i
if t==True and x<1:
ans+=1-x
x=1
elif t==False and x>-1:
ans+=x+1
x=-1
t = not t
return ans
print(min(seq(A,True),seq(A,False))) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | java | import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.next());
long[] a1 = new long[n];
long[] a2 = new long[n];
for (int i = 0; i < n; i++) {
long temp = Long.parseLong(sc.next());
a1[i] = temp;
a2[i] = temp;
}
long ans1 = 0;
long ans2 = 0;
long temp = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0 && temp + a1[i] >= 0) {
ans1 += temp + a1[i] + 1;
a1[i] -= temp + a1[i] + 1;
}
if (i % 2 != 0 && temp + a1[i] <= 0) {
ans1 += Math.abs(temp + a1[i] - 1);
a1[i] += Math.abs(temp + a1[i] - 1);
}
temp += a1[i];
}
if (Arrays.stream(a1).sum() == 0) {
ans1++;
}
temp = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0 && temp + a2[i] <= 0) {
ans2 += Math.abs(temp + a2[i] - 1);
a2[i] += Math.abs(temp + a2[i] - 1);
}
if (i % 2 != 0 && temp + a2[i] >= 0) {
ans2 += temp + a2[i] + 1;
a2[i] -= temp + a2[i] + 1;
}
temp += a2[i];
}
if (Arrays.stream(a2).sum() == 0) {
ans2++;
}
System.out.println(Math.min(ans1, ans2));
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | python3 | n = input()
A = list(map(int, input().split()))
def Chk(a, pos):
cnt = 0
tmp = 0
for a in A:
tmp += a
if pos and tmp < 1:
cnt += 1 - tmp
tmp = 1
elif not pos and tmp > -1:
cnt += 1 + tmp
tmp = -1
pos = not pos
return cnt
print(min(Chk(A, True), Chk(A, False))) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
int n; cin>>n;
vector<ll> a(n);
for (int i=0; i<n; i++) cin>>a[i];
vector<ll> ret(2);
for (int c=0; c<2; ++c){
int sgn=c?1:-1;
for (ll s=0, t=0; t<n; t++, sgn*=-1) {
s+=a[t];
if(sgn*s<=0) ret[c]+=abs(s)+1, s=sgn;
}
}
cout<<min(ret[0],ret[1])<<"\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": []
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
long long c1 = 0, c2 = 0, s1 = 0, s2 = 0;
for (int i = 0; i < n; i++) {
s1 += a[i];
s2 += a[i];
if (i & 1) {
if (s1 >= 0) {c1 += s1 + 1; s1 = -1;}
if (s2 <= 0) {c2 += 1 - s2; s2 = 1;}
} else {
if (s1 <= 0) {c1 += 1 - s1; s1 = 1;}
if (s2 >= 0) {c2 += s2 + 1; s2 = -1;}
}
}
cout << min(c1, c2) << '\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": []
} | CORRECT | java |
import java.io.*;
import static java.lang.Math.*;
import static java.lang.Math.min;
import java.util.*;
import java.util.stream.*;
/**
* @author baito
*/
class P implements Comparable<P> {
int x, y;
P(int a, int b) {
x = a;
y = b;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof P)) return false;
P p = (P) o;
return x == p.x && y == p.y;
}
@Override
public int hashCode() {
return Objects.hash(x, y);
}
@Override
public int compareTo(P p) {
return x == p.x ? y - p.y : x - p.x; //xで昇順にソート
//return (x == p.x ? y - p.y : x - p.x) * -1; //xで降順にソート
//return y == p.y ? x - p.x : y - p.y;//yで昇順にソート
//return (y == p.y ? x - p.x : y - p.y)*-1;//yで降順にソート
}
}
@SuppressWarnings("unchecked")
public class Main {
static StringBuilder sb = new StringBuilder();
static int INF = 1234567890;
static int MINF = -1234567890;
static long LINF = 123456789123456789L;
static long MLINF = -123456789123456789L;
static long MOD = 1000000007;
static double EPS = 1e-10;
static int[] y4 = {0, 1, 0, -1};
static int[] x4 = {1, 0, -1, 0};
static int[] y8 = {0, 1, 0, -1, -1, 1, 1, -1};
static int[] x8 = {1, 0, -1, 0, 1, -1, 1, -1};
static ArrayList<Long> Fa;
static boolean[] isPrime;
static int[] primes;
static char[][] ban;
static long maxRes = MLINF;
static long minRes = LINF;
static boolean DEBUG = true;
static int N;
static long[] B;
public static void solve() throws Exception {
//longを忘れるなオーバーフローするぞ
N = ni();
B = nla(N);
for (int _ = 0; _ < 2; _++) {
long[] A = B.clone();
boolean plus = _ == 0 ? true : false;
long cou = 0;
if (plus) {
if (A[0] == 0) {
cou++;
A[0]++;
} else if (A[0] < 0) {
cou += (-A[0]) + 1;
A[0] += (-A[0]) + 1;
}
} else {
if (A[0] == 0) {
cou++;
A[0]--;
} else if (A[0] > 0) {
cou += (A[0]) + 1;
A[0] -= (A[0]) + 1;
}
}
plus ^= true;
long sum = A[0];
for (int i = 1; i < N; i++) {
if (plus) {
long now = sum + A[i];
if (now < 0) {
cou += (-now) + 1;
A[i] += (-now) + 1;
} else if (now == 0) {
cou++;
A[i]++;
}
sum += A[i];
plus = false;
} else {
long now = sum + A[i];
if (now > 0) {
cou += (now) + 1;
A[i] -= (now) + 1;
} else if (now == 0) {
cou++;
A[i]--;
}
sum += A[i];
plus = true;
}
}
chMin(cou);
}
System.out.println(minRes);
}
public static boolean calc(long va) {
//貪欲にギリギリセーフを選んでいく。
int v = (int) va;
return true;
}
//条件を満たす最大値、あるいは最小値を求める
static int mgr(long ok, long ng, long w) {
//int ok = 0; //解が存在する
//int ng = N; //解が存在しない
while (Math.abs(ok - ng) > 1) {
long mid;
if (ok < 0 && ng > 0 || ok > 0 && ng < 0) mid = (ok + ng) / 2;
else mid = ok + (ng - ok) / 2;
if (calc(mid)) {
ok = mid;
} else {
ng = mid;
}
}
if (calc(ok)) return (int) ok;
else return -1;
}
boolean equal(double a, double b) {
return a == 0 ? abs(b) < EPS : abs((a - b) / a) < EPS;
}
public static void matPrint(long[][] a) {
for (int hi = 0; hi < a.length; hi++) {
for (int wi = 0; wi < a[0].length; wi++) {
System.out.print(a[hi][wi] + " ");
}
System.out.println("");
}
}
//rにlを掛ける l * r
public static long[][] matMul(long[][] l, long[][] r) throws IOException {
int lh = l.length;
int lw = l[0].length;
int rh = r.length;
int rw = r[0].length;
//lwとrhが,同じである必要がある
if (lw != rh) throw new IOException();
long[][] res = new long[lh][rw];
for (int i = 0; i < lh; i++) {
for (int j = 0; j < rw; j++) {
for (int k = 0; k < lw; k++) {
res[i][j] = modSum(res[i][j], modMul(l[i][k], r[k][j]));
}
}
}
return res;
}
public static long[][] matPow(long[][] a, int n) throws IOException {
int h = a.length;
int w = a[0].length;
if (h != w) throw new IOException();
long[][] res = new long[h][h];
for (int i = 0; i < h; i++) {
res[i][i] = 1;
}
long[][] pow = a.clone();
while (n > 0) {
if (bitGet(n, 0)) res = matMul(pow, res);
pow = matMul(pow, pow);
n >>= 1;
}
return res;
}
public static void chMax(long v) {
maxRes = Math.max(maxRes, v);
}
public static void chMin(long v) {
minRes = Math.min(minRes, v);
}
//2点間の行き先を配列に持たせる
static int[][] packE(int n, int[] from, int[] to) {
int[][] g = new int[n][];
int[] p = new int[n];
for (int f : from)
p[f]++;
for (int t : to)
p[t]++;
for (int i = 0; i < n; i++)
g[i] = new int[p[i]];
for (int i = 0; i < from.length; i++) {
g[from[i]][--p[from[i]]] = to[i];
g[to[i]][--p[to[i]]] = from[i];
}
return g;
}
public static boolean bitGet(BitSet bit, int keta) {
return bit.nextSetBit(keta) == keta;
}
public static boolean bitGet(long bit, int keta) {
return ((bit >> keta) & 1) == 1;
}
public static int restoreHashA(long key) {
return (int) (key >> 32);
}
public static int restoreHashB(long key) {
return (int) (key & -1);
}
//正の数のみ
public static long getHashKey(int a, int b) {
return (long) a << 32 | b;
}
public static long sqrt(long v) {
long res = (long) Math.sqrt(v);
while (res * res > v) res--;
return res;
}
public static int u0(int a) {
if (a < 0) return 0;
return a;
}
public static long u0(long a) {
if (a < 0) return 0;
return a;
}
public static int[] toIntArray(int a) {
int[] res = new int[keta(a)];
for (int i = res.length - 1; i >= 0; i--) {
res[i] = a % 10;
a /= 10;
}
return res;
}
public static Integer[] toIntegerArray(int[] ar) {
Integer[] res = new Integer[ar.length];
for (int i = 0; i < ar.length; i++) {
res[i] = ar[i];
}
return res;
}
public static long bitGetCombSizeK(int k) {
return (1 << k) - 1;
}
//k個の次の組み合わせをビットで返す 大きさに上限はない 110110 -> 111001
public static long bitNextComb(long comb) {
long x = comb & -comb; //最下位の1
long y = comb + x; //連続した下の1を繰り上がらせる
return ((comb & ~y) / x >> 1) | y;
}
public static int keta(long num) {
int res = 0;
while (num > 0) {
num /= 10;
res++;
}
return res;
}
public static boolean isOutofIndex(int x, int y, int w, int h) {
if (x < 0 || y < 0) return true;
if (w <= x || h <= y) return true;
return false;
}
public static boolean isOutofIndex(int x, int y, char[][] ban) {
if (x < 0 || y < 0) return true;
if (ban[0].length <= x || ban.length <= y) return true;
return false;
}
public static int arrayCount(int[] a, int v) {
int res = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] == v) res++;
}
return res;
}
public static int arrayCount(long[] a, int v) {
int res = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] == v) res++;
}
return res;
}
public static int arrayCount(int[][] a, int v) {
int res = 0;
for (int hi = 0; hi < a.length; hi++) {
for (int wi = 0; wi < a[0].length; wi++) {
if (a[hi][wi] == v) res++;
}
}
return res;
}
public static int arrayCount(long[][] a, int v) {
int res = 0;
for (int hi = 0; hi < a.length; hi++) {
for (int wi = 0; wi < a[0].length; wi++) {
if (a[hi][wi] == v) res++;
}
}
return res;
}
public static int arrayCount(char[][] a, char v) {
int res = 0;
for (int hi = 0; hi < a.length; hi++) {
for (int wi = 0; wi < a[0].length; wi++) {
if (a[hi][wi] == v) res++;
}
}
return res;
}
public static void setPrimes() {
int n = 100001;
isPrime = new boolean[n];
List<Integer> prs = new ArrayList<>();
Arrays.fill(isPrime, true);
isPrime[0] = isPrime[1] = false;
for (int i = 2; i * i <= n; i++) {
if (!isPrime[i]) continue;
prs.add(i);
for (int j = i * 2; j < n; j += i) {
isPrime[j] = false;
}
}
primes = new int[prs.size()];
for (int i = 0; i < prs.size(); i++)
primes[i] = prs.get(i);
}
public static void revSort(int[] a) {
Arrays.sort(a);
reverse(a);
}
public static void revSort(long[] a) {
Arrays.sort(a);
reverse(a);
}
public static int[][] copy(int[][] ar) {
int[][] nr = new int[ar.length][ar[0].length];
for (int i = 0; i < ar.length; i++)
for (int j = 0; j < ar[0].length; j++)
nr[i][j] = ar[i][j];
return nr;
}
/**
* <h1>指定した値以上の先頭のインデクスを返す</h1>
* <p>配列要素が0のときは、0が返る。</p>
*
* @return<b>int</b> : 探索した値以上で、先頭になるインデクス
* 値が無ければ、挿入できる最小のインデックス
*/
public static <T extends Number> int lowerBound(final List<T> lis, final T value) {
int low = 0;
int high = lis.size();
int mid;
while (low < high) {
mid = ((high - low) >>> 1) + low; //(low + high) / 2 (オーバーフロー対策)
if (lis.get(mid).doubleValue() < value.doubleValue()) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
/**
* <h1>指定した値より大きい先頭のインデクスを返す</h1>
* <p>配列要素が0のときは、0が返る。</p>
*
* @return<b>int</b> : 探索した値より上で、先頭になるインデクス
* 値が無ければ、挿入できる最小のインデックス
*/
public static <T extends Number> int upperBound(final List<T> lis, final T value) {
int low = 0;
int high = lis.size();
int mid;
while (low < high) {
mid = ((high - low) >>> 1) + low; //(low + high) / 2 (オーバーフロー対策)
if (lis.get(mid).doubleValue() < value.doubleValue()) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
/**
* <h1>指定した値以上の先頭のインデクスを返す</h1>
* <p>配列要素が0のときは、0が返る。</p>
*
* @return<b>int</b> : 探索した値以上で、先頭になるインデクス
* 値が無ければ、挿入できる最小のインデックス
*/
public static int lowerBound(final int[] arr, final int value) {
int low = 0;
int high = arr.length;
int mid;
while (low < high) {
mid = ((high - low) >>> 1) + low; //(low + high) / 2 (オーバーフロー対策)
if (arr[mid] < value) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
/**
* <h1>指定した値より大きい先頭のインデクスを返す</h1>
* <p>配列要素が0のときは、0が返る。</p>
*
* @return<b>int</b> : 探索した値より上で、先頭になるインデクス
* 値が無ければ、挿入できる最小のインデックス
*/
public static int upperBound(final int[] arr, final int value) {
int low = 0;
int high = arr.length;
int mid;
while (low < high) {
mid = ((high - low) >>> 1) + low; //(low + high) / 2 (オーバーフロー対策)
if (arr[mid] <= value) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
/**
* <h1>指定した値以上の先頭のインデクスを返す</h1>
* <p>配列要素が0のときは、0が返る。</p>
*
* @return<b>int</b> : 探索した値以上で、先頭になるインデクス
* 値がなければ挿入できる最小のインデックス
*/
public static long lowerBound(final long[] arr, final long value) {
int low = 0;
int high = arr.length;
int mid;
while (low < high) {
mid = ((high - low) >>> 1) + low; //(low + high) / 2 (オーバーフロー対策)
if (arr[mid] < value) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
/**
* <h1>指定した値より大きい先頭のインデクスを返す</h1>
* <p>配列要素が0のときは、0が返る。</p>
*
* @return<b>int</b> : 探索した値より上で、先頭になるインデクス
* 値がなければ挿入できる最小のインデックス
*/
public static long upperBound(final long[] arr, final long value) {
int low = 0;
int high = arr.length;
int mid;
while (low < high) {
mid = ((high - low) >>> 1) + low; //(low + high) / 2 (オーバーフロー対策)
if (arr[mid] <= value) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
//次の順列に書き換える、最大値ならfalseを返す
public static boolean nextPermutation(int A[]) {
int len = A.length;
int pos = len - 2;
for (; pos >= 0; pos--) {
if (A[pos] < A[pos + 1]) break;
}
if (pos == -1) return false;
//posより大きい最小の数を二分探索
int ok = pos + 1;
int ng = len;
while (Math.abs(ng - ok) > 1) {
int mid = (ok + ng) / 2;
if (A[mid] > A[pos]) ok = mid;
else ng = mid;
}
swap(A, pos, ok);
reverse(A, pos + 1, len - 1);
return true;
}
//次の順列に書き換える、最小値ならfalseを返す
public static boolean prevPermutation(int A[]) {
int len = A.length;
int pos = len - 2;
for (; pos >= 0; pos--) {
if (A[pos] > A[pos + 1]) break;
}
if (pos == -1) return false;
//posより小さい最大の数を二分探索
int ok = pos + 1;
int ng = len;
while (Math.abs(ng - ok) > 1) {
int mid = (ok + ng) / 2;
if (A[mid] < A[pos]) ok = mid;
else ng = mid;
}
swap(A, pos, ok);
reverse(A, pos + 1, len - 1);
return true;
}
static long ncr2(int a, int b) {
if (b == 0) return 1;
else if (a < b) return 0;
long res = 1;
for (int i = 0; i < b; i++) {
res *= a - i;
res /= i + 1;
}
return res;
}
static long ncrdp(int n, int r) {
if (n < r) return 0;
long[][] dp = new long[n + 1][r + 1];
for (int ni = 0; ni < n + 1; ni++) {
dp[ni][0] = dp[ni][ni] = 1;
for (int ri = 1; ri < ni; ri++) {
dp[ni][ri] = dp[ni - 1][ri - 1] + dp[ni - 1][ri];
}
}
return dp[n][r];
}
public static int mod(int a, int m) {
return a >= 0 ? a % m : (int) (a + ceil(-a * 1.0 / m) * m) % m;
}
static long modNcr(int n, int r) {
if (n < 0 || r < 0 || n < r) return 0;
if (Fa == null || Fa.size() <= n) factorial(n);
long result = Fa.get(n);
result = modMul(result, modInv(Fa.get(n - r)));
result = modMul(result, modInv(Fa.get(r)));
return result;
}
public static long modSum(long... lar) {
long res = 0;
for (long l : lar)
res = (res + l % MOD) % MOD;
if (res < 0) res += MOD;
res %= MOD;
return res;
}
public static long modDiff(long a, long b) {
long res = a % MOD - b % MOD;
if (res < 0) res += MOD;
res %= MOD;
return res;
}
public static long modMul(long... lar) {
long res = 1;
for (long l : lar)
res = (res * l % MOD) % MOD;
if (res < 0) res += MOD;
res %= MOD;
return res;
}
public static long modDiv(long a, long b) {
long x = a % MOD;
long y = b % MOD;
long res = (x * modInv(y)) % MOD;
return res;
}
static long modInv(long n) {
return modPow(n, MOD - 2);
}
static void factorial(int n) {
if (Fa == null) {
Fa = new ArrayList<>();
Fa.add(1L);
Fa.add(1L);
}
for (int i = Fa.size(); i <= n; i++) {
Fa.add((Fa.get(i - 1) * i) % MOD);
}
}
static long modPow(long x, long n) {
long res = 1L;
while (n > 0) {
if ((n & 1) == 1) {
res = res * x % MOD;
}
x = x * x % MOD;
n >>= 1;
}
return res;
}
//↑nCrをmod計算するために必要
static long lcm(long n, long r) {
return n / gcd(n, r) * r;
}
static int gcd(int n, int r) {
return r == 0 ? n : gcd(r, n % r);
}
static long gcd(long n, long r) {
return r == 0 ? n : gcd(r, n % r);
}
static <T> void swap(T[] x, int i, int j) {
T t = x[i];
x[i] = x[j];
x[j] = t;
}
static void swap(int[] x, int i, int j) {
int t = x[i];
x[i] = x[j];
x[j] = t;
}
public static void reverse(int[] x) {
int l = 0;
int r = x.length - 1;
while (l < r) {
int temp = x[l];
x[l] = x[r];
x[r] = temp;
l++;
r--;
}
}
public static void reverse(long[] x) {
int l = 0;
int r = x.length - 1;
while (l < r) {
long temp = x[l];
x[l] = x[r];
x[r] = temp;
l++;
r--;
}
}
public static void reverse(char[] x) {
int l = 0;
int r = x.length - 1;
while (l < r) {
char temp = x[l];
x[l] = x[r];
x[r] = temp;
l++;
r--;
}
}
public static void reverse(int[] x, int s, int e) {
int l = s;
int r = e;
while (l < r) {
int temp = x[l];
x[l] = x[r];
x[r] = temp;
l++;
r--;
}
}
static int length(int a) {
int cou = 0;
while (a != 0) {
a /= 10;
cou++;
}
return cou;
}
static int length(long a) {
int cou = 0;
while (a != 0) {
a /= 10;
cou++;
}
return cou;
}
static int cou(boolean[] a) {
int res = 0;
for (boolean b : a) {
if (b) res++;
}
return res;
}
static int cou(String s, char c) {
int res = 0;
for (char ci : s.toCharArray()) {
if (ci == c) res++;
}
return res;
}
static int countC2(char[][] a, char c) {
int co = 0;
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[0].length; j++)
if (a[i][j] == c) co++;
return co;
}
static int countI(int[] a, int key) {
int co = 0;
for (int i = 0; i < a.length; i++)
if (a[i] == key) co++;
return co;
}
static int countI(int[][] a, int key) {
int co = 0;
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[0].length; j++)
if (a[i][j] == key) co++;
return co;
}
static void fill(int[][] a, int v) {
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[0].length; j++)
a[i][j] = v;
}
static void fill(char[][] a, char c) {
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[0].length; j++)
a[i][j] = c;
}
static void fill(long[][] a, long v) {
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[0].length; j++)
a[i][j] = v;
}
static void fill(int[][][] a, int v) {
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[0].length; j++)
for (int k = 0; k < a[0][0].length; k++)
a[i][j][k] = v;
}
static int max(int... a) {
int res = Integer.MIN_VALUE;
for (int i : a) {
res = Math.max(res, i);
}
return res;
}
static long max(long... a) {
long res = Integer.MIN_VALUE;
for (long i : a) {
res = Math.max(res, i);
}
return res;
}
static long min(long... a) {
long res = Long.MAX_VALUE;
for (long i : a) {
res = Math.min(res, i);
}
return res;
}
static int max(int[][] ar) {
int res = Integer.MIN_VALUE;
for (int i[] : ar)
res = Math.max(res, max(i));
return res;
}
static long max(long[][] ar) {
long res = Integer.MIN_VALUE;
for (long i[] : ar)
res = Math.max(res, max(i));
return res;
}
static int min(int... a) {
int res = Integer.MAX_VALUE;
for (int i : a) {
res = Math.min(res, i);
}
return res;
}
static int min(int[][] ar) {
int res = Integer.MAX_VALUE;
for (int i[] : ar)
res = Math.min(res, min(i));
return res;
}
static int sum(int[] a) {
int cou = 0;
for (int i : a)
cou += i;
return cou;
}
static long sum(long[] a) {
long cou = 0;
for (long i : a)
cou += i;
return cou;
}
//FastScanner
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer tokenizer = null;
public static String next() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
/*public String nextChar(){
return (char)next()[0];
}*/
public static String nextLine() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
return reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken("\n");
}
public static long nl() {
return Long.parseLong(next());
}
public static String n() {
return next();
}
public static int ni() {
return Integer.parseInt(next());
}
public static double nd() {
return Double.parseDouble(next());
}
public static int[] nia(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = ni();
}
return a;
}
//1-index
public static int[] niao(int n) {
int[] a = new int[n + 1];
for (int i = 1; i < n + 1; i++) {
a[i] = ni();
}
return a;
}
public static int[] niad(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = ni() - 1;
}
return a;
}
public static P[] npa(int n) {
P[] p = new P[n];
for (int i = 0; i < n; i++) {
p[i] = new P(ni(), ni());
}
return p;
}
public static P[] npad(int n) {
P[] p = new P[n];
for (int i = 0; i < n; i++) {
p[i] = new P(ni() - 1, ni() - 1);
}
return p;
}
public static int[][] nit(int h, int w) {
int[][] a = new int[h][w];
for (int hi = 0; hi < h; hi++) {
for (int wi = 0; wi < w; wi++) {
a[hi][wi] = ni();
}
}
return a;
}
public static int[][] nitd(int h, int w) {
int[][] a = new int[h][w];
for (int hi = 0; hi < h; hi++) {
for (int wi = 0; wi < w; wi++) {
a[hi][wi] = ni() - 1;
}
}
return a;
}
static int[][] S_ARRAY;
static long[][] S_LARRAY;
static int S_INDEX;
static int S_LINDEX;
//複数の配列を受け取る
public static int[] niah(int n, int w) {
if (S_ARRAY == null) {
S_ARRAY = new int[w][n];
for (int i = 0; i < n; i++) {
for (int ty = 0; ty < w; ty++) {
S_ARRAY[ty][i] = ni();
}
}
}
return S_ARRAY[S_INDEX++];
}
public static long[] nlah(int n, int w) {
if (S_LARRAY == null) {
S_LARRAY = new long[w][n];
for (int i = 0; i < n; i++) {
for (int ty = 0; ty < w; ty++) {
S_LARRAY[ty][i] = ni();
}
}
}
return S_LARRAY[S_LINDEX++];
}
public static char[] nca() {
char[] a = next().toCharArray();
return a;
}
public static char[][] nct(int h, int w) {
char[][] a = new char[h][w];
for (int i = 0; i < h; i++) {
a[i] = next().toCharArray();
}
return a;
}
//スペースが入っている場合
public static char[][] ncts(int h, int w) {
char[][] a = new char[h][w];
for (int i = 0; i < h; i++) {
a[i] = nextLine().replace(" ", "").toCharArray();
}
return a;
}
public static char[][] nctp(int h, int w, char c) {
char[][] a = new char[h + 2][w + 2];
//char c = '*';
int i;
for (i = 0; i < w + 2; i++)
a[0][i] = c;
for (i = 1; i < h + 1; i++) {
a[i] = (c + next() + c).toCharArray();
}
for (i = 0; i < w + 2; i++)
a[h + 1][i] = c;
return a;
}
//スペースが入ってる時用
public static char[][] nctsp(int h, int w, char c) {
char[][] a = new char[h + 2][w + 2];
//char c = '*';
int i;
for (i = 0; i < w + 2; i++)
a[0][i] = c;
for (i = 1; i < h + 1; i++) {
a[i] = (c + nextLine().replace(" ", "") + c).toCharArray();
}
for (i = 0; i < w + 2; i++)
a[h + 1][i] = c;
return a;
}
public static long[] nla(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = nl();
}
return a;
}
public static long[][] nlt(int h, int w) {
long[][] a = new long[h][w];
for (int hi = 0; hi < h; hi++) {
for (int wi = 0; wi < w; wi++) {
a[hi][wi] = nl();
}
}
return a;
}
public static void main(String[] args) throws Exception {
long startTime = System.currentTimeMillis();
solve();
System.out.flush();
long endTime = System.currentTimeMillis();
if (DEBUG) System.err.println(endTime - startTime);
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | python2 | #coding:utf-8
def count(seq, is_positive):
operation = 0
sum = 0
for i in range(n):
sum += seq[i]
if sum == 0:
operation += 1
if is_positive:
sum -= 1
else:
sum += 1
elif sum > 0 and is_positive:
operation += abs(sum) + 1
sum = -1
elif sum < 0 and not is_positive:
operation += abs(sum) + 1
sum = 1
if sum > 0:
is_positive = True
else:
is_positive = False
return operation
if __name__ == "__main__":
n = int(raw_input())
seq = map(int, raw_input().split(" "))
print min(count(seq, False), count(seq, True))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | python3 | n = int(input())
A = list(map(int, input().split()))
ans = {True:0, False:0}
for s in [True, False]:
sign = s
total = 0
for i in range(len(A)):
if total + A[i] == 0 or sign == (total + A[i] > 0):
ans[s] += abs(total + A[i]) + 1
total = -1 if sign else 1
else:
total += A[i]
sign = total > 0
print(min(ans.values()))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | java | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = scanner.nextInt();
long c1 = 0;
long sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 0 && sum <= 0) {
long d = 1 - sum;
c1 += d;
sum += d;
} else if (i % 2 == 1 && sum >= 0){
long d = sum + 1;
c1 += d;
sum -= d;
}
}
long c2 = 0;
sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 0 && sum >= 0) {
long d = sum + 1;
c2 += d;
sum -= d;
} else if (i % 2 == 1 && sum <= 0){
long d = 1 - sum;
c2 += d;
sum += d;
}
}
System.out.println(Math.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": []
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long n,ansa=0,ansb=0,x=0,y=0;
cin >> n;
vector<long> a(n),wa(n);
for(long i=0;i<n;i++) cin >> a[i];
wa[0]=a[0];
for(long i=1;i<n;i++) wa[i]=wa[i-1]+a[i];
for(long i=0;i<n;i++) {
if(i%2==0) {
if(wa[i]+x<=0) {
ansa+=1-wa[i]-x;
x+=1-wa[i]-x;
}
if(wa[i]+y>=0) {
ansb+=1+wa[i]+y;
y+=-1-wa[i]-y;
}
} else {
if(wa[i]+x>=0) {
ansa+=1+wa[i]+x;
x+=-1-wa[i]-x;
}
if(wa[i]+y<=0) {
ansb+=1-wa[i]-y;
y+=1-wa[i]-y;
}
}
}
cout << min(ansa,ansb) << 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": []
} | CORRECT | cpp | #include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
vector<int> v(n);
for(auto &i:v) cin>>i;
long long ansq=1e17;
for(int x=0;x<2;x++)
{
int h=x;
long long int sum=0,ans=0;
for(int i=0;i<n;i++)
{
sum+=v[i];
if(h)
{
if(sum<=0)
{ ans+= abs(1-sum); sum=1;}
}
else
{
if(sum>=0)
{
ans+=abs(sum+1);
sum=-1;
}
}
h = h^1;
}
ansq=min(ansq,ans);
}
cout<<ansq<<"\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": []
} | CORRECT | python3 | n=int(input())
a=list(map(int,input().split()))
ans=999999999999999
temp=True
for i in range(2):
count=0
temp=0
if i==0:
t=True
else:
t=False
for j in a:
temp=temp+j
if t==True and temp<1:
count=count+1-temp
temp=1
elif t==False and temp>-1:
count=count+1+temp
temp=-1
if t==True:
t=False
else:
t=True
ans=min(ans,count)
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": []
} | CORRECT | python3 | n, *a = map(int, open(0).read().split())
results = []
for sign in [1, -1]:
ans = 0
acc = 0
for v in a:
acc += v
if acc * sign <= 0:
ans += abs(acc - sign)
acc = sign
sign *= -1
results.append(ans)
print(min(results)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | java | import java.util.*;
public class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long sum1 = 0;
long count1 = 0;
long sum2 = 0;
long count2 = 0;
for (int i = 0; i < n; i++) {
int a = sc.nextInt();
sum1 += a;
sum2 += a;
if (i % 2 == 0) {
if (sum1 <= 0) {
count1 += 1 - sum1;
sum1 = 1;
}
if (sum2 >= 0) {
count2 += sum2 + 1;
sum2 = -1;
}
} else {
if (sum2 <= 0) {
count2 += 1 - sum2;
sum2 = 1;
}
if (sum1 >= 0) {
count1 += sum1 + 1;
sum1 = -1;
}
}
}
System.out.println(Math.min(count1, count2));
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | python3 | n = int(input())
a = list(map(int,input().split(" ")))
o = []
for b in [-1,1]:
c = 0
s = [0]*n
for i in range(0,n):
s[i] = a[i] + (i and s[i-1])
if s[i] == 0 or s[i] < 0 < b or b < 0 < s[i]:
c += abs(s[i] - b)
s[i] = b
b *= -1
o.append(c)
print(min(o)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
ll s1, s2, c1, c2;
int main() {
ll n;
cin>>n;
rep(i,n){
int a;
cin>>a;
s1 += a;
s2 += a;
if (i % 2) {
if (s1 <= 0) c1 += 1 - s1,s1 = 1;
if (s2 >= 0) c2 += 1 + s2,s2 = -1;
}
else {
if (s1 >= 0) c1 += 1 + s1,s1 = -1;
if (s2 <= 0) c2 += 1 - s2,s2 = 1;
}
}
cout << min(c1, c2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | java | import java.util.*;
public class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long valueA = 0;
long countA = 0;
long valueB = 0;
long countB = 0;
for (int i = 0; i < n; i++) {
long a = sc.nextLong();
valueA += a;
valueB += a;
if (i % 2 == 0) {
if (valueA >= 0) {
countA += valueA + 1;
valueA = -1;
}
if (valueB <= 0) {
countB += -valueB + 1;
valueB = 1;
}
} else {
if (valueA <= 0) {
countA += -valueA + 1;
valueA = 1;
}
if (valueB >= 0) {
countB += valueB + 1;
valueB = -1;
}
}
}
if (countA < countB) {
System.out.println(countA);
} else {
System.out.println(countB);
}
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | python3 | n=int(input())
a=list(map(int,input().split()))
#+-+-
ans=0
s=0
for i in range(n):
f=i%2
s+=a[i]
if f==0:
if s<=0:
ans+=-s+1
s=1
else:
if s>=0:
ans+=1+s
s=-1
#-+-+
anss=0
s=0
for i in range(n):
f=i%2
s+=a[i]
if f==1:
if s<=0:
anss+=-s+1
s=1
else:
if s>=0:
anss+=1+s
s=-1
print(min(ans,anss))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
vector<long long> a(n);
for (auto&& ai : a) cin >> ai;
long long r1 = 0, r2 = 0;
long long sum = 0;
for (int i = 0; i < n; i++)
{
sum += a[i];
if (i%2 == 0 && sum <= 0)
{
r1 += 1 - sum;
sum = 1;
}
else if (i%2 == 1 && sum >= 0)
{
r1 += sum + 1;
sum = -1;
}
}
sum = 0;
for (int i = 0; i < n; i++)
{
sum += a[i];
if (i%2 == 0 && sum >= 0)
{
r2 += sum + 1;
sum = -1;
}
else if (i%2 == 1 && sum <= 0)
{
r2 += 1 - sum;
sum = 1;
}
}
cout << min(r1, r2) << 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": []
} | CORRECT | python3 | n = int(input())
lst = [int(x) for x in input().split()]
total = [0, 0]
ret = [0, 0]
for i in range(2):
for j in range(n):
total[i] += lst[j]
if (i + j) % 2 == 0:
if total[i] >= 0:
ret[i] += abs(total[i] + 1)
total[i] = -1
else:
if total[i] <= 0:
ret[i] += abs(total[i] - 1)
total[i] = 1
print(min(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": []
} | CORRECT | java | import java.util.*;
import java.io.*;
public class Main{
static final Reader sc = new Reader();
static final PrintWriter out = new PrintWriter(System.out,false);
public static void main(String[] args) throws Exception {
int n = sc.nextInt();
long[] a = new long[n];
for(int i=0;i<n;i++){
a[i] = sc.nextLong();
}
long counter = 0;
long total = a[0];
if(a[0]==0){
total++;
counter++;
}
for(int i=1;i<n;i++){
if(total>0 && total+a[i]<0){
total += a[i];
}
else if(total<0 && total+a[i]>0){
total += a[i];
}
else{
long x = 0;
if(total>0){
x = -1 - total - a[i];
total = -1;
}
else{
x = 1 - total - a[i];
total = 1;
}
counter += (long)Math.abs(x);
}
//out.println(total+" "+counter);
}
long total1 = 0;
long counter1 = 0;
if(a[0]<0){
total1 = 1;
counter1 = 1 - a[0];
}
else if(a[0]>0){
total1 = -1;
counter1 = 1 + a[0];
}
else{
total1 = -1;
counter1 = 1;
}
for(int i=1;i<n;i++){
if(total1>0 && total1+a[i]<0){
total1 += a[i];
}
else if(total1<0 && total1+a[i]>0){
total1 += a[i];
}
else{
long x = 0;
if(total1>0){
x = -1 - total1 - a[i];
total1 = -1;
}
else{
x = 1 - total1 - a[i];
total1 = 1;
}
counter1 += (long)Math.abs(x);
}
//out.println(total+" "+counter);
}
if(counter>counter1){
out.println(counter1);
}
else{
out.println(counter);
}
out.flush();
sc.close();
out.close();
}
static void trace(Object... o) { System.out.println(Arrays.deepToString(o));}
}
class Reader {
private final InputStream in;
private final byte[] buf = new byte[1024];
private int ptr = 0;
private int buflen = 0;
public Reader() { this(System.in);}
public Reader(InputStream source) { this.in = source;}
private boolean hasNextByte() {
if (ptr < buflen) return true;
ptr = 0;
try{
buflen = in.read(buf);
}catch (IOException e) {e.printStackTrace();}
if (buflen <= 0) return false;
return true;
}
private int readByte() { if (hasNextByte()) return buf[ptr++]; else return -1;}
private boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
private void skip() { while(hasNextByte() && !isPrintableChar(buf[ptr])) ptr++;}
public boolean hasNext() {skip(); return hasNextByte();}
public String next() {
if (!hasNext()) throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while (isPrintableChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public long nextLong() {
if (!hasNext()) throw new NoSuchElementException();
boolean minus = false;
long num = readByte();
if(num == '-'){
num = 0;
minus = true;
}else if (num < '0' || '9' < num){
throw new NumberFormatException();
}else{
num -= '0';
}
while(true){
int b = readByte();
if('0' <= b && b <= '9')
num = num * 10 + (b - '0');
else if(b == -1 || !isPrintableChar(b))
return minus ? -num : num;
else
throw new NoSuchElementException();
}
}
public int nextInt() {
long num = nextLong();
if (num < Integer.MIN_VALUE || Integer.MAX_VALUE < num)
throw new NumberFormatException();
return (int)num;
}
public double nextDouble() {
return Double.parseDouble(next());
}
public char nextChar() {
if (!hasNext()) throw new NoSuchElementException();
return (char)readByte();
}
public String nextLine() {
while (hasNextByte() && (buf[ptr] == '\n' || buf[ptr] == '\r')) ptr++;
if (!hasNextByte()) throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while (b != '\n' && b != '\r') {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public int[] nextIntArray(int n) {
int[] res = new int[n];
for (int i=0; i<n; i++) res[i] = nextInt();
return res;
}
public char[] nextCharArray(int n) {
char[] res = new char[n];
for (int i=0; i<n; i++) res[i] = nextChar();
return res;
}
public void close() {try{ in.close();}catch(IOException e){ e.printStackTrace();}};
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | // C - Sequence
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int N;
vector<ll> A(100000);
ll counter(int sign){//sign: (+)start -> 1, (-)start -> -1
ll c = 0;
for(ll s=0, i=0; i<N; ++i, sign*=-1){
s += A[i];
if(sign*s<=0){ c += abs(s) + 1; s = sign; }
}
return c;
}
int main(){
cin>>N;
for(int i=0; i<N; ++i) cin>>A[i];
ll a = counter(1), b = counter(-1);
cout<< (a<b?a:b) <<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": []
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
ll n;
cin>>n;
ll a[n];
for(int i=0;i<n;i++)
cin>>a[i];
ll c1=0,c2=0,sum=0;
for(int i=0;i<n;i++){
if(i%2==0){
sum=sum+a[i];
if(sum<0)
continue;
c1+=abs(-1-sum);
sum=-1;
}
else{
sum=sum+a[i];
if(sum>0)
continue;
c1+=abs(1-sum);
sum=1;
}
}
sum=0;
for(int i=0;i<n;i++){
if(i%2==1){
sum=sum+a[i];
if(sum<0)
continue;
c2+=abs(-1-sum);
sum=-1;
}
else{
sum=sum+a[i];
if(sum>0)
continue;
c2+=abs(1-sum);
sum=1;
}
}
cout<<min(c1,c2)<<endl;
return 0;
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
int N;
cin>>N;
vector<long long> a(N);
for(int i=0;i<N;i++) cin>>a[i];
int initial_sign[2]={-1,1};
vector<long long> res(2);
for(int i=0;i<2;i++){
long long cumsum=0;
long long current_sign=initial_sign[i];
for(int j=0;j<N;j++){
cumsum+=a[j];
if(cumsum*current_sign<=0){
res[i]+=abs(cumsum-current_sign);
cumsum=current_sign;
}
current_sign=-1*current_sign;
}
}
cout << min(res[0],res[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": []
} | CORRECT | cpp | #include<iostream>
#include<vector>
using namespace std;
int main(){
int n;
cin >> n;
vector<long long> a(n+1,0);
for(int i=1;i<=n;i++){
cin >> a[i];
a[i]+=a[i-1];
}
long long ans=1LL<<50;
for(int k=-1;k<=1;k+=2){
int sign=k;
long long plus=0,minus=0;
for(int i=1;i<=n;i++){
if(a[i]+plus-minus>0){
if(sign==-1) minus+=a[i]+plus-minus+1;
}else if(a[i]+plus-minus<0){
if(sign==1) plus+=-(a[i]+plus-minus)+1;
}else{
if(sign==-1) minus+=1;
else if(sign==1) plus+=1;
}
sign*=-1;
}
if(ans>plus+minus) ans=plus+minus;
}
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": []
} | CORRECT | java | import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
long count1 = 0;
long sum1 = 0;
// 最初の和が正
for (int i = 0; i < n; i++) {
sum1 += a[i];
if (i % 2 == 0 && sum1 <= 0) {
count1 += (-sum1) + 1;
sum1 = 1;
// while (sum1 <= 0) {
// sum1++;
// count1++;
// }
} else if (i % 2 == 1 && sum1 >= 0) {
count1 += sum1 + 1;
sum1 = -1;
// while (sum1 >= 0) {
// sum1--;
// count1++;
// }
}
}
long count2 = 0;
long sum2 = 0;
// 最初の和が負
for (int i = 0; i < n; i++) {
sum2 += a[i];
if (i % 2 == 0 && sum2 >= 0) {
count2 += sum2 + 1;
sum2 = -1;
// while (sum2 >= 0) {
// sum2--;
// count2++;
// }
} else if (i % 2 == 1 && sum2 <= 0) {
count2 += (-sum2) + 1;
sum2 = 1;
// while (sum2 <= 0) {
// sum2++;
// count2++;
// }
}
}
long ans = Long.min(count1, count2);
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": []
} | CORRECT | java | /* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Main
{
static long result1 = Long.MAX_VALUE;
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.next());
long[] input = new long[n];
long[] result = new long[n];
for(int i = 0; i < n; i++) {
input[i] = sc.nextLong();
}
counting(input, result, 0, 0, true);
counting(input, result, 0, 0, false);
System.out.println(result1);
}
public static void counting(long[] input, long[] result, long count, int index, boolean sign) {
if(index > 0) {
result[index] = result[index-1] + input[index];
} else {
result[index] = input[index];
}
if(sign) {
if(result[index] <= 0) {
count += Math.abs(result[index]) + 1;
result[index] = result[index] + Math.abs(result[index]) + 1;
}
sign = false;
} else {
if(result[index] >= 0) {
count += Math.abs(result[index]) + 1;
result[index] = result[index] - Math.abs(result[index]) - 1;
}
sign = true;
}
if(index < result.length-1) {
counting(input, result, count, index+1, sign);
} else {
if(result1 > count) {
result1 = 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": []
} | CORRECT | python2 | n = int(raw_input())
a = map(int, raw_input().split())
ans = []
for k in range(2):
s, cur = 0, 0
for j in range(n):
s += a[j]
if (k+j) % 2 == 0:
if s <= 0:
cur += abs(1 - s)
s = 1
else:
if s >= 0:
cur += abs(-1 - s)
s = -1
ans.append(cur)
print min(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": []
} | CORRECT | python3 | n=input()
a=[int(i) for i in input().split()]
def chk(a,t):
ans=0
x=0
for i in a:
x+=i
if t==True and x<1:
ans+=1-x
x=1
elif t==False and x>-1:
ans+=x+1
x=-1
t=not t
return ans
print(min(chk(a,True),chk(a,False))) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | python3 | import copy
n=int(input())
a=[int(x) for x in input().rstrip().split()]
ac=copy.deepcopy(a)
now=0
ans1=0
for i in range(n):
now+=a[i]
if i%2==0:
if now<=0:
ans1+=abs(now)+1
now=1
else:
if 0<=now:
ans1+=abs(now)+1
now=-1
now=0
ans2=0
for i in range(n):
now+=a[i]
if i%2==0:
if 0<=now:
ans2+=abs(now)+1
now=-1
else:
if now<=0:
ans2+=abs(now)+1
now=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": []
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
int n; cin >> n;
vector<ll> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
ll ans1 = 0, ans2 = 0;
ll S = 0;
for (int i = 0; i < n; i++) {
S += a[i];
if (i % 2 == 0 && S <= 0) {
ans1 += 1 - S;
S = 1ll;
}
else if (i % 2 == 1 && S >= 0) {
ans1 += S + 1;
S = -1ll;
}
}
S = 0;
for (int i = 0; i < n; i++) {
S += a[i];
if (i % 2 == 1 && S <= 0) {
ans2 += 1 - S;
S = 1ll;
}
else if (i % 2 == 0 && S >= 0) {
ans2 += S + 1;
S = -1ll;
}
}
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": []
} | CORRECT | python3 | from copy import copy
n = int(input())
a = [int(x) for x in input().split()]
ans1=[(-1)**i for i in range(n)]
b=copy(a)
res_b=0
sb=0
c=copy(a)
res_c=0
sc=0
for i in range(n):
sb+=a[i]
if ans1[i]*sb>0:
pass
else:
b[i]=ans1[i]-(sb-b[i])
sb=sb-a[i]+b[i]
res_b+=abs(b[i]-a[i])
sc+=a[i]
if -1*ans1[i]*sc>0:
pass
else:
c[i]=-1*ans1[i]-(sc-c[i])
sc=sc-a[i]+c[i]
res_c+=abs(c[i]-a[i])
print(min(res_b,res_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": []
} | CORRECT | java | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.next());
long[] a1 = new long[n];
long[] a2 = new long[n];
for (int i = 0; i < n; i++) {
long temp = Long.parseLong(sc.next());
a1[i] = temp;
a2[i] = temp;
}
long ans1 = 0;
long ans2 = 0;
long temp = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0 && temp + a1[i] >= 0) {
ans1 += temp + a1[i] + 1;
a1[i] -= temp + a1[i] + 1;
}
if (i % 2 != 0 && temp + a1[i] <= 0) {
ans1 += Math.abs(temp + a1[i] - 1);
a1[i] += Math.abs(temp + a1[i] - 1);
}
temp += a1[i];
}
temp = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0 && temp + a2[i] <= 0) {
ans2 += Math.abs(temp + a2[i] - 1);
a2[i] += Math.abs(temp + a2[i] - 1);
}
if (i % 2 != 0 && temp + a2[i] >= 0) {
ans2 += temp + a2[i] + 1;
a2[i] -= temp + a2[i] + 1;
}
temp += a2[i];
}
System.out.println(Math.min(ans1, ans2));
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
vector<int> a(n);
for(int i=0; n>i; i++)
cin>>a.at(i);
long long sum=0,cntp=0;
for(int i=0; i<n; i++){
sum = sum+a.at(i);
if(i%2==0)
if(sum<=0){
cntp = cntp+1-sum;
sum=1;
}
if(i%2==1)
if(sum>=0){
cntp = cntp+sum+1;
sum=-1;
}
}
long long cntm = 0;
sum = 0;
for(int i=0; i<n; i++){
sum = sum+a.at(i);
if(i%2==1)
if(sum<=0){
cntm = cntm+1-sum;
sum=1;
}
if(i%2==0)
if(sum>=0){
cntm = cntm+sum+1;
sum=-1;
}
}
cout << min(cntm,cntp) << 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": []
} | CORRECT | cpp | #include <cstdio>
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int a[n+1];
for(int i=0;i<n;i++)
cin >> a[i];
int64_t sum = 0;
int c = 1;
int64_t ans0 = 0, ans1 = 0;
for(int i=0;i<n;i++) {
sum += a[i];
if(sum * c < 1) {
ans0 += 1 - sum * c;
sum = c;
}
c *= -1;
}
c = -1;
sum = 0;
for(int i=0;i<n;i++) {
sum += a[i];
if(sum * c < 1) {
ans1 += 1 - sum * c;
sum = c;
}
c *= -1;
}
if(ans0 < ans1)
cout << ans0 << endl;
else
cout << ans1 << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | python3 | from sys import stdin
N = int(stdin.readline().rstrip())
A = [int(_) for _ in stdin.readline().rstrip().split()]
def solve(s):
ans, tmp = 0, 0
for i in range(N):
tmp += A[i]
if tmp * s <= 0:
ans += abs(tmp - s)
tmp = s
s *= -1
return ans
print(min(solve(1), solve(-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": []
} | CORRECT | python3 | N = int(input())
A = [int(x) for x in input().split()]
def f(sgn):
cum = 0
cnt_operation = 0
for a in A:
sgn *= -1
cum += a
if cum * sgn > 0:
continue
else:
cnt_operation += abs(cum) + 1
cum = sgn
return cnt_operation
print(min(f(-1), f(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": []
} | CORRECT | java | import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] a = new int[n];
for(int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
long plus_op = 0, minus_op = 0; // start with +, -
long sum = 0;
for(int i = 0; i < n; i++) {
sum += a[i];
if(i % 2 == 0 && sum > 0 || i % 2 == 1 && sum < 0) continue;
if(i % 2 == 0) {
// + なのに - になってる -> sumを+1にもっていく
plus_op += (long)Math.abs(1 - sum);
sum = 1;
}else {
// - なのに + になってる -> sumを-1にもっていく
plus_op += (long)Math.abs(sum + 1);
sum = -1;
}
}
sum = 0;
for(int i = 0; i < n; i++) {
sum += a[i];
if(i % 2 == 0 && sum < 0 || i % 2 == 1 && sum > 0) continue;
if(i % 2 == 0) {
// - なのに + になってる -> sumを-1にもっていく
minus_op += (long)Math.abs(sum + 1);
sum = -1;
}else {
// + なのに - になってる -> sumを+1にもっていく
minus_op += (long)Math.abs(1 - sum);
sum = 1;
}
}
System.out.println(Math.min(plus_op, minus_op));
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | java | import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner io = new Scanner(System.in);
int n = io.nextInt();
int[] a = new int[n+1];
for(int i=1;i<=n;i++){
a[i] = io.nextInt();
}
// + -
long sum = 0;
long now=0;
long border = 1;
long end = 0;
long ans_p=0;
for(int i=1;i<=n;i++){
sum += a[i];
end = border-sum;
if(border>0){
if(now<end){
ans_p += Math.abs(now-end);
now = end;
}
}else{
if(now>end){
ans_p += Math.abs(now-end);
now = end;
}
}
border = -border;
}
//- +
sum=0;
now=0;
border = -1;
end = 0;
long ans_m=0;
for(int i=1;i<=n;i++){
sum += a[i];
end = border-sum;
if(border>0){
if(now<end){
ans_m += Math.abs(now-end);
now = end;
}
}else{
if(now>end){
ans_m += Math.abs(now-end);
now = end;
}
}
border = -border;
}
System.out.println(Math.min(ans_p,ans_m));
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
result = []
for i in range(2):
num = 0
r = 0
for j in range(len(a)):
num += a[j]
if (j + i) % 2 == 0:
if num <= 0:
r -= num - 1
num = 1
else:
if num >= 0:
r += num + 1
num = -1
result.append(r)
print(min(result))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | python3 | n = int(input())
a = list(map(int,input().split()))
def calc(t):
ans = su = 0
for i in range(n):
su += a[i]
if i % 2 == t:
if su > 0:
pass
else:
x = 1 - su
ans += abs(x)
su = 1
else:
if su < 0:
pass
else:
x = - 1 - su
ans += abs(x)
su = -1
return ans
print(min(calc(1), calc(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": []
} | CORRECT | java | import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException{
Sequence solver = new Sequence();
solver.readInput();
solver.solve();
solver.writeOutput();
}
static class Sequence {
private int n;
private long a[];
private long output;
private Scanner scanner;
public Sequence() {
this.scanner = new Scanner(System.in);
}
public void readInput() {
n = Integer.parseInt(scanner.next());
a = new long[n];
for(int i=0; i<n; i++) {
a[i] = Integer.parseInt(scanner.next());
}
}
private long count(int sign) {
long count=0;
long sum=0;
for(int i=0; i<n; i++) {
sum += a[i];
if(i%2==sign) {
// a[i]までの合計を正にするとき
if(sum<=0) {
count += 1-sum;
sum = 1;
}
} else {
// a[i]までの合計を負にするとき
if(0<=sum) {
count += 1+sum;
sum = -1;
}
}
}
return count;
}
public void solve() {
long count1 = count(0);
long count2 = count(1);
output = Math.min(count1,count2);
}
public void writeOutput() {
System.out.println(output);
}
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include<stdio.h>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int n;
long long sum = 0;
long long ans1 = 0;
long long ans2 = 0;
int i, j;
int t = 1;
int main() {
cin >> n ;
int a[100010];
for (i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
if (sum*t <= 0) {
ans1 += abs(sum-t);
sum = t;
}
t *= -1;
}
t = -1;
sum = 0;
for (i = 0; i < n; i++) {
sum += a[i];
if (sum*t <= 0) {
ans2 += abs(sum - t);
sum = t;
}
t *= -1;
}
printf("%lld\n", min(ans1, ans2));
return 0;
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include<bits/stdc++.h>
using namespace std;
int main(){
long long n,a,s1=0,s2=0,c1=0,c2=0;
cin>>n;
for(int i=0;i<n;i++){
cin>>a;
s1+=a;
s2+=a;
if(i%2){
if(s1<=0){
c1+=1-s1;
s1=1;
}
if(s2>=0){
c2+=1+s2;
s2=-1;
}
}
else{
if(s1>=0){
c1+=1+s1;
s1=-1;
}
if(s2<=0){
c2+=1-s2;
s2=1;
}
}
}
cout<<min(c1,c2)<<endl;
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
#define ll long long
template<typename T> void drop(const T &x){cout<<x<<endl;exit(0);}
void solve() {
int n; cin >> n;
ll a;
vector<ll> sum(2), cnt(2);
for(int i=0; i<n; ++i) {
cin >> a;
for(int j : {0,1}) {
sum[j] += a;
int d = 1 - (i+j) % 2 * 2;
if(sum[j]*d <= 0) {
cnt[j] += abs(d-sum[j]);
sum[j] = d;
}
}
}
cout << min(cnt[0],cnt[1]) << '\n';
return;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int T=1;
//cin >> T;
while(T--) 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": []
} | CORRECT | java | import java.util.Arrays;
import java.util.Scanner;
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
int n = scanner.nextInt();
long[] a = IntStream.range(0, n).mapToLong(i -> scanner.nextLong()).toArray();
// sum1=[1,-1,...], sum2=[-1,1,...]
int[] sum1 = new int[n], sum2 = new int[n];
Arrays.fill(sum1, 1);
Arrays.fill(sum2, -1);
IntStream.range(0, n / 2).forEach(i -> {
sum1[2 * i + 1] = -1;
sum2[2 * i + 1] = 1;
});
System.out.println(Math.min(getResult(a, sum1), getResult(a, sum2)));
}
}
/**
* @param a 数値配列
* @param sum 変更したい合計値の配列
* @return 変更が必要なステップ数
*/
private static long getResult(final long[] a, final int[] sum) {
int n = a.length;
long now = 0, result = 0;
for (int i = 0; i < n; i++) {
now += a[i];
if (sum[i] * now <= 0) {
result += Math.abs(sum[i] - now);
now = sum[i];
}
}
return result;
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | java | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastScanner in = new FastScanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
static class TaskC {
public void solve(int testNumber, FastScanner in, PrintWriter out) {
int n = in.nextInt();
long ans1 = 0;
long a1n = 0;
long ans2 = 0;
long a2n = 0;
for (int i = 0; i < n; i++) {
long cn = in.nextLong();
a1n += cn;
a2n += cn;
if (i % 2 == 0) {
ans1 += Math.max(0, -a1n + 1);
a1n = Math.max(1, a1n);
ans2 += Math.max(0, a2n + 1);
a2n = Math.min(-1, a2n);
} else {
ans2 += Math.max(0, -a2n + 1);
a2n = Math.max(1, a2n);
ans1 += Math.max(0, a1n + 1);
a1n = Math.min(-1, a1n);
}
}
out.println(Math.min(ans1, ans2));
}
}
static class FastScanner {
private InputStream in;
private byte[] buffer = new byte[1024];
private int bufPointer;
private int bufLength;
public FastScanner(InputStream in) {
this.in = in;
}
private int readByte() {
if (bufPointer >= bufLength) {
if (bufLength == -1)
throw new InputMismatchException();
bufPointer = 0;
try {
bufLength = in.read(buffer);
} catch (IOException e) {
throw new InputMismatchException();
}
if (bufLength <= 0)
return -1;
}
return buffer[bufPointer++];
}
private static boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public long nextLong() {
long n = 0;
int b = readByte();
while (isSpaceChar(b))
b = readByte();
boolean minus = (b == '-');
if (minus)
b = readByte();
while (b >= '0' && b <= '9') {
n *= 10;
n += b - '0';
b = readByte();
}
if (!isSpaceChar(b))
throw new NumberFormatException();
return minus ? -n : n;
}
public int nextInt() {
long n = nextLong();
if (n < Integer.MIN_VALUE || n > Integer.MAX_VALUE)
throw new NumberFormatException();
return (int) n;
}
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | python3 | n = int(input())
A = list(map(int,input().split()))
ans = float("inf")
for ii in [1, -1]:
i = ii
s = 0
ans0 = 0
for j in range(n):
s += A[j]
if s * i <= 0:
ans0 += abs(s - i)
s = i
i = -i
ans = min(ans, ans0)
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": []
} | CORRECT | java | import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.next());
ArrayList<Integer> a = new ArrayList<>();
for (int i = 0; i < n; i++) {
a.add(Integer.parseInt(sc.next()));
}
sc.close();
long ans1 = 0;
long sum1 = 0;
long sign1 = 1;
for (int i = 0; i < n; i++) {
sum1 += a.get(i);
if (sum1 * sign1 <= 0) {
ans1 += Math.abs(sum1) + 1;
sum1 = sign1;
}
sign1 *= -1;
}
long ans2 = 0;
long sum2 = 0;
long sign2 = -1;
for (int i = 0; i < n; i++) {
sum2 += a.get(i);
if (sum2 * sign2 <= 0) {
ans2 += Math.abs(sum2) + 1;
sum2 = sign2;
}
sign2 *= -1;
}
System.out.println(Math.min(ans1, ans2));
}
public static int[] arrayInt(Scanner sc, int n) {
int[] array = new int[n];
for (int i = 0; i < n; i++) {
array[i] = sc.nextInt();
}
return array;
}
public static long[] arrayLong(Scanner sc, int n) {
long[] array = new long[n];
for (int i = 0; i < n; i++) {
array[i] = sc.nextLong();
}
return array;
}
public static double[] arrayDouble(Scanner sc, int n) {
double[] array = new double[n];
for (int i = 0; i < n; i++) {
array[i] = sc.nextDouble();
}
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": []
} | CORRECT | python3 | def c_sequence():
N = int(input())
A = [int(i) for i in input().split()]
def solver(sign):
ret, total = 0, 0
for a in A:
total += a
if sign * total <= 0:
ret += abs(sign - total) # 総和がsignになるまでaを変化させる
total = sign
sign *= -1
return ret
# 1: 奇数番目を正、偶数番目を負にする場合。 -1: その逆
return min(solver(1), solver(-1))
print(c_sequence()) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | python3 | import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)
n, *a = map(int, read().split())
ans = float('inf')
for i in (-1, 1):
cnt, now = 0, 0
for aa in a:
now += aa
if now * i <= 0:
cnt += abs(now - i)
now = i
i *= -1
ans = min(ans, cnt)
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": []
} | CORRECT | python3 | n=int(input())
a=map(int,input().split())
ans1,ans2=0,0
m1,m2=0,0
k=1
for i in a:
m1+=i
m2+=i
if k:
k=0
if m1>=0:
ans1+=m1+1
m1=-1
if m2<=0:
ans2+=1-m2
m2=1
else:
k=1
if m1<=0:
ans1+=1-m1
m1=1
if m2>=0:
ans2+=m2+1
m2=-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": []
} | CORRECT | python3 | N = int(input())
A = list(map(int,input().split()))
def dfs(sign):
cnt = 0
tmp = 0
for a in A:
tmp += a
if(tmp*sign >= 0):
cnt += abs(tmp) + 1
tmp = -sign
sign *= -1
return cnt
print(min(dfs(1), dfs(-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": []
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin>>n;
vector<long long> a(n);
for(long long &x: a) cin>>x;
long long ans_1=0,ans=0;
long long sum_1=a[0],sum=a[0];
while(sum_1<=0) {sum_1++; ans_1++;}
while(sum>=0) {sum--; ans++;}
for(int i=1;i<n;i++){
sum_1+=a[i];
if(i%2==1){
while(sum_1>=0) {sum_1--; ans_1++;}
}
if(i%2==0){
while(sum_1<=0) {sum_1++; ans_1++;}
}
}
for(int i=1;i<n;i++){
sum+=a[i];
if(i%2==1){
while(sum<=0) {sum++; ans++;}
}
if(i%2==0){
while(sum>=0) {sum--; ans++;}
}
}
cout<<min(ans_1,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": []
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main(){
int n;
cin >> n;
ll A[n];
for(int i=0; i<n; ++i) cin >> A[i];
ll sum = 0, pm = 0, mp = 0;
for(int i=0; i<n; ++i){
sum += A[i];
if(i%2 == 0 && sum <= 0){ pm += 1-sum; sum = 1; }
if(i%2 != 0 && sum >= 0){ pm += 1+sum; sum = -1; }
}
sum = 0;
for(int i=0; i<n; ++i){
sum += A[i];
if(i%2 == 0 && sum >= 0){ mp += 1+sum; sum = -1; }
if(i%2 != 0 && sum <= 0){ mp += 1-sum; sum = 1; }
}
cout << min(pm, mp) << 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": []
} | CORRECT | cpp | #include<iostream>
#define rep(i,n) for(int i=0;i<n;++i)
using namespace std;
int n, a[100000];
int64_t calc(int sign) {
int64_t ret = 0, sum = 0;
rep(i, n) {
sum += a[i];
if (sum * sign <= 0) {
ret += abs(sum) + 1;
sum = sign;
}
sign *= -1;
}
return ret;
}
int main() {
cin >> n;
rep(i, n) cin >> a[i];
cout << min(calc(1), calc(-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": []
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, N) for (int i = 0; i < (int)N; i++)
int main () {
int n;
cin >> n;
vector<int> a(n);
rep(i,n)cin >> a[i];
ll c1 = 0, c2 = 0;
ll s1 = 0, s2 = 0;
for(int i = 0; i < n; i++) {
s1 += a[i];
if(i % 2 == 0 && s1 <= 0) {
c1 += -s1 + 1;
s1 = 1;
} else if(i % 2 == 1 && s1 >= 0) {
c1 += s1 + 1;
s1 = -1;
}
s2 += a[i];
if(i % 2 == 1 && s2 <= 0) {
c2 += -s2 + 1;
s2 = 1;
} else if(i % 2 == 0 && s2 >= 0) {
c2 += s2 + 1;
s2 = -1;
}
}
cout << min(c1, c2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | python3 | n=int(input())
A=list(map(int,input().split()))
if A[0]>0:
S=A[0]
ans_1=0
else:
S=1
ans_1=1-A[0]
for i in A[1:]:
if S*(S+i)<0:
S+=i
else:
ans_1+=abs(S+i)+1
if S<0:
S=1
else:
S=-1
if A[0]<0:
S=A[0]
ans_2=0
else:
S=-1
ans_2=A[0]+1
for i in A[1:]:
if S*(S+i)<0:
S+=i
else:
ans_2+=abs(S+i)+1
if S<0:
S=1
else:
S=-1
print(min(ans_1,ans_2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
int n;
ll a[100000];
ll solve(ll t){
ll res = 0, sum = 0;
for(int i = 0; i < n; i++, t = -t){
sum += a[i];
if(sum * t > 0) continue;
res += abs(sum - t);
sum += t * abs(sum - t);
}
return res;
}
int main(){
scanf("%d", &n);
for(int i = 0; i < n; i++) scanf("%lld", &a[i]);
ll res = solve(1);
res = min(res, solve(-1));
printf("%lld\n", res);
return 0;
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | java | import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws Exception {
// Your code here!
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String[] str_a = br.readLine().split(" ");
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = Integer.parseInt(str_a[i]);
}
long sum = 0;
long count = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 1) {
if (sum >= 0) {
count += sum + 1;
sum = -1;
}
}
else {
if (sum <= 0) {
count += 1 - sum;
sum = 1;
}
}
}
long count2 = 0;
sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 0) {
if (sum >= 0) {
count2 += sum + 1;
sum = -1;
}
}
else {
if (sum <= 0) {
count2 += 1 - sum;
sum = 1;
}
}
}
System.out.println(count>=count2?count2: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": []
} | CORRECT | java | import java.util.*;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long[] nums = new long[n];
for(int i = 0; i < n; i++){
nums[i] = sc.nextLong();
}
long result = 0;
long sum = 0;
// + - + -
for(int i = 0; i < n; i++){
if(i % 2 == 0 && sum + nums[i] <= 0){
result += Math.abs(1 - (sum + nums[i]));
sum = 1;
}
else if(i % 2 == 1 && sum + nums[i] >= 0){
result += Math.abs(-1 - (sum + nums[i]));
sum = -1;
}
else{
sum += nums[i];
}
}
long result2 = 0;
sum = 0;
// - + - +
for(int i = 0; i < n; i++){
if(i % 2 == 1 && sum + nums[i] <= 0){
result2 += Math.abs(1 - (sum + nums[i]));
sum = 1;
}
else if(i % 2 == 0 && sum + nums[i] >= 0){
result2 += Math.abs(-1 - (sum + nums[i]));
sum = -1;
}
else{
sum += nums[i];
}
}
System.out.println(Math.min(result, result2));
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": []
} | CORRECT | java | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
long[] a=new long[n];
for(int i=0;i<n;i++)a[i]=sc.nextLong();
long sum=0;
long cost1=0;
long cost2=0;
//+-+-の順番のコスト
for(int i=0;i<n;i++){
if(i%2==0 && sum+a[i]<=0){
cost1+=-(sum+a[i]-1);
sum=1;
}else if(i%2!=0 && sum+a[i]>=0){
cost1+=sum+a[i]+1;
sum=-1;
}else{
sum+=a[i];
}
}
sum=0;
//-+-+の順番のコスト
for(int i=0;i<n;i++){
if(i%2==0 && sum+a[i]>=0){
cost2+=sum+a[i]+1;
sum=-1;
}else if(i%2!=0 && sum+a[i]<=0){
cost2+=-(sum+a[i]-1);
sum=1;
}else{
sum+=a[i];
}
}
System.out.println(Math.min(cost1, cost2));
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
def check(flag):
acc = 0
cnt = 0
flag = flag
for i in a:
acc += i
if (flag == 1 and flag > acc) or (flag == -1 and flag < acc) or i == 0:
cnt += abs(flag - acc)
acc = flag
flag *= -1
return cnt
print(min(check(1), check(-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": []
} | CORRECT | cpp | #include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main()
{
ll n;
cin >> n;
ll ans = 1e18;
vector<ll> a(n);
for(int i = 0;i<n;i++)cin >> a[i];
for(int aaa = 0;aaa<2;aaa++)
{
if(aaa)for(auto &i:a)i = -i;
ll sum = 0;
ll now = 0;
for(int i = 0;i<n;i++)
{
sum += a[i];
if(i%2)//負になるようにする
{
now += max(0LL,sum-(-1));
sum = min<ll>(-1,sum);
}
else//正になるようにする
{
now += max(0LL,(1)-sum);
sum = max<ll>(1,sum);
}
}
ans = min<ll>(now,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": []
} | CORRECT | cpp | #include<bits/stdc++.h>
using namespace std;
#define LL long long
LL ans1,ans2,sum;
int n;
int a[100010];
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
sum=0;
for(int i=1,s=1;i<=n;i++,s*=-1){
sum+=a[i];
if(sum*s<=0) ans1+=abs(sum-s),sum=s;
}
sum=0;
for(int i=1,s=-1;i<=n;i++,s*=-1){
sum+=a[i];
if(sum*s<=0) ans2+=abs(sum-s),sum=s;
}
printf("%lld\n",min(ans1,ans2));
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <bits/stdc++.h>
#define ll long long
#define vl vector<ll>
#define pl pair<ll,ll>
#define FOR(i,a,b) for(ll i=a;i<b;i++)
#define rep(i,b) for(ll i=0;i<b;i++)
#define RFOR(i,a,b) for(ll i=b-1;i>=a;i--)
#define rsort(v) sort((v).rbegin(), (v).rend())
#define all(v) (v).begin(),(v).end()
using namespace std;
ll mod=1000000007;
signed main(){
ll n,s,sum,ans=0,ans2=0;
cin>>n;
vl a(n);
rep(i,n)cin>>a[i];
sum = 0,s=1;
rep(i,n){
s*=-1;
sum += a[i];
if(sum * s <= 0)ans+=abs(sum-s),sum=s;
}
sum = 0,s=-1;
rep(i,n){
s*=-1;
sum += a[i];
if(sum * s <= 0)ans2+=abs(sum-s),sum=s;
}
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": []
} | CORRECT | java | import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) {
Solver solver = new Solver();
solver.solve();
solver.exit();
}
static class FastScanner {
private final InputStream in = System.in;
private final byte[] buffer = new byte[1024];
private int ptr = 0;
private int buflen = 0;
private boolean hasNextByte() {
if (ptr < buflen) {
return true;
}else{
ptr = 0;
try {
buflen = in.read(buffer);
} catch (IOException e) {
e.printStackTrace();
}
if (buflen <= 0) {
return false;
}
}
return true;
}
private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
private boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
private void skipUnprintable() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;}
public boolean hasNext() { skipUnprintable(); return hasNextByte();}
public String next() {
if (!hasNext()) throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while(isPrintableChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public long nextLong() {
if (!hasNext()) throw new NoSuchElementException();
long n = 0;
boolean minus = false;
int b = readByte();
if (b == '-') {
minus = true;
b = readByte();
}
if (b < '0' || '9' < b) {
throw new NumberFormatException();
}
while(true){
if ('0' <= b && b <= '9') {
n *= 10;
n += b - '0';
}else if(b == -1 || !isPrintableChar(b)){
return minus ? -n : n;
}else{
throw new NumberFormatException();
}
b = readByte();
}
}
}
static class Solver {
FastScanner sc = new FastScanner();
public Solver() { }
String ns() { return sc.next(); }
String[] ns(int n) {
String a[] = new String[n];
for(int i = 0; i < n; i ++) { a[i] = ns(); }
return a;
}
String[][] ns(int n, int m) {
String a[][] = new String[n][m];
for(int i = 0; i < n; i ++) { a[i] = ns(m); }
return a;
}
char[] nc(int n) {
String str = ns();
char a[] = new char[str.length()];
for(int i = 0; i < str.length(); i ++) { a[i] = str.charAt(i); }
return a;
}
char[][] nc(int n, int m) {
char a[][] = new char[n][m];
for(int i = 0; i < n; i ++) { a[i] = nc(m); }
return a;
}
boolean[] nb(int n, char t) {
boolean a[] = new boolean[n];
char c[] = nc(n);
for(int i = 0; i < n; i ++) { a[i] = c[i] == t; }
return a;
}
boolean[][] nb(int n, int m, char t) {
boolean a[][] = new boolean[n][m];
for(int i = 0; i < n; i ++) { a[i] = nb(m, t); }
return a;
}
int ni() { return (int)sc.nextLong(); }
int[] ni(int n) {
int a[] = new int[n];
for(int i = 0; i < n; i ++) { a[i] = ni(); }
return a;
}
int[][] ni(int n, int m) {
int a[][] = new int[n][m];
for(int i = 0; i < n; i ++) { a[i] = ni(m); }
return a;
}
long nl() { return sc.nextLong(); }
long[] nl(int n) {
long a[] = new long[n];
for(int i = 0; i < n; i ++) { a[i] = nl(); }
return a;
}
long[][] nl(int n, int m) {
long a[][] = new long[n][m];
for(int i = 0; i < n; i ++) { a[i] = nl(m); }
return a;
}
PrintWriter out = new PrintWriter(System.out);
PrintWriter err = new PrintWriter(System.err);
void prt() { out.print(""); }
<T> void prt(T a) { out.print(a); }
void prtln() { out.println(""); }
<T> void prtln(T a) { out.println(a); }
void prtln(int... a) {
StringBuilder sb = new StringBuilder();
for(int element : a){ sb.append(element+" "); }
prtln(sb.toString().trim());
}
void prtln(long... a) {
StringBuilder sb = new StringBuilder();
for(long element : a){ sb.append(element+" "); }
prtln(sb.toString().trim());
}
void prtln(double... a) {
StringBuilder sb = new StringBuilder();
for(double element : a){ sb.append(element+" "); }
prtln(sb.toString().trim());
}
void prtln(String... a) {
StringBuilder sb = new StringBuilder();
for(String element : a){ sb.append(element+" "); }
prtln(sb.toString().trim());
}
void prtln(char... a) {
StringBuilder sb = new StringBuilder();
for(char element : a){ sb.append(element); }
prtln(sb.toString().trim());
}
void prtln(int[][] a) { for(int[] element : a){ prtln(element); } }
void prtln(long[][] a) { for(long[] element : a){ prtln(element); } }
void prtln(double[][] a) { for(double[] element : a){ prtln(element); } }
void prtln(String[][] a) { for(String[] element : a){ prtln(element); } }
void prtln(char[][] a) { for(char[] element : a){ prtln(element); } }
void errprt() { err.print(""); }
<T> void errprt(T a) { err.print(a); }
void errprt(boolean a) { errprt(a ? "#" : "."); }
void errprtln() { err.println(""); }
<T> void errprtln(T a) { err.println(a); }
void errprtln(boolean a) { errprtln(a ? "#" : "."); }
void errprtln(int... a) {
StringBuilder sb = new StringBuilder();
for(int element : a){ sb.append(element+" "); }
errprtln(sb.toString().trim());
}
void errprtln(long... a) {
StringBuilder sb = new StringBuilder();
for(long element : a){ sb.append(element+" "); }
errprtln(sb.toString().trim());
}
void errprtln(double... a) {
StringBuilder sb = new StringBuilder();
for(double element : a){ sb.append(element+" "); }
errprtln(sb.toString().trim());
}
void errprtln(String... a) {
StringBuilder sb = new StringBuilder();
for(String element : a){ sb.append(element+" "); }
errprtln(sb.toString().trim());
}
void errprtln(char... a) {
StringBuilder sb = new StringBuilder();
for(char element : a){ sb.append(element); }
errprtln(sb.toString().trim());
}
void errprtln(boolean... a) {
StringBuilder sb = new StringBuilder();
for(boolean element : a){ sb.append((element ? "#" : ".")+" "); }
errprtln(sb.toString().trim());
}
void errprtln(int[][] a) { for(int[] element : a){ errprtln(element); } }
void errprtln(long[][] a) { for(long[] element : a){ errprtln(element); } }
void errprtln(double[][] a) { for(double[] element : a){ errprtln(element); } }
void errprtln(String[][] a) { for(String[] element : a){ errprtln(element); } }
void errprtln(char[][] a) { for(char[] element : a){ errprtln(element); } }
void errprtln(boolean[][] a) { for(boolean[] element : a){ errprtln(element); } }
void reply(boolean b) { prtln(b ? "Yes" : "No"); }
void REPLY(boolean b) { prtln(b ? "YES" : "NO"); }
void flush() { out.flush(); err.flush(); }
void exit() { flush(); System.exit(0); }
int min(int a, int b) { return Math.min(a, b); }
long min(long a, long b) { return Math.min(a, b); }
double min(double a, double b) { return Math.min(a, b); }
int min(int... x) {
int min = x[0];
for(int val : x) { min = min(min, val); }
return min;
}
long min(long... x) {
long min = x[0];
for(long val : x) { min = min(min, val); }
return min;
}
double min(double... x) {
double min = x[0];
for(double val : x) { min = min(min, val); }
return min;
}
int max(int a, int b) { return Math.max(a, b); }
long max(long a, long b) { return Math.max(a, b); }
double max(double a, double b) { return Math.max(a, b); }
int max(int... x) {
int max = x[0];
for(int val : x) { max = max(max, val); }
return max;
}
long max(long... x) {
long max = x[0];
for(long val : x) { max = max(max, val); }
return max;
}
double max(double... x) {
double max = x[0];
for(double val : x) { max = max(max, val); }
return max;
}
long sum(int... a) {
long sum = 0;
for(int element : a) { sum += element; }
return sum;
}
long sum(long... a) {
long sum = 0;
for(long element : a) { sum += element; }
return sum;
}
double sum(double... a) {
double sum = 0;
for(double element : a) { sum += element; }
return sum;
}
long abs(double x) { return (long)Math.abs(x); }
long round(double x) { return Math.round(x); }
long floor(double x) { return (long)Math.floor(x); }
long ceil(double x) { return (long)Math.ceil(x); }
double sqrt(double x) { return Math.sqrt(x); }
double pow(double x, double y) { return Math.pow(x, y); }
long pow(long x, long y) { return (long)Math.pow(x, y); }
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); }
long lcm(long a, long b) { return a * b / gcd(a, b); }
int upperToInt(char a) { return a - 'A'; }
int lowerToInt(char a) { return a - 'a'; }
int charToInt(char a) { return a >= 'a' ? lowerToInt(a) : upperToInt(a); }
char intToUpper(int a) { return (char)(a + 'A'); }
char intToLower(int a) { return (char)(a + 'a'); }
long[] div(long a) {
List<Long> divList = new ArrayList<Long>();
for(long i = 1; i * i <= a; i ++) {
if(a % i == 0) {
divList.add(i);
if(i * i != a) { divList.add(a / i); };
}
}
long div[] = new long[divList.size()];
for(int i = 0; i < divList.size(); i ++) { div[i] = divList.get(i); }
return div;
}
long[][] factor(long a) {
List<Long> factorList = new ArrayList<Long>();
List<Long> degreeList = new ArrayList<Long>();
for(long i = 2; i * i <= a; i ++) {
if(a % i == 0) {
long count = 0;
while(a % i == 0) {
a /= i;
count ++;
}
factorList.add(i);
degreeList.add(count);
}
}
if(a > 1) {
factorList.add(a);
degreeList.add(1L);
}
long factor[][] = new long[factorList.size()][2];
for(int i = 0; i < factorList.size(); i ++) {
factor[i][0] = factorList.get(i);
factor[i][1] = degreeList.get(i);
}
return factor;
}
long[][] countElements(long[] a, boolean sort) {
int len = a.length;
long array[] = new long[len];
for(int i = 0; i < len; i ++) {
array[i] = a[i];
}
if(sort) { Arrays.sort(array); }
List<Long> elem = new ArrayList<Long>();
List<Long> cnt = new ArrayList<Long>();
long tmp = 1;
for(int i = 1; i <= len; i ++) {
if(i == len || array[i] != array[i - 1]) {
elem.add(array[i - 1]);
cnt.add(tmp);
tmp = 1;
}else {
tmp ++;
}
}
long counts[][] = new long[elem.size()][2];
for(int i = 0; i < elem.size(); i ++) {
counts[i][0] = elem.get(i);
counts[i][1] = cnt.get(i);
}
return counts;
}
long[][] countElements(String str, boolean sort) {
int len = str.length();
char array[] = str.toCharArray();
if(sort) { Arrays.sort(array); }
List<Long> elem = new ArrayList<Long>();
List<Long> cnt = new ArrayList<Long>();
long tmp = 1;
for(int i = 1; i <= len; i ++) {
if(i == len || array[i] != array[i - 1]) {
elem.add((long)array[i - 1]);
cnt.add(tmp);
tmp = 1;
}else {
tmp ++;
}
}
long counts[][] = new long[elem.size()][2];
for(int i = 0; i < elem.size(); i ++) {
counts[i][0] = elem.get(i);
counts[i][1] = cnt.get(i);
}
return counts;
}
int numDigits(long a) { return Long.toString(a).length(); }
long bitFlag(int a) { return 1L << (long)a; }
boolean isFlagged(long x, int a) { return (x & bitFlag(a)) != 0; }
long countString(String str, String a) { return (str.length() - str.replace(a, "").length()) / a.length(); }
long countStringAll(String str, String a) { return str.length() - str.replaceAll(a, "").length(); }
void reverse(String array[]) {
String reversed[] = new String[array.length];
for(int i = 0; i < array.length; i ++) { reversed[array.length - i - 1] = array[i]; }
for(int i = 0; i < array.length; i ++) { array[i] = reversed[i]; }
}
void reverse(int array[]) {
int reversed[] = new int[array.length];
for(int i = 0; i < array.length; i ++) { reversed[array.length - i - 1] = array[i]; }
for(int i = 0; i < array.length; i ++) { array[i] = reversed[i]; }
}
void reverse(long array[]) {
long reversed[] = new long[array.length];
for(int i = 0; i < array.length; i ++) { reversed[array.length - i - 1] = array[i]; }
for(int i = 0; i < array.length; i ++) { array[i] = reversed[i]; }
}
void reverse(double array[]) {
double reversed[] = new double[array.length];
for(int i = 0; i < array.length; i ++) { reversed[array.length - i - 1] = array[i]; }
for(int i = 0; i < array.length; i ++) { array[i] = reversed[i]; }
}
void reverse(boolean array[]) {
boolean reversed[] = new boolean[array.length];
for(int i = 0; i < array.length; i ++) { reversed[array.length - i - 1] = array[i]; }
for(int i = 0; i < array.length; i ++) { array[i] = reversed[i]; }
}
void fill(int array[], int x) { Arrays.fill(array, x); }
void fill(long array[], long x) { Arrays.fill(array, x); }
void fill(double array[], double x) { Arrays.fill(array, x); }
void fill(boolean array[], boolean x) { Arrays.fill(array, x); }
void fill(int array[][], int x) { for(int a[] : array) { fill(a, x); } }
void fill(long array[][], long x) { for(long a[] : array) { fill(a, x); } }
void fill(double array[][], double x) { for(double a[] : array) { fill(a, x); } }
void fill(boolean array[][], boolean x) { for(boolean a[] : array) { fill(a, x); } }
void fill(int array[][][], int x) { for(int a[][] : array) { fill(a, x); } }
void fill(long array[][][], long x) { for(long a[][] : array) { fill(a, x); } }
void fill(double array[][][], double x) { for(double a[][] : array) { fill(a, x); } }
void fill(boolean array[][][], boolean x) { for(boolean a[][] : array) { fill(a, x); } }
long INF = (long)1e15;
boolean isINF(long a) { return abs(a) > INF / 1000; }
boolean isPlusINF(long a) { return a > 0 && isINF(a); }
boolean isMinusINF(long a) { return isPlusINF(- a); }
// mods
long MOD = (long)1e9 + 7; // 998244353;
public long mod(long i) { return i % MOD + ((i % MOD) < 0 ? MOD : 0); }
long pow_m(long x, long y) {
if (y == 0) { return 1;
}else {
long tmp = pow_m(x, y / 2);
return mod(mod(tmp * tmp) * (y % 2 == 0 ? 1 : x));
}
}
long inv(long x) { return pow_m(x, MOD - 2); }
int MAX_FACT = 5_000_100;
long fact[];
long invFact[];
void prepareFact() {
fact = new long[MAX_FACT];
Arrays.fill(fact, 0);
invFact = new long[MAX_FACT];
Arrays.fill(invFact, 0);
fact[0] = 1;
int maxIndex = min(MAX_FACT, (int)MOD);
for(int i = 1; i < maxIndex; i ++) { fact[i] = mod(fact[i - 1] * i); }
invFact[maxIndex - 1] = inv(fact[maxIndex - 1]);
for(int i = maxIndex - 1; i > 0; i --) { invFact[i - 1] = mod(invFact[i] * i); }
}
long P(int n, int r) {
if(n < 0 || r < 0 || n < r) { return 0; }
return mod(fact[n] * invFact[n - r]);
}
long C(int n, int r) {
if(n < 0 || r < 0 || n < r) { return 0; }
return mod(P(n, r) * invFact[r]);
}
long H(int n, int r) { return C((n - 1) + r, r); }
// grid
class Grid implements Comparable<Grid> {
int h;
int w;
long val;
Grid() { }
Grid(int h, int w) {
this.h = h;
this.w = w;
}
Grid(int h, int w, long val) {
this.h = h;
this.w = w;
this.val = val;
}
@Override
public int compareTo(Grid g) {
return Long.compare(this.val, g.val);
}
}
// graph
class Graph {
int numNode;
int numEdge;
boolean directed;
Edge edges[];
Node nodes[];
Node reversedNodes[];
Graph(int numNode, int numEdge, Edge edges[], boolean directed) {
this.numNode = numNode;
this.numEdge = numEdge;
this.directed = directed;
this.edges = edges;
nodes = new Node[numNode];
reversedNodes = new Node[numNode];
for(int i = 0; i < numNode; i ++) {
nodes[i] = new Node(i);
reversedNodes[i] = new Node(i);
}
for(Edge edge : edges) {
nodes[edge.source].add(edge.target, edge.cost);
if(directed) {
reversedNodes[edge.target].add(edge.source, edge.cost);
}else {
nodes[edge.target].add(edge.source, edge.cost);
}
}
}
void clearNodes() {
for(Node n : nodes) { n.clear(); }
for(Node n : reversedNodes) { n.clear(); }
}
}
class Node {
int id;
ArrayList<Edge> edges = new ArrayList<Edge>();
Node(int id) {
this.id = id;
}
void add(int target, long cost) {
edges.add(new Edge(id, target, cost));
}
void clear() {
edges.clear();
}
}
class Edge implements Comparable<Edge> {
int source;
int target;
long cost;
Edge(int source, int target, long cost) {
this.source = source;
this.target = target;
this.cost = cost;
}
@Override
public int compareTo(Edge e) {
return Long.compare(this.cost, e.cost);
}
}
public void solve() {
int num = ni();
long a[] = nl(num);
long ans = INF;
for(int x = 0; x < 2; x ++) {
long cnt = 0;
long sum = 0;
boolean plus = x == 0;
for(int i = 0; i < num; i ++) {
sum += a[i];
if(plus) {
if(sum <= 0) { cnt += 1 - sum; sum = 1; }
}else {
if(sum >= 0) { cnt += sum + 1; sum = -1; }
}
plus = !plus;
}
ans = min(ans, cnt);
}
prtln(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": []
} | CORRECT | java |
import java.io.*;
import java.util.*;
public class Main {
static StringBuilder sb = new StringBuilder();
static FastScanner sc = new FastScanner(System.in);
static int INF = 12345678;
static long MOD = 1000000007;
static int[] y4 = {0, 1, 0, -1};
static int[] x4 = {1, 0, -1, 0};
static int[] y8 = {0, 1, 0, -1, -1, 1, 1, -1};
static int[] x8 = {1, 0, -1, 0, 1, -1, 1, -1};
static long[] F;//factorial
static boolean[] isPrime;
static int[] primes;
static char[][] map;
static int N, M;
static long T;
static int[] A;
public static void main(String[] args) {
int n = sc.nextInt();
long[] a = sc.nextLongArray(n);
long ans_1 = 0;//初期値正
long ans_2 = 0;
long sum_1 = 0;
long sum_2 = 0;
for (int i = 0; i < n; i++) {
if(i%2==1){
if(sum_1 + a[i] >= 0){
ans_1 += abs(sum_1 + a[i]) + 1;
sum_1 = -1;
}else{
sum_1 += a[i];
}
if(sum_2 + a[i] <= 0){
ans_2 += abs(sum_2 + a[i]) + 1;
sum_2 = 1;
}else{
sum_2 += a[i];
}
}else{
if(sum_2 + a[i] >= 0){
ans_2 += abs(sum_2 + a[i]) + 1;
sum_2 = -1;
}else{
sum_2 += a[i];
}
if(sum_1 + a[i] <= 0){
ans_1 += abs(sum_1 + a[i]) + 1;
sum_1 = 1;
}else{
sum_1 += a[i];
}
}
}
System.out.println(min(ans_1,ans_2));
}
static class Dijkstra {
long initValue = -1;
Node[] nodes;
int n;
long[] d;
Dijkstra(int n) {
this.n = n;
nodes = new Node[n];
for (int i = 0; i < n; i++)
nodes[i] = new Node(i);
d = new long[n];
Arrays.fill(d, initValue);
}
Dijkstra(int n, int edge, boolean isDirectedGraph) {
this.n = n;
nodes = new Node[n];
for (int i = 0; i < n; i++)
nodes[i] = new Node(i);
d = new long[n];
Arrays.fill(d, initValue);
if (isDirectedGraph) {
for (int ei = 0; ei < edge; ei++) {
int f = sc.nextInt() - 1;
int t = sc.nextInt() - 1;
long c = sc.nextLong();
addEdge(f, t, c);
}
} else {
for (int ei = 0; ei < edge; ei++) {
int f = sc.nextInt() - 1;
int t = sc.nextInt() - 1;
long c = sc.nextLong();
addEdge(f, t, c);
addEdge(t, f, c);
}
}
}
void addEdge(int f, int t, long c) {
nodes[f].edges.add(new Edge(t, c));
}
long[] solve(int s) {
d[s] = 0;
//最短距離と頂点を持つ
PriorityQueue<Dis> q = new PriorityQueue<>();
q.add(new Dis(s, 0));
while (!q.isEmpty()) {
Dis now = q.poll();
int nowId = now.p;
long nowC = now.cos;
for (Edge edge : nodes[nowId].edges) {
int to = edge.toId;
long needsCost = edge.toCost + nowC;
if (d[to] == initValue || needsCost < d[to]) {
d[to] = needsCost;
q.add(new Dis(to, needsCost));
}
}
}
return d;
}
//O( E ^ 2) 辺が密の時用
long[] solve2(int s) {
boolean[] used = new boolean[n];
long[][] cost = new long[n][n];
Main.fill(cost, initValue);
Arrays.fill(d, initValue);
d[s] = 0;
for (Node node : nodes) {
for (Edge edge : node.edges) {
int fromId = node.id;
int toId = edge.toId;
long toCost = edge.toCost;
cost[fromId][toId] = toCost;
}
}
while (true) {
int v = -1;
//まだ使われていない頂点のうち、距離が最小のものを探す。
for (int u = 0; u < n; u++)
if (!used[u] && (v == -1 || d[u] < d[v])) v = u;
if (v == -1) break;
used[v] = true;
for (int u = 0; u < n; u++)
d[u] = Math.min(d[u], d[v] + cost[v][u]);
}
return d;
}
static class Dis implements Comparable<Dis> {
//現在地点 最短距離
int p;
long cos;
Dis(int p, long cost) {
this.p = p;
cos = cost;
}
public int compareTo(Dis d) {
if (cos != d.cos) {
if (cos > d.cos) return 1;
else if (cos == d.cos) return 0;
else return -1;
} else {
return p - d.p;
}
}
}
static class Node {
int id;
List<Edge> edges;
Node(int id) {
edges = new ArrayList<>();
this.id = id;
}
}
static class Edge {
int toId;
long toCost;
Edge(int id, long cost) {
toId = id;
toCost = cost;
}
}
}
public static long toLong(int[] ar) {
long res = 0;
for (int i : ar) {
res *= 10;
res += i;
}
return res;
}
public static int toInt(int[] ar) {
int res = 0;
for (int i : ar) {
res *= 10;
res += i;
}
return res;
}
//k個の次の組み合わせをビットで返す 大きさに上限はない 110110 -> 111001
public static int nextCombSizeK(int comb, int k) {
int x = comb & -comb; //最下位の1
int y = comb + x; //連続した下の1を繰り上がらせる
return ((comb & ~y) / x >> 1) | y;
}
public static int keta(long num) {
int res = 0;
while (num > 0) {
num /= 10;
res++;
}
return res;
}
public static long getHashKey(int a, int b) {
return (long) a << 32 | b;
}
public static boolean isOutofIndex(int x, int y) {
if (x < 0 || y < 0) return true;
if (map[0].length <= x || map.length <= y) return true;
return false;
}
public static void setPrimes() {
int n = 100001;
isPrime = new boolean[n];
List<Integer> prs = new ArrayList<>();
Arrays.fill(isPrime, true);
isPrime[0] = isPrime[1] = false;
for (int i = 2; i * i <= n; i++) {
if (!isPrime[i]) continue;
prs.add(i);
for (int j = i * 2; j < n; j += i) {
isPrime[j] = false;
}
}
primes = new int[prs.size()];
for (int i = 0; i < prs.size(); i++)
primes[i] = prs.get(i);
}
public static void revSort(int[] a) {
Arrays.sort(a);
reverse(a);
}
public static void revSort(long[] a) {
Arrays.sort(a);
reverse(a);
}
public static int[][] copy(int[][] ar) {
int[][] nr = new int[ar.length][ar[0].length];
for (int i = 0; i < ar.length; i++)
for (int j = 0; j < ar[0].length; j++)
nr[i][j] = ar[i][j];
return nr;
}
/**
* <h1>指定した値以上の先頭のインデクスを返す</h1>
* <p>配列要素が0のときは、0が返る。</p>
*
* @return<b>int</b> : 探索した値以上で、先頭になるインデクス
* 値が無ければ、挿入できる最小のインデックス
*/
public static int lowerBound(final int[] arr, final int value) {
int low = 0;
int high = arr.length;
int mid;
while (low < high) {
mid = ((high - low) >>> 1) + low; //(low + high) / 2 (オーバーフロー対策)
if (arr[mid] < value) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
/**
* <h1>指定した値より大きい先頭のインデクスを返す</h1>
* <p>配列要素が0のときは、0が返る。</p>
*
* @return<b>int</b> : 探索した値より上で、先頭になるインデクス
* 値が無ければ、挿入できる最小のインデックス
*/
public static int upperBound(final int[] arr, final int value) {
int low = 0;
int high = arr.length;
int mid;
while (low < high) {
mid = ((high - low) >>> 1) + low; //(low + high) / 2 (オーバーフロー対策)
if (arr[mid] <= value) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
/**
* <h1>指定した値以上の先頭のインデクスを返す</h1>
* <p>配列要素が0のときは、0が返る。</p>
*
* @return<b>int</b> : 探索した値以上で、先頭になるインデクス
* 値がなければ挿入できる最小のインデックス
*/
public static long lowerBound(final long[] arr, final long value) {
int low = 0;
int high = arr.length;
int mid;
while (low < high) {
mid = ((high - low) >>> 1) + low; //(low + high) / 2 (オーバーフロー対策)
if (arr[mid] < value) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
/**
* <h1>指定した値より大きい先頭のインデクスを返す</h1>
* <p>配列要素が0のときは、0が返る。</p>
*
* @return<b>int</b> : 探索した値より上で、先頭になるインデクス
* 値がなければ挿入できる最小のインデックス
*/
public static long upperBound(final long[] arr, final long value) {
int low = 0;
int high = arr.length;
int mid;
while (low < high) {
mid = ((high - low) >>> 1) + low; //(low + high) / 2 (オーバーフロー対策)
if (arr[mid] <= value) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
//次の順列に書き換える、最大値ならfalseを返す
public static boolean nextPermutation(int A[]) {
int len = A.length;
int pos = len - 2;
for (; pos >= 0; pos--) {
if (A[pos] < A[pos + 1]) break;
}
if (pos == -1) return false;
//posより大きい最小の数を二分探索
int ok = pos + 1;
int ng = len;
while (Math.abs(ng - ok) > 1) {
int mid = (ok + ng) / 2;
if (A[mid] > A[pos]) ok = mid;
else ng = mid;
}
swap(A, pos, ok);
reverse(A, pos + 1, len - 1);
return true;
}
//次の順列に書き換える、最小値ならfalseを返す
public static boolean prevPermutation(int A[]) {
int len = A.length;
int pos = len - 2;
for (; pos >= 0; pos--) {
if (A[pos] > A[pos + 1]) break;
}
if (pos == -1) return false;
//posより小さい最大の数を二分探索
int ok = pos + 1;
int ng = len;
while (Math.abs(ng - ok) > 1) {
int mid = (ok + ng) / 2;
if (A[mid] < A[pos]) ok = mid;
else ng = mid;
}
swap(A, pos, ok);
reverse(A, pos + 1, len - 1);
return true;
}
//↓nCrをmod計算するために必要。 ***factorial(N)を呼ぶ必要がある***
static long ncr(int n, int r) {
if (n < r) return 0;
else if (r == 0) return 1;
factorial(n);
return F[n] / (F[n - r] * F[r]);
}
static long ncr2(int a, int b) {
if (b == 0) return 1;
else if (a < b) return 0;
long res = 1;
for (int i = 0; i < b; i++) {
res *= a - i;
res /= i + 1;
}
return res;
}
static long ncrdp(int n, int r) {
if (n < r) return 0;
long[][] dp = new long[n + 1][r + 1];
for (int ni = 0; ni < n + 1; ni++) {
dp[ni][0] = dp[ni][ni] = 1;
for (int ri = 1; ri < ni; ri++) {
dp[ni][ri] = dp[ni - 1][ri - 1] + dp[ni - 1][ri];
}
}
return dp[n][r];
}
static long modNcr(int n, int r) {
if (n < r) return 0;
long result = F[n];
result = result * modInv(F[n - r]) % MOD;
result = result * modInv(F[r]) % MOD;
return result;
}
public static long modSum(long... lar) {
long res = 0;
for (long l : lar)
res = (res + l % MOD) % MOD;
return res;
}
public static long modDiff(long a, long b) {
long res = a - b;
if (res < 0) res += MOD;
res %= MOD;
return res;
}
public static long modMul(long... lar) {
long res = 1;
for (long l : lar)
res = (res * l) % MOD;
if (res < 0) res += MOD;
res %= MOD;
return res;
}
public static long modDiv(long a, long b) {
long x = a % MOD;
long y = b % MOD;
long res = (x * modInv(y)) % MOD;
return res;
}
static long modInv(long n) {
return modPow(n, MOD - 2);
}
static void factorial(int n) {
F = new long[n + 1];
F[0] = F[1] = 1;
// for (int i = 2; i <= n; i++)
// {
// F[i] = (F[i - 1] * i) % MOD;
// }
//
for (int i = 2; i <= 100000; i++) {
F[i] = (F[i - 1] * i) % MOD;
}
for (int i = 100001; i <= n; i++) {
F[i] = (F[i - 1] * i) % MOD;
}
}
static long modPow(long x, long n) {
long res = 1L;
while (n > 0) {
if ((n & 1) == 1) {
res = res * x % MOD;
}
x = x * x % MOD;
n >>= 1;
}
return res;
}
//↑nCrをmod計算するために必要
static int gcd(int n, int r) {
return r == 0 ? n : gcd(r, n % r);
}
static long gcd(long n, long r) {
return r == 0 ? n : gcd(r, n % r);
}
static <T> void swap(T[] x, int i, int j) {
T t = x[i];
x[i] = x[j];
x[j] = t;
}
static void swap(int[] x, int i, int j) {
int t = x[i];
x[i] = x[j];
x[j] = t;
}
public static void reverse(int[] x) {
int l = 0;
int r = x.length - 1;
while (l < r) {
int temp = x[l];
x[l] = x[r];
x[r] = temp;
l++;
r--;
}
}
public static void reverse(long[] x) {
int l = 0;
int r = x.length - 1;
while (l < r) {
long temp = x[l];
x[l] = x[r];
x[r] = temp;
l++;
r--;
}
}
public static void reverse(int[] x, int s, int e) {
int l = s;
int r = e;
while (l < r) {
int temp = x[l];
x[l] = x[r];
x[r] = temp;
l++;
r--;
}
}
static int length(int a) {
int cou = 0;
while (a != 0) {
a /= 10;
cou++;
}
return cou;
}
static int length(long a) {
int cou = 0;
while (a != 0) {
a /= 10;
cou++;
}
return cou;
}
static int cou(boolean[] a) {
int res = 0;
for (boolean b : a) {
if (b) res++;
}
return res;
}
static int cou(String s, char c) {
int res = 0;
for (char ci : s.toCharArray()) {
if (ci == c) res++;
}
return res;
}
static int countC2(char[][] a, char c) {
int co = 0;
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[0].length; j++)
if (a[i][j] == c) co++;
return co;
}
static int countI(int[] a, int key) {
int co = 0;
for (int i = 0; i < a.length; i++)
if (a[i] == key) co++;
return co;
}
static int countI(int[][] a, int key) {
int co = 0;
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[0].length; j++)
if (a[i][j] == key) co++;
return co;
}
static void fill(int[][] a, int v) {
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[0].length; j++)
a[i][j] = v;
}
static void fill(long[][] a, long v) {
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[0].length; j++)
a[i][j] = v;
}
static void fill(int[][][] a, int v) {
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[0].length; j++)
for (int k = 0; k < a[0][0].length; k++)
a[i][j][k] = v;
}
static int max(int... a) {
int res = Integer.MIN_VALUE;
for (int i : a) {
res = Math.max(res, i);
}
return res;
}
static long max(long... a) {
long res = Long.MIN_VALUE;
for (long i : a) {
res = Math.max(res, i);
}
return res;
}
static int max(int[][] ar) {
int res = Integer.MIN_VALUE;
for (int i[] : ar)
res = Math.max(res, max(i));
return res;
}
static int min(int... a) {
int res = Integer.MAX_VALUE;
for (int i : a) {
res = Math.min(res, i);
}
return res;
}
static long min(long... a) {
long res = Long.MAX_VALUE;
for (long i : a) {
res = Math.min(res, i);
}
return res;
}
static int min(int[][] ar) {
int res = Integer.MAX_VALUE;
for (int i[] : ar)
res = Math.min(res, min(i));
return res;
}
static int sum(int[] a) {
int cou = 0;
for (int i : a)
cou += i;
return cou;
}
static int abs(int a) {
return Math.abs(a);
}
static long abs(long a) {
return Math.abs(a);
}
static class FastScanner {
private BufferedReader reader = null;
private StringTokenizer tokenizer = null;
public FastScanner(InputStream in) {
reader = new BufferedReader(new InputStreamReader(in));
tokenizer = null;
}
public String next() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
/*public String nextChar(){
return (char)next()[0];
}*/
public String nextLine() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
return reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken("\n");
}
public long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public int[] nextIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public int[] nextIntArrayDec(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt() - 1;
}
return a;
}
public int[][] nextIntArray2(int h, int w) {
int[][] a = new int[h][w];
for (int hi = 0; hi < h; hi++) {
for (int wi = 0; wi < w; wi++) {
a[hi][wi] = nextInt();
}
}
return a;
}
public int[][] nextIntArray2Dec(int h, int w) {
int[][] a = new int[h][w];
for (int hi = 0; hi < h; hi++) {
for (int wi = 0; wi < w; wi++) {
a[hi][wi] = nextInt() - 1;
}
}
return a;
}
//複数の配列を受け取る
public void nextIntArrays2ar(int[] a, int[] b) {
for (int i = 0; i < a.length; i++) {
a[i] = sc.nextInt();
b[i] = sc.nextInt();
}
}
public void nextIntArrays2arDec(int[] a, int[] b) {
for (int i = 0; i < a.length; i++) {
a[i] = sc.nextInt() - 1;
b[i] = sc.nextInt() - 1;
}
}
//複数の配列を受け取る
public void nextIntArrays3ar(int[] a, int[] b, int[] c) {
for (int i = 0; i < a.length; i++) {
a[i] = sc.nextInt();
b[i] = sc.nextInt();
c[i] = sc.nextInt();
}
}
//複数の配列を受け取る
public void nextIntArrays3arDecLeft2(int[] a, int[] b, int[] c) {
for (int i = 0; i < a.length; i++) {
a[i] = sc.nextInt() - 1;
b[i] = sc.nextInt() - 1;
c[i] = sc.nextInt();
}
}
public Integer[] nextIntegerArray(int n) {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public char[] nextCharArray(int n) {
char[] a = next().toCharArray();
return a;
}
public char[][] nextCharArray2(int h, int w) {
char[][] a = new char[h][w];
for (int i = 0; i < h; i++) {
a[i] = next().toCharArray();
}
return a;
}
//スペースが入っている場合
public char[][] nextCharArray2s(int h, int w) {
char[][] a = new char[h][w];
for (int i = 0; i < h; i++) {
a[i] = nextLine().replace(" ", "").toCharArray();
}
return a;
}
public char[][] nextWrapCharArray2(int h, int w, char c) {
char[][] a = new char[h + 2][w + 2];
//char c = '*';
int i;
for (i = 0; i < w + 2; i++)
a[0][i] = c;
for (i = 1; i < h + 1; i++) {
a[i] = (c + next() + c).toCharArray();
}
for (i = 0; i < w + 2; i++)
a[h + 1][i] = c;
return a;
}
//スペースが入ってる時用
public char[][] nextWrapCharArray2s(int h, int w, char c) {
char[][] a = new char[h + 2][w + 2];
//char c = '*';
int i;
for (i = 0; i < w + 2; i++)
a[0][i] = c;
for (i = 1; i < h + 1; i++) {
a[i] = (c + nextLine().replace(" ", "") + c).toCharArray();
}
for (i = 0; i < w + 2; i++)
a[h + 1][i] = c;
return a;
}
public long[] nextLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = nextLong();
}
return a;
}
public long[][] nextLongArray2(int h, int w) {
long[][] a = new long[h][w];
for (int hi = 0; hi < h; hi++) {
for (int wi = 0; wi < w; wi++) {
a[hi][wi] = nextLong();
}
}
return a;
}
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | python3 | def re(k1,l,s):
r=0
for i in range(0,n):
if s==1:
s=0
k1+=l[i]
if k1>=0:
r+=k1+1
k1=-1
continue
if s==0:
s=1
k1+=l[i]
if k1<=0:
r+=abs(k1)+1
k1=1
return r
n=int(input())
l=list(map(int,input().split()))
ans=0
k1=0
ans=min(re(k1,l,1),re(k1,l,0))
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": []
} | CORRECT | java | import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int[] A = new int[N + 1];
A[0] = 0;
for (int i= 1; i <= N; ++i) {
A[i] = sc.nextInt();
}
sc.close();
long sum1 = 0;
long sum2 = 0;
long ans1 = 0;
long ans2 = 0;
for (int i= 1; i <= N; ++i) {
sum1 += A[i];
if (i % 2 == 0 && sum1 >= 0) {
ans1 += Math.abs(sum1) +1;
sum1 = -1;
} else if (i % 2 != 0 && sum1 <= 0) {
ans1 += Math.abs(sum1) + 1;
sum1 = 1;
}
}
for (int i= 1; i <= N; ++i) {
sum2 += A[i];
if (i % 2 == 0 && sum2 <= 0) {
ans2 += Math.abs(sum2) +1;
sum2 = 1;
} else if (i % 2 != 0 && sum2 >= 0) {
ans2 += Math.abs(sum2) + 1;
sum2 = -1;
}
}
System.out.println(Math.min(ans1, ans2));
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | java | import java.util.Scanner;
import java.util.Arrays;
public class Main{
static int n;
static long[] a;
static final boolean DEBUG = false;
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
a = new long[n];
for(int i = 0; i < n; i++){
a[i] = sc.nextInt();
}
long count = 0, count2 = 0;
long sum = a[0];
long sum2 = a[0] < 0 ? 1 : -1;
count2 = Math.abs(sum2 - a[0]);
if(a[0] == 0){
sum = 1;
count = count2 = 1;
sum2 = -1;
}
for(int i = 1; i < n; i++){
long val = a[i], val2 = a[i];
if(!((sum > 0 && sum + a[i] < 0) ||
(sum < 0 && sum + a[i] > 0))){
val = -sum + ((sum < 0) ? 1 : -1);
count += Math.abs(val - a[i]);
}
if(!((sum2 > 0 && sum2 + a[i] < 0) ||
(sum2 < 0 && sum2 + a[i] > 0))){
val2 = -sum2 + ((sum2 < 0) ? 1 : -1);
count2 += Math.abs(val2 - a[i]);
}
sum += val;
sum2 += val2;
}
if(DEBUG){
System.out.println(Arrays.toString(a));
}
System.out.println(Math.min(count, count2));
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | java | //package AtCoder.Indeed10;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) {
int n = in.NI();
List<Long> q = new ArrayList<>(n);
for (int i=0;i<n;i++) q.add(in.NL());
long fans = Long.MAX_VALUE;
long sign = 1; long presum = 0;
long ans = func(n, q, sign, presum);
fans = Long.min(fans, ans);
sign = -1; presum = 0;
ans = func(n, q, sign, presum);
fans = Long.min(fans, ans);
out.println(fans);
out.close();
}
private static long func(final int n, final List<Long> q, long sign, long presum) {
long ans =0;
for (int i=0;i<n;i++) {
presum+=q.get(i);
if (presum*sign > 0) { sign = sign*-1L; continue; }
if (presum==0) {
if (sign==1) {ans++; presum++;}
else {ans++; presum--;}
} else if (presum>0) {
ans += (1+presum);
presum = -1;
} else {
ans += (1-presum);
presum = 1;
}
sign = sign * -1L;
}
return ans;
}
static InputStream inputStream = System.in;
static OutputStream outputStream = System.out;
static InputReader in = new InputReader(inputStream);
static PrintWriter out = new PrintWriter(outputStream);
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int NI() {
return Integer.parseInt(next());
}
public long NL() {
return Long.parseLong(next());
}
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | java | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.lang.Character.Subset;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.time.temporal.ValueRange;
import java.util.AbstractMap;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Set;
import java.util.Stack;
import java.util.TreeMap;
import java.util.TreeSet;
import static java.util.Comparator.*;
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
MyInput in = new MyInput(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Solver solver = new Solver(in, out);
solver.solve();
out.close();
}
// ======================================================================
static class Solver {
MyInput in;
PrintWriter out;
public Solver(MyInput in, PrintWriter out) {
this.in = in;
this.out = out;
}
// ======================================================================
public void solve() {
int N = ni();
long a, sum_P = 0, sum_M=0, cnt_P = 0, cnt_M = 0;;
for (int i = 0; i < N; i++) {
a = nl();
if(i % 2 == 0) {
sum_P += a;
if(sum_P <= 0) {
cnt_P += (-sum_P + 1);
sum_P = 1;
}
sum_M += a;
if(sum_M >= 0) {
cnt_M += (sum_M + 1);
sum_M = -1;
}
} else {
sum_P += a;
if(sum_P >= 0) {
cnt_P += (sum_P + 1);
sum_P = -1;
}
sum_M += a;
if(sum_M <= 0) {
cnt_M += (-sum_M + 1);
sum_M = 1;
}
}
}
cnt_P += (sum_P == 0 ? 1 : 0);
cnt_M += (sum_M == 0 ? 1 : 0);
prn(Math.min(cnt_P, cnt_M));
}
// -----------------------------------------
// Integer のキーに対して、カウントを管理する(カウントを足したり、引いたりする)
static class MapCounter {
private Map<Integer, Long> map = new HashMap<>();
public MapCounter() {}
// キーのカウントに値を足す
public void add(int key) {
add(key, 1);
}
public void add(int key, int cnt) {
Long val = map.get(key);
if(val == null) {
map.put(key, (long)cnt);
} else {
map.put(key, val + cnt);
}
}
// キーのカウントから値を引く
public void sub(int key) {
sub(key, 1);
}
public void sub(int key, int cnt) {
Long val = map.get(key);
if(val == null) {
map.put(key, (long)-cnt);
} else {
map.put(key, val - cnt);
}
}
// キーのカウントを取得する(なければ NULLを返す)
public Long getCountwithNull(int key) {
return map.get(key);
}
// キーのカウントを取得する(なければ 0 を返す)
public Long getCount(int key) {
Long val = map.get(key);
if(val == null) return 0L;
else return val;
}
public Set<Integer> getKey() {
return map.keySet();
}
// 登録されているキーの数を返す
public int getKeyCount() {
return map.keySet().size();
}
}
// -----------------------------------------
// 配列のバイナリーサーチ 1
boolean isRightMin(int[] a, boolean f, int index, int key) {
if (f && a[index] >= key) return true; // 以上
else if (!f && a[index] > key) return true; // より大きい
else return false;
}
// 配列 a の中で key 以上(f=true)または、より大きく(f=false)、一番小さい値を返す
int binarySearchRightMin(int[] a, boolean f, int key) {
int ng = -1; //「index = 0」が条件を満たすこともあるので、初期値は -1
int ok = (int)a.length; // 「index = a.length-1」が条件を満たさないこともあるので、初期値は a.length()
/* ok と ng のどちらが大きいかわからないことを考慮 */
while (Math.abs(ok - ng) > 1) {
int mid = (ok + ng) / 2;
if (isRightMin(a, f, mid, key)) ok = mid; // 下半分を対象とする
else ng = mid; // 上半分を対象とする
}
return ok; // ← ここで返すのは isOK() が true の時にセットする方(ok / ng)
}
// -----------------------------------------
// 配列のバイナリーサーチ 2
boolean isLeftMax(int[] a, boolean f, int index, int key) {
if (f && a[index] <= key) return true; // 以下
else if (!f && a[index] < key) return true; // より小さい
else return false;
}
// 配列 a の中で key 以下(f=true)または、より小さい(f=false)、一番大きい値を返す
int binarySearchLeftMax(int[] a, boolean f, int key) {
int ng = -1; //「index = 0」が条件を満たすこともあるので、初期値は -1
int ok = (int)a.length; // 「index = a.length-1」が条件を満たさないこともあるので、初期値は a.length()
/* ok と ng のどちらが大きいかわからないことを考慮 */
while (Math.abs(ok - ng) > 1) {
int mid = (ok + ng) / 2;
if (isLeftMax(a, f, mid, key)) ng = mid; // 上半分を対象とする
else ok = mid; // 下半分を対象とする
}
return ng; // ← ここで返すのは isOK() が true の時にセットする方(ok / ng)
}
// -----------------------------------------
// オイラーツアー(部分木対応)
static class EulerTour {
Graph g;
List<Integer> euler_tour = new ArrayList<>();
int[] begin, end;
int k = 0, root = 0;
void dfs(int v,int p, PrintWriter out) {
out.println("v = " + v + " p = " + p);
begin[v] = k;
euler_tour.add(v);
k++;
if(!g.contains(v)) {
return;
}
for(int i : g.get(v)) {
if(i != p) {
dfs(i, v, out);
euler_tour.add(v);
k++;
}
}
end[v]=k;
}
// 初期化
public void init(int p_cnt, int root, Graph g, PrintWriter out) {
begin = new int[p_cnt + 1];
end = new int[p_cnt + 1];
this.root = root;
this.g = g;
dfs(root, -1, out);
}
// 部分木の頂点を渡すと、オイラーツアーの部分木を返す
public List getPartTour(int v) {
return euler_tour.subList(begin[v], end[v]);
}
// 部分木の頂点を渡すと、頂点のリストを返す
public List<Integer> getPartList(int v) {
Set<Integer> set = new TreeSet<>();
set.addAll(getPartTour(v));
List<Integer> ans = new ArrayList<>();
for(Integer p : set) {
ans.add(p);
}
return ans;
}
}
// -----------------------------------------
// グラフのリンクリスト
static class Graph {
// 頂点に紐づく頂点のリスト
private Map<Integer, List<Integer>> data = new HashMap<Integer, List<Integer>>();
// // 全ての頂点のセット
// private Set<Integer> point = new TreeSet<>();
// 頂点と頂点の繋がりを追加する
void add(int key, int value) {
List<Integer> list = data.get(key);
if(list == null) {
list = new ArrayList<Integer>();
data.put(key, list);
}
list.add(value);
// point.add(key);
// point.add(value);
}
// 指定された頂点に紐づく、頂点のリストを返す
List<Integer> get(int key) {
return data.get(key);
}
// 頂点 key が登録されているか?
boolean contains(int key) {
return data.containsKey(key);
}
// 頂点のセットを返す
Set<Integer> keySet() {
return data.keySet();
}
// 頂点 key_1 と 頂点 key_2 がつながっていれば true を返す
boolean isConnect(int key_1, int key_2) {
List<Integer> list = data.get(key_1);
if(list == null) return false;
else return list.contains(key_2);
}
// 指定された頂点から、すべての頂点への距離を返す
List<PP> distList(int key) {
List<PP> dist = new ArrayList<>(); // 頂点と距離のペアのリスト
Set<Integer> mark = new HashSet<>(); // 処理したら入れる
Stack<PP> stack = new Stack<>(); // スタックの宣言
stack.push(new PP(key, 0)); // スタートをスタックに保存
while(!stack.isEmpty()) {
PP wk = stack.pop(); // スタックから次の頂点を取得
int pp = wk.getKey();
int dd = wk.getVal();
mark.add(pp); // 通過マーク
dist.add(new PP(pp, dd)); // 距離を登録
List<Integer> list = get(pp); // つながっている頂点のリストを取得
for(int next : list) {
if(mark.contains(next)) continue;
stack.push(new PP(next, dd + 1));
}
}
return dist;
}
// ダンプ
void dump(PrintWriter out) {
for(int key : data.keySet()) {
out.print(key + " : ");
for(int val : data.get(key)) {
out.print(val + " ");
}
out.println("");
}
}
}
// -----------------------------------------
// 重さを持ったグラフのリンクリスト
static class GraphWith {
// キーに紐づくリストに、頂点番号と重さのペアを持つ
private Map<Integer, List<PP>> data = new HashMap<Integer, List<PP>>();
// 指定された頂点に紐づく、頂点と重さのペアを追加する
void add(int key, PP p) {
List<PP> list = data.get(key);
if(list == null) {
list = new ArrayList<PP>();
data.put(key, list);
}
list.add(p);
}
// 頂点に紐づく、頂点と重さのペアのリストを返す
List<PP> get(int key) {
return data.get(key);
}
// グラフの存在チェック(重さは関係しない)
// 頂点 key と 頂点 value がつながっていれば true を返す
boolean contains(int key, int value) {
List<PP> list = data.get(key);
if(list == null) return false;
boolean ans = false;
for(PP p : list) {
if(p.getKey() == value) {
ans = true;
break;
}
}
return ans;
}
}
// -----------------------------------------
// グラフのリンクリスト(Long)
static class GraphLong {
private Map<Long, List<Long>> G = new HashMap<Long, List<Long>>();
void add(long key, long value) {
List<Long> list = G.get(key);
if(list == null) {
list = new ArrayList<Long>();
G.put(key, list);
}
list.add(value);
}
List<Long> get(long key) {
return G.get(key);
}
}
// -----------------------------------------
// 重さを持ったグラフのリンクリスト(Long)
static class GraphLongWith {
private Map<Long, List<PPL>> G = new HashMap<Long, List<PPL>>();
void add(long key, PPL p) {
List<PPL> list = G.get(key);
if(list == null) {
list = new ArrayList<PPL>();
G.put(key, list);
}
list.add(p);
}
List<PPL> get(long key) {
return G.get(key);
}
}
// -----------------------------------------
void prn(String s) {
out.println(s);
}
void prn(int i) {
out.println(i);
}
void prn(long i) {
out.println(i);
}
void prr(String s) {
out.print(s);
}
int ni() {
return in.nextInt();
}
long nl() {
return in.nextLong();
}
double nd() {
return in.nextDouble();
}
String ns() {
return in.nextString();
}
int[] ndi(int n) {
int[] ans = new int[n];
for(int i=0; i < n; i++) {
ans[i] = ni();
}
return ans;
}
long[] ndl(int n) {
long[] ans = new long[n];
for(int i=0; i < n; i++) {
ans[i] = nl();
}
return ans;
}
double[] ndd(int n) {
double[] ans = new double[n];
for(int i=0; i < n; i++) {
ans[i] = nd();
}
return ans;
}
String[] nds(int n) {
String[] ans = new String[n];
for(int i=0; i < n; i++) {
ans[i] = ns();
}
return ans;
}
int[][] nddi(int n, int m) {
int[][] ans = new int[n][m];
for(int i=0; i < n; i++) {
for(int j=0; j < m; j++) {
ans[i][j] = ni();
}
}
return ans;
}
long[][] nddl(int n, int m) {
long[][] ans = new long[n][m];
for(int i=0; i < n; i++) {
for(int j=0; j < m; j++) {
ans[i][j] = nl();
}
}
return ans;
}
}
static class PP{
public int key, val;
public PP(int key, int val) {
this.key = key;
this.val = val;
}
public int getKey() {
return key;
}
public void setKey(int key) {
this.key = key;
}
public int getVal() {
return val;
}
public void setVal(int val) {
this.val = val;
}
}
static class PPL {
public long key, val;
public PPL(long key, long val) {
this.key = key;
this.val = val;
}
public long getKey() {
return key;
}
public void setKey(long key) {
this.key = key;
}
public long getVal() {
return val;
}
public void setVal(long val) {
this.val = val;
}
}
static class PPDL {
public long key;
public long[] val;
public PPDL(long key, long[] val) {
this.key = key;
this.val = val;
}
public long getKey() {
return key;
}
public void setKey(long key) {
this.key = key;
}
public long[] getVal() {
return val;
}
public void setVal(long[] val) {
this.val = val;
}
public void dump(PrintWriter out) {
out.print("key = " + key + " val ");
for(int i=0; i < val.length; i++) {
out.print("[" + val[i] + "] ");
}
out.println("");
}
}
// HashMap のキーに使う
static final class PPKEY{
private final int key, val;
public PPKEY(int key, int val) {
this.key = key;
this.val = val;
}
public int getKey() {
return key;
}
public int getVal() {
return val;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof PPKEY) {
PPKEY dest = (PPKEY) obj;
return this.key == dest.key && this.val == dest.val;
} else {
return false;
}
}
@Override
public int hashCode() {
return Objects.hash(key, val);
}
}
// HashMap のキーに使う
static final class PPLKEY{
private final long key, val;
public PPLKEY(long key, long val) {
this.key = key;
this.val = val;
}
public long getKey() {
return key;
}
public long getVal() {
return val;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof PPKEY) {
PPKEY dest = (PPKEY) obj;
return this.key == dest.key && this.val == dest.val;
} else {
return false;
}
}
@Override
public int hashCode() {
return Objects.hash(key, val);
}
}
// ======================================================================
static class Pair<K, V> extends AbstractMap.SimpleEntry<K, V> {
/** serialVersionUID. */
private static final long serialVersionUID = 6411527075103472113L;
public Pair(final K key, final V value) {
super(key, value);
}
}
static class MyInput {
private final BufferedReader in;
private static int pos;
private static int readLen;
private static final char[] buffer = new char[1024 * 8];
private static char[] str = new char[500 * 8 * 2];
private static boolean[] isDigit = new boolean[256];
private static boolean[] isSpace = new boolean[256];
private static boolean[] isLineSep = new boolean[256];
static {
for (int i = 0; i < 10; i++) {
isDigit['0' + i] = true;
}
isDigit['-'] = true;
isSpace[' '] = isSpace['\r'] = isSpace['\n'] = isSpace['\t'] = true;
isLineSep['\r'] = isLineSep['\n'] = true;
}
public MyInput(InputStream is) {
in = new BufferedReader(new InputStreamReader(is));
}
public int read() {
if (pos >= readLen) {
pos = 0;
try {
readLen = in.read(buffer);
} catch (IOException e) {
throw new RuntimeException();
}
if (readLen <= 0) {
throw new MyInput.EndOfFileRuntimeException();
}
}
return buffer[pos++];
}
public int nextInt() {
int len = 0;
str[len++] = nextChar();
len = reads(len, isSpace);
int i = 0;
int ret = 0;
if (str[0] == '-') {
i = 1;
}
for (; i < len; i++) ret = ret * 10 + str[i] - '0';
if (str[0] == '-') {
ret = -ret;
}
return ret;
}
public long nextLong() {
int len = 0;
str[len++] = nextChar();
len = reads(len, isSpace);
int i = 0;
long ret = 0L;
if (str[0] == '-') {
i = 1;
}
for (; i < len; i++) ret = ret * 10 + str[i] - '0';
if (str[0] == '-') {
ret = -ret;
}
return ret;
}
public double nextDouble() {
int len = 0;
str[len++] = nextChar();
len = reads(len, isSpace);
int i = 0;
double ret = 0;
if (str[0] == '-') {
i = 1;
}
int cnt = 0;
for (; i < len; i++) {
if(str[i] == '.') {
cnt = 10;
continue;
}
if(cnt == 0) {
ret = ret * 10 + str[i] - '0';
} else {
ret = ret + ((double)(str[i] - '0') / cnt);
cnt *= 10;
}
}
if (str[0] == '-') {
ret = -ret;
}
return ret;
}
public String nextString() {
String ret = new String(nextDChar()).trim();
return ret;
}
public char[] nextDChar() {
int len = 0;
len = reads(len, isSpace);
char[] ret = new char[len + 1];
for (int i=0; i < len; i++) ret[i] = str[i];
ret[len] = 0x00;
return ret;
}
public char nextChar() {
while (true) {
final int c = read();
if (!isSpace[c]) {
return (char) c;
}
}
}
int reads(int len, boolean[] accept) {
try {
while (true) {
final int c = read();
if (accept[c]) {
break;
}
if (str.length == len) {
char[] rep = new char[str.length * 3 / 2];
System.arraycopy(str, 0, rep, 0, str.length);
str = rep;
}
str[len++] = (char) c;
}
} catch (MyInput.EndOfFileRuntimeException e) {
}
return len;
}
static class EndOfFileRuntimeException extends RuntimeException {
}
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
int main(){
int n; cin >> n;
int a[n+1];
for(int i=1; i<=n; i++) cin >> a[i];
ll p1=0, p2=0, sum1=0, sum2=0;
for(int i=1; i<=n; i++){
sum1 += a[i]; sum2 += a[i];
if(i%2){
p1 += max(-sum1+1, 1LL*0); sum1 = max(1LL, sum1);
p2 += max(sum2+1, 1LL*0); sum2 = min(-1LL, sum2);
}
else{
p1 += max(sum1+1, 1LL*0); sum1 = min(-1LL, sum1);
p2 += max(-sum2+1, 1LL*0); sum2 = max(1LL, sum2);
}
}
cout << min(p1, p2) << 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": []
} | CORRECT | cpp | #include <bits/stdc++.h>
#define ll long long
using namespace std;
int main(){
int n;
cin >> n;
ll a[n], sum, res1 = 0, res2 = 0;
for(int i = 0; i < n; i++) cin >> a[i];
// odd is -
sum = 0;
for(int i = 0; i < n; i++){
sum += a[i];
if(i % 2){
if(sum >= 0) res1 += 1 + sum, sum = -1;
}else{
if(sum <= 0) res1 += 1 - sum, sum = 1;
}
}
// odd is +
sum = 0;
for(int i = 0; i < n; i++){
sum += a[i];
if(i % 2){
if(sum <= 0) res2 += 1 - sum, sum = 1;
}else{
if(sum >= 0) res2 += 1 + sum, sum = -1;
}
}
cout << min(res1, res2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | python3 | n = int(input())
arr = list(map(int , input().split()))
cur = 0
a = 0
b = 0
prea=0
preb=0
resa = 0
resb =0
for i in range(0 , n):
prea = prea+arr[i]
if (i% 2 ==0 and prea<=0):
resa = resa-prea+1
prea=1
if i%2 ==1 and prea>=0:
resa = resa+prea+1
prea=-1
for i in range(0 , n):
preb = preb+arr[i]
if (i%2==1 and preb<=0):
resb = resb+(-preb+1)
preb=1
if i%2 ==0 and preb>=0:
resb =resb+ preb+1
preb=-1
print(min(resa , resb)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | java | import java.util.Scanner;
public class Main {
void run() {
try (Scanner sc = new Scanner(System.in)) {
int n = sc.nextInt();
int[] a = new int[n];
for(int i=0;i<n;i++) {
a[i] = sc.nextInt();
}
long sum1 = 0;
long cnt1 = 0;
// a[0] > 0
for(int i=0;i<n;i++) {
sum1 += a[i];
if(i%2==0 && sum1 <= 0) {
cnt1 += (1-sum1);
sum1 = 1;
}else if(i%2==1 && sum1 >= 0) {
cnt1 += (1+sum1);
sum1 = -1;
}
}
long sum2 = 0;
long cnt2 = 0;
// a[0] < 0
for(int i=0;i<n;i++) {
sum2 += a[i];
if(i%2==1 && sum2 <= 0) {
cnt2 += (1-sum2);
sum2 = 1;
}else if(i%2==0 && sum2 >= 0) {
cnt2 += (1+sum2);
sum2 = -1;
}
}
System.out.println(Math.min(cnt1, cnt2));
}
}
public static void main(String[] args) {
new Main().run();
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | python3 | n = int(input())
A = list(map(int, input().split()))
ans = 1e16
for s in (1, -1):
res, acc = 0, 0
for a in A:
acc += a
if acc * s <= 0:
res += abs(acc-s)
acc = s
s *= -1
ans = min(ans, res)
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include<bits/stdc++.h>
#define REP(i,n) for(int i=0,i##_len=(n);i<i##_len;++i)
#define rep(i,a,b) for(int i=int(a);i<int(b);++i)
using namespace std;
signed main(){
int N;cin>>N;
vector<long long> A(N);
REP(i, N) cin >> A[i];
long long ans=LLONG_MAX;
int sig[2]={1,-1};
REP(j,2){
long long sum=0;
long long count=0;
REP(i,N){
sum+=A[i];
if(sig[(i^j)&1]*sum>=0){
count+=llabs(sum)+1;
sum=-sig[(i^j)&1];
}
}
ans=min(ans,count);
}
cout<<ans<<endl;
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | python3 | n = int(input())
a = list(map(int,input().split()))
ans=10**15
for i in [1,-1]:
b=0
c=0
for j in a:
b+=j
if b*i<=0:
c+=abs(b-i)
b=i
i*=-1
ans=min(ans,c)
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": []
} | CORRECT | java | import java.util.*;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] a = new int[n];
for(int i = 0; i<n; i++) {
a[i] = sc.nextInt();
}
long min = Long.MAX_VALUE;
long tmp = 0;
long cnt = 0;
for(int i = 0; i<n; i++) {
tmp+=a[i];
System.err.println(i+" "+tmp);
if(i%2==0) {
if(tmp<=0) {
cnt += 1 - tmp;
tmp = 1;
}
}else {
if(tmp>=0) {
cnt += tmp + 1;
tmp = -1;
}
}
}
min = cnt;
cnt = 0;
tmp = 0;
System.err.println(min);
for(int i = 0; i<n; i++) {
tmp+=a[i];
if(i%2==1) {
if(tmp<=0) {
cnt += 1 - tmp;
tmp = 1;
}
}else {
if(tmp>=0) {
cnt += tmp + 1;
tmp = -1;
}
}
}
min = Math.min(min, cnt);
System.out.println(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": []
} | CORRECT | python3 | N = int(input())
a = list(map(int, input().split()))
total = 0
values = []
for s in (1, -1):
xs = a[::]
signs = [s * (-1) ** i for i in range(N)]
total = 0
for i in range(N):
if (total + xs[i]) * signs[i] <= 0:
xs[i] = signs[i] - total
total += xs[i]
values.append(sum(abs(a[i] - xs[i]) for i in range(N)))
print(min(values)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | python3 | N = int(input())
A = tuple(map(int,input().split()))
def solve(f):
acc = 0
cnt = 0
for a in A:
acc += a
if f and acc <= 0:
cnt += 1 - acc
acc = 1
elif not f and acc >= 0:
cnt += acc + 1
acc = -1
f = not f
return cnt
print(min(solve(True),solve(False))) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
int n;
cin>>n;
vector<int> A(n);
for(int &i:A)
cin>>i;
int j,k,l=100000000000000000;
for(int a=0;a<2;a++){
k=0,j=0;
for(int i=0;i<n;i++){
k+=A[i];
if(((i+a)%2*2-1)*k<=0){
j+=1+abs(k);
k=(i+a)%2*2-1;
}
}
l=min(l,j);
}
cout<<l<<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": []
} | CORRECT | java | import java.util.*;
public class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
sc.close();
// +-+-+...の場合
long sum1 = 0;
long count1 = 0;
for (int i = 0; i < n; i++) {
sum1 += a[i];
if (i % 2 == 0) {
if (sum1 <= 0) {
count1 += (1 - sum1);
sum1 = 1;
}
} else {
if (0 <= sum1) {
count1 += (sum1 + 1);
sum1 = -1;
}
}
}
// -+-+_...の場合
long sum2 = 0;
long count2 = 0;
for (int i = 0; i < n; i++) {
sum2 += a[i];
if (i % 2 == 0) {
if (0 <= sum2) {
count2 += (sum2 + 1);
sum2 = -1;
}
} else {
if (sum2 <= 0) {
count2 += (1 - sum2);
sum2 = 1;
}
}
}
System.out.println(Math.min(count1, count2));
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <iostream>
#define REP(i, n) for (int i = 0; i < n; ++i)
using namespace std;
using ll = long long;
ll sum1,sum2,cost1,cost2;
int main() {
int n; cin >> n;
REP(i,n) {
int cur; cin >> cur;
sum1 += cur, sum2 += cur;
if (i&1) {
if (sum1<=0) cost1 += 1-sum1, sum1 = 1;
if (sum2>=0) cost2 += sum2+1, sum2 = -1;
} else {
if (sum1>=0) cost1 += sum1+1, sum1 = -1;
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": []
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin>>n;
long long int a[n],sum1=0,sum2=0,cost1=0,cost2=0;
for(int i=0;i<n;i++){
cin>>a[i];
sum1+=a[i];
sum2+=a[i];
if(i%2==0 && sum1>=0){
cost1+=1+sum1;
sum1=-1;
}
else if(i%2==1 && sum1<=0){
cost1+=1-sum1;
sum1=1;
}
if(i%2==0 && sum2<=0){
cost2+=1-sum2;
sum2=1;
}
else if(i%2==1 && sum2>=0){
cost2+=1+sum2;
sum2=-1;
}
}
cout<<min(cost1,cost2);
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | python3 | n = int(input())
a = [int(x) for x in input().split()]
def check(a, t):
ans = 0
x = 0
for i in a:
x += i
if t == True and x < 1:
ans += 1 - x
x = 1
elif t == False and x > -1:
ans += x + 1
x = -1
t = not t
return ans
print(min(check(a, True), check(a, False)))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> V(N);
for (int i = 0, a, sum = 0; i < N && cin >> a; i++)
V.at(i) = sum += a;
long long cnt1 = 0;
for (int i = 0, j = 0; i < N; i++) {
int tmp = V.at(i) + j;
if (!(i % 2) && tmp >= 0) cnt1 += abs(-1 - tmp), j += -1 - tmp;
if (i % 2 && tmp <= 0) cnt1 += 1 - tmp, j += 1 - tmp;
}
long long cnt2 = 0;
for (int i = 0, j = 0; i < N; i++) {
int tmp = V.at(i) + j;
if (i % 2 && tmp >= 0) cnt2 += abs(-1 - tmp), j += -1 - tmp;
if (!(i % 2) && tmp <= 0) cnt2 += 1 - tmp, j += 1 - tmp;
}
cout << min(cnt1, cnt2) << "\n";
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | python3 | n = int(input())
a = list(map(int,input().split()))
trueans = float('inf')
for k in [-1,1]:
sign = k
ans = 0
s = 0
for i in range(n):
s += a[i]
if s == 0:
s = sign
ans += 1
elif s//abs(s) == sign:
pass
else:
ans += abs(sign-s)
s = sign
sign *= -1
trueans = min(ans,trueans)
print(trueans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <cstdio>
#include <algorithm>
#define maxn 100000
#define ll long long
int a[maxn];
ll det(int n, int a[], int l) {
int sum = 0;
ll rez = 0;
for (int i = 1; i <= n; i++, l = 1 - l) {
sum += a[i];
if (l == 0 && sum <= 0) {
rez += 1 - sum;
sum = 1;
}
else if (l == 1 && sum >= 0) {
rez += 1 + sum;
sum = -1;
}
}
return rez;
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
ll c1 = det(n, a, 0);
ll c2 = det(n, a, 1);
ll rez = std::min(c1, c2);
printf("%lld", rez);
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": []
} | CORRECT | cpp | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef long long ll;
using namespace std;
int n, a[100010];
ll calc(ll s) {
ll sum = 0, res = 0;
for (int i = 0; i < n; ++i, s *= -1) {
sum += a[i];
if (sum * s > 0) continue;
res += abs(sum - s);
sum += s * abs(sum - s);
}
return res;
}
int main() {
cin >> n;
rep(i, n) cin >> a[i];
cout << min(calc(1), calc(-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": []
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int>pa;
const int N=2e5+100;
const int mod=1e9+7;
ll a[N];
int n;
ll slove(ll f)
{
ll sum=0,ans=0;
for(int i=1;i<=n;i++){
sum+=a[i];
if(sum*f<=0) {
ans+=abs(f-sum);
sum=f;
}
f=-f;
}
return ans;
}
int main()
{
ios::sync_with_stdio(0);cin.tie(0);
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
ll ans=min(slove(1),slove(-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": []
} | CORRECT | cpp | #include <iostream>
int a[1000005];
int n;
long long calc(int mod){
long long sum = 0, res = 0;
for (int i = 0; i < n; i++){
sum += a[i];
if (i%2 == mod && sum <= 0) {
res += 1-sum;
sum = 1;
}
else if (i%2 != mod && sum >= 0){
res += sum+1;
sum=-1;
}
}
return res;
}
int main(){
std::cin.tie(0);
std::cin >> n;
for(int i=0; i<n; i++) std::cin >> a[i];
std::cout << std::min(calc(0), calc(1)) <<'\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": []
} | CORRECT | python3 | n = int(input())
As = list(map(int, input().split()))
rs = []
for m in {0, 1}:
r = 0
s = 0
for i in range(n):
A = As[i]
if i%2 == m:
if s+A >= 0:
r += s+A+1
s = -1
else:
s += A
else:
if s+A <= 0:
r += -(s+A)+1
s = +1
else:
s += A
#print(m, r)
rs.append(r)
r = min(rs)
print(r)
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.