Search is not available for this dataset
name
stringlengths 2
88
| description
stringlengths 31
8.62k
| public_tests
dict | private_tests
dict | solution_type
stringclasses 2
values | programming_language
stringclasses 5
values | solution
stringlengths 1
983k
|
---|---|---|---|---|---|---|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import sys
import math
INF = 10**9+7
def k(i):
if(i == 1):
return 1
else:
return(i * k(i-1))
def comb(n, r):
if(n == r or r == 1):
return 1
else:
return k(n) / (k(n-r) * k(r))
stdin = sys.stdin
def na(): return map(int, stdin.readline().split())
def ns(): return stdin.readline().strip()
def nsl(): return list(stdin.readline().strip())
def ni(): return int(stdin.readline())
def nil(): return list(map(int, stdin.readline().split()))
n = ni()
a = nil()
b = []
for i in range(n):
b.append(a[i])
sum = 0
c1 = 0
c2 = 0
if a[0] == 0:
a[0] = 1
c1 += 1;
for i in range(0, n-1):
sum += a[i]
sum2 = sum + a[i+1]
if sum2 == 0:
if sum >0:
a[i+1] -= 1
sum2 -= 1
c1 +=1
if(sum * sum2 >= 0):
k = abs(sum2) + 1
h = k - (abs(sum) - 1)
l = k - h
if sum > 0 :
a[i] -= l
sum -= l
a[i + 1] -= h
else:
a[i] += l
sum += l
a[i + 1] += h
c1 += h+l
sum = 0
a = b
if a[0] == 0:
a[0] = 1
c2 += 1;
else:
c2 = abs(a[0]) + 1
if a[0] > 0:
a[0] = -1
else:
a[0] = 1
for i in range(0, n-1):
sum += a[i]
sum2 = sum + a[i+1]
if sum2 == 0:
if sum >0:
a[i+1] -= 1
sum2 -= 1
c2 +=1
if(sum * sum2 >= 0):
k = abs(sum2) + 1
h = k - (abs(sum) - 1)
l = k - h
if sum > 0 :
a[i] -= l
sum -= l
a[i + 1] -= h
else:
a[i] += l
sum += l
a[i + 1] += h
c2 += k
print(min(c1, c2))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
long long a[n];
long long res = 0;
for (long long i = 0; i < n; ++i) {
cin >> a[i];
}
bool can1 = false;
if (a[0] == 0) {
for (long long i = 1; i < n; ++i) {
if (a[i] > 0) {
a[0] = -1;
res++;
can1 = true;
break;
} else if (a[i] < 0) {
a[0] = 1;
res++;
can1 = true;
break;
}
}
} else
can1 = true;
long long total = a[0];
for (long long i = 1; i < n; ++i) {
if (total < 0 && total + a[i] >= 0) {
total += a[i];
if (total == 0) {
total++;
res++;
}
} else if (total > 0 && total + a[i] <= 0) {
total += a[i];
if (total == 0) {
total--;
res++;
}
} else if (total < 0 && total + a[i] <= 0) {
res += 1 - total - a[i];
a[i] = 1 - total;
total += a[i];
if (total == 0) {
total++;
res++;
}
} else if (total > 0 && total + a[i] >= 0) {
res += abs(-1 - total - a[i]);
a[i] = -1 - total;
total += a[i];
if (total == 0) {
total--;
res++;
}
}
}
if (can1 == false)
cout << 2 * n + 1 << endl;
else
cout << res << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long a[100000];
int n;
void solve() {
long long sum0;
long long sum1;
long long ans1 = 0;
long long ans2 = 0;
if (a[0] == 0) {
ans1++;
sum0 = 1;
} else {
sum0 = a[0];
}
for (int i = 1; i < n; i++) {
sum1 = sum0 + a[i];
if (sum1 * sum0 < 0) {
} else if (sum1 * sum0 > 0) {
ans1 += abs(sum1) + 1;
sum1 = -1 * sum0 / abs(sum0);
} else {
ans1++;
sum1 = -1 * sum0 / abs(sum0);
}
sum0 = sum1;
}
if (a[0] == 0) {
ans2++;
sum0 = -1;
} else {
sum0 = a[0];
}
for (int i = 1; i < n; i++) {
sum1 = sum0 + a[i];
if (sum1 * sum0 < 0) {
} else if (sum1 * sum0 > 0) {
ans2 += abs(sum1) + 1;
sum1 = -1 * sum0 / abs(sum0);
} else {
ans2++;
sum1 = -1 * sum0 / abs(sum0);
}
sum0 = sum1;
}
cout << min(ans1, ans2) << endl;
return;
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
solve();
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a.at(i);
}
int total1 = 0;
int ans1 = 0;
for (int i = 0; i < n; i++) {
total1 += a.at(i);
if (i % 2 == 0) {
if (total1 <= 0) {
ans1 += total1 * (-1) + 1;
total1 = 1;
}
} else {
if (total1 >= 0) {
ans1 += total1 + 1;
total1 = -1;
}
}
}
int total2 = 0;
int ans2 = 0;
for (int i = 0; i < n; i++) {
total2 += a.at(i);
if (i % 2 == 1) {
if (total2 <= 0) {
ans2 += total2 * (-1) + 1;
total2 = 1;
}
} else {
if (total2 >= 0) {
ans2 += total2 + 1;
total2 = -1;
}
}
}
cout << min(ans1, ans2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
signed main() {
int n;
cin >> n;
int a[n];
for (size_t i = 0; i < n; i++) {
cin >> a[i];
}
int s = a[0];
int res = 0;
for (size_t i = 1; i < n; i++) {
if (s > 0) {
int s2 = s + a[i];
if (s2 <= -1) {
s = s2;
} else {
s = -1;
res += (s2 + 1);
}
} else {
int s2 = s + a[i];
if (s2 >= 1) {
s = s2;
} else {
s = 1;
res += (-s2 + 1);
}
}
}
cout << res << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long INFl = 1e+18 + 1;
int INF = 1e+9 + 1;
int sign(int A) { return (A > 0) - (A < 0); }
int main() {
int N;
cin >> N;
int a[100001];
int asum[100001] = {0};
for (int i = 0; i < N; i++) {
cin >> a[i];
if (i != 0)
asum[i] = asum[i - 1] + a[i];
else
asum[i] = a[i];
}
long long ansmin = INFl;
for (int pari = 0; pari < 2; pari++) {
long long ans = 0, sumplus = 0;
for (int i = 0; i < N; i++) {
int goal = (i + pari) % 2 * 2 - 1;
if (sign(asum[i] + sumplus) != goal) {
ans += abs(asum[i] + sumplus - goal);
sumplus += goal - (asum[i] + sumplus);
}
}
ansmin = min(ansmin, ans);
}
cout << ansmin << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | require 'prime'
include Math
def max(a,b); a > b ? a : b end
def min(a,b); a < b ? a : b end
def swap(a,b); a, b = b, a end
def gif; gets.to_i end
def gff; gets.to_f end
def gsf; gets.chomp end
def gi; gets.split.map(&:to_i) end
def gf; gets.split.map(&:to_f) end
def gs; gets.chomp.split.map(&:to_s) end
def gc; gets.chomp.split('') end
def pr(num); num.prime_division end
def digit(num); num.to_s.length end
def array(s,ini=nil); Array.new(s){ini} end
def darray(s1,s2,ini=nil); Array.new(s1){Array.new(s2){ini}} end
def rep(num); num.times{|i|yield(i)} end
def repl(st,en,n=1); st.step(en,n){|i|yield(i)} end
n = gif
a = gi
sum = []
count = 0
sum << a[0]
repl 1,a.size-1 do |i|
sum << a[i]+sum[i-1]
if sum[i-1] > 0
loop{
break if sum[i] < 0
sum[i] -= 1
count += 1
}
else
loop{
break if sum[i] > 0
sum[i] += 1
count += 1
}
end
end
puts count |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int getsign(int n) {
if (n > 0) {
return 1;
}
if (n < 0) {
return -1;
}
return -1;
}
int count(int sign0, long a[], int n) {
int sum = 0;
int sign = sign0;
int count = 0;
for (int i = 0; i < n; ++i) {
sum += a[i];
if (getsign(sum) != sign) {
count += abs(sign - sum);
sum = sign;
}
sign = (sign * -1);
}
return count;
}
int main() {
int n;
cin >> n;
long a[n];
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
cout << min(count(1, a, n), count(-1, a, n)) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
sum = a[0]
change = 0
if a[0]==0:
i=0
while a[i]==0:
i+=1
if a[i]<0:
a[i-1]=1
for idx in range(i-2,-1,-1):
a[idx]=a[i-1]*-1
if a[i]>0:
a[i-1]=-1
for idx in range(i-2,-1,-1):
a[idx]=a[i-1]*-1
change += i-1
for i in range(1,n):
val = 0
tempsum = sum+a[i]
if sum < 0 and tempsum <=0:
val = 1 - tempsum
if sum > 0 and tempsum >=0:
val = -1 - tempsum
sum = tempsum + val
change += abs(val)
print(change) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9 + 7;
long long n, g, ans, t;
int main() {
cin >> n;
cin >> t;
if (t == 0) {
int a[100001];
g = 1;
ans += 1;
for (int i = 2; i <= n; i++) {
cin >> a[i];
if (g > 0) {
g += a[i];
if (g > -1) {
ans += g + 1;
g = -1;
}
} else {
g += a[i];
if (g < 1) {
ans += 1 - g;
g = 1;
}
}
}
long long ans2 = 1;
g = -1;
for (int i = 2; i <= n; i++) {
if (g > 0) {
g += a[i];
if (g > -1) {
ans2 += g + 1;
g = -1;
}
} else {
g += a[i];
if (g < 1) {
ans2 += 1 - g;
g = 1;
}
}
}
cout << min(ans, ans2);
return 0;
}
g = t;
while (--n) {
cin >> t;
if (g > 0) {
g += t;
if (g > -1) {
ans += g + 1;
g = -1;
}
} else {
g += t;
if (g < 1) {
ans += 1 - g;
g = 1;
}
}
}
cout << ans;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int ans(int n, int *, int change);
int main() {
int n;
int a[110000];
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
int pAns, nAns;
pAns = ans(n, a, 1);
nAns = ans(n, a, -1);
printf("%d\n", min(pAns, nAns));
}
int ans(int n, int *a, int change) {
int Ans = 0;
int sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
switch (change) {
case -1:
if (sum > -1) Ans += 1 + sum, sum = -1;
change *= -1;
break;
case 1:
if (sum < 1) Ans += 1 - sum, sum = 1;
change *= -1;
break;
}
}
return Ans;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
enum State { Plus, Minus, Zero };
State GetState(long long sum) {
State state;
if (sum > 0)
state = Plus;
else if (sum == 0)
state = Zero;
else
state = Minus;
return state;
}
int main() {
int n;
cin >> n;
vector<long long> a(n);
cin >> a[0];
unsigned long long count = 0;
State state = GetState(a[0]);
if (state == Zero) {
a[0] = -1;
state = Minus;
count++;
}
long long sum = a[0];
for (int i = 1; i < n; i++) {
cin >> a[i];
State nextState = GetState(sum + a[i]);
switch (nextState) {
case Plus:
if (state == Plus) {
long long bf_a = a[i];
a[i] = -1 - sum;
count += abs(a[i] - bf_a);
nextState = Minus;
}
break;
case Minus:
if (state == Minus) {
long long bf_a = a[i];
a[i] = 1 - sum;
count += abs(a[i] - bf_a);
nextState = Plus;
}
break;
case Zero:
if (state == Plus) {
long long bf_a = a[i];
a[i] = -1 - sum;
count += abs(a[i] - bf_a);
nextState = Minus;
} else if (state == Minus) {
long long bf_a = a[i];
a[i] = 1 - sum;
count += abs(a[i] - bf_a);
nextState = Plus;
}
default:
break;
}
sum += a[i];
state = nextState;
}
if (sum == 0) count++;
cout << count << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void) {
long int n;
cin >> n;
vector<long int> a(n);
for (int i = 0; i < (n); i++) {
cin >> a[i];
}
long int oddcount = 0, evencount = 0;
long int oddsum = 0, evensum = 0;
bool oddplus = true, evenplus = false;
for (int i = 0; i < (n); i++) {
oddsum += a[i];
evensum += a[i];
if (oddplus && oddsum <= 0) {
oddcount += 1 - oddsum;
oddsum = 1;
} else if (!oddplus && oddsum >= 0) {
oddcount += 1 + oddsum;
oddsum = -1;
}
if (evenplus && evensum <= 0) {
evencount += 1 - evensum;
evensum = 1;
} else if (!evenplus && evensum >= 0) {
evencount += 1 + evensum;
evensum = -1;
}
oddplus = !oddplus;
evenplus = !evenplus;
}
cout << fmin(oddcount, evencount) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import sys
def input():
return sys.stdin.readline().strip()
sys.setrecursionlimit(20000000)
def main():
N = int(input())
A = list(map(int, input().split()))
S = A[0]
cnt = 0
if A[0] == 0:
answer = []
for a in (1, -1):
cnt = 1
S = a
for i in range(1, N):
s = S + A[i]
if A[i] == 0:
if S > 0:
cnt += S + 1
S = -1
else:
cnt += abs(S) + 1
S = 1
else:
if s == 0:
if S < 0:
cnt += 1
S = 1
else:
cnt += 1
S = -1
else:
if S * s > 0:
if S < 0:
cnt += abs(s) + 1
S = 1
else:
cnt += s + 1
S = -1
else:
S = s
answer.append(cnt)
print(min(answer))
else:
for i in range(1, N):
s = S + A[i]
if A[i] == 0:
if S > 0:
cnt += S + 1
S = -1
else:
cnt += abs(S) + 1
S = 1
else:
if s == 0:
if S < 0:
cnt += 1
S = 1
else:
cnt += 1
S = -1
else:
if S * s > 0:
if S < 0:
cnt += abs(s) + 1
S = 1
else:
cnt += s + 1
S = -1
else:
S = s
print(cnt)
if __name__ == "__main__":
main()
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | 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();
}
sc.close();
int ans = 0;
int tmp = 0;
for (int type = 0; type < 2; type++) {
ans = 0;
long wa = 0;
for (int i = 0; i < n; i++) {
wa += a[i];
if (i % 2 == type % 2) {
if (wa >= 0) {
ans += (wa + 1);
wa = -1;
}
} else {
if (wa <= 0) {
ans -= (wa - 1);
wa = 1;
}
}
}
if (type == 0)
tmp = ans;
}
System.out.println(Math.min(ans, tmp));
}
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": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
int main(void) {
int n;
std::cin >> n;
std::vector<long long int> a(n);
for (int i = 0; i < n; i++) {
std::cin >> a[i];
}
long long int count = 0;
long long int sum = a[0];
for (int i = 1; i < n; i++) {
if (sum > 0) {
sum += a[i];
if (sum >= 0) {
count += sum + 1;
sum = -1;
}
} else {
sum += a[i];
if (sum <= 0) {
count += -sum + 1;
sum = 1;
}
}
}
std::cout << count << std::endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | 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 (auto& ai : a) cin >> ai;
int c1 = 0;
int s1 = a[0];
if (s1 <= 0) {
c1 = -s1 + 1;
s1 = 1;
}
for (int i = (1); i < (N); ++i) {
if (s1 > 0) {
s1 += a[i];
if (s1 >= 0) {
c1 += s1 + 1;
s1 = -1;
}
} else {
s1 += a[i];
if (s1 <= 0) {
c1 += -s1 + 1;
s1 = 1;
}
}
}
int c2 = 0;
int s2 = a[0];
if (s2 >= 0) {
c2 = s2 + 1;
s2 = -1;
}
for (int i = (1); i < (N); ++i) {
if (s2 > 0) {
s2 += a[i];
if (s2 >= 0) {
c2 += s2 + 1;
s2 = -1;
}
} else {
s2 += a[i];
if (s2 <= 0) {
c2 += -s2 + 1;
s2 = 1;
}
}
}
if (c1 < c2) {
cout << c1 << endl;
} else {
cout << 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": []
} | IN-CORRECT | UNKNOWN | using System;
using static System.Console;
using static System.Math;
public class Hello{
public static void Main(){
int kazu = int.Parse(ReadLine());
string[] number = ReadLine().Split(' ');
long wa = 0;
long wa2 = 0;
int num0 = int.Parse(number[0]);
long count = 0;
long ans =0;
if(num0 == 0){
wa = 1;
wa2 = -1;
count++;
}else{
wa = num0;
}
for(int i=1;i<kazu;i++){
int numi = int.Parse(number[i]);
long temp = wa + numi;
if(wa > 0){
if(temp < 0){
wa = temp;
}else{
count += Abs(numi + wa) + 1;
wa = -1;
}
}else if(wa < 0){
if(temp > 0){
wa = temp;
}else{
count += Abs(numi + wa) + 1;
wa = 1;
}
}
}
ans = count;
if(wa2 != 0){
count = 1;
for(int i=1;i<kazu;i++){
int numi = int.Parse(number[i]);
long temp = wa2 + numi;
if(wa2 > 0){
if(temp < 0){
wa2 = temp;
}else{
count += Abs(numi + wa2) + 1;
wa2 = -1;
}
}else if(wa2 < 0){
if(temp > 0){
wa2 = temp;
}else{
count += Abs(numi + wa2) + 1;
wa2 = 1;
}
}
}
}
WriteLine(Min(count,ans));
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java |
import java.io.*;
import java.util.*;
import java.lang.*;
public class Main {
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 static boolean isPrintableChar(int c) {
return 33 <= c && c <= 126;
}
public boolean hasNext() {
while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;
return hasNextByte();
}
public String next() {
if (!hasNext()) throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while (isPrintableChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public long nextLong() {
if (!hasNext()) throw new NoSuchElementException();
long n = 0;
boolean minus = false;
int b = readByte();
if (b == '-') {
minus = true;
b = readByte();
}
if (b < '0' || '9' < b) {
throw new NumberFormatException();
}
while (true) {
if ('0' <= b && b <= '9') {
n *= 10;
n += b - '0';
} else if (b == -1 || !isPrintableChar(b)) {
return minus ? -n : n;
} else {
throw new NumberFormatException();
}
b = readByte();
}
}
public int nextInt() {
long nl = nextLong();
if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
return (int) nl;
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
public static void main(String[] args) {
FastScanner fs = new FastScanner();
int n = fs.nextInt();
long[] a = new long[n];
a[0] = fs.nextLong();
a[1] = fs.nextLong();
long ans = 0L;
long sum = 0L;
if (a[0] == 0 && a[1] > a[0]) {
sum = -1L;
ans += 1;
} else if (a[0] == 0 && a[1] < a[0]) {
sum = 1L;
ans += 1;
} else {
sum = a[0];
}
if (check(sum, sum + a[1])) {
sum += a[1];
} else {
if (sum > 0) {
ans += (sum + a[1] + 1L);
sum = -1L;
} else {
ans += (1L - sum - a[1]);
sum = 1L;
}
}
for (int i = 2; i < n; ++i) {
a[i] = fs.nextLong();
if (check(sum, sum + a[i])) {
sum += a[i];
} else {
if (sum > 0) {
ans += (sum + a[i] + 1L);
sum = -1L;
} else {
ans += (1L - sum - a[i]);
sum = 1L;
}
}
}
System.out.println(ans);
}
private static boolean check(long prev, long sum) {
if (prev > 0 && sum < 0) {
return true;
} else if (prev < 0 && sum > 0) {
return true;
}
return 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": []
} | IN-CORRECT | python3 | n=int(input())
a=list(map(int,input().split()))
st = 0
while a[st]==0:
st+=1
if (a[st]>0 and st%2==0) or (a[st]<0 and st%2==1):
fg=1
else:
fg=-1
ret = 0
tot = 0
for i in range(n):
tot+=a[i]
if tot*fg<=0:
ret += abs(fg - tot)
tot += fg - tot
fg*=-1
print(ret) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int solve(vector<int>& a, vector<int>& S, int signS0 = +1) {
int op = 0;
S[0] = a[0];
if (S[0] * signS0 <= 0) {
op = abs(S[0]) + 1;
S[0] = signS0;
}
for (size_t i = 1; i < a.size(); ++i) {
S[i] = S[i - 1] + a[i];
if (S[i] == 0) {
S[i] = -(S[i - 1] / abs(S[i - 1]));
++op;
} else {
if (S[i - 1] * S[i] > 0) {
op = op + abs(S[i]) + 1;
S[i] = -(S[i] / abs(S[i]));
}
}
}
return op;
}
int main() {
uint n;
cin >> n;
vector<int> a(n, 0);
vector<int> S(n, 0);
for (auto& x : a) cin >> x;
int op1 = solve(a, S, +1);
int op2 = solve(a, S, -1);
int op = min(op1, op2);
cout << op << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int posi(long long x) {
if (x > 0) return 1;
if (x < 0) return -1;
return 0;
}
int main() {
int N;
cin >> N;
vector<long long> a(N);
for (auto &i : a) cin >> i;
long long ans = 0, tmp = 0;
long long sum = a[0];
for (int i = 1; i < N; i++) {
if (posi(sum + a[i]) * posi(sum) == 1 || sum + a[i] == 0) {
tmp += abs(sum + a[i]) + 1;
sum = (sum > 0) ? -1 : 1;
} else
sum += a[i];
}
ans = tmp;
tmp = abs(a[0]) + 1;
sum = (a[0] > 0) ? -1 : 1;
for (int i = 1; i < N; i++) {
if (posi(sum + a[i]) * posi(sum) == 1 || sum + a[i] == 0) {
tmp += abs(sum + a[i]) + 1;
sum = (sum > 0) ? -1 : 1;
} else
sum += a[i];
}
ans = min(ans, tmp);
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int ar[100005];
bool flag = false;
int n, m;
int main() {
int n;
cin >> n;
int i;
long long int sum = 0;
cin >> ar[1];
int sgn = ar[1] > 0 ? 1 : -1;
sum += ar[1];
long long int cont = 0;
for (i = 2; i <= n; i++) {
cin >> ar[i];
sum += (long long int)ar[i];
if (sgn == 1) {
if (sum >= 0) {
cont += abs(sum + 1);
sum = -1;
}
sgn = -1;
} else if (sgn == -1) {
if (sum <= 0) {
cont += abs(-sum + 1);
sum = 1;
}
sgn = 1;
}
}
cout << cont << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <bits/stdc++.h>
int main(void) {
int n;
long sum1 = 0;
long sum2 = 0;
long tmp;
long lcount = 0;
long rcount = 0;
long a[100000];
char input[1000000];
int i = 0, j = 0;
int cp = 0, tcp = 0;
char tp[12];
tp[12] = '\0';
fgets(input, 1000000, stdin);
n = atoi(input);
fgets(input, 1000000, stdin);
for (i = 0; i < n; i++) {
while (input[cp] != ' ' && input[cp] != '\n') {
tp[tcp] = input[cp];
tcp++;
cp++;
}
tp[tcp] = '\0';
tcp = 0;
cp++;
a[i] = atoi(tp);
}
tmp = a[0];
for (i = 1; i < n; i++) {
if (i % 2 == 0) {
tmp += a[i];
if (tmp > -1) {
lcount += tmp + 1;
tmp = -1;
}
} else {
tmp += a[i];
if (tmp < 1) {
lcount += 1 - tmp;
tmp = 1;
}
}
}
tmp = a[0];
for (i = 1; i < n; i++) {
if (i % 2 == 1) {
tmp += a[i];
if (tmp > -1) {
rcount += tmp + 1;
tmp = -1;
}
} else {
tmp += a[i];
if (tmp < 1) {
rcount += 1 - tmp;
tmp = 1;
}
}
}
printf("%ld\n", lcount > rcount ? rcount : lcount);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
template <class T>
void chmin(T& a, const T& b) {
a = min(a, b);
}
template <class T>
void chmax(T& a, const T& b) {
a = max(a, b);
}
const double EPS = 1e-9;
const long INF = 999999999;
const long MOD = 1000000007;
int dy[] = {0, 0, 1, -1, 1, 1, -1, -1};
int dx[] = {1, -1, 0, 0, 1, -1, -1, 1};
bool check(int sum) {
if (sum > 0)
return true;
else
return false;
}
int main() {
int n, A[100001], sum = 0, counter = 0;
bool plus = false;
cin >> n;
for (int i = 0; i < (n); i++) cin >> A[i];
sum += A[0];
plus = check(sum);
for (int i = (1); i < (n); i++) {
sum += A[i];
if (sum == 0) {
if (plus)
sum = -1;
else
sum = 1;
counter++;
} else {
if (sum > 0 && plus) {
counter += (1 + sum);
sum = -1;
} else if (sum < 0 && plus == false) {
counter += (1 - sum);
sum = 1;
}
}
plus = !plus;
}
printf("%d\n", counter);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | fun main(args: Array<String>) {
val N = nextInt()
val A = listOfLong()
val B = A.toMutableList()
var sum = if (A[0] != 0L) A[0] else 1
var sign = sum.sign()
fun resolv(init: Long = 0L): Long {
var ans = init
for (n in 1 until N) {
val now = sum + A[n]
when (now.sign()) {
1 ->
if (sign != -1) {
ans += (1 + now)
sum = -1
} else sum = now
-1 ->
if (sign != 1) {
ans += (1 - now)
sum = 1
} else sum = now
0 ->
if (sign == 1) {
ans += 1
sum = -1
} else {
ans += 1
sum = 1
}
}
sign = sum.sign()
}
return ans
}
val ans1 = resolv() + if (A[0] != 0L) 0 else 1
//System.err.println(ans1)
sum = if (A[0] != 0L) A[0] else -1
sign = sum.sign()
val ans2 =
if (sign == -1) {
sum = 1
sign = 1
resolv((1 - A[0]))
} else {
sum = -1
sign = -1
resolv((1 + A[0]))
} + if (A[0] != 0L) 0 else 1
println(Math.min(ans1, ans2))
}
fun Long.sign() = if (this > 0) 1 else if (this < 0) -1 else 0
//32988396395189 ac
//32988396395187
fun next() = readLine()!!
fun nextInt(delta: Int = 0) = Integer.parseInt(next()) + delta
fun listOfString() = next().split(" ")
fun listOfInt() = listOfString().map(String::toInt)
fun listOfLong() = listOfString().map(String::toLong)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 |
def check1(a):
sum = 0
for i in range(len(a)):
if(i % 2 == 0):
sum += a[i]
if(sum >= 0):
return (i, -1)
else:
sum += a[i]
if(sum <= 0):
return (i, 1)
return True
def check2(a):
sum = 0
for i in range(len(a)):
if(i % 2 == 0):
sum += a[i]
if(sum <= 0):
return (i, 1)
else:
sum += a[i]
if(sum >= 0):
return (i, -1)
return True
n = input()
b = input().split()
a = [int(b[i]) for i in range(len(b))]
a2 = list(a)
ans1 = 0
ans2 = 0
while(True):
c = check1(a)
if(c == True):
break
a[c[0]] += c[1]
ans1 += 1
while(True):
c = check2(a2)
if(check2(a2) == True):
break
a2[c[0]] += c[1]
ans2 += 1
print(ans1) if(ans1<ans2) else print(ans2) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
void fnInput(vector<int>& rvnNum) {
int nSize;
cin >> nSize;
rvnNum.resize(nSize);
for (int& rnElm : rvnNum) cin >> rnElm;
}
int fnSignChgTimes(const vector<int>& cnrvnNum) {
vector<int64_t> vnTimes(2);
for (int nParity = 0; nParity < vnTimes.size(); nParity++) {
int64_t nTimes = 0;
int64_t nSum = 0;
for (int n = 0; n < cnrvnNum.size(); n++) {
nSum += cnrvnNum[n];
if (n % 2 == nParity)
if (nSum > 0)
;
else {
nTimes += 1 - nSum;
nSum = 1;
}
else if (nSum >= 0) {
nTimes += 1 + nSum;
nSum = -1;
} else
;
}
vnTimes[nParity] = nTimes;
}
auto itElm = min_element(begin(vnTimes), end(vnTimes));
return *itElm;
}
int main() {
vector<int> vnNum;
fnInput(vnNum);
cout << fnSignChgTimes(vnNum) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | cost=0
n=int(input())
a=list(map(int,input().split()))
sums=a[0]
if a[0]==0:
if a[1]>0:
a[0]=-1
else:
a[0]=1
cost+=1
for i in range(1,n):
m=sums+a[i]
if m==0:
if i==n-1:
cost+=1
break
else:
if sums>0:
m=-1
cost+=1
else:
m=1
cost+=1
else:
if m*sums>0:
if sums>0:
cost+=m+1
m=-1
else:
cost+=abs(m)+1
m=1
sums=m
print(cost) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int N;
int64_t cont = 0, ans = INT_MAX;
;
void dfs(bool odd, int64_t sum, int i, vector<int64_t> &a) {
if (i == N) {
ans = min(ans, cont);
cont = 0;
return;
}
if (odd) {
if (a[i] + sum <= 0) {
cont += abs(1 - sum - a[i]);
sum = 1;
} else {
sum += a[i];
}
dfs(false, sum, i + 1, a);
} else {
if (a[i] + sum >= 0) {
cont += abs(-1 - sum - a[i]);
sum = -1;
} else {
sum += a[i];
}
dfs(true, sum, i + 1, a);
}
}
int main() {
cin >> N;
vector<int64_t> a(N);
for (int i = 0; i < N; i++) {
cin >> a.at(i);
}
dfs(true, 0, 0, a);
dfs(false, 0, 0, a);
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int MX = 100005, INF = 1001001001;
const long long int LINF = 1e18;
const double eps = 1e-10;
int n;
vector<int> a;
long long int ansp = 0;
long long int ansm = 0;
int main() {
cin >> n;
int inp;
for (int i = 0; i < n; i++) {
cin >> inp;
a.push_back(inp);
}
long long int sum = 0;
for (int i = 0; i < (n); ++i) {
sum += a[i];
if (i % 2 == 1) {
while (sum >= 0) {
sum++;
ansm++;
}
} else {
while (sum <= 0) {
sum--;
ansm++;
}
}
}
sum = 0;
for (int i = 0; i < (n); ++i) {
sum += a[i];
if (i % 2 == 0) {
while (sum >= 0) {
sum++;
ansp++;
}
} else {
while (sum <= 0) {
sum--;
ansp++;
}
}
}
cout << min(ansm, ansp) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
bool should_continue(int p_num, int n_sum) {
if (p_num < 0 && n_sum > 0) {
return true;
}
if (p_num > 0 && n_sum < 0) {
return true;
}
return false;
}
int main() {
int n;
scanf("%d", &n);
int a[n];
for (int i = 0; i < n; i++) {
scanf("%d", a + i);
}
int p_sum = a[0];
int n_sum = a[0] + a[1];
int i = 2;
int cnt = 0;
while (true) {
if (should_continue(p_sum, n_sum)) {
if (i == n) {
break;
}
p_sum = n_sum;
n_sum += a[i];
++i;
continue;
}
if (p_sum == 0) {
if (n_sum > 0) {
--p_sum;
--n_sum;
} else {
++p_sum;
++n_sum;
}
} else if (p_sum < 0) {
++n_sum;
} else {
--n_sum;
}
++cnt;
}
printf("%d", cnt);
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = [int(i) for i in input().split()]
count1 = 0
count2 = 0
sum = 0
for i in range(0,n-1):
if i % 2 == 0:
if sum + a[i] > 0:
continue
elif sum + a[i] <= 0:
count1 += abs(1 - sum -a[i])
sum = 1
if i % 2 == 1:
if sum + a[i] < 0:
continue
else:
count1 += abs(-1 - sum - a[i])
sum = -1
sum = 0
for i in range(0,n-1):
if i % 2 == 1:
if sum + a[i] > 0:
continue
elif sum + a[i] <= 0:
count2 += abs(1 - sum -a[i])
sum = 1
if i % 2 == 0:
if sum + a[i] < 0:
continue
else:
count2 += abs(-1 - sum - a[i])
sum = -1
if count1 >= count2:
print(count1)
else:
print(count2) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[]) {
long long int n, ans = 0, sum = 0;
cin >> n;
vector<long long int> a(n);
for (size_t i = 0; i < n; i++) {
cin >> a[i];
if (i == 0)
sum += a[i];
else {
if (sum * (sum + a[i]) > 0) {
if (sum < 0) {
ans += abs(1 - (sum + a[i]));
a[i] += abs(1 - (sum + a[i]));
} else if (sum > 0) {
ans += abs(-1 - (sum + a[i]));
a[i] += -1 - (sum + a[i]);
}
}
if (sum + a[i] == 0) {
if (sum > 0)
a[i]--;
else
a[i]++;
ans++;
}
sum += a[i];
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void) {
long int n, i, j, sum = 0, count, cost1 = 0, cost2 = 0;
cin >> n;
vector<long int> a(n);
for (i = 0; i < n; i++) {
cin >> a[i];
}
if (a[0] < 0) {
cost1 += abs(a[0]) + 1;
sum = 1;
} else {
sum += a[0];
}
for (i = 1; i < n; i++) {
sum += a[i];
if (sum <= 0 && i % 2 == 0) {
cost1 += abs(sum) + 1;
sum = 1;
}
if (sum >= 0 && i % 2 == 1) {
cost1 += abs(sum) + 1;
sum = -1;
}
}
sum = 0;
if (a[0] >= 0) {
cost2 += abs(a[0]) + 1;
a[0] = -1;
sum = -1;
} else
sum += a[0];
for (i = 1; i < n; i++) {
sum += a[i];
if (sum >= 0 && i % 2 == 0) {
cost2 += abs(sum) + 1;
sum = -1;
}
if (sum <= 0 && i % 2 == 1) {
cost2 += abs(sum) + 1;
sum = 1;
}
}
cout << min(cost1, cost2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class Main {
public static void main(String args[]) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
Solver solver = new Solver();
solver.init();
solver.readHead(in.readLine());
for (; solver.hasNext();) {
solver.readBody(in.readLine());
}
}
}
class Solver {
int N;
int cnt;
public void init() {
N = 0;
cnt = 0;
}
public void readHead(String str) {
String[] strArr = str.split(" ");
N = Integer.parseInt(strArr[0]);
// System.out.println(N);
}
public boolean hasNext() {
return cnt < 1;
}
int[] A;
public void readBody(String str) {
// System.out.println(str);]
String[] strArr = str.split(" ");
A = new int[N];
for (int i = 0; i < N; i++)
A[i] = Integer.parseInt(strArr[i]);
if (A[0] != 0) {
System.out.println(cnt());
} else {
A[0] = 1;
long cnt1 = cnt() + 1;
A[0] = -1;
long cnt2 = cnt() + 1;
if (cnt1 < cnt2)
System.out.println(cnt1);
else
System.out.println(cnt2);
}
cnt++;
}
private long cnt() {
int s = 1;
if (A[0] < 0)
s = -1;
long ans = 0;
int sum = A[0];
for (int i = 1; i < N; i++) {
s = s * -1;
sum = sum + A[i];
if (s == -1 && sum >= 0) {
ans += (sum + 1);
sum = -1;
} else if (s == 1 && sum <= 0) {
ans += (-sum + 1);
sum = 1;
}
}
return ans;
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
for (int i = (0); i < (n); ++i) cin >> a[i];
long long ans = 0;
long long sum = a[0];
for (int i = (1); i < (n); ++i) {
int sign = int(abs(sum)) / sum;
if (sign * (sum + a[i]) < 0)
sum += a[i];
else {
ans += 1 + sign * (sum + a[i]);
sum = -sign;
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n=int(input())
a=list(map(int,input().split()))
b=[]
for i in range(n):
b.append(a[i])
ct1=0
if a[0]<=0:
ct1+=1-a[0]
a[0]=1
x=a[0]
for i in range(1,n):
y=x+a[i]
if i%2==1:
if y>=0:
ct1+=y+1
a[i]=-x-1
else:
if y<=0:
ct1+=1-y
a[i]=-x+1
x+=a[i]
ct2=0
if b[0]>=0:
ct2+=b[0]-1
b[0]=-1
x=b[0]
for i in range(1,n):
y=x+b[i]
if i%2==0:
if y>=0:
ct2+=y+1
b[i]=-x-1
else:
if y<=0:
ct2+=1-y
b[i]=-x+1
x+=b[i]
print(min(ct1,ct2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include<iostream>
#include<algorithm>
#include<math.h>
#include<string>
#include<tuple>
#include<vector>
#include<cstdlib>
#include<cstdint>
#include<stdio.h>
#include<cmath>
#include<limits>
#include<iomanip>
#include<ctime>
#include<climits>
#include<random>
#include<queue>
#include<map>
using namespace std;
template <class T> using V = vector<T>;
template<class T> inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template<class T> inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
const long long INF = 1LL << 60;
const double pi=acos(-1);
using ll = long long;
using db = long double;
using st = string;
using ch = char;
using vll = V<ll>;
using vpll =V<pair<ll,ll>>;
using vst = V<st>;
using vdb = V<db>;
using vch = V<ch>;
using graph = V<V<int>>;
using pq = priority_queue<ll>;
#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
#define bgn begin()
#define en end()
#define SORT(a) sort((a).bgn,(a).en)
#define REV(a) reverse((a).bgn,(a).en)
#define fi first
#define se second
#define sz size()
#define gcd(a,b) __gcd(a,b)
#define pb(a) push_back(a);
#define ALL(a) (a).begin(),(a).end()
ll Sum(ll n) {
ll m=0;
while(n){
m+=n%10;
n/=10;
}
return m;
}
const int MAX = 510000;
// change
const int MOD = 1000000007;
long long fac[MAX], finv[MAX], inv[MAX];
void Comuse() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++){
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
#define comuse Comuse()
ll combi(int n, int k){
if (n < k) return 0;
if (n < 0 || k < 0) return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
ll perm(int n,int k){
if(n < k) return 0;
if(n < 0 || k < 0) return 0;
return fac[n] * (finv[k] % MOD) % MOD;
}
ll modpow(ll a,ll n,ll mod){
ll ans=1;
while(n>0){
if(n&1){
ans=ans*a%mod;
}
a=a*a%mod;
n>>=1;
}
return ans;
}
ll modinv(ll a, ll mod) {
return modpow(a, mod - 2, mod);
}
ll modcombi(int n,int k,int mod){
ll ans=1;
for(ll i=n;i>n-k;i--){
ans*=i;
ans%=mod;
}
for(ll i=1;i<=k;i++){
ans*=modinv(i,mod);
ans%=mod;
}
return ans;
}
ll lcm(ll a,ll b){
ll n;
n=a/gcd(a,b)*b;
return n;
}
vll div(ll n){
vll ret;
for(ll i=1;i*i<=n;i++){
if(n%i==0){
ret.push_back(i);
if(i*i!=n){
ret.push_back(n/i);
}
}
}
SORT(ret);
return (ret);
}
vector<bool> isprime(MAX+100,true);
void primeuse(){
isprime[0]=false;
isprime[1]=false;
for(int i=2;i<MAX+50;i++){
int up=sqrt(i)+1;
for(int j=2;j<up;j++){
if(i%j==0){
isprime[i]=false;
}
}
}
}
void bf(ll n,string s){
for(ll i=0;i<n;i++){
cout<<s;
}
cout<<"\n";
}
void Solve();
const int MAX_N = 131072;
//segment tree
int NN;
int seg[MAX_N*2-1];
void seguse(){
for(int i=0;i<2*NN-1;i++){
seg[i]=INT_MAX;
}
}
signed main(){
cin.tie(0);
ios::sync_with_stdio(false);
cout<<setprecision(20)<<fixed;
Solve();
}
/****************************************\
| Thank you for viewing my code:) |
| Written by RedSpica a.k.a. RanseMirage |
| Twitter:@asakaakasaka |
\****************************************/
//segtreeの葉の先頭の添え字はN-1
void Solve(){
ll n;
cin>>n;
vll A(n);
vll B(n);
FOR(i,0,n){
cin>>A[i];
}
ll ans=0;
ll all=A[0];
bool can=true;
FOR(i,1,n){
if((all+A[i])*all>=0){
can=false;
break;
}
}
if(can){
cout<<"0\n";
return;
}
B[0]=A[0];
if(A[0]==0){
ans++;
if(A[1]>0){
B[0]=-1;
}
else if(A[1]<0){
B[0]=1;
}
}
FOR(i,1,n){
B[i]=B[i-1]+A[i];
if(B[i]*B[i-1]<0){
continue;
}
ans+=abs(B[i])+1;
if(B[i-1]<0){
B[i]=1;
}
else{
B[i]=-1;
}
}
cout<<ans<<"\n";
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n=int(input())
a=list(map(int,input().split()))
sm=a[0]
ans=[0, 0]
for i in range(n-1):
sm1=sm+a[i+1]
if sm*sm1>=0:
ans[0]+=abs(sm1)+1
if sm>0:
sm=-1
else:
sm=1
else:
sm=sm1
sm=-1
ans[1]+=abs(a[0])+1
for i in range(n-1):
sm1=sm+a[i+1]
if sm*sm1>=0:
ans[1]+=abs(sm1)+1
if sm>0:
sm=-1
else:
sm=1
else:
sm=sm1
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": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long MOD = 1000000007;
const int MAX_N = 100000005;
int n;
int a[MAX_N];
int check(long long sum, long long ans) {
for (int i = 1; i < n; i++) {
long long t = sum + a[i];
if ((sum >= 0 && t < 0) || (sum < 0 && t >= 0)) {
sum = t;
if (sum == 0) {
sum = 1;
ans++;
}
continue;
}
long long at;
if (sum >= 0)
at = -1 - sum;
else
at = 1 - sum;
ans = ans + abs(a[i] - at);
sum = sum + at;
}
return ans;
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long another;
if (a[0] >= 0)
another = -1;
else
another = 1;
long long a1 = check(a[0], 0);
long long a2 = check(another, abs(a[0] - another));
cout << min(a1, a2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
A = list(map(int,input().split()))
def func(list1,op,acc = 0,count = 0):
for i in range(n):
acc += list1[i]
if i % 2 == 1 and acc * op >= 0:
count += op * acc + 1
acc = -op
elif i % 2 == 0 and acc * op <= 0:
count += -op * acc + 1
acc = op
if acc == 0:
count += 1
return count
if A[0] > 0:
ans = func(A,1)
elif A[0] < 0:
ans = func(A,-1)
elif A[0] == 0:
ans = min(func(A,1),func(A,-1))
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int n, a, ans0 = 0, ans1 = 0, sum0 = 0, sum1 = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a;
sum0 += a;
sum1 += a;
if (i % 2 == 0) {
if (sum0 <= 0) {
ans0 += 1 - sum0;
sum0 = 1;
}
if (sum1 >= 0) {
ans1 += sum1 + 1;
sum1 = -1;
}
} else {
if (sum1 <= 0) {
ans1 += 1 - sum1;
sum1 = 1;
}
if (sum0 >= 0) {
ans0 += sum0 + 1;
sum0 = -1;
}
}
}
cout << min(ans0, ans1) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python2 | n = int(raw_input())
a = map(int, raw_input().split())
count = 0
if a[0] == 0:
count += 1
if 0 < a[1]:
a[0] = 1
else:
a[0] = -1
SUM = a[0]
for i in range(1, n):
SUM_next = SUM + a[i]
if 0 <= SUM * SUM_next:
if 0 < SUM:
a[i] -= (SUM_next + 1)
count += (SUM_next + 1)
else:
a[i] += (-SUM_next + 1)
count += (-SUM_next + 1)
SUM = SUM_next
print(count) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
template <typename T>
void remove(std::vector<T>& vector, unsigned int index) {
vector.erase(vector.begin() + index);
}
long long INF = 1000000000000000000;
long long MOD = 1e9 + 7;
int main() {
long long N;
cin >> N;
vector<long long> A(N), sum(N + 1), sum2(N + 1);
sum[0] = 0;
for (int i = 0; i < N; i++) {
cin >> A[i];
sum[i + 1] = sum[i] + A[i];
sum2[i + 1] = sum[i + 1];
}
long long ans = 0;
long long memo1 = 0, memo2 = 0;
long long cnt = 0;
for (int i = 1; i <= N; i++) {
if (i == 1) {
if (sum[i] < 0) {
cnt += abs(sum[i] - 1);
memo1 += abs(sum[i] - 1);
sum[i] = 1;
}
} else {
sum[i] += cnt;
if (i % 2 == 0) {
if (sum[i] > 0) {
cnt -= abs(sum[i] + 1);
memo1 += abs(sum[i] + 1);
sum[i] = -1;
} else if (sum[i] == 0) {
cnt--;
memo1++;
sum[i] = -1;
}
} else {
if (sum[i] < 0) {
cnt += abs(sum[i] - 1);
memo1 += abs(sum[i] - 1);
sum[i] = 1;
} else if (sum[i] == 0) {
cnt++;
memo1++;
sum[i] = 1;
}
}
}
}
for (int i = 1; i <= N; i++) {
if (i == 1) {
if (sum2[i] > 0) {
cnt -= abs(sum2[i] + 1);
memo2 += abs(sum2[i] + 1);
sum2[i] = -1;
}
} else {
sum2[i] += cnt;
if (i % 2 == 0) {
if (sum2[i] < 0) {
cnt += abs(sum2[i] - 1);
memo2 += abs(sum2[i] - 1);
sum2[i] = 1;
} else if (sum[i] == 0) {
cnt++;
memo2++;
sum2[i] = 1;
}
} else {
if (sum2[i] > 0) {
cnt -= abs(sum2[i] + 1);
memo2 += abs(sum2[i] + 1);
sum2[i] = 1;
} else if (sum2[i] == 0) {
cnt--;
memo2++;
sum2[i] = 1;
}
}
}
}
cout << min(memo1, memo2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main {
private static final int MOD = (int)Math.pow(10, 9);
public static void main(String[] args) {
FastReader sc = new FastReader();
int n = sc.nextInt();
int[] nums = new int[n];
int[] prefix = new int[n];
for (int i = 0; i < nums.length; i++) {
nums[i] = sc.nextInt();
}
prefix[0] = nums[0];
// Compute prefix sum
for (int i = 1; i < prefix.length; i++) {
prefix[i] = nums[i] + prefix[i-1];
}
int firstDigitPostive = 0;
int prefixSum = 0;
for (int i = 0; i < nums.length; i++) {
prefixSum += nums[i];
if (i % 2 == 0) {
// odd is postive
if (prefixSum == 0) {
prefixSum--;
firstDigitPostive++;
} else if (prefixSum < 0) {
firstDigitPostive += (1 - prefixSum);
prefixSum += 1 - prefixSum;
}
} else {
// even is negative
if (prefixSum == 0) {
prefixSum++;
} else if (prefixSum > 0) {
firstDigitPostive += (1 + prefixSum);
prefixSum -= (prefixSum + 1) ;
}
}
}
prefixSum = 0;
int firstDigitNegative = 0;
for (int i = 0; i < nums.length; i++) {
prefixSum += nums[i];
if (i % 2 == 0) {
// odd is negative
if (prefixSum == 0) {
prefixSum++;
} else if (prefixSum > 0) {
firstDigitNegative += (1 + prefixSum);
prefixSum -= (prefixSum + 1) ;
}
} else {
// even is postive
if (prefixSum == 0) {
prefixSum--;
firstDigitNegative++;
} else if (prefixSum < 0) {
firstDigitNegative += (1 - prefixSum);
prefixSum += 1 - prefixSum;
}
}
}
System.out.println(Math.min(firstDigitPostive, firstDigitNegative));
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try{
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
enum State { Plus, Minus, Zero };
State GetState(int sum) {
State state;
if (sum > 0)
state = Plus;
else if (sum == 0)
state = Zero;
else
state = Minus;
return state;
}
int main() {
int n;
cin >> n;
vector<int> a(n);
cin >> a[0];
int count = 0;
State state = GetState(a[0]);
if (state == Zero) {
a[0] = 1;
state = Plus;
count++;
}
int sum = a[0];
for (int i = 1; i < n; i++) {
cin >> a[i];
State nextState = GetState(sum + a[i]);
switch (nextState) {
case Plus:
if (state == Plus) {
int bf_a = a[i];
a[i] = -1 - sum;
count += abs(a[i] - bf_a);
nextState = Minus;
}
break;
case Minus:
if (state == Minus) {
int bf_a = a[i];
a[i] = 1 - sum;
count += abs(a[i] - bf_a);
nextState = Plus;
}
break;
case Zero:
if (state == Plus) {
int bf_a = a[i];
a[i] = -1 - sum;
count += abs(a[i] - bf_a);
nextState = Minus;
} else if (state == Minus) {
int bf_a = a[i];
a[i] = 1 - sum;
count += abs(a[i] - bf_a);
nextState = Plus;
}
default:
break;
}
sum += a[i];
state = nextState;
}
if (sum == 0) count++;
cout << count << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long a[100000];
long n;
long long s[100000];
long long s2[100000];
long long ans = 0;
long long ans2 = 0;
scanf("%ld", &n);
for (long i = 0; i < n; i++) {
scanf("%lld", &a[i]);
}
s[0] = a[0];
for (long i = 1; i < n; i++) {
s[i] = s[i - 1] + a[i];
if ((s[i - 1] > 0 && s[i] < 0) || (s[i - 1] < 0 && s[i] > 0)) {
continue;
} else {
ans += abs((-s[i - 1] / abs(s[i - 1])) - s[i]);
s[i] = -s[i - 1] / abs(s[i - 1]);
}
}
s2[0] = -a[0] / abs(a[0]);
for (long i = 1; i < n; i++) {
s2[i] = s2[i - 1] + a[i];
if ((s2[i - 1] > 0 && s2[i] < 0) || (s2[i - 1] < 0 && s2[i] > 0)) {
continue;
} else {
ans2 += abs((-s2[i - 1] / abs(s2[i - 1])) - s2[i]);
s2[i] = -s2[i - 1] / abs(s2[i - 1]);
}
}
if (ans2 < ans) ans = ans2;
printf("%lld\n", ans);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
b = [int(x) for x in input().split()]
a = list()
temp = 0
count1 = 0
count2 = 0
a = b.copy()
if a[0] == 0:
a[0] = 1
count1 = 1
sum = a[0]
for i in range(1, n):
if abs(a[i]) <= abs(sum) or a[i] * sum >= 0:
if sum > 0:
temp = -1 * abs(sum) - 1
count1 += abs(temp - a[i])
else:
temp = abs(sum) + 1
count1 += abs(temp - a[i])
a[i] = temp
sum += a[i]
a = b.copy()
count2 = abs(a[0]) + 1
if a[0] == 0:
a[0] = 1
count1 = 1
if a[0] > 0:
a[0] = -1
else:
a[0] = 1
sum = a[0]
for i in range(1, n):
if abs(a[i]) <= abs(sum) or a[i] * sum >= 0:
count2 += abs(sum - a[i]) + 1
if sum > 0:
temp = -1 * abs(sum) - 1
count2 += abs(temp - a[i])
else:
temp = abs(sum) + 1
count2 += abs(temp - a[i])
a[i] = temp
sum += a[i]
print(min(count1, count2))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<map>
#include<cstdio>
#include<stack>
#include<queue>
#include<fstream>
#include<cstdio>
using namespace std;
#define ok1 printf("ok1\n");
#define ok2 printf("ok2\n");
#define M 1000000
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,s,n) for(int i=(s);i<(n);i++)
#define repr(i,n) for(int i=n-1;i>=0;i--)
#define REPR(i,s,n) for(int i=(s);i>=(g);(i)--)
#define all(a) (a).begin(),(a).end()
#define reall(a) (a).rbegin(),(a).rend()
#define pb push_back
#define MIN(a,b) a=min((a),(b))
#define MAX(a,b) a=max((a),(b))
#define SIZE(v) (int)v.size()
typedef vector<int> vi;
typedef vector<string> vs;
typedef long long ll;
typedef vector<ll> vll;
#ifdef TESTING
#define DEBUG fprintf(stderr,"====TESTING====\n")
#define VALUE(x) cerr << "The value of " << #x << " is " << x << endl
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define DEBUG
#define VALUE(x)
#define debug(...)
#endif
inline string IntToString(ll a) {
char x[100];
sprintf_s(x, "%lld", a); string s = x;
return s;
}
inline ll StringToInt(string a) {
char x[100]; ll res;
strcpy_s(x, a.c_str()); sscanf_s(x, "%lld", &res);
return res;
}
inline string uppercase(string s) {
int n = SIZE(s);
rep(i, n) if (s[i] >= 'a' && s[i] <= 'z') s[i] = s[i] - 'a' + 'A';
return s;
}
inline string lowercase(string s) {
int n = SIZE(s);
rep(i, n) if (s[i] >= 'A' && s[i] <= 'Z') s[i] = s[i] - 'A' + 'a';
return s;
}
int n;
ll d[100005];
ll solve() {
ll nans = 0;
ll nowsum = 0;
rep(i, n) {
if (i % 2 == 0) {
if (nowsum + d[i] <= 0) {
nans += llabs(nowsum + d[i] - 1);
nowsum = 1;
}
else {
nowsum += d[i];
}
}
else {
if (nowsum + d[i] >= 0){
nans += llabs(nowsum + d[i] - (-1));
nowsum = -1;
}
else
nowsum += d[i];
}
//cout << nans << endl;
}
return nans;
}
int main()
{
cin >> n;
rep(i, n) {
cin >> d[i];
}
ll ans = solve();
rep(i, n) d[i] *= -1;
ans = MIN(ans, solve());
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
t = a[0]
r, tmp, count = 0, 0, 0
for i in range(1, n):
tmp = t + a[i]
if t < 0 and tmp < 0:
r = 1 - tmp
elif t > 0 and tmp > 0:
r = -tmp - 1
elif tmp == 0:
if t < 0:
r += 1 - t - a[i]
else:
r += -1 - t - a[i]
else:
r = 0
count += abs(r)
t = tmp + r
print(count) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.function.IntFunction;
import java.util.function.Supplier;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner();
int n=scanner.nextInt();
long[] a=new long[n+1];
for(int i=1;i<=n;i++){
a[i]=scanner.nextInt();
}
Arrays.parallelPrefix(a,(c,b)->c+b);
//put(Arrays.toString(a));
long ruiseki=1-a[1];
long ans=Math.abs(ruiseki);
for(int i=2;i<=n;i++){
//put(format("i=%d",i));
//put(format("ruiseki=%d",ruiseki));
long val=a[i]+ruiseki;
long val_=a[i-1]+ruiseki;
//put(format("val=%d",val));
//put(format("val_=%d",val_));
if(val==0){
int bit=Long.signum(val_);
ruiseki+=-bit;
ans+=Math.abs(bit);
}else if(val>0&&val_>0){
ruiseki-=(val+1);
ans+=Math.abs(val+1);
}else if(val<0&&val_<0){
ruiseki+=Math.abs(val)+1;
ans+=Math.abs(val)+1;
}
//put(ans);
//put();
}
long min=ans;
ruiseki=-1-a[1];
ans=Math.abs(ruiseki);
for(int i=2;i<=n;i++){
//put(format("i=%d",i));
//put(format("ruiseki=%d",ruiseki));
long val=a[i]+ruiseki;
long val_=a[i-1]+ruiseki;
//put(format("val=%d",val));
//put(format("val_=%d",val_));
if(val==0){
int bit=Long.signum(val_);
ruiseki+=-bit;
ans+=Math.abs(bit);
}else if(val>0&&val_>0){
ruiseki-=(val+1);
ans+=Math.abs(val+1);
}else if(val<0&&val_<0){
ruiseki+=Math.abs(val)+1;
ans+=Math.abs(val)+1;
}
//put(ans);
//put();
}
put(Math.min(min,ans));
}
public static void print(Object object){
System.out.print(object);
}
public static void put(Object object) {
System.out.println(object);
}
public static void put(){
System.out.println();
}
public static String format(String string, Object... args) {
return String.format(string, args);
}
}
final class Scanner {
private final InputStream in = System.in;
private final byte[] buffer = new byte[1024];
private int ptr = 0;
private int buflen = 0;
private boolean hasNextByte() {
if (ptr < buflen) {
return true;
} else {
ptr = 0;
try {
buflen = in.read(buffer);
} catch (IOException e) {
e.printStackTrace();
}
if (buflen <= 0) {
return false;
}
}
return true;
}
private int readByte() {
if (hasNextByte())
return buffer[ptr++];
else
return -1;
}
private boolean isPrintableChar(int c) {
return 33 <= c && c <= 126;
}
public boolean hasNext() {
while (hasNextByte() && !isPrintableChar(buffer[ptr]))
ptr++;
return hasNextByte();
}
public String next() {
if (!hasNext())
throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while (isPrintableChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public long nextLong() {
if (!hasNext())
throw new NoSuchElementException();
long n = 0;
boolean minus = false;
int b = readByte();
if (b == '-') {
minus = true;
b = readByte();
}
if (b < '0' || '9' < b) {
throw new NumberFormatException();
}
while (true) {
if ('0' <= b && b <= '9') {
n *= 10;
n += b - '0';
} else if (b == -1 || !isPrintableChar(b)) {
return minus ? -n : n;
} else {
throw new NumberFormatException();
}
b = readByte();
}
}
public int nextInt() {
long nl = nextLong();
if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE)
throw new NumberFormatException();
return (int) nl;
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
final class Pair {
final public int x, y;
Pair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int hashCode() {
return x+y;
}
@Override
public boolean equals(Object obj) {
boolean result=super.equals(obj);
if(obj.getClass()!=this.getClass()){
return false;
}
Pair pair=(Pair)obj;
if(this.x==pair.x&&this.y==pair.y) return true;
return false;
}
@Override
public String toString() {
return String.format("(%d,%d)", x, y);
}
}
final class Tuple<T,V>{
//immutabl1でないことに注意(T,Vがmutableの場合変更可能)
final public T t;
final public V v;
Tuple(T t,V v){
this.t=t;
this.v=v;
}
@Override
public int hashCode() {
return (t.hashCode()+v.hashCode());
}
@Override
public boolean equals(Object obj) {
if(obj.getClass()!=this.getClass()){
return false;
}
Tuple<T,V> tuple=(Tuple)obj;
return tuple.t.equals(this.t)&&tuple.v.equals(this.v);
}
@Override
public String toString() {
return String.format("<Tuple>=<%s,%s>",t,v);
}
}
final class LowerBoundComparator<T extends Comparable<? super T>>
implements Comparator<T>
{
public int compare(T x, T y)
{
return (x.compareTo(y) >= 0) ? 1 : -1;
}
}
final class UpperBoundComparator<T extends Comparable<? super T>>
implements Comparator<T>
{
public int compare(T x, T y)
{
return (x.compareTo(y) > 0) ? 1 : -1;
}
}
final class Util {
static long gcd(long a,long b){
if(a%b==0)return b;
return gcd(b,a%b);
}
static long lcm(long a,long b){
long gcd=gcd(a,b);
long result=b/gcd;
return a*result;
}
static long kaijoMod(int n,int mod){
if(n<1) return -1;
long result=1;
for(int i=n;i>1;i--){
result*=i;
result%=mod;
}
return result;
}
static <T extends Comparable> Map<T,Integer> count(List<T> list){
//副作用
Collections.sort(list);
Map<T,Integer> result=new HashMap<>();
int l=0,r=0;
while(l<list.size()){
while(r<list.size()-1&&list.get(r).equals(r+1)){
r++;
}
result.put(list.get(r),r-l+1);
r++;
l=r;
}
return result;
}
static Map<Integer,Integer> count(int[] array){
//副作用
Arrays.sort(array);
Map<Integer,Integer> result=new HashMap<>();
int l=0,r=0;
while(l<array.length){
while(r<array.length-1&&array[r]==array[r+1]){
r++;
}
result.put(array[l],r-l+1);
r++;
l=r;
}
return result;
}
static String toStringBWS(Iterable iterable){
Iterator ite=iterable.iterator();
return toStringBWS(ite);
}
static String toStringBWS(Iterator ite){
StringBuilder sb=new StringBuilder();
sb.append(ite.next());
while(ite.hasNext()){
sb.append(" ");
sb.append(ite.next());
}
return sb.toString();
}
static String toStringBWS(int[] array){
StringBuilder sb=new StringBuilder();
for(int i=0;i<array.length-1;i++){
sb.append(array[i]);
sb.append(" ");
}
sb.append(array[array.length-1]);
return sb.toString();
}
static String toStringBWS(long[] array){
StringBuilder sb=new StringBuilder();
for(int i=0;i<array.length-1;i++){
sb.append(array[i]);
sb.append(" ");
}
sb.append(array[array.length-1]);
return sb.toString();
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n=int(input())
a=list(map(lambda x: int(x), input().split()))
a.sort()
cnt=[]
val=[]
last=-1
for i in a:
if i == last:
cnt[-1]+=1
else:
cnt.append(1)
val.append(i)
last = i
low_val=-10
low_cnt=0
mid_val=0
mid_cnt=0
ans=0
for i, v in enumerate(val):
if v>=low_val+3:
ans=max(ans, low_cnt+mid_cnt+cnt[i])
low_val=v
low_cnt=cnt[i]
mid_val=0
mid_cnt=0
elif v==low_val+2:
ans=max(ans, low_cnt+mid_cnt+cnt[i])
if mid_val>0:
low_val=mid_val
low_cnt=mid_cnt
mid_val=v
mid_cnt=cnt[i]
else:
low_val=v
low_cnt=cnt[i]
mid_val=0
mid_cnt=0
if v==low_val+1:
mid_val=v
mid_cnt=cnt[i]
ans=max(ans, low_cnt+mid_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": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
long long ans = 0;
long long sum = a[0];
for (int i = 1; i < n; i++) {
if (sum * (sum + a[i]) >= 0) {
ans += abs(sum * (-1) - sum / abs(sum) - a[i]);
sum += sum * (-1) - sum / abs(sum);
} else
sum += a[i];
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int INF = 100000000;
int dx[] = {0, 1, -1, 0, 1, -1, 1, -1};
int dy[] = {1, 0, 0, -1, 1, -1, -1, 1};
int main() {
int n;
cin >> n;
vector<int> v(n + 1, 0), w(n + 1, 0);
for (int i = 1; i <= n; i++) {
cin >> v[i];
w[i] += w[i - 1] + v[i];
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (w[i] == 0 && w[i - 1] < 0) {
v[i]++;
w[i]++;
ans++;
} else if (w[i] == 0 && w[i - 1] > 0) {
v[i]--;
w[i]--;
ans++;
} else if (w[i] < 0 && w[i - 1] < 0) {
while (w[i] <= 0) {
v[i]++;
w[i]++;
ans++;
}
} else if (w[i] > 0 && w[i - 1] > 0) {
while (w[i] >= 0) {
v[i]--;
w[i]--;
ans++;
}
}
if (i < n) {
w[i + 1] = w[i] + v[i + 1];
}
}
int sum = accumulate(v.begin(), v.end(), 0);
if (sum == 0) ans++;
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long sum = 0, num = 0;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
if (sum > 0 && sum + a >= 0) {
num += sum + a + 1;
a -= sum + a + 1;
} else if (sum < 0 && sum + a <= 0) {
num += abs(sum + a) + 1;
a += abs(sum + a) + 1;
}
sum += a;
}
cout << num << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
double EPS = 1e-9;
int INF = 1000000005;
long long INFF = 1000000000000000005LL;
double PI = acos(-1);
int dirx[8] = {-1, 0, 0, 1, -1, -1, 1, 1};
int diry[8] = {0, 1, -1, 0, -1, 1, -1, 1};
inline string IntToString(long long a) {
char x[100];
sprintf(x, "%lld", a);
string s = x;
return s;
}
inline long long StringToInt(string a) {
char x[100];
long long res;
strcpy(x, a.c_str());
sscanf(x, "%lld", &res);
return res;
}
inline string GetString(void) {
char x[1000005];
scanf("%s", x);
string s = x;
return s;
}
inline string uppercase(string s) {
int n = ((int)(s).size());
for (int(i) = (0); (i) < (n); ++(i))
if (s[i] >= 'a' && s[i] <= 'z') s[i] = s[i] - 'a' + 'A';
return s;
}
inline string lowercase(string s) {
int n = ((int)(s).size());
for (int(i) = (0); (i) < (n); ++(i))
if (s[i] >= 'A' && s[i] <= 'Z') s[i] = s[i] - 'A' + 'a';
return s;
}
inline void OPEN(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
using namespace std;
inline bool feq(const double& a, const double& b) {
return fabs(a - b) < 1e-10;
}
inline int gcd(int a, int b) {
if (b == 0) return a;
return a < b ? gcd(b, a) : gcd(b, a % b);
}
long long mo = 1000000007;
bool f(pair<long long, long long> p1, pair<long long, long long> p2) {
return p1.first < p2.first;
}
long long solve(vector<long long>& ns, int n) {
long long res = 0;
long long sum = 0;
for (int i = 0; i < n; ++i) {
if (i % 2 == 0) {
if (sum + ns.at(i) >= 0) {
res = llabs(sum + ns.at(i)) + 1;
sum = -1;
} else {
sum += ns.at(i);
}
} else {
if (sum + ns.at(i) <= 0) {
res = llabs(sum + ns.at(i)) + 1;
sum = 1;
} else {
sum += ns.at(i);
}
}
}
return res;
}
int main() {
int n;
cin >> n;
vector<long long> ns(n);
for (int i = 0; i < n; ++i) {
cin >> ns.at(i);
}
long long cnt = solve(ns, n);
for (int i = 0; i < n; ++i) {
ns.at(i) *= -1;
}
cout << min(cnt, solve(ns, n)) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int minuss(int n, vector<int> a) {
int x = 0;
int sum = 0;
int counter = 0;
while (x < n) {
if (x % 2 == 0 && sum + a.at(x) >= 0) {
int olda = a.at(x);
a.at(x) = -1 - sum;
counter += abs(a.at(x) - olda);
}
if (x % 2 == 1 && sum + a.at(x) <= 0) {
int olda = a.at(x);
a.at(x) = 1 - sum;
counter += abs(a.at(x) - olda);
}
sum += a.at(x);
x++;
}
return counter;
}
int pluss(int n, vector<int> a) {
int x = 0;
int sum = 0;
int counter = 0;
while (x < n) {
if (x % 2 == 0 && sum + a.at(x) <= 0) {
int olda = a.at(x);
a.at(x) = 1 - sum;
counter += abs(a.at(x) - olda);
}
if (x % 2 == 1 && sum + a.at(x) >= 0) {
int olda = a.at(x);
a.at(x) = -1 - sum;
counter += abs(a.at(x) - olda);
}
sum += a.at(x);
x++;
}
return counter;
}
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a.at(i);
}
cout << min(minuss(n, a), pluss(n, a)) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
list_a = list(map(int,input().split()))
i = 0
k = 0
count = 0
if list_a[0] == 0:
while list_a[i] != 0:
if i == n:
ans = 2*n -1
break
k = i
i += 1
else:
if list_a[k+1] > 0:
count += abs(list_a[0] - (-1) ** (k+1))
list_a[0] = (-1) ** (k+1)
else:
count += abs(list_a[0] - (-1) ** k)
list_a[0] = (-1) ** k
ans = list_a[0]
if list_a[0] > 0:
for i in range(1,n):
ans += list_a[i]
if ans / ((-1) ** i) <= 0:
count += abs(ans - (-1) ** i)
ans = (-1) ** i
else:
for i in range(1,n):
ans += list_a[i]
if ans / ((-1) ** (i+1)) <= 0:
count += abs(ans - (-1) ** (i+1))
ans = (-1) ** (i+1)
print(count)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int MAX = 1e+5;
int n;
int a[MAX + 1];
int main() {
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
bool plus;
int sum = a[0] + a[1];
if (sum < 0)
plus = true;
else
plus = false;
long long ans = 0;
for (int i = 2; i < n; i++) {
sum += a[i];
if (plus && sum <= 0) {
ans += -sum + 1;
sum = 1;
} else if (!plus && sum >= 0) {
ans += sum + 1;
sum = -1;
}
plus = !plus;
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n=int(input())
a=[int(i) for i in input().split()]
f_sum=a[0]
n_sum=a[0]
ans=0
for i in range(1,n):
n_sum+=a[i]
if((f_sum>0 and n_sum>0) or (f_sum<0 and n_sum<0) or n_sum==0):
if(n_sum==0):
if(f_sum>0):
ans+=1
a[i]+=-1
else:
ans+=1
a[i]+=1
elif(n_sum>0):
ans+=abs(-1-n_sum)
a[i]+=(-1-n_sum)
elif(n_sum<0):
ans+=abs(1-n_sum)
a[i]+=(1-n_sum)
f_sum+=a[i]
n_sum=f_sum
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | N=gets.to_i
list=gets.split(" ").map(&:to_i)
sum = 0
res = 0
if(list[0] == 0) then
temp = 0
flag= true
(N-1).times{|i|
temp += list[i+1]
if(temp > 0 && flag) then
res += 1
list[0] = 1
flag = false
elsif(temp < 0 && flag) then
res +=1
list[0] = -1
flag = false
end
if(i == N-2) then
res += 1
list[0]=1
end
}
end
N.times{|i|
before_sum = sum
sum += list[i]
if (sum*before_sum> 0) then
if(sum > 0) then
res += (sum+1)
sum = -1
else
res += (-sum+1)
sum = 1
end
elsif sum*before_sum==0 then
if(before_sum < 0 )then
res += 1
sum = 1
elsif(before_sum > 0) then
sum = -1
res += 1
end
end
}
puts(res)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
vector<int> T;
cin >> N;
for (int i = 0; i < N; i++) {
int tmp;
cin >> tmp;
T.push_back(tmp);
}
int ans = 0;
int sum = 0;
bool pre_pm;
sum = T.at(0);
if (sum > 0) {
pre_pm = true;
} else if (sum < 0) {
pre_pm = false;
} else {
ans++;
sum++;
}
for (int i = 1; i < N; i++) {
if (pre_pm) {
sum += T.at(i);
while (0 <= sum) {
sum--;
ans++;
}
pre_pm = false;
} else {
sum += T.at(i);
while (sum <= 0) {
sum++;
ans++;
}
pre_pm = true;
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
int inf = 1000000007;
using namespace std;
int main() {
int n;
cin >> n;
vector<int> data(n);
int ans = 0;
for (int i = 0; i < n; i++) {
cin >> data.at(i);
}
vector<int> data2 = data;
int64_t sum = data.at(0);
int64_t sump = sum;
for (int i = 1; i < n; i++) {
sump += data.at(i);
if (sum * sump >= 0) {
int64_t c = sump;
if (c < 0) c *= -1;
c++;
ans += c;
if (sump > 0) {
data.at(i) -= c;
sump -= c;
} else {
data.at(i) += c;
sump += c;
}
}
sum += data.at(i);
}
int64_t ans2 = 0;
int64_t sum2 = data2.at(0) * -1;
int64_t sump2 = sum2;
for (int i = 1; i < n; i++) {
sump2 += data2.at(i);
if (sum2 * sump2 >= 0) {
int64_t c = sump2;
if (c < 0) c *= -1;
c++;
ans2 += c;
if (sump2 > 0) {
data2.at(i) -= c;
sump2 -= c;
} else {
data2.at(i) += c;
sump2 += c;
}
}
sum2 += data2.at(i);
}
if (ans < ans2) {
cout << ans << endl;
} else {
cout << ans2 << endl;
}
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | 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 Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] input = new int[n];
int[] result = new int[n];
int even = 0;
int odd = 0;
boolean sign = true; //正=true, 負=false
for(int i = 0; i < n; i++) {
input[i] = sc.nextInt();
if(i % 2 == 0) {
even += input[i];
} else {
odd += input[i];
}
}
if(even > 0 && odd < 0) { //正負
sign = true;
} else if(even < 0 && odd > 0) { //負正
sign = false;
} else if(even > 0 && odd > 0) { //正正
if(even > odd) {
sign = true;
} else {
sign = false;
}
} else if(even < 0 && odd < 0) { //負負
if(even > odd) {
sign = false;
} else {
sign = true;
}
} else if(even == 0) {
if(odd < 0) {
sign = true;
} else {
sign = false;
}
} else if(odd == 0){
if(even > 0) {
sign = true;
} else {
sign = false;
}
}
//System.out.println(Arrays.toString(input));
//System.out.println(sign + "");
//System.out.println(counting(input, result, 0, 0, sign));
counting(input, result, 0, 0, sign);
}
public static void counting(int[] input, int[] result, int 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 {
System.out.println(count);
}
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
original_A = [int(x) for x in input().split()]
ans = []
for k in range(2):
A = original_A.copy()
count = 0
if not k%2:
if A[0] >= 0:
A[0] = -1
count += (abs(A[0]) +1)
else:
if A[0] < 0:
A[0] = 1
count += (abs(A[0]) +1)
sum_before = A[0]
#print('***', k, A, '***')
for i in range(n):
if i == 0:
continue
sum_for_i = sum_before + A[i]
#print('[',i,']: before',sum_before,'after',sum_for_i, 'before', A)
if sum_for_i == 0 and sum_before > 0:
#print("case 1")
A[i] -= 1
count += 1
elif sum_for_i == 0 and sum_before <0:
#print("case 2")
A[i] += 1
count += 1
elif sum_before >0 and sum_for_i>0:
#print("case 3")
count += (abs(sum_for_i)+1)
A[i] -= (abs(sum_for_i)+1)
elif sum_before <0 and sum_for_i<0:
#print("case 4")
count += (abs(sum_for_i)+1)
A[i] += (abs(sum_for_i)+1)
#print('[',i,']: ','modified', A, 'count', count)
sum_before += A[i]
ans.append(count)
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": []
} | IN-CORRECT | UNKNOWN | n = gets.to_i
A = gets.split.map(&:to_i)
x = A[0]
answer = 0
for i in 0..n-2
s = x + A[i+1]
if x * s >= 0
if s < 0
answer = answer - s + 1
A[i+1] = A[i+1] - s + 1
else
answer = answer + s + 1
A[i+1] = A[i+1] - s - 1
end
end
x = x + A[i+1]
end
puts answer |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T>
inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
template <class T>
inline T sqr(T x) {
return x * x;
}
const double EPS = 1e-10;
const double PI = acos(-1.0);
pair<long long, long long> maxP(vector<long long> a, long long size) {
pair<long long, long long> p;
long long Max = a[0];
long long place = 0;
for (int i = (0); i < (size); ++i) {
if (a[i] > Max) {
Max = a[i];
place = i;
}
}
p.first = Max;
p.second = place;
return p;
}
pair<long long, long long> minP(vector<long long> a, long long size) {
pair<long long, long long> p;
long long min = a[0];
long long place = 0;
for (int i = (0); i < (size); ++i) {
if (a[i] < min) {
min = a[i];
place = i;
}
}
p.first = min;
p.second = place;
return p;
}
long long sumL(vector<long long> a, long long size) {
long long sum = 0;
for (int i = (0); i < (size); ++i) {
sum += a[i];
}
return sum;
}
long long counT(vector<long long> a, long long t) {
sort(a.begin(), a.end());
return upper_bound(a.begin(), a.end(), t) -
lower_bound(a.begin(), a.end(), t);
}
long long DIV[1000 + 1][1000 + 1];
void divide(long long n, long long m) {
DIV[0][0] = 1;
for (int i = (1); i < (n + 1); ++i) {
DIV[i][0] = 0;
}
for (int i = (0); i < (n + 1); ++i) {
DIV[i][1] = 1;
}
for (int i = (1); i < (m + 1); ++i) {
for (int t = (0); t < (n + 1); ++t) {
if (DIV[t][i] > 0) continue;
if (t >= i) {
DIV[t][i] = DIV[t - i][i] + DIV[t][i - 1];
} else {
DIV[t][i] = DIV[t][i - 1];
}
}
}
}
bool IsPrime(int num) {
if (num < 2)
return false;
else if (num == 2)
return true;
else if (num % 2 == 0)
return false;
double sqrtNum = sqrt(num);
for (int i = 3; i <= sqrtNum; i += 2) {
if (num % i == 0) {
return false;
}
}
return true;
}
class UnionFind {
public:
vector<long long> par;
vector<long long> rank;
UnionFind(long long N) : par(N), rank(N) {
for (int i = (0); i < (N); ++i) par[i] = i;
for (int i = (0); i < (N); ++i) rank[i] = 0;
}
~UnionFind() {}
long long root(long long x) {
if (par[x] == x)
return x;
else {
par[x] = root(par[x]);
return par[x];
}
}
void unite(long long x, long long y) {
long long rx = root(x);
long long ry = root(y);
if (rx == ry) return;
if (rank[rx] < rank[ry]) {
par[rx] = ry;
} else {
par[ry] = rx;
if (rank[rx] == rank[ry]) {
rank[rx]++;
}
}
}
bool same(long long x, long long y) {
long long rx = root(x);
long long ry = root(y);
return rx == ry;
}
};
class BFS_shortestDistance {
public:
BFS_shortestDistance(vector<vector<char> > p_, long long h_, long long w_) {
p = p_;
h = h_;
w = w_;
initial_number = h * w * 2;
for (int i = (0); i < (h); ++i) {
vector<long long> k(w);
for (int t = (0); t < (w); ++t) k[t] = initial_number;
field.push_back(k);
}
}
vector<vector<char> > p;
long long h;
long long w;
long long initial_number;
vector<vector<long long> > field;
pair<long long, long long> plus(pair<long long, long long> &a,
pair<long long, long long> &b) {
pair<long long, long long> p;
p.first = a.first + b.first;
p.second = a.second + b.second;
return p;
}
bool equal(pair<long long, long long> &a, pair<long long, long long> &b) {
return (a.first == b.first && a.second == b.second);
}
bool is_in_field(int h, int w, const pair<long long, long long> &point) {
const int c = point.second;
const int r = point.first;
return (0 <= c && c < w) && (0 <= r && r < h);
}
void init() {
for (int i = (0); i < (field.size()); ++i) {
for (int t = (0); t < (field[i].size()); ++t) {
field[i][t] = initial_number;
}
}
}
void shortest(long long sy, long long sx) {
init();
pair<long long, long long> c[4];
c[0].first = 0;
c[0].second = 1;
c[1].first = 0;
c[1].second = -1;
c[2].first = 1;
c[2].second = 0;
c[3].first = -1;
c[3].second = 0;
queue<pair<long long, long long> > Q;
pair<long long, long long> s;
s.first = sy;
s.second = sx;
field[sy][sx] = 0;
Q.push(s);
while (Q.empty() == false) {
pair<long long, long long> now = Q.front();
Q.pop();
for (int u = 0; u < 4; u++) {
pair<long long, long long> x = c[u];
pair<long long, long long> next = plus(now, x);
if (is_in_field(h, w, next)) {
if (p[next.first][next.second] == '.') {
if (field[next.first][next.second] == initial_number) {
field[next.first][next.second] = field[now.first][now.second] + 1;
Q.push(next);
} else {
}
}
}
}
}
}
};
bool Ischanged(long long a, long long b) {
if (a * b < 0) {
return true;
} else {
return false;
}
}
int main() {
long long n;
cin >> n;
vector<long long> a(n);
for (int i = (0); i < (n); ++i) cin >> a[i];
long long sum = 0;
long long count = 0;
for (int i = (0); i < (n); ++i) {
if (i == 0) {
sum += a[i];
if (sum == 0 && n != 1) {
if (a[1] >= 0) {
sum = -1;
} else {
sum = 1;
}
}
count++;
} else {
long long was = sum;
sum += a[i];
if (Ischanged(was, sum)) {
continue;
} else {
if (sum < 0) {
count += abs(sum) + 1;
sum = 1;
} else {
count += abs(sum) + 1;
sum = -1;
}
}
}
}
cout << count << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int ans(int n, int *, int change);
int main() {
int n;
int a[110000];
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
long long pAns, nAns;
pAns = ans(n, a, 1);
nAns = ans(n, a, -1);
printf("%d\n", min(pAns, nAns));
}
int ans(int n, int *a, int change) {
long long Ans = 0;
long long sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
switch (change) {
case -1:
if (sum > -1) Ans += 1 + sum, sum = -1;
change *= -1;
break;
case 1:
if (sum < 1) Ans += 1 - sum, sum = 1;
change *= -1;
break;
}
}
return Ans;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
int main() {
ll N;
cin >> N;
vector<ll> A(N);
ll osum = 0;
ll esum = 0;
for (int i = 0; i < ((int)(N)); i++) {
cin >> A.at(i);
if (A[i] < 0) {
if (i % 2 == 0)
esum++;
else
osum++;
}
}
bool evenIsPositive = (osum > esum) ? true : false;
ll step = 0;
ll sum = 0;
for (int i = 0; i < N; i++) {
ll tmp = sum + A[i];
if (i % 2 == 0) {
if (evenIsPositive) {
if (tmp < 0) {
step += abs(tmp) + 1;
sum = 1;
} else if (tmp == 0) {
sum = 1;
step++;
} else {
sum = tmp;
}
} else {
if (tmp < 0) {
sum = tmp;
} else if (tmp == 0) {
sum = -1;
step++;
} else {
step += tmp + 1;
sum = -1;
}
}
} else {
if (evenIsPositive) {
if (tmp < 0) {
sum = tmp;
} else if (tmp == 0) {
sum = -1;
step++;
} else {
step += sum + 1;
sum = -1;
}
} else {
if (tmp < 0) {
step += abs(tmp) + 1;
sum = 1;
} else if (tmp == 0) {
sum = 1;
step++;
} else {
sum = tmp;
}
}
}
}
cout << step << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
int N;
std::vector<long> A;
bool bxor(bool a, bool b) { return !a != !b; }
long solve(bool f) {
long sum = 0;
long ans = 0;
for (long i = 0; i < (N); ++i) {
sum += A[i];
if (!bxor(bxor(f, i % 2 == 0), sum > 0)) {
long dist = bxor(f, i % 2 == 0) ? (-1) : 1;
ans += std::abs(sum - dist);
sum = dist;
}
}
return ans;
}
int main() {
std::cin >> N;
A.resize(N);
for (long i = 0; i < (N); ++i) {
std::cin >> A[i];
}
std::cout << std::min(solve(true), solve(false)) << std::endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
ll n;
cin >> n;
vector<ll> a(n);
for (ll i = 0; i < n; i++) cin >> a[i];
ll sumodd = 0;
ll sumeven = 0;
ll ans = 0;
for (ll i = 0; i < n; i++) {
if (i % 2 == 0) {
if (sumodd + abs(a[i]) == 0)
sumodd += 1;
else if (i > 1 && sumodd + abs(a[i]) <= sumeven) {
ans += sumeven - sumodd + 1;
sumodd += abs(a[i]);
}
}
if (i % 2 != 0) {
if (sumeven + abs(a[i]) == 0)
sumeven += 1;
else if (sumeven + abs(a[i]) <= sumodd)
ans += sumodd - sumeven + 1;
sumeven += abs(a[i]);
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
long long A[100001];
cin >> n;
for (int i = 0; i < n; i++) {
cin >> A[i];
}
long long sum = 0;
int counter = 0;
for (int i = 0; i < n; i++) {
sum += A[i];
if (i % 2 == 0) {
while (sum <= 0) {
sum++;
counter++;
}
} else {
while (sum >= 0) {
sum--;
counter++;
}
}
}
int counterNeg = 0;
sum = 0;
for (int i = 0; i < n; i++) {
sum += A[i];
if (i % 2 == 1) {
while (sum <= 0) {
sum++;
counterNeg++;
}
} else {
while (sum >= 0) {
sum--;
counterNeg++;
}
}
}
cout << min(counter, counterNeg) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, t = 0;
long long r1 = 0, r2 = 0;
cin >> n;
vector<int> a(n);
for (int i = 0; i < (n); ++i) cin >> a.at(i);
for (int i = 0; i < (n); ++i) {
t += a.at(i);
if (i % 2 == 0 && t <= 0) {
r1 += abs(-t) + 1;
t = 1;
} else if (i % 2 && t >= 0) {
r1 += t + 1;
t = -1;
}
}
t = 0;
for (int i = 0; i < (n); ++i) {
t += a.at(i);
if (i % 2 && t <= 0) {
r2 += abs(-t) + 1;
t = 1;
} else if (i % 2 == 0 && t >= 0) {
r2 += t + 1;
t = -1;
}
}
cout << min(r1, r2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL << 60;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<int> A(n);
vector<int> B(n + 1);
vector<int> B2(n + 1);
B[0] = 0;
B2[0] = 0;
for (long long i = 0; i < n; i++) {
cin >> A[i];
B[i + 1] = A[i] + B[i];
B2[i + 1] = B[i + 1];
}
long long sum_p = 0;
long long pm = 0;
for (long long i = 1; i < n + 1; i++) {
long long del = 0;
if (i % 2 && B[i] + pm <= 0) del = abs(B[i] + pm) + 1;
if (i % 2 == 0 && B[i] + pm >= 0) del = -(B[i] + pm + 1);
pm += del;
sum_p += abs(del);
}
long long sum_m = 0;
pm = 0;
for (long long i = 1; i < n + 1; i++) {
long long del = 0;
if (i % 2 == 0 && B2[i] + pm <= 0) del = abs(B2[i] + pm) + 1;
if (i % 2 && B2[i] + pm >= 0) del = -(B2[i] + pm + 1);
pm += del;
sum_m += abs(del);
}
cout << ((sum_p < sum_m) ? sum_p : sum_m) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long ans1 = 0, ans2 = 0;
long long total;
for (int p = 0; p < 2; p++) {
ans2 = ans1;
ans1 = 0;
long long temp;
if (p == 0) {
if (a[0] == 0) {
total = 1;
ans1++;
} else {
total = a[0];
}
}
if (p == 1) {
if (a[0] == 0) {
total = -1;
ans1++;
} else {
total = -a[0];
ans1 = 2 * abs(a[0]);
}
}
for (int i = 1; i < n; i++) {
temp = a[i];
if (total > 0) {
if (total + a[i] >= 0) {
a[i] = -(total + 1);
}
total += a[i];
ans1 += abs(a[i] - temp);
a[i] = temp;
} else if (total < 0) {
if (total + a[i] <= 0) {
a[i] = (-total + 1);
}
total += a[i];
ans1 += abs(a[i] - temp);
a[i] = temp;
}
}
}
cout << min(ans1, ans2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const constexpr int INF = 1e9;
int N;
string s;
void solve() {
vector<long long> v(N);
long long minV = INF;
for (int i = 0; i < N; ++i) cin >> v[i];
long long cnt = 0;
long long tmp = v[0];
if (v[0] <= 0) {
cnt += -v[0] + 1;
v[0] = 1;
}
long long sum = v[0];
v[0] = tmp;
for (int i = 1; i < N; ++i) {
sum += v[i];
if (i % 2 != 0 && sum > 0) {
cnt += sum + 1;
sum = -1;
}
if (i % 2 == 0 && sum < 0) {
cnt += -sum + 1;
sum = 1;
}
if (sum == 0) {
if (i % 2 == 0)
sum = -1;
else
sum = 1;
cnt++;
}
}
minV = min(minV, cnt);
cnt = 0;
sum = 0;
if (v[0] >= 0) {
cnt += v[0] + 1;
v[0] = -1;
}
sum = v[0];
for (int i = 1; i < N; ++i) {
sum += v[i];
if (i % 2 != 0 && sum < 0) {
cnt += -sum + 1;
sum = 1;
}
if (i % 2 == 0 && sum > 0) {
cnt += sum + 1;
sum = -1;
}
if (sum == 0) {
if (i % 2 == 0)
sum = 1;
else
sum = -1;
cnt++;
}
}
cout << min(minV, cnt) << endl;
}
int main() {
cin >> N;
solve();
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python2 | import sys
from collections import deque
import copy
import math
def get_read_func(fileobject):
if fileobject == None :
return raw_input
else:
return fileobject.readline
def calc(A, N):
s = 0
count = 0
pre_sign = 0
for i in range(N):
s += A[i]
if s == 0:
if pre_sign == 1:
s -= 1
elif pre_sign == -1:
s += 1
count += 1
if pre_sign == s/abs(s):
if s < 0:
ope = 1 - s
else:
ope = -1 - s
s += ope
count += abs(ope)
pre_sign = s/abs(s)
return count
def main():
if len(sys.argv) > 1:
f = open(sys.argv[1])
else:
f = None
read_func = get_read_func(f);
input_raw = read_func().strip().split()
[N] = [int(input_raw[0])]
input_raw = read_func().strip().split()
A = [int(input_raw[i]) for i in range(N)]
s = 0
count = 0
pre_sign = 0
if A[0] != 0:
count = calc(A, N)
else:
A[0] = -1
count_minus =calc(A, N) + 1
A[0] = 1
count_plus =calc(A, N) + 1
count = min(count_minus, count_plus)
print count
if __name__ == '__main__':
main()
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import numpy as np
n = int(input())
A = [int(x) for x in input().split()]
sum_start_plus = 0
sum_start_minus = 0
ans_start_plus = 0
ans_start_minus = 0
# プラス始まりの場合を計算する
for i in range(n):
sum_start_plus += A[i]
if i % 2 == 0:
if np.sign(sum_start_plus) == 1:
continue
else:
ans_start_plus += abs(1 - sum_start_plus)
sum_start_plus = 1
else:
if np.sign(sum_start_plus) == -1:
continue
else:
ans_start_plus += abs(-1 - sum_start_plus)
sum_start_plus = -1
# マイナス始まりの場合を計算する
for i in range(n):
sum_start_minus += A[i]
print(sum_start_minus)
if i % 2 == 0:
if np.sign(sum_start_minus) == -1:
continue
else:
ans_start_minus += abs(-1 - sum_start_minus)
sum_start_minus = -1
else:
if np.sign(sum_start_minus) == 1:
continue
else:
ans_start_minus += abs(1 - sum_start_minus)
sum_start_minus = 1
print(min(ans_start_plus, ans_start_minus)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int store[200007];
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) scanf("%d", &store[i]);
long long cnt = 0;
long long sum = store[1];
if (sum == 0) sum++, cnt++;
for (int i = 2; i <= n; i++) {
if (sum < 0 && sum + store[i] <= 0) {
cnt += 1 - (sum + store[i]);
sum = 1;
} else if (sum > 0 && sum + store[i] > 0) {
cnt += (sum + store[i]) + 1;
sum = -1;
} else
sum += store[i];
}
cout << cnt << "\n";
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
int ju = 0;
for (int i = 0; i < (int)(n); i++) {
cin >> a[i];
if (i % 2 == 0 && a[i] > 0) ju++;
if (i % 2 == 1 && a[i] < 0) ju++;
}
long long int sum = 0, ans = 0;
if (ju > n / 2) {
for (int i = 0; i < (int)(n); i++) {
sum += a[i];
if (sum <= 0 && i % 2 == 0) {
ans += abs(1 - sum);
sum = 1;
} else if (sum >= 0 && i % 2 == 1) {
ans += abs(-1 - sum);
sum = -1;
}
}
} else {
for (int i = 0; i < (int)(n); i++) {
sum += a[i];
if (sum >= 0 && i % 2 == 0) {
ans += abs(-1 - sum);
sum = -1;
} else if (sum <= 0 && i % 2 == 1) {
ans += abs(1 - sum);
sum = 1;
}
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | def f(c, cost):
for i in a[1:]:
if c > 0:
if c + i >= 0:
cost += c + i + 1
c = -1
else:
c += i
else:
if c + i <= 0:
cost += 1 - c - i
c = 1
else:
c += i
return c, cost
n = int(input())
a = list(map(int, input().split()))
cost = (a[0] == 0)
c = a[0] + (a[0] == 0)
c, cost = f(c, cost)
tmp = cost
cost = 0
if a[0] >= 0:
cost = a[0] + 1
c = -1
else:
cost = -a[0] + 1
c = 1
c, cost = f(c, cost)
print(min(cost, tmp))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
// Your code here!
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] array = new int[n];
for (int i = 0; i < n; i++) {
array[i] = sc.nextInt();
}
int countA = 0;
int sum = 0;
for (int i = 0; i < n; i++) {
sum += array[i];
if (i % 2 == 0) {
if (sum <= 0) {
countA += sum * (-1) + 1;
sum = 1;
}
} else {
if (sum >= 0) {
countA += sum + 1;
sum = -1;
}
}
}
int countB = 0;
sum = 0;
for (int i = 0; i < n; i++) {
sum += array[i];
if (i % 2 == 1) {
if (sum <= 0) {
countB += sum * (-1) + 1;
sum = 1;
}
} else {
if (sum >= 0) {
countB += sum + 1;
sum = -1;
}
}
}
System.out.println(Math.min(countA, 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": []
} | IN-CORRECT | python3 | n = int(input())
a = [int(x) for x in input().split()]
def odd_positive(List, n):
sum_a = 0
cost = 0
i = 0
for a in List:
i += 1
nextsum = sum_a + a
if i & 1 and nextsum <= 0:
cost = - nextsum + 1
sum_a = 1
elif (not i & 1) and nextsum >= 0:
cost = nextsum + 1
sum_a = -1
return cost
def odd_negative(List, n):
sum_a = 0
cost = 0
i = 0
for a in List:
i += 1
nextsum = sum_a + a
if i & 1 and nextsum >= 0:
cost = nextsum + 1
sum_a = -1
elif (not i & 1) and nextsum <= 0:
cost = - nextsum + 1
sum_a = 1
return cost
ans = min(odd_positive(a,n), odd_negative(a,n))
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
i = input()
i = i.split()
for item in range(len(i)):
i[item] = int(i[item])
totn = 0
totp = 0
countp = 0
countn = 0
for x in range(len(i)):
totp += i[x]
totn += i[x]
'''
if x == 0:
if totp == 0:
totn = -1
countn = 1
totp = 1
countp = 1
elif totp < 0:
countp = abs(totp) + 1
totp = 1
elif totp > 0:
countn = abs(totn) + 1
totn = -1
'''
if x %2 == 1:
if totn == 0:
countn += 1
totn = 1
elif totn < 0:
countn += abs(totn) + 1
totn = 1
if totp == 0:
countp += 1
totp = -1
elif totp > 0:
countp += abs(totp) + 1
totp = -1
elif x %2 == 0:
if totn == 0:
countn += 1
totn = -1
elif totn > 0:
countn += abs(totn) + 1
totn = -1
if totp == 0:
countp += 1
totp = 1
elif totp < 0:
countp += abs(totp) + 1
totp = 1
'''print('totn', totn)
print('countn', countn)
print('totp', totp)
print('countp', countp) '''
count = min(countn, countp)
print(count)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate
def exit(*argv, **kwarg):
print(*argv, **kwarg)
sys.exit()
def mina(*argv, sub=1): return list(map(lambda x: x - sub, argv))
# 受け渡されたすべての要素からsubだけ引く.リストを*をつけて展開しておくこと
def a_int(): return int(read())
def ints(): return list(map(int, read().split()))
def read_col(H):
'''H is number of rows
A列、B列が与えられるようなとき
ex1)A,B=read_col(H) ex2) A,=read_col(H) #一列の場合'''
ret = []
for _ in range(H):
ret.append(list(map(int, read().split())))
return tuple(map(list, zip(*ret)))
def read_tuple(H):
'''H is number of rows'''
ret = []
for _ in range(H):
ret.append(tuple(map(int, read().split())))
return ret
def read_matrix(H):
'''H is number of rows'''
ret = []
for _ in range(H):
ret.append(list(map(int, read().split())))
return ret
# return [list(map(int, read().split())) for _ in range(H)] # 内包表記はpypyでは遅いため
MOD = 10**9 + 7
INF = 2**31 # 2147483648 > 10**9
# default import
from collections import defaultdict, Counter, deque
from operator import itemgetter, xor, add
from itertools import accumulate, product, permutations, combinations
from bisect import bisect_left, bisect_right # , insort_left, insort_right
from functools import reduce
from math import gcd
def lcm(a, b):
# 最小公倍数
g = gcd(a, b)
return a // g * b
# https://atcoder.jp/contests/abc059/tasks/arc072_a
# si=a1+..+aiと定義する
# s1...si,si+1,snの符号は+-+-... or -+-+...にしたい←両方試せばいい
# +-+-にするのに最小の操作は?
n = a_int()
A = ints()
S = list(accumulate(A))
#+-+-のとき
now = 1 # 正負
pad = 0
ans1 = 0
for s in S:
s += pad
if s * now < 0: # 異符号だったら修正する必要あり
if now == 1: # +にしたい場合
n_ope = 1 - s
pad += n_ope
else: # -にしたい場合
n_ope = s + 1
pad -= n_ope
ans1 += n_ope
now *= -1
#-+-+のとき
now = -1 # 正負
pad = 0
ans2 = 0
for s in S:
s += pad
if s * now <= 0: # 異符号だったら修正する必要あり
if now == 1: # +にしたい場合
n_ope = 1 - s
pad += n_ope
else: # -にしたい場合
n_ope = s + 1
pad -= n_ope
ans2 += n_ope
now *= -1
# print(S)
# print(ans1, ans2)
print(min(ans1, ans2))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
const double EPS = 1e-7;
const int inf = 1e8;
const long long INF = 1e16;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
long long n;
vector<long long> in;
long long f() {
long long ans = 0;
long long sum = in[0];
for (int i = 1; i < n; i++) {
if (sum < 0) {
long long t = -sum + 1;
ans += max(t - in[i], 0ll);
sum += max(t - in[i], 0ll) + in[i];
} else {
long long t = -sum - 1;
ans += max(in[i] - t, 0ll);
sum += in[i] - max(in[i] - t, 0ll);
}
}
return ans;
}
int main() {
cin >> n;
in = vector<long long>(n);
for (int i = 0; i < n; i++) cin >> in[i];
long long out = INF;
if (in[0] == 0) {
in[0] = 1;
out = min(out, f());
in[0] = -1;
out = min(out, f());
} else
out = min(out, f());
cout << out << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int N;
cin >> N;
vector<ll> a(N);
for (int i = 0; i < N; i++) {
cin >> a.at(i);
}
ll ans = 0;
if (a.at(0) != 0) {
vector<ll> sum(N + 1, 0);
for (int i = 0; i < N; i++) {
sum.at(i + 1) = sum.at(i) + a.at(i);
}
for (int i = 2; i <= N; i++) {
sum.at(i) = sum.at(i - 1) + a.at(i - 1);
if (sum.at(i) == 0) {
if (sum.at(i - 1) < 0) {
sum.at(i) = 1;
} else {
sum.at(i) = -1;
}
ans++;
} else if (sum.at(i - 1) * sum.at(i) > 0) {
if (sum.at(i - 1) < 0) {
ans += 1 - sum.at(i);
sum.at(i) = 1;
} else {
ans += 1 + sum.at(i);
sum.at(i) = -1;
}
}
}
} else {
vector<ll> sum(N + 1, 0);
ans = 1;
a.at(0) = 1;
for (int i = 0; i < N; i++) {
sum.at(i + 1) = sum.at(i) + a.at(i);
}
for (int i = 2; i <= N; i++) {
sum.at(i) = sum.at(i - 1) + a.at(i - 1);
if (sum.at(i) == 0) {
if (sum.at(i - 1) < 0) {
sum.at(i) = 1;
} else {
sum.at(i) = -1;
}
ans++;
} else if (sum.at(i - 1) * sum.at(i) > 0) {
if (sum.at(i - 1) < 0) {
sum.at(i) = 1;
ans += 1 - sum.at(i);
} else {
sum.at(i) = -1;
ans += 1 + sum.at(i);
}
}
}
ll ans2 = 1;
a.at(0) = -1;
for (int i = 0; i < N; i++) {
sum.at(i + 1) = sum.at(i) + a.at(i);
}
for (int i = 2; i <= N; i++) {
sum.at(i) = sum.at(i - 1) + a.at(i - 1);
if (sum.at(i) == 0) {
if (sum.at(i - 1) < 0) {
sum.at(i) = 1;
} else {
sum.at(i) = -1;
}
ans2++;
} else if (sum.at(i - 1) * sum.at(i) > 0) {
if (sum.at(i - 1) < 0) {
sum.at(i) = 1;
ans2 += 1 - sum.at(i);
} else {
sum.at(i) = -1;
ans2 += 1 + sum.at(i);
}
}
}
ans = min(ans, ans2);
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
if (scanf("%d", &n) < 1) return 0;
int tmp;
int sm = 0;
long long int cnt = 0;
for (int i = 0; i < n; i++) {
if (scanf("%d", &tmp) < 1) return 0;
if ((0 <= sm + tmp) && (0 < sm)) {
cnt = cnt + (1 + sm + tmp);
sm = -1;
} else if ((sm + tmp <= 0) && (sm < 0)) {
cnt = cnt + (1 - sm - tmp);
sm = 1;
} else
sm = sm + tmp;
}
printf("%lld\n", cnt);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = Integer.parseInt(scanner.nextLine());
int[] array = new int[n];
int i = 0;
for(String ai : scanner.nextLine().split(" ")) {
array[i++] = Integer.parseInt(ai);
}
int op = 0;
if (array[0] == 0) {
if (array[1] > 0) {
array[0]--;
} else {
array[0]++;
}
op++;
}
int pos;
while ((pos = getpos(array, n)) >= 0) {
if (array[0] > 0) {
if (pos % 2 == 0) {
array[pos]++;
} else {
array[pos]--;
}
} else {
if (pos % 2 == 0) {
array[pos]--;
} else {
array[pos]++;
}
}
op++;
}
System.out.println(op);
}
public static int getpos(int[] array, int n) {
int sum = array[0];
if (sum == 0) {
return 0;
}
boolean previous = sum > 0; // +: true -: false
for(int i = 1; i < n; i++) {
sum += array[i];
if (sum == 0) {
return i;
}
if (previous && sum > 0) {
return i;
}
if (!previous && sum < 0) {
return i;
}
previous = sum > 0;
}
return -1;
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int INF = INT_MAX;
const long long INFL = LLONG_MAX;
const long double pi = acos(-1);
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
long long xx(vector<long long> &v) {
long long s = 0;
long long ans = 0;
int n = int((v).size());
for (int i = 0; i < n; i++) {
if (!i)
s = v[0];
else {
if (s > 0) {
if (s + v[i] < 0) {
s += v[i];
continue;
}
long long x, y;
x = max(s + v[i] + 1, (long long)0);
ans += x;
if (x != 0) s = -1;
} else {
if (s + v[i] > 0) {
s += v[i];
continue;
}
long long x, y;
x = max(1 - (s + v[i]), (long long)0);
ans += x;
if (x != 0) s = 1;
}
}
}
return ans;
}
int main() {
ios_base::sync_with_stdio(0);
cout.precision(15);
cout << fixed;
cout.tie(0);
cin.tie(0);
int n;
cin >> n;
vector<long long> v(n);
for (int(i) = 0; (i) < (n); (i)++) cin >> v[i];
if (n == 1) {
cout << 0 << '\n';
return 0;
}
long long ans = INFL;
if (v[0] == 0) {
v[0] += 1;
ans = min(ans, xx(v));
v[0] = -1;
ans = min(ans, xx(v));
} else if (v[0] > 0) {
ans = min(ans, xx(v));
v[0] = -1;
ans = min(ans, xx(v));
} else {
ans = min(ans, xx(v));
v[0] = 1;
ans = min(ans, xx(v));
}
cout << ans << '\n';
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
template <class T>
bool umax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T>
bool umin(T &a, const T &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template <typename T>
T gcd(T a, T b) {
if (a == 0) return b;
return gcd(b % a, a);
}
int findGCD(int arr[], int n) {
int result = arr[0];
for (int i = 1; i < n; i++) result = gcd(arr[i], result);
return result;
}
template <typename T>
T lcm(T m, T n) {
if ((0 == m) || (0 == n)) return 0;
return ((m / gcd(m, n)) * n);
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
fill((T *)array, (T *)(array + N), val);
}
int dx[5] = {1, 0, -1, 0};
int dy[5] = {0, 1, 0, -1};
vector<int> G[200010];
vector<int> c(10010, 0);
int num[200010];
bool used[200010];
int now_c = 0;
int dfs(int s) {
if (used[s]) {
return 0;
} else {
used[s] = true;
}
num[s] = c[now_c];
now_c++;
for (int i = 0; i < G[s].size(); i++) {
dfs(G[s][i]);
}
return 0;
}
int main() {
int N;
cin >> N;
vector<int> a(N);
for (int i = 0; i < (int)(N); i++) {
cin >> a[i];
}
long long int ans1 = 0LL;
long long int sum = 0LL;
for (auto a_temp : a) {
if (sum == 0LL) {
if (a_temp < 0) {
sum = a_temp;
} else if (a_temp > 0) {
sum = -1;
ans1 += (a_temp + 1LL);
} else {
sum = -1LL;
ans1 += 1LL;
}
} else if (sum < 0) {
if (sum + a_temp <= 0) {
ans1 += -(sum + a_temp) + 1LL;
sum = 1LL;
} else {
sum += a_temp;
}
} else if (sum > 0) {
if (sum + a_temp >= 0) {
ans1 += (sum + a_temp + 1LL);
sum = -1LL;
} else {
sum += a_temp;
}
}
}
sum = 0LL;
long long int ans2 = 0LL;
for (auto a_temp : a) {
if (sum == 0LL) {
if (a_temp < 0) {
sum = a_temp;
ans2 += (-a_temp + 1LL);
} else if (a_temp > 0) {
sum = a_temp;
} else {
sum = 1LL;
ans2 += 1LL;
}
} else if (sum < 0) {
if (sum + a_temp <= 0) {
ans2 += -(sum + a_temp) + 1LL;
sum = 1LL;
} else {
sum += a_temp;
}
} else if (sum > 0) {
if (sum + a_temp >= 0) {
ans2 += (sum + a_temp + 1LL);
sum = -1LL;
} else {
sum += a_temp;
}
}
}
cout << min(ans1, ans2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const long long LINF = 1e18;
const double EPS = 1e-9;
const double PI = M_PI;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
void yes() { cout << "Yes" << endl; }
void no() { cout << "No" << endl; }
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<long long> a(n);
for (long long(i) = 0; (i) < (long long)(n); i++) {
cin >> a[i];
}
vector<long long> sum(n);
sum[0] = a[0];
long long ans = 0;
for (long long(i) = 1; (i) < (long long)n; i++) {
sum[i] = sum[i - 1] + a[i];
if (sum[i] * sum[i - 1] < 0) {
continue;
} else if (sum[i] * sum[i - 1] > 0) {
if (sum[i] > 0 && sum[i - 1] > 0) {
ans += sum[i] + 1;
sum[i] = -1;
} else {
ans += -sum[i] + 1;
sum[i] = 1;
}
} else {
ans++;
if (sum[i - 1] > 0) {
sum[i] = -1;
} else {
sum[i] = 1;
}
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(int(i) for i in input().split())
b = []
for i in range(0,len(a)):
b.append(a[i])
def solve(cnt,A,N):
for i in range(1, N):
if sum(A[0:i])>0:
if sum(A[0:i+1])>=0:
r = A[i]
A[i]=-sum(A[0:i])-1
cnt+=abs(r-A[i])
else:
if sum(A[0:i+1])<=0:
r = A[i]
A[i]=-sum(A[0:i])+1
cnt+=abs(r-A[i])
return cnt
cnt1=0
if b[0]<=0:
while b[0]<=0:
b[0]+=1
cnt1+=1
ans1=solve(cnt1,b,n)
cnt2=0
if a[0]>=0:
while a[0]>=0:
a[0]-=1
cnt2+=1
ans2=solve(cnt2,a,n)
print(min(ans1,ans2))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | n = gets.to_i
arr = gets.chomp.split(" ").map(&:to_i)
count = 0
num = arr[0] + arr[1]
(2...arr.size).each do |i|
diff = num + arr[i]
# puts %(num : #{num})
# puts %(diff : #{diff})
if num > 0
if diff > 0
arr[i] -= diff.abs+1
count += diff.abs+1
end
else
if diff < 0
arr[i] += diff.abs+1
count += diff.abs+1
end
end
if diff == 0
if num > 0
arr[i] -= 1
else
arr[i] += 1
end
count += 1
end
num += arr[i]
end
#p arr
puts count |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
long long a[100005], dp[100005];
cin >> n;
long long sum = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
dp[i] = sum;
}
long long diff = 0, ans = 0;
if (dp[0] == 0) {
ans++;
diff++;
}
for (int i = 1; i < n; i++) {
if (dp[i] + diff == 0) {
if (dp[i - 1] + diff < 0) diff++, ans++;
if (dp[i - 1] + diff > 0) diff--, ans++;
continue;
}
if ((dp[i - 1] + diff) / llabs(dp[i - 1] + diff) ==
(dp[i] + diff) / llabs(dp[i] + diff)) {
if (dp[i] + diff < 0) {
ans += llabs(dp[i] + diff) + 1;
diff += llabs(dp[i] + diff) + 1;
} else {
ans += llabs(dp[i] + diff) + 1;
diff -= llabs(dp[i] + diff) + 1;
}
}
}
long long tans = ans;
diff = 0;
ans = 0;
if (dp[0] == 0) {
ans++;
diff--;
} else {
dp[0] *= (-1);
ans += llabs(dp[0]);
if (dp[0] < 0)
diff -= llabs(dp[0]);
else
diff += llabs(dp[0]);
}
for (int i = 1; i < n; i++) {
if (dp[i] + diff == 0) {
if (dp[i - 1] + diff < 0) diff++, ans++;
if (dp[i - 1] + diff > 0) diff--, ans++;
continue;
}
if ((dp[i - 1] + diff) / llabs(dp[i - 1] + diff) ==
(dp[i] + diff) / llabs(dp[i] + diff)) {
if (dp[i] + diff < 0) {
ans += llabs(dp[i] + diff) + 1;
diff += llabs(dp[i] + diff) + 1;
} else {
ans += llabs(dp[i] + diff) + 1;
diff -= llabs(dp[i] + diff) + 1;
}
}
}
cout << min(ans, tans) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
double mod = 1e9 + 7;
char alphabet[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
int cnt(ll n) {
int c = 0;
while (true) {
if (n < 1) break;
n = n / 10;
c++;
}
return c;
}
int main() {
ll N = 0, answer = 0, sum1 = 0, sum2 = 0, tmp = 0;
int cnt1 = 0, cnt2 = 0;
cin >> N;
int *a = new int[N];
for (int i = 0; i < (int)N; ++i) cin >> a[i];
sum1 += a[0];
sum2 += a[0];
for (int i = 1; i < (int)N; ++i) {
sum1 += a[i];
sum2 += a[i];
if (i % 2 == 0) {
if (sum1 > 0) {
cnt1 += abs(sum1) + 1;
sum1 -= abs(sum1) + 1;
} else if (sum1 == 0) {
sum1++;
cnt1++;
}
if (sum2 < 0) {
cnt2 += abs(sum2) + 1;
sum2 += abs(sum2) + 1;
} else if (sum2 == 0) {
sum2++;
cnt2++;
}
} else {
if (sum1 < 0) {
cnt1 += abs(sum1) + 1;
sum1 += abs(sum1) + 1;
} else if (sum1 == 0) {
sum1++;
cnt1++;
}
if (sum2 > 0) {
cnt2 += abs(sum2) + 1;
sum2 -= abs(sum2) + 1;
} else if (sum2 == 0) {
sum2++;
cnt2++;
}
}
}
cout << min(cnt1, cnt2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int MOD = 1000000007;
const int mod = 1000000007;
const int INF = 1000000000;
const long long LINF = 1e18;
const int MAX = 510000;
bool code(long long int n) {
if (n < 0)
return 1;
else if (n > 0)
return 0;
}
int main() {
int n;
long long int sum = 0;
unsigned long long int ans = 0;
unsigned long long int ans2 = 0;
cin >> n;
vector<long long int> a(n);
for (int i = 0; i < n; i++) {
cin >> a.at(i);
}
if (a.at(0) != 0) {
sum = a.at(0);
for (int i = 1; i < n; i++) {
if (sum + a.at(i) == 0) {
ans++;
if (sum > 0)
sum = -1;
else if (sum < 0)
sum = 1;
} else if (code(sum + a.at(i)) == code(sum)) {
ans += abs(sum + a.at(i)) + 1;
if (sum > 0)
sum = -1;
else if (sum < 0)
sum = 1;
} else {
sum = a.at(i) + sum;
}
}
cout << ans << endl;
return 0;
} else if (a.at(0) == 0) {
sum = -1;
ans = 1;
for (int i = 1; i < n; i++) {
if (sum + a.at(i) == 0) {
ans++;
if (sum > 0)
sum = -1;
else if (sum < 0)
sum = 1;
} else if (code(sum + a.at(i)) == code(sum)) {
ans += abs(sum + a.at(i)) + 1;
if (sum > 0)
sum = -1;
else if (sum < 0)
sum = 1;
} else {
sum = a.at(i) + sum;
}
}
sum = 1;
ans2 = 1;
for (int i = 1; i < n; i++) {
if (sum + a.at(i) == 0) {
ans2++;
if (sum > 0)
sum = -1;
else if (sum < 0)
sum = 1;
} else if (code(sum + a.at(i)) == code(sum)) {
ans2 += abs(sum + a.at(i)) + 1;
if (sum > 0)
sum = -1;
else if (sum < 0)
sum = 1;
} else {
sum = a.at(i) + sum;
}
}
if (ans > ans2)
cout << ans2 << endl;
else {
cout << ans << endl;
}
}
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.Scanner;
public class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.next());
long[] a = new long[n+1];
long[] b = new long[n+1];
long ans1 =0, ans2 =0,total =0;
//a[1]を+とするケース
for (int i =1; i<=n; i++) {
a[i] = Long.parseLong(sc.next());
b[i] = a[i];
total += a[i];
if (i % 2 == 1 && total <=0) {
ans1 += 1 - (total);
a[i] += ans1;
total += ans1;
}
if (i % 2 == 0 && total >=0) {
ans1 += 1 + total;
a[i] += -ans1;
total += -ans1;
}
}
total =0;
//a[1]を-とするケース
for (int i =1; i<=n; i++) {
total += b[i];
if (i % 2 == 0 && total <=0) {
ans2 += 1 - (total);
b[i] += ans2;
total += ans2;
}
if (i % 2 == 1 && total >=0) {
ans2 += 1 + total;
b[i] += -ans2;
total += -ans2;
}
}
System.out.print(Math.min(ans1,ans2));
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void) {
int n;
cin >> n;
vector<long long> a(n);
cin >> a[0];
for (int i = 1; i < n; i++) {
cin >> a[i];
a[i] += a[i - 1];
}
for (int i = 0; i < n; i++) cout << a[i] << ' ';
cout << endl;
long long move = 0, cnt1 = 0, cnt2 = 0;
for (int i = 0; i < n; i++) {
long long new_move;
long long tmp = a[i] + move;
if (i % 2 == 0) {
new_move = max((long long)0, 1 - tmp);
cnt1 += abs(new_move);
move += new_move;
} else {
new_move = -1 * max((long long)0, 1 + tmp);
cnt1 += abs(new_move);
move += new_move;
}
}
move = 0;
for (int i = 0; i < n; i++) {
long long new_move;
long long tmp = a[i] + move;
if (i % 2 == 0) {
new_move = -1 * max((long long)0, 1 + tmp);
cnt2 += abs(new_move);
move += new_move;
} else {
new_move = max((long long)0, 1 - tmp);
cnt2 += abs(new_move);
move += new_move;
}
}
cout << min(cnt1, cnt2) << endl;
return 0;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.