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 | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
int sum_ = a[0];
int count = 0;
if (a[0] > 0) {
for (int i = 1; i < n; ++i) {
if (i % 2 == 1) {
while (sum_ + a[i] >= 0) {
a[i]--;
count++;
}
} else {
while (sum_ + a[i] <= 0) {
a[i]++;
count++;
}
}
sum_ += a[i];
}
} else {
for (int i = 1; i < n; ++i) {
if (i % 2 == 1) {
while (sum_ + a[i] <= 0) {
a[i]++;
count++;
}
} else {
while (sum_ + a[i] >= 0) {
a[i]--;
count++;
}
}
sum_ += a[i];
}
}
cout << count << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <bits/stdc++.h>
using namespace std;
long long int ans1, ans2, sum1, sum2;
int n, i;
long long int a[100005];
int main() {
cin >> n;
for (i = 1; i <= n; i++) {
cin >> a[i];
}
if (a[1] > 0) {
sum1 = a[1];
ans2 = a[1] + 1;
sum2 = -1;
} else if (a[1] == 0) {
sum1 = 1;
ans1 = ans2 = 1;
sum2 = -1;
} else {
sum1 = 1;
ans1 = abs(a[1]) + 1;
sum2 = a[1];
}
for (i = 2; i <= n; i++) {
if (sum1 > 0) {
if (a[i] + sum1 >= 0) {
ans1 += a[i] + sum1 + 1;
sum1 = -1;
} else {
sum1 += a[i];
}
} else {
if (a[i] + sum1 <= 0) {
ans1 += abs(sum1 + a[i]) + 1;
sum1 = 1;
} else {
sum1 += a[i];
}
}
}
if (sum1 == 0) {
ans1++;
}
for (i = 2; i <= n; i++) {
if (sum2 > 0) {
if (a[i] + sum2 >= 0) {
ans2 += a[i] + sum2 + 1;
sum2 = -1;
} else {
sum2 += a[i];
}
} else {
if (a[i] + sum2 <= 0) {
ans2 += abs(sum2 + a[i]) + 1;
sum2 = 1;
} else
sum2 += a[i];
}
}
if (sum2 == 0) {
ans2++;
}
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 | python3 | n = int(input())
sa = input().split()
a = [0 for i in range(n)]
sumeven = 0
sumodd = 0
for i,sai in enumerate(sa):
a[i] = int(sai)
if i % 2 == 0:
sumeven += a[i]
else:
sumodd += a[i]
ret = 0
sm = 0
for i,ai in enumerate(a):
if sumeven < sumodd:
if i % 2 == 1:
ret += max(-sm+1-ai,0)
sm += max(ai, -sm+1)
else:
ret += max(sm+1 + ai, 0)
sm += min(ai, -(sm+1))
elif sumeven > sumodd:
if i % 2 == 0:
ret += max(-sm+1-ai,0)
sm += max(ai, -sm+1)
else:
ret += max(sm+1 + ai, 0)
sm += min(ai, -(sm+1))
else:
if a[0] > 0:
if i % 2 == 1:
ret += max(-sm+1-ai,0)
sm += max(ai, -sm+1)
else:
ret += max(sm+1 + ai, 0)
sm += min(ai, -(sm+1))
else:
if i % 2 == 0:
ret += max(-sm+1-ai,0)
sm += max(ai, -sm+1)
else:
ret += max(sm+1 + ai, 0)
sm += min(ai, -(sm+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 | python2 | # -*- coding:utf-8 -*-
n = int(raw_input())
numlist = (raw_input()).split(' ')
count = 0
if (int(numlist[0]) == 0):
if (int(numlist[1]) > 0):
numlist[0] = -1
else:
numlist[0] = 1
sumlist = [int(numlist[0])]
for i in range(1, n):
sumlist.append(sumlist[i-1] + int(numlist[i]))
while (True):
if (sumlist[i-1] > 0 and sumlist[i] > 0): #i-1,i番目までのsumがともに正
numlist[i] = int(numlist[i]) - (sumlist[i] + 1)
count += sumlist[i] + 1
sumlist[i] = -1
elif (sumlist[i-1] < 0 and sumlist[i] < 0): #i-1,i番目までのsumがともに負
numlist[i] = int(numlist[i]) + ((-1)*sumlist[i] + 1)
count += (-1)*sumlist[i] + 1
sumlist[i] = 1
elif (sumlist[i] == 0): #i番目までのsum=0
if (sumlist[i-1] > 0):
numlist[i] = int(numlist[i]) - 1
sumlist[i] -= 1
if (sumlist[i-1] < 0):
numlist[i] = int(numlist[i]) + 1
sumlist[i] += 1
count += 1
else:
break
#print numlist
#print sumlist
print count
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int,input().split()))
ans=10**10
s=0
cnt=0
for i in range(n):
s+=a[i]
if i%2==1:
if s<=0:
cnt += -s + 1
s = 1
else:
if s>=0:
cnt += s + 1
s = -1
ans=min(ans,cnt)
s=0
cnt=0
for i in range(n):
s+=a[i]
if i%2==0:
if s<=0:
cnt += -s + 1
s = 1
else:
if s>=0:
cnt += s + 1
s = -1
ans=min(ans,cnt)
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import numpy as np
n = int(input())
L = np.array([int(i) for i in input().split()])
count = 0
s = L[0]
# print(L)
loopnum = n//2
if n%2 == 0:
loopnum -= 1
#+-+-...
for i in range(loopnum):
s = s + L[2*i+1]
if s >= 0:
subt = s + 1
count += subt
s = s - subt
s = s + L[2*i+2]
if s < 0:
subt = s - 1
count -= subt
s = s - subt
if n%2 == 0:
s = s + L[-1]
if s >= 0:
subt = s + 1
count += subt
cand1 = count
count = 0
s = L[0]
#-+-+...
for i in range(loopnum):
s = s + L[2*i+1]
if s <= 0:
subt = s - 1
count -= subt
s = s - subt
s = s + L[2*i+2]
if s > 0:
subt = s + 1
count += subt
s = s - subt
if n%2 == 0:
s = s + L[-1]
if s <= 0:
subt = s - 1
count -= subt
cand2 = count
#print(cand1)
#print(cand2)
print(min(cand1,cand2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long a[n];
for (int i = 0; i < n; i++) cin >> a[i];
long long sum = a[0];
long long ans = 0;
for (int i = 1; i < n; i++) {
long long tmp = sum + a[i];
if (sum > 0 && tmp > 0) {
ans += tmp + 1;
sum = -1;
} else if (sum < 0 && tmp < 0) {
ans += -tmp + 1;
sum = 1;
} else if (tmp == 0) {
ans++;
if (sum < 0)
sum = 1;
else
sum = -1;
} else
sum = tmp;
}
long long sum2 = -1 * a[0] / abs(a[0]);
long long ans2 = a[0] + 1;
for (int i = 1; i < n; i++) {
long long tmp = sum2 + a[i];
if (sum2 > 0 && tmp > 0) {
ans2 += tmp + 1;
sum2 = -1;
} else if (sum2 < 0 && tmp < 0) {
ans2 += -tmp + 1;
sum2 = 1;
} else if (tmp == 0) {
ans2++;
if (sum2 < 0)
sum2 = 1;
else
sum2 = -1;
} else
sum2 = tmp;
}
cout << min(ans2, ans);
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int,input().split()))
cnt = 0
sum = [0]*n
sum[0] = a[0]
for i in range(1,n):
sum[i] = sum[i-1]+a[i]
if sum[i]*sum[i-1]<0:
continue
elif sum[i-1]*a[i]<0:
cnt += abs(sum[i-1])-abs(a[i])+1
sum[i] = -sum[i-1]//abs(sum[i-1])
else:
cnt += abs(sum[i-1])+abs(a[i])+1
sum[i] = -sum[i-1]//abs(sum[i-1])
print(cnt) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import numpy as np
input()
a = list(map(int, input().split()))
def calc(a):
value = a[0]
sign = value >= 0
result = 0
for i in a[1:]:
value += i
diff = 0
if sign and value >= 0:
diff = value + 1
elif not sign and value < 0:
diff = value - 1
result += abs(diff)
sign = not sign
value -= diff
if value == 0:
diff += sign
value += diff
result += diff
return result
r1 = calc(a[:])
diff = -a[0] - a[0] // abs(a[0])
a[0] += diff
r2 = calc(a[:]) + abs(diff)
print(min(r1, r2))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
template <class T>
bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T>
bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
int main() {
long long int N;
cin >> N;
vector<long long int> a(N);
for (int i = 0; i < N; i++) cin >> a[i];
vector<long long int> rui(N);
for (int i = 0; i < N; i++) {
if (i == 0)
rui[i] = a[i];
else {
rui[i] = rui[i - 1] + a[i];
}
}
long long int tmp1 = 0;
long long int ct1 = 0;
long long int tmp2 = 0;
long long int ct2 = 0;
long long int now = 0;
for (int i = 0; i < N; i++) {
if (i % 2 == 0) {
if (rui[i] + tmp1 > 0)
continue;
else if (rui[i] + tmp1 == 0) {
tmp1 += 1;
ct1 += 1;
} else {
tmp1 += abs(rui[i]) + 1;
ct1 += abs(rui[i]) + 1;
}
} else {
if (rui[i] + tmp1 < 0)
continue;
else if (rui[i] + tmp1 == 0) {
tmp1 -= 1;
ct1 += 1;
} else {
tmp1 += -(abs(rui[i]) + 1);
ct1 += abs(rui[i]) + 1;
}
}
}
for (int i = 0; i < N; i++) {
if (i % 2 == 0) {
if (rui[i] + tmp2 < 0)
continue;
else if (rui[i] + tmp2 == 0) {
tmp2 -= 1;
ct2 += 1;
} else {
tmp2 += -(abs(rui[i]) + 1);
ct2 += abs(rui[i]) + 1;
}
} else {
if (rui[i] + tmp2 > 0)
continue;
else if (rui[i] + tmp2 == 0) {
tmp2 += 1;
ct2 += 1;
} else {
tmp2 += (abs(rui[i]) + 1);
ct2 += abs(rui[i]) + 1;
}
}
}
long long int ans = min(ct1, ct2);
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;
string divide[4] = {"dream", "dreamer", "erase", "eraser"};
int main() {
int N, C, K;
cin >> N >> C >> K;
vector<int> T(N);
for (int i = 0; i < N; i++) {
cin >> T.at(i);
}
int sum = 0;
int cnt1 = 0;
for (int i = 0; i < N; i++) {
sum += T.at(i);
if (i % 2 == 0) {
if (sum <= 0) {
cnt1 += -sum + 1;
sum = 1;
}
} else {
if (sum >= 0) {
cnt1 += sum + 1;
sum = -1;
}
}
}
int cnt2 = 0;
sum = 0;
for (int i = 0; i < N; i++) {
sum += T.at(i);
if (i % 2 == 1) {
if (sum <= 0) {
cnt1 += -sum + 1;
sum = 1;
}
} else {
if (sum >= 0) {
cnt1 += sum + 1;
sum = -1;
}
}
}
cout << min(cnt1, cnt2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, i, j;
vector<int> a;
int sum, count, count2;
cin >> n;
a.resize(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
count = 0;
count2 = 0;
sum = 0;
if (a[0] <= 0) {
sum = 1;
count += abs(a[0]) + 1;
} else {
sum = a[0];
}
for (int i = 0; i < n - 1; i++) {
if (sum + a[i + 1] == 0) {
count++;
if (sum < 0)
sum = 1;
else
sum = -1;
} else if (sum < 0 && (sum + a[i + 1]) < 0) {
j = 1 - sum;
count += j - a[i + 1];
sum = 1;
} else if (sum > 0 && (sum + a[i + 1]) > 0) {
j = sum + 1;
count += j + a[i + 1];
sum = -1;
} else {
sum += a[i + 1];
}
}
if (a[0] >= 0) {
sum = -1;
count2 += abs(a[0]) + 1;
} else {
sum = a[0];
}
for (int i = 0; i < n - 1; i++) {
if (sum + a[i + 1] == 0) {
count2++;
if (sum < 0)
sum = 1;
else
sum = -1;
} else if (sum < 0 && (sum + a[i + 1]) < 0) {
j = 1 - sum;
count2 += j - a[i + 1];
sum = 1;
} else if (sum > 0 && (sum + a[i + 1]) > 0) {
j = sum + 1;
count2 += j + a[i + 1];
sum = -1;
} else {
sum += a[i + 1];
}
}
cout << min(count, count2);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1 << 29;
int main() {
int n;
cin >> n;
vector<ll> a(n), b(n);
for (int i = 0; i < int(n); i++) {
ll aa;
cin >> aa;
a[i] = aa;
b[i] = aa;
}
ll countmin = INF;
ll count = 0LL;
ll sum = 0LL;
for (int i = 0; i < int(n); i++) {
sum += a[i];
if (i % 2 == 0) {
if (sum <= 0LL) {
count += llabs(sum) + 1LL;
sum = 1LL;
}
}
if (i % 2 == 1) {
if (sum >= 0LL) {
count += llabs(sum) + 1LL;
sum = -1LL;
}
}
}
countmin = min(countmin, count);
count = 0LL;
sum = 0LL;
for (int i = 0; i < int(n); i++) {
sum += b[i];
if (i % 2 == 0) {
if (sum >= 0LL) {
count += llabs(sum) + 1LL;
sum = -1LL;
}
}
if (i % 2 == 1) {
if (sum <= 0LL) {
count += llabs(sum) + 1LL;
sum = 1LL;
}
}
}
countmin = min(countmin, count);
cout << countmin << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include<iostream>
#include<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];
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 | cpp | #include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("avx")
using namespace std;
const int cm = 1 << 17;
char cn[cm], *ci = cn + cm, ct;
inline char getcha() {
if (ci - cn == cm) {
fread_unlocked(cn, 1, cm, stdin);
ci = cn;
}
return *ci++;
}
inline int getint() {
int A = 0;
if (ci - cn + 16 > cm)
while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';
else
while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';
return A;
}
int main() {
int N = getint();
int s1 = 0;
long long kotae1 = 0;
int s2 = 0;
long long kotae2 = 0;
int a = 0;
for (int i = 0; i < (N / 2); i++) {
a = getint();
s1 += a;
s2 += a;
if (s1 <= 0) {
kotae1 += 1 - s1;
s1 = 1;
}
if (s2 >= 0) {
kotae2 += s2 + 1;
s2 = -1;
}
a = getint();
s1 += a;
s2 += a;
if (s2 <= 0) {
kotae2 += 1 - s2;
s2 = 1;
}
if (s1 >= 0) {
kotae1 += s1 + 1;
s1 = -1;
}
}
if (N & 1) {
a = getint();
s1 += a;
s2 += a;
if (s1 <= 0) {
kotae1 += 1 - s1;
s1 = 1;
}
if (s2 >= 0) {
kotae2 += s2 + 1;
s2 = -1;
}
}
printf("%d", min(kotae1, kotae2));
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);
}
long long sumi = 0, val1 = 0;
int ne = 0;
if (a.at(0) == 0) {
while (a.at(ne) == 0) {
if (ne == 0)
val1++;
else
val1 += 2;
ne++;
if (ne == n) break;
}
}
long long val2 = val1;
for (int i = ne; i < n; i++) {
if (a.at(0) == 0 && sumi == 0) {
if (ne % 2 == 0)
sumi = -1;
else
sumi = 1;
}
if (i == 0) {
sumi = a.at(i);
continue;
}
if (i % 2 == 1) {
if (sumi + a.at(i) < 0)
sumi += a.at(i);
else {
val1 += (sumi + a.at(i) + 1);
sumi = -1;
}
} else {
if (sumi + a.at(i) > 0)
sumi += a.at(i);
else {
val1 += (abs(sumi + a.at(i)) + 1);
sumi = 1;
}
}
}
sumi = 0;
for (int i = ne; i < n; i++) {
if (a.at(0) == 0 && sumi == 0) {
if (ne % 2 == 0)
sumi = 1;
else
sumi = -1;
}
if (i == 0) {
sumi = a.at(i);
continue;
}
if (i % 2 == 1) {
if (sumi + a.at(i) > 0)
sumi += a.at(i);
else {
val2 += (abs(sumi + a.at(i)) + 1);
sumi = 1;
}
} else {
if (sumi + a.at(i) < 0)
sumi += a.at(i);
else {
val2 += (sumi + a.at(i) + 1);
sumi = -1;
}
}
}
cout << min(val1, val2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 |
N = int(input())
A = list(map(int, input().split()))
cum_A = [0] * N
ans = 0
if A[0] == 0:
ans += 1
cum_A[0] = 1
else:
cum_A[0] = A[0]
for i in range(1,N):
if cum_A[i-1] < 0:
if cum_A[i-1] + A[i] >= 1:
cum_A[i] = cum_A[i-1] + A[i]
else:
ans += 1 - (cum_A[i-1] + A[i])
cum_A[i] = 1
else:
if cum_A[i-1] + A[i] <= -1:
cum_A[i] = cum_A[i-1] + A[i]
else:
ans += 1 + (cum_A[i-1] + A[i])
cum_A[i] = -1
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java |
import java.io.*;
import java.util.*;
public class Main {
static StringBuilder sb = new StringBuilder();
static FastScanner sc = new FastScanner(System.in);
static int INF = 12345678;
static long MOD = 1000000007;
static int[] y4 = {0, 1, 0, -1};
static int[] x4 = {1, 0, -1, 0};
static int[] y8 = {0, 1, 0, -1, -1, 1, 1, -1};
static int[] x8 = {1, 0, -1, 0, 1, -1, 1, -1};
static long[] F;//factorial
static boolean[] isPrime;
static int[] primes;
static char[][] map;
static int N, M;
static long T;
static int[] A;
public static void main(String[] args) {
int n = sc.nextInt();
long[] a = sc.nextLongArray(n);
long ans_1 = 0;//初期値正
long ans_2 = 0;
long sum_1 = a[0];
long sum_2 = a[0];
for (int i = 1; i < n; i++) {
if(i%2==1){
if(sum_1 + a[i] >= 0){
ans_1 += sum_1 + a[i] + 1;
sum_1 = -1;
}else{
sum_1 += a[i];
}
if(sum_2 + a[i] <= 0){
ans_2 += abs(sum_2 + a[i]) + 1;
sum_2 = 1;
}else{
sum_2 += a[i];
}
}else{
if(sum_2 + a[i] >= 0){
ans_2 += sum_2 + a[i] + 1;
sum_2 = -1;
}else{
sum_2 += a[i];
}
if(sum_1 + a[i] <= 0){
ans_1 += abs(sum_1 + a[i]) + 1;
sum_1 = 1;
}else{
sum_1 += a[i];
}
}
}
System.out.println(min(ans_1,ans_2));
}
static class Dijkstra {
long initValue = -1;
Node[] nodes;
int n;
long[] d;
Dijkstra(int n) {
this.n = n;
nodes = new Node[n];
for (int i = 0; i < n; i++)
nodes[i] = new Node(i);
d = new long[n];
Arrays.fill(d, initValue);
}
Dijkstra(int n, int edge, boolean isDirectedGraph) {
this.n = n;
nodes = new Node[n];
for (int i = 0; i < n; i++)
nodes[i] = new Node(i);
d = new long[n];
Arrays.fill(d, initValue);
if (isDirectedGraph) {
for (int ei = 0; ei < edge; ei++) {
int f = sc.nextInt() - 1;
int t = sc.nextInt() - 1;
long c = sc.nextLong();
addEdge(f, t, c);
}
} else {
for (int ei = 0; ei < edge; ei++) {
int f = sc.nextInt() - 1;
int t = sc.nextInt() - 1;
long c = sc.nextLong();
addEdge(f, t, c);
addEdge(t, f, c);
}
}
}
void addEdge(int f, int t, long c) {
nodes[f].edges.add(new Edge(t, c));
}
long[] solve(int s) {
d[s] = 0;
//最短距離と頂点を持つ
PriorityQueue<Dis> q = new PriorityQueue<>();
q.add(new Dis(s, 0));
while (!q.isEmpty()) {
Dis now = q.poll();
int nowId = now.p;
long nowC = now.cos;
for (Edge edge : nodes[nowId].edges) {
int to = edge.toId;
long needsCost = edge.toCost + nowC;
if (d[to] == initValue || needsCost < d[to]) {
d[to] = needsCost;
q.add(new Dis(to, needsCost));
}
}
}
return d;
}
//O( E ^ 2) 辺が密の時用
long[] solve2(int s) {
boolean[] used = new boolean[n];
long[][] cost = new long[n][n];
Main.fill(cost, initValue);
Arrays.fill(d, initValue);
d[s] = 0;
for (Node node : nodes) {
for (Edge edge : node.edges) {
int fromId = node.id;
int toId = edge.toId;
long toCost = edge.toCost;
cost[fromId][toId] = toCost;
}
}
while (true) {
int v = -1;
//まだ使われていない頂点のうち、距離が最小のものを探す。
for (int u = 0; u < n; u++)
if (!used[u] && (v == -1 || d[u] < d[v])) v = u;
if (v == -1) break;
used[v] = true;
for (int u = 0; u < n; u++)
d[u] = Math.min(d[u], d[v] + cost[v][u]);
}
return d;
}
static class Dis implements Comparable<Dis> {
//現在地点 最短距離
int p;
long cos;
Dis(int p, long cost) {
this.p = p;
cos = cost;
}
public int compareTo(Dis d) {
if (cos != d.cos) {
if (cos > d.cos) return 1;
else if (cos == d.cos) return 0;
else return -1;
} else {
return p - d.p;
}
}
}
static class Node {
int id;
List<Edge> edges;
Node(int id) {
edges = new ArrayList<>();
this.id = id;
}
}
static class Edge {
int toId;
long toCost;
Edge(int id, long cost) {
toId = id;
toCost = cost;
}
}
}
public static long toLong(int[] ar) {
long res = 0;
for (int i : ar) {
res *= 10;
res += i;
}
return res;
}
public static int toInt(int[] ar) {
int res = 0;
for (int i : ar) {
res *= 10;
res += i;
}
return res;
}
//k個の次の組み合わせをビットで返す 大きさに上限はない 110110 -> 111001
public static int nextCombSizeK(int comb, int k) {
int x = comb & -comb; //最下位の1
int y = comb + x; //連続した下の1を繰り上がらせる
return ((comb & ~y) / x >> 1) | y;
}
public static int keta(long num) {
int res = 0;
while (num > 0) {
num /= 10;
res++;
}
return res;
}
public static long getHashKey(int a, int b) {
return (long) a << 32 | b;
}
public static boolean isOutofIndex(int x, int y) {
if (x < 0 || y < 0) return true;
if (map[0].length <= x || map.length <= y) return true;
return false;
}
public static void setPrimes() {
int n = 100001;
isPrime = new boolean[n];
List<Integer> prs = new ArrayList<>();
Arrays.fill(isPrime, true);
isPrime[0] = isPrime[1] = false;
for (int i = 2; i * i <= n; i++) {
if (!isPrime[i]) continue;
prs.add(i);
for (int j = i * 2; j < n; j += i) {
isPrime[j] = false;
}
}
primes = new int[prs.size()];
for (int i = 0; i < prs.size(); i++)
primes[i] = prs.get(i);
}
public static void revSort(int[] a) {
Arrays.sort(a);
reverse(a);
}
public static void revSort(long[] a) {
Arrays.sort(a);
reverse(a);
}
public static int[][] copy(int[][] ar) {
int[][] nr = new int[ar.length][ar[0].length];
for (int i = 0; i < ar.length; i++)
for (int j = 0; j < ar[0].length; j++)
nr[i][j] = ar[i][j];
return nr;
}
/**
* <h1>指定した値以上の先頭のインデクスを返す</h1>
* <p>配列要素が0のときは、0が返る。</p>
*
* @return<b>int</b> : 探索した値以上で、先頭になるインデクス
* 値が無ければ、挿入できる最小のインデックス
*/
public static int lowerBound(final int[] arr, final int value) {
int low = 0;
int high = arr.length;
int mid;
while (low < high) {
mid = ((high - low) >>> 1) + low; //(low + high) / 2 (オーバーフロー対策)
if (arr[mid] < value) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
/**
* <h1>指定した値より大きい先頭のインデクスを返す</h1>
* <p>配列要素が0のときは、0が返る。</p>
*
* @return<b>int</b> : 探索した値より上で、先頭になるインデクス
* 値が無ければ、挿入できる最小のインデックス
*/
public static int upperBound(final int[] arr, final int value) {
int low = 0;
int high = arr.length;
int mid;
while (low < high) {
mid = ((high - low) >>> 1) + low; //(low + high) / 2 (オーバーフロー対策)
if (arr[mid] <= value) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
/**
* <h1>指定した値以上の先頭のインデクスを返す</h1>
* <p>配列要素が0のときは、0が返る。</p>
*
* @return<b>int</b> : 探索した値以上で、先頭になるインデクス
* 値がなければ挿入できる最小のインデックス
*/
public static long lowerBound(final long[] arr, final long value) {
int low = 0;
int high = arr.length;
int mid;
while (low < high) {
mid = ((high - low) >>> 1) + low; //(low + high) / 2 (オーバーフロー対策)
if (arr[mid] < value) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
/**
* <h1>指定した値より大きい先頭のインデクスを返す</h1>
* <p>配列要素が0のときは、0が返る。</p>
*
* @return<b>int</b> : 探索した値より上で、先頭になるインデクス
* 値がなければ挿入できる最小のインデックス
*/
public static long upperBound(final long[] arr, final long value) {
int low = 0;
int high = arr.length;
int mid;
while (low < high) {
mid = ((high - low) >>> 1) + low; //(low + high) / 2 (オーバーフロー対策)
if (arr[mid] <= value) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
//次の順列に書き換える、最大値ならfalseを返す
public static boolean nextPermutation(int A[]) {
int len = A.length;
int pos = len - 2;
for (; pos >= 0; pos--) {
if (A[pos] < A[pos + 1]) break;
}
if (pos == -1) return false;
//posより大きい最小の数を二分探索
int ok = pos + 1;
int ng = len;
while (Math.abs(ng - ok) > 1) {
int mid = (ok + ng) / 2;
if (A[mid] > A[pos]) ok = mid;
else ng = mid;
}
swap(A, pos, ok);
reverse(A, pos + 1, len - 1);
return true;
}
//次の順列に書き換える、最小値ならfalseを返す
public static boolean prevPermutation(int A[]) {
int len = A.length;
int pos = len - 2;
for (; pos >= 0; pos--) {
if (A[pos] > A[pos + 1]) break;
}
if (pos == -1) return false;
//posより小さい最大の数を二分探索
int ok = pos + 1;
int ng = len;
while (Math.abs(ng - ok) > 1) {
int mid = (ok + ng) / 2;
if (A[mid] < A[pos]) ok = mid;
else ng = mid;
}
swap(A, pos, ok);
reverse(A, pos + 1, len - 1);
return true;
}
//↓nCrをmod計算するために必要。 ***factorial(N)を呼ぶ必要がある***
static long ncr(int n, int r) {
if (n < r) return 0;
else if (r == 0) return 1;
factorial(n);
return F[n] / (F[n - r] * F[r]);
}
static long ncr2(int a, int b) {
if (b == 0) return 1;
else if (a < b) return 0;
long res = 1;
for (int i = 0; i < b; i++) {
res *= a - i;
res /= i + 1;
}
return res;
}
static long ncrdp(int n, int r) {
if (n < r) return 0;
long[][] dp = new long[n + 1][r + 1];
for (int ni = 0; ni < n + 1; ni++) {
dp[ni][0] = dp[ni][ni] = 1;
for (int ri = 1; ri < ni; ri++) {
dp[ni][ri] = dp[ni - 1][ri - 1] + dp[ni - 1][ri];
}
}
return dp[n][r];
}
static long modNcr(int n, int r) {
if (n < r) return 0;
long result = F[n];
result = result * modInv(F[n - r]) % MOD;
result = result * modInv(F[r]) % MOD;
return result;
}
public static long modSum(long... lar) {
long res = 0;
for (long l : lar)
res = (res + l % MOD) % MOD;
return res;
}
public static long modDiff(long a, long b) {
long res = a - b;
if (res < 0) res += MOD;
res %= MOD;
return res;
}
public static long modMul(long... lar) {
long res = 1;
for (long l : lar)
res = (res * l) % MOD;
if (res < 0) res += MOD;
res %= MOD;
return res;
}
public static long modDiv(long a, long b) {
long x = a % MOD;
long y = b % MOD;
long res = (x * modInv(y)) % MOD;
return res;
}
static long modInv(long n) {
return modPow(n, MOD - 2);
}
static void factorial(int n) {
F = new long[n + 1];
F[0] = F[1] = 1;
// for (int i = 2; i <= n; i++)
// {
// F[i] = (F[i - 1] * i) % MOD;
// }
//
for (int i = 2; i <= 100000; i++) {
F[i] = (F[i - 1] * i) % MOD;
}
for (int i = 100001; i <= n; i++) {
F[i] = (F[i - 1] * i) % MOD;
}
}
static long modPow(long x, long n) {
long res = 1L;
while (n > 0) {
if ((n & 1) == 1) {
res = res * x % MOD;
}
x = x * x % MOD;
n >>= 1;
}
return res;
}
//↑nCrをmod計算するために必要
static int gcd(int n, int r) {
return r == 0 ? n : gcd(r, n % r);
}
static long gcd(long n, long r) {
return r == 0 ? n : gcd(r, n % r);
}
static <T> void swap(T[] x, int i, int j) {
T t = x[i];
x[i] = x[j];
x[j] = t;
}
static void swap(int[] x, int i, int j) {
int t = x[i];
x[i] = x[j];
x[j] = t;
}
public static void reverse(int[] x) {
int l = 0;
int r = x.length - 1;
while (l < r) {
int temp = x[l];
x[l] = x[r];
x[r] = temp;
l++;
r--;
}
}
public static void reverse(long[] x) {
int l = 0;
int r = x.length - 1;
while (l < r) {
long temp = x[l];
x[l] = x[r];
x[r] = temp;
l++;
r--;
}
}
public static void reverse(int[] x, int s, int e) {
int l = s;
int r = e;
while (l < r) {
int temp = x[l];
x[l] = x[r];
x[r] = temp;
l++;
r--;
}
}
static int length(int a) {
int cou = 0;
while (a != 0) {
a /= 10;
cou++;
}
return cou;
}
static int length(long a) {
int cou = 0;
while (a != 0) {
a /= 10;
cou++;
}
return cou;
}
static int cou(boolean[] a) {
int res = 0;
for (boolean b : a) {
if (b) res++;
}
return res;
}
static int cou(String s, char c) {
int res = 0;
for (char ci : s.toCharArray()) {
if (ci == c) res++;
}
return res;
}
static int countC2(char[][] a, char c) {
int co = 0;
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[0].length; j++)
if (a[i][j] == c) co++;
return co;
}
static int countI(int[] a, int key) {
int co = 0;
for (int i = 0; i < a.length; i++)
if (a[i] == key) co++;
return co;
}
static int countI(int[][] a, int key) {
int co = 0;
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[0].length; j++)
if (a[i][j] == key) co++;
return co;
}
static void fill(int[][] a, int v) {
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[0].length; j++)
a[i][j] = v;
}
static void fill(long[][] a, long v) {
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[0].length; j++)
a[i][j] = v;
}
static void fill(int[][][] a, int v) {
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[0].length; j++)
for (int k = 0; k < a[0][0].length; k++)
a[i][j][k] = v;
}
static int max(int... a) {
int res = Integer.MIN_VALUE;
for (int i : a) {
res = Math.max(res, i);
}
return res;
}
static long max(long... a) {
long res = Long.MIN_VALUE;
for (long i : a) {
res = Math.max(res, i);
}
return res;
}
static int max(int[][] ar) {
int res = Integer.MIN_VALUE;
for (int i[] : ar)
res = Math.max(res, max(i));
return res;
}
static int min(int... a) {
int res = Integer.MAX_VALUE;
for (int i : a) {
res = Math.min(res, i);
}
return res;
}
static long min(long... a) {
long res = Long.MAX_VALUE;
for (long i : a) {
res = Math.min(res, i);
}
return res;
}
static int min(int[][] ar) {
int res = Integer.MAX_VALUE;
for (int i[] : ar)
res = Math.min(res, min(i));
return res;
}
static int sum(int[] a) {
int cou = 0;
for (int i : a)
cou += i;
return cou;
}
static int abs(int a) {
return Math.abs(a);
}
static long abs(long a) {
return Math.abs(a);
}
static class FastScanner {
private BufferedReader reader = null;
private StringTokenizer tokenizer = null;
public FastScanner(InputStream in) {
reader = new BufferedReader(new InputStreamReader(in));
tokenizer = null;
}
public String next() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
/*public String nextChar(){
return (char)next()[0];
}*/
public String nextLine() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
return reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken("\n");
}
public long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public int[] nextIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public int[] nextIntArrayDec(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt() - 1;
}
return a;
}
public int[][] nextIntArray2(int h, int w) {
int[][] a = new int[h][w];
for (int hi = 0; hi < h; hi++) {
for (int wi = 0; wi < w; wi++) {
a[hi][wi] = nextInt();
}
}
return a;
}
public int[][] nextIntArray2Dec(int h, int w) {
int[][] a = new int[h][w];
for (int hi = 0; hi < h; hi++) {
for (int wi = 0; wi < w; wi++) {
a[hi][wi] = nextInt() - 1;
}
}
return a;
}
//複数の配列を受け取る
public void nextIntArrays2ar(int[] a, int[] b) {
for (int i = 0; i < a.length; i++) {
a[i] = sc.nextInt();
b[i] = sc.nextInt();
}
}
public void nextIntArrays2arDec(int[] a, int[] b) {
for (int i = 0; i < a.length; i++) {
a[i] = sc.nextInt() - 1;
b[i] = sc.nextInt() - 1;
}
}
//複数の配列を受け取る
public void nextIntArrays3ar(int[] a, int[] b, int[] c) {
for (int i = 0; i < a.length; i++) {
a[i] = sc.nextInt();
b[i] = sc.nextInt();
c[i] = sc.nextInt();
}
}
//複数の配列を受け取る
public void nextIntArrays3arDecLeft2(int[] a, int[] b, int[] c) {
for (int i = 0; i < a.length; i++) {
a[i] = sc.nextInt() - 1;
b[i] = sc.nextInt() - 1;
c[i] = sc.nextInt();
}
}
public Integer[] nextIntegerArray(int n) {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public char[] nextCharArray(int n) {
char[] a = next().toCharArray();
return a;
}
public char[][] nextCharArray2(int h, int w) {
char[][] a = new char[h][w];
for (int i = 0; i < h; i++) {
a[i] = next().toCharArray();
}
return a;
}
//スペースが入っている場合
public char[][] nextCharArray2s(int h, int w) {
char[][] a = new char[h][w];
for (int i = 0; i < h; i++) {
a[i] = nextLine().replace(" ", "").toCharArray();
}
return a;
}
public char[][] nextWrapCharArray2(int h, int w, char c) {
char[][] a = new char[h + 2][w + 2];
//char c = '*';
int i;
for (i = 0; i < w + 2; i++)
a[0][i] = c;
for (i = 1; i < h + 1; i++) {
a[i] = (c + next() + c).toCharArray();
}
for (i = 0; i < w + 2; i++)
a[h + 1][i] = c;
return a;
}
//スペースが入ってる時用
public char[][] nextWrapCharArray2s(int h, int w, char c) {
char[][] a = new char[h + 2][w + 2];
//char c = '*';
int i;
for (i = 0; i < w + 2; i++)
a[0][i] = c;
for (i = 1; i < h + 1; i++) {
a[i] = (c + nextLine().replace(" ", "") + c).toCharArray();
}
for (i = 0; i < w + 2; i++)
a[h + 1][i] = c;
return a;
}
public long[] nextLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = nextLong();
}
return a;
}
public long[][] nextLongArray2(int h, int w) {
long[][] a = new long[h][w];
for (int hi = 0; hi < h; hi++) {
for (int wi = 0; wi < w; wi++) {
a[hi][wi] = nextLong();
}
}
return a;
}
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | package main
import (
"fmt"
"strconv"
)
func stringToInt(s string) int {
i, _ := strconv.Atoi(s)
return i
}
func min(a, b int) int {
if a < b {
return a
} else {
return b
}
}
func main() {
var n int
fmt.Scan(&n)
a := make([]int, 0, 0)
f := make([]int, 0, 0)
for i := 0; i < n; i++ {
tmp := 0
fmt.Scan(&tmp)
a = append(a, tmp)
}
f = append(f, a[0])
for i := 1; i < n; i++ {
f = append(f, f[i-1]+a[i])
}
f1 := make([]int, 0, 0)
f2 := make([]int, 0, 0)
for i := 0; i < n; i++ {
f1 = append(f1, f[i])
f2 = append(f2, f[i])
}
num := 0
tmpNum := 0
for i := 0; i < n; i++ {
tmpNum = 0
if i%2 == 1 {
if f1[i] <= 0 {
tmpNum = 1 - f1[i]
for j := i; j < n; j++ {
f1[j] += tmpNum
}
}
} else {
if f1[i] >= 0 {
tmpNum = f1[i] + 1
for j := i; j < n; j++ {
f1[j] -= tmpNum
}
}
}
num += tmpNum
}
num1 := num
num = 0
tmpNum = 0
for i := 0; i < n; i++ {
tmpNum = 0
if i%2 == 0 {
if f2[i] <= 0 {
tmpNum = 1 - f2[i]
for j := i; j < n; j++ {
f2[j] += tmpNum
}
}
} else {
if f2[i] >= 0 {
tmpNum = f2[i] + 1
for j := i; j < n; j++ {
f2[j] -= tmpNum
}
}
}
num += tmpNum
}
num2 := num
fmt.Println(min(num1, num2))
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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, class U>
inline void chmin(T &t, U f) {
if (t > f) t = f;
}
template <class T, class U>
inline void chmax(T &t, U f) {
if (t < f) t = f;
}
int n;
void solve() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> n;
vector<long long> v(n);
long long ans1 = 0, ans2 = 0;
long long sum = 0;
for (int i = (int)(0); i < (int)(n); i++) {
cin >> v[i];
}
for (int i = (int)(0); i < (int)(n); i++) {
sum += v[i];
if (i % 2 == 0) {
if (sum < 0) {
ans1 += abs(1 - sum);
sum = 1;
}
} else {
if (sum > 0) {
ans1 += abs(sum + 1);
sum = -1;
}
}
}
sum = 0;
for (int i = (int)(0); i < (int)(n); i++) {
sum += v[i];
if (i % 2 == 1) {
if (sum < 0) {
ans2 += abs(1 - sum);
sum = 1;
}
} else {
if (sum > 0) {
ans2 += abs(sum + 1);
sum = -1;
}
}
}
if (ans1 > ans2) {
cout << ans2 << endl;
} else {
cout << ans1 << endl;
}
}
int main() {
solve();
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n, i, j, ans = 0, sum = 0;
cin >> n;
vector<long long> a(n);
for (i = 0; i < n; i++) {
cin >> a[i];
}
sum += a[0];
if (sum > 0) {
for (i = 1; i < n; i++) {
if (i % 2 == 1) {
sum += a[i];
if (sum >= 0) {
ans += (sum + 1);
sum = -1;
}
} else {
sum += a[i];
if (sum <= 0) {
ans += 1 - sum;
sum = 1;
}
}
}
} else {
for (i = 1; i < n; i++) {
if (i % 2 == 1) {
sum += a[i];
if (sum <= 0) {
ans += 1 - sum;
sum = 1;
}
} else {
sum += a[i];
if (sum >= 0) {
ans += sum + 1;
sum = -1;
}
}
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n=int(input())
l=list(map(int,input().split()))
l.append(0)
a1=0
b1=0
a2=0
b2=0
for i in range(n):
b1+=l[i]
b2+=l[i]
if i%2==0:
if b1>=0:
a1+=abs(-1-b1)
b1=-1-l[i+1]
if b2=<0:
a2+=abs(1-b2)
b2=1-l[i+1]
else:
if b1<=0:
a1+=abs(1-b1)
b1=1-l[i+1]
if b2>=0:
a2+=abs(-1-b2)
b2=-1-l[i+1]
print(min(a1,a2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
vector<int> num;
long long sequence(int s) {
long long sum = s;
long long res = 0;
for (int i = 1; i < num.size(); i++) {
if (sum > 0) {
if (sum + num[i] >= 0) {
res += 1 + num[i] + sum;
sum = -1;
} else {
sum += num[i];
}
} else {
if (sum + num[i] <= 0) {
res += 1 - num[i] - sum;
sum = 1;
} else {
sum += num[i];
}
}
}
return res;
}
int main() {
int n;
scanf("%d", &n);
num.resize(n);
for (int i = 0; i < n; i++) scanf("%d", &num[i]);
long long ans1;
long long ans2;
if (num[0] == 0) {
ans1 = sequence(1) + 1;
ans2 = sequence(-1) + 1;
} else if (num[0] > 0) {
ans1 = sequence(num[0]);
ans2 = sequence(-1) + num[0] + 1;
} else {
ans1 = sequence(1) + abs(num[0]) + 1;
ans2 = sequence(num[0]);
}
printf("%d", 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() {
bool ch = false;
long long N, i;
long long ans = 0, count = 0;
cin >> N;
long long a[N];
cin >> a[0];
ans += a[0];
if (ans > 0)
ch = true;
else
ch = false;
if (ans == 0) {
count += 1;
ans = -1;
}
for (i = 1; i < N; i++) {
cin >> a[i];
if (ch) {
if (ans >= -a[i]) {
count += ans + a[i] + 1;
ans = -1;
} else
ans += a[i];
ch = false;
} else {
if (ans <= -a[i]) {
count += -ans - a[i] + 1;
ans = 1;
} else
ans += a[i];
ch = true;
}
}
long long con = 0;
if (a[0] > 0) {
ans = -1;
ch = false;
} else {
ans = 1;
ch = true;
}
con = a[0] + 1;
for (i = 1; i < N; i++) {
if (ch) {
if (ans >= -a[i]) {
con += ans + a[i] + 1;
ans = -1;
} else
ans += a[i];
ch = false;
} else {
if (ans <= -a[i]) {
con += -ans - a[i] + 1;
ans = 1;
} else
ans += a[i];
ch = true;
}
}
cout << min(count, con) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include<bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define ll long long
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define set0(a) memset ((a), 0 , sizeof(a))
#define set1(a) memset((a),-1,sizeof (a))
#define pi pair<int, int>
#define ps pair<string, string>
#define pl pair<long, long>
#define pll pair<long long, long long>
#define vll vector<long long>
#define vl vector<long>
#define vi vector<int>
#define vs vector<string>
#define vps vector< ps >
#define vpi vector< pi >
#define vpl vector< pl >
#define vpll vector< pll >
#define flash ios_base::sync_with_stdio(false); cin.tie(NULL);
#define tc(t) for(long long l=0;l<t;l++)
#define rep(i,s,n,d) for(long long i=s;i<n;i=i+d)
bool sortbysec(const pll &a,
const pll &b)
{
return (a.second < b.second);
}
void func(void)
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
}
int main(){
ll n;
cin>>n;
ll a[n];
rep(i,0,n,1){
cin>>a[i];
}
ll count1=0;
if(a[0]==0){
if(a[1]>0){
a[0]=-1;
}
else a[0]=1;
count1++;
}
ll sum[n]={};
sum[0]=a[0];
rep(i,1,n,1){
sum[i]=sum[i-1]+a[i];
}
ll sum1=a[0];
rep(i,1,n,1){
ll d=0;
ll dif=0;
if(sum1>0){
if(a[i]+sum1>=0){
d=-1;
dif=abs(a[i]+sum1-d);
count1=count1+dif;
sum1=d;
}
else{
sum1=sum1+a[i];
}
}
else{
if(a[i]+sum1<=0){
d=1;
dif=abs(a[i]+sum1-d);
count1=count1+dif;
sum1=d;
}
else{
sum1=sum1+a[i];
}
}
}
cout<<count1<<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 MAXN = 100 * 1000 + 10;
int main() {
long long n, f = 0, z = 0, s = 0, sum = 0;
cin >> n;
long long b[n];
for (long long i = 0; i < n; i++) {
cin >> b[i];
if (i % 2 == 0) {
f += b[i];
} else {
z += b[i];
}
}
if (f >= z) {
if (b[0] <= 0) {
s += -1 * b[0] + 1;
b[0] = 1;
}
} else {
if (b[0] >= 0) {
s += b[0] + 1;
b[0] = -1;
}
}
for (long long i = 0; i < n - 1; i++) {
sum += b[i];
if (sum < 0 && sum + b[i + 1] < 0) {
s += -1 * (sum + b[i + 1]);
b[i + 1] += -1 * (sum + b[i + 1]);
} else if (sum > 0 && sum + b[i + 1] >= 0) {
s += sum + b[i + 1];
b[i + 1] -= sum + b[i + 1];
}
if (sum + b[i + 1] == 0) {
if (sum < 0) {
b[i + 1] += 1;
} else {
b[i + 1] -= 1;
}
s++;
}
}
cout << s;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n;
long long a[1004], temp[1004];
long long solve() {
long long ans = 0;
for (int i = (2); i <= (int)(n); ++i) {
if (a[i - 1] > 0) {
if (a[i] + a[i - 1] < 0) {
a[i] += a[i - 1];
continue;
}
ans += abs(a[i] + 1 + a[i - 1]);
a[i] = -1;
} else {
if (a[i] + a[i - 1] > 0) {
a[i] += a[i - 1];
continue;
}
ans += abs(a[i] - 1 + a[i - 1]);
a[i] = 1;
}
}
return ans;
}
int main() {
scanf("%d", &n);
for (int i = (1); i <= (int)(n); ++i) scanf("%lld", &a[i]);
long long ans = 0;
if (!a[1]) {
a[1] = 1;
memcpy(temp, a, sizeof(temp));
ans = solve() + 1;
memcpy(a, temp, sizeof(a));
a[1] = -1;
ans = min(ans, solve() + 1);
} else
ans = solve();
printf("%lld\n", ans);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int atcoder() {
int n;
cin >> n;
long long ans = 0;
long long a[110000] = {};
long long notzero = 0;
for (int i = 0; i < n; ++i) {
cin >> a[i];
if (notzero == i && a[i] == 0) notzero++;
}
long long tmpsum = a[0];
if (notzero % 2 == 1) {
if (a[notzero] > 0) {
ans++;
tmpsum = -1;
} else {
ans++;
tmpsum = 1;
}
} else if (notzero % 2 == 0 && notzero != 0) {
if (a[notzero] > 0) {
ans++;
tmpsum = 1;
} else {
ans++;
tmpsum = -1;
}
}
for (auto i = 1; i < n; ++i) {
if (tmpsum * (a[i] + tmpsum) >= 0) {
if (a[i] + tmpsum > 0) {
ans += a[i] + tmpsum + 1;
tmpsum = -1;
} else if (a[i] + tmpsum < 0) {
ans += -(a[i] + tmpsum) + 1;
tmpsum = 1;
} else {
ans++;
if (tmpsum < 0)
tmpsum = 1;
else if (tmpsum > 0)
tmpsum = -1;
}
} else
tmpsum += a[i];
}
cout << ans << "\n";
return 0;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
atcoder();
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.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws Exception {
// Your code here!
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String[] str_a = br.readLine().split(" ");
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = Integer.parseInt(str_a[i]);
}
int sum = 0;
int count = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 1) {
if (sum >= 0) {
count += sum + 1;
sum = -1;
}
}
else {
if (sum <= 0) {
count += 1 - sum;
sum = 1;
}
}
}
int count2 = 0;
sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 0) {
if (sum >= 0) {
count2 += sum + 1;
sum = -1;
}
}
else {
if (sum <= 0) {
count2 += 1 - sum;
sum = 1;
}
}
}
System.out.println(count>=count2?count2:count);
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | 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];
}
if (a[0] < 0) {
for (int i = 0; i < n; i++) {
a[i] *= -1;
}
}
long long ans = 0, def = 0;
if (a[0] == 0) {
ans++;
def++;
}
for (int i = 1; i < n; i++) {
if (i % 2 == 0 && a[i] + def <= 0) {
ans += 1 - (a[i] + def);
def += 1 - (a[i] + def);
} else if (i % 2 == 1 && a[i] + def >= 0) {
ans += a[i] + def + 1;
def -= a[i] + def + 1;
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long n;
cin >> n;
vector<long> a(n + 1);
for (long i = 1; i <= n; i++) cin >> a.at(i);
long ans = 0;
for (long i = 1; i <= n - 1; i++) {
if (abs(a.at(i + 1)) > abs(a.at(i)) && a.at(i + 1) * a.at(i) < 0) {
a.at(i + 1) += a.at(i);
} else {
ans +=
abs(a.at(i + 1) - ((abs(a.at(i)) + 1) * -1 * a.at(i) / abs(a.at(i))));
a.at(i + 1) = -1 * a.at(i) / abs(a.at(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 | python3 | from functools import reduce
N =int(input())
A = list(map(int, input().split()))
def check(A):
arr1 = [0]*N
ans1 = 0
for i in range(len(A)):
if i ==0:
arr1[0] = A[0]
else:
arr1[i] += arr1[i-1] + A[i]
if arr1[i]*arr1[i-1] >0:
if arr1[i-1] >=0: #-に
arr1[i] = -1
elif arr1[i-1] <=0: # 無理やり+にする.
arr1[i] = 1
print(arr1[i-1],arr1[i])
ans1 += abs(A[i])+1
ans2 = 0
arr2 = [0]*N
#change head.
for i in range(len(A)):
if i ==0:
if A[0]>=0:
arr2[0] = -1
elif A[0]<=0:
arr2[0] = 1
ans2 += abs(A[0])+1
else:
arr2[i] += arr2[i-1] + A[i]
if arr2[i]*arr2[i-1] >0:
if arr2[i-1]>=0:
arr2[i] = -1
elif arr2[i-1]<=0:
arr2[i] = 1
print(arr2[i-1],arr2[i])
ans2 += abs(A[i])+1
return min(ans1,ans2)
print(check(A))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
vector<long long> a(n);
for (long long i = 0; i < n; i++) cin >> a[i];
long long now = 0;
long long ans1 = 0;
for (long long i = 0; i < n; i++) {
now += a[i];
if (i % 2 == 0) {
if (now < 0) {
ans1 += 1 - now;
now = 1;
}
}
if (i % 2 == 1) {
if (now > 0) {
ans1 += now + 1;
now = -1;
}
}
}
now = 0;
long long ans2 = 0;
for (long long i = 0; i < n; i++) {
now += a[i];
if (i % 2 == 1) {
if (now <= 0) {
ans2 += 1 - now;
now = 1;
}
}
if (i % 2 == 0) {
if (now >= 0) {
ans2 += now + 1;
now = -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 | python3 | # -*- coding: utf-8 -*-
# 整数の入力
n=int(input())
a=input().split()
counter=0
# 出力
for i in range(1,n):
S=0
for j in range(0,i):
S=S+int(a[j])
if S<0 and S+int(a[i])<0:
counter=counter-S-int(a[i])+1
a[i]=-S+1
elif S>0 and S+int(a[i])>0:
counter=counter+S+int(a[i])+1
a[i]=-S-1
print(counter) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <iostream>
#include <algorithm>
#include <string>
#define int long long
using namespace std;
int main() {
int n;
int A[100001];
cin >> n;
for(int i = 0; i < n; i++) {
cin >> A[i];
}
int 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 | java | import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int[] A = new int[N];
for (int i = 0; i < N; ++i) {
A[i] = sc.nextInt();
}
sc.close();
int sum1 = 0;
int sum2 = 0;
int ans1 = 0;
int ans2 = 0;
for (int i = 1; i < N; ++i) {
sum1 += A[i];
if (i % 2 == 0 && sum1 >= 0) {
ans1 += sum1 +1;
sum1 = -1;
} else if (i % 2 != 0 && sum1 <= 0) {
ans1 += Math.abs(sum1) + 1;
sum1 = 1;
}
}
for (int i = 1; i < N; ++i) {
sum2 += A[i];
if (i % 2 == 0 && sum2 <= 0) {
ans2 += sum1 +1;
sum2 = 1;
} else if (i % 2 != 0 && sum2 >= 0) {
ans2 += Math.abs(sum2) + 1;
sum2 = -1;
}
}
System.out.println(Math.min(ans1, ans2));
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long int;
template <class T>
ostream &operator<<(ostream &os, vector<T> &v) {
for (auto i = v.begin(); i != v.end(); i++) {
os << *i << " ";
}
return os;
}
ll gcd(ll a, ll b) {
ll tmp;
if (b > a) {
tmp = a;
a = b;
b = tmp;
}
while (a % b != 0) {
tmp = b;
b = a % b;
a = tmp;
}
return b;
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
int main(void) {
ll n;
vector<ll> v;
cin >> n;
for (int i = 0; i < n; i++) {
ll x;
cin >> x;
v.push_back(x);
}
ll cnt = 0;
if (v[0] == 0) {
if (v[1] > 0) {
v[0] = -1;
} else if (v[1] < 0) {
v[0] = 1;
}
cnt++;
}
ll sum = v[0];
ll prev = v[0];
for (int i = 1; i < n; i++) {
sum += v[i];
if (prev >= 0 and sum >= 0) {
cnt += abs(-1 - sum);
sum += -1 - sum;
} else if (prev <= 0 and sum <= 0) {
cnt += abs(1 - sum);
sum += abs(1 - sum);
}
prev = sum;
}
std::cout << cnt << std::endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input().strip())
A = list(map(int, input().strip().split(" ")))
s = A[0]
count = 0
if s == 0:
if n == 1:
count += 1
else:
count += 1
if A[1] > 0:
s = -1
else:
s = 1
for a in A[1:]:
prev = s
sign = prev > 0
s += a
if s == 0:
count += 1
if sign: # previous is positive
s = -1
else: # prev is negative
s = 1
elif sign == (s > 0): # previous and current have the same sign
count += abs(s)+1
if s > 0:
s = -1
else:
s = 1
else:
pass
print(count)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> a(N);
for (int j = 0; j < N; j++) {
cin >> a[j];
}
int c1 = 0, c2 = 0;
int sum = 0;
for (int i = 0; i < N; i++) {
sum += a[i];
if (i % 2 == 1 && sum >= 0) {
c1 += sum + 1;
sum = -1;
}
if (i % 2 == 0 && sum <= 0) {
c1 += -sum + 1;
sum = 1;
}
}
sum = 0;
for (int i = 0; i < N; i++) {
sum += a[i];
if (i % 2 == 1 && sum <= 0) {
c2 += -sum + 1;
sum = 1;
}
if (i % 2 == 0 && sum >= 0) {
c2 += sum + 1;
sum = -1;
}
}
cout << c1 << " " << c2 << endl;
cout << min(c1, c2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long int a[100005] = {0};
long long int solve(long long int n, long long int sum) {
long long int ans = 0;
for (int i = 1; i < n; ++i) {
long long int s = sum + a[i];
if ((s ^ sum) >= 0 || s == 0) {
ans += abs(s) + 1;
s = (sum < 0 ? 1 : -1);
}
sum = s;
}
return ans;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; ++i) cin >> a[i];
long long int ans = 0;
if (a[0] != 0)
ans = solve(n, a[0]);
else {
ans = solve(n, 1) + 1;
ans = min(solve(n, -1) + 1, ans);
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
int main(void) {
long long n, now = 0, ans = 0;
bool flag;
scanf("%ld", &n);
long long a[n];
scanf("%ld", &a[0]);
now = a[0];
if (now < 0) {
flag = false;
} else {
flag = true;
}
for (long long i = 1; i < n; i++) {
scanf("%ld", &a[i]);
now += a[i];
if (flag) {
if (now >= 0) {
ans += (-1 - now) * (-1);
now = -1;
}
flag = false;
} else {
if (now <= 0) {
ans += 1 - now;
now = 1;
}
flag = true;
}
}
printf("%ld", ans);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> a(N);
for (int i = 0; i < N; i++) cin >> a.at(i);
bool fla = false;
for (int i = 0; i < N; i++) {
if (a.at(i) != 0) {
if ((a.at(i) > 0) && (i % 2 == 0))
fla = true;
else if ((a.at(i) < 0) && (i % 2 == 1))
fla = true;
break;
}
}
int64_t t = 0LL, res = 0LL;
for (int i = 0; i < N; i++) {
int b = a.at(i);
if (fla) {
if (t + b <= 0) {
b = t * -1 + 1;
res += b - a.at(i);
}
} else {
if (t + b >= 0) {
b = t * -1 - 1;
res += abs(b - a.at(i));
}
}
t += b;
fla = !fla;
}
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 | java | import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) {
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
String[] inputString = br.readLine().split(" ");
int[] input = new int[N];
for(int i = 0 ; i < N ; i++){
input[i] = Integer.parseInt(inputString[i]);
}
int result = 0;
int base = input[0];
if(base == 0){
result += 1;
for(int i = 1 ; i < N ; i++){
if(input[i] != 0){
int k = input[i] * input[i];
k = (int) Math.sqrt(k) / (int)input[i];
base = (i%2 == 0)? k : -k;
}else if(i == N-1){
base = 1;
}
}
}
for(int i = 1 ; i < N ; i++){
int temp = base;
base += input[i];
if(temp*base >= 0){
result += Math.sqrt(base*base)+1;
base = (temp > 0)? -1 : 1;
}
}
System.out.println(result);
}catch(Exception e){
e.printStackTrace();
}
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long int check(long int sum, long int ans, vector<int> T, int N, bool pre_pm) {
for (int i = 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;
}
}
return ans;
}
int main() {
int N;
vector<int> T;
cin >> N;
for (int i = 0; i < N; i++) {
int tmp;
cin >> tmp;
T.push_back(tmp);
}
long int ans = 0;
long int sum = 0;
bool pre_pm;
sum = T.at(0);
if (0 <= sum) {
pre_pm = true;
if (!sum) {
sum++;
ans++;
}
long int tmp1 = check(sum, ans, T, N, pre_pm);
pre_pm = false;
long int tmp2 = check(-1, 1 + sum, T, N, pre_pm);
cout << min(tmp1, tmp2) << endl;
} else {
pre_pm = false;
long int tmp1 = check(sum, ans, T, N, pre_pm);
pre_pm = true;
long int tmp2 = check(1, 1 + sum, T, N, pre_pm);
cout << min(tmp1, tmp2) << 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 lint = long long;
using P = pair<int, int>;
using vec = vector<lint>;
using mat = vector<vector<int>>;
constexpr int MOD = 1000000007;
const int INF = 1 << 30;
int main() {
int n;
cin >> n;
vec a(n);
for (int i = 0; i < (int)(n); i++) cin >> a[i];
lint res = INF;
lint tres = 0;
lint cur = 0;
for (int i = 0; i < (int)(n); i++) {
if (i % 2 == 0) {
if (cur + a[i] <= 0) {
tres += abs(1 - (cur + a[i]));
cur = 1;
} else {
cur += a[i];
}
} else {
if (cur + a[i] >= 0) {
tres += abs((cur + a[i]) + 1);
cur = -1;
} else {
cur += a[i];
}
}
}
res = min(res, tres);
tres = 0;
cur = 0;
for (int i = 0; i < (int)(n); i++) {
if (i % 2 == 1) {
if (cur + a[i] <= 0) {
tres += abs(1 - (cur + a[i]));
cur = 1;
} else {
cur += a[i];
}
} else {
if (cur + a[i] >= 0) {
tres += abs((cur + a[i]) + 1);
cur = -1;
} else {
cur += a[i];
}
}
}
res = min(res, tres);
cout << res << "\n";
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
int sum = a[0];
int cnt = 0;
for (int i = 1; i < n; i++) {
if (sum * (sum + a[i]) < 0) {
sum += a[i];
continue;
}
if (sum > 0) {
cnt += a[i] + sum + 1;
a[i] = -(sum + 1);
} else if (sum < 0) {
cnt += -sum + 1 - a[i];
a[i] = -sum + 1;
}
sum += a[i];
}
cout << cnt << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int INF = LONG_LONG_MAX;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < (n); i++) cin >> a[i];
long long ans = INF;
long long sum = 0;
long long count = 0;
for (int i = 0; i < (n); i++) {
sum += a[i];
if (i % 2 == 0) {
if (sum < 1) {
count += (1 - sum);
sum = 1;
}
} else {
if (sum > -1) {
count += (sum - (-1));
sum = -1;
}
}
}
ans = min(ans, count);
sum = 0;
count = 0;
for (int i = 0; i < (n); i++) {
sum += a[i];
if (i % 2 != 0) {
if (sum < 1) {
count += (1 - sum);
sum = 1;
}
} else {
if (sum > -1) {
count += (sum - (-1));
sum = -1;
}
}
}
ans = min(ans, count);
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long a[n];
int i;
for (i = 0; i < n; i++) {
cin >> a[i];
}
long long sum = 0;
int cnt = 0;
if (a[0] > 0) {
for (i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 0) {
while (sum <= 0) {
sum++;
cnt++;
}
} else {
while (sum >= 0) {
sum--;
cnt++;
}
}
}
} else {
for (i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 0) {
while (sum >= 0) {
sum--;
cnt++;
}
} else {
while (sum <= 0) {
sum++;
cnt++;
}
}
}
}
cout << cnt << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
int a[n];
long long int s = 0;
long long int ans = 400000000000000000;
int i;
for (i = 0; i < n; i++) cin >> a[i];
s = a[0];
long long int p = 0;
if (s > 0) {
for (i = 1; i < n; i++) {
if (i % 2) {
if (s + a[i] < 0) {
s += a[i];
} else {
p += 1 + s + a[i];
s = -1;
}
} else {
if (s + a[i] > 0)
s += a[i];
else {
p += 1 - s - a[i];
s = 1;
}
}
}
s = -1;
ans = min(ans, p);
p = a[0] + 1;
for (i = 1; i < n; i++) {
if (i % 2 == 0) {
if (s + a[i] < 0) {
s += a[i];
} else {
p += 1 + s + a[i];
s = -1;
}
} else {
if (s + a[i] > 0)
s += a[i];
else {
p += 1 - s - a[i];
s = 1;
}
}
}
ans = min(ans, p);
cout << ans << endl;
} else if (s < 0) {
for (i = 1; i < n; i++) {
if (i % 2 == 0) {
if (s + a[i] < 0) {
s += a[i];
} else {
p += 1 + s + a[i];
s = -1;
}
} else {
if (s + a[i] > 0)
s += a[i];
else {
p += 1 - s - a[i];
s = 1;
}
}
}
s = -1;
ans = min(ans, p);
p = a[0] + 1;
for (i = 1; i < n; i++) {
if (i % 2 == 1) {
if (s + a[i] < 0) {
s += a[i];
} else {
p += 1 + s + a[i];
s = -1;
}
} else {
if (s + a[i] > 0)
s += a[i];
else {
p += 1 - s - a[i];
s = 1;
}
}
}
ans = min(ans, p);
cout << ans << endl;
} else {
p = 1;
s = 1;
for (i = 1; i < n; i++) {
if (i % 2) {
if (s + a[i] < 0) {
s += a[i];
} else {
p += 1 + s + a[i];
s = -1;
}
} else {
if (s + a[i] > 0)
s += a[i];
else {
p += 1 - s - a[i];
s = 1;
}
}
}
s = -1;
ans = min(ans, p);
p = 1;
for (i = 1; i < n; i++) {
if (i % 2 == 0) {
if (s + a[i] < 0) {
s += a[i];
} else {
p += 1 + s + a[i];
s = -1;
}
} else {
if (s + a[i] > 0)
s += a[i];
else {
p += 1 - s - a[i];
s = 1;
}
}
}
ans = min(ans, p);
cout << ans << endl;
}
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
class Program {
static List<long> rep;
static void Main(string[] args){
//入力を受け取る
var N = long.Parse(Console.ReadLine());
var A = Console.ReadLine().Split().Select(a => long.Parse(a)).ToArray();
var B = A;
long ans = 0;
long sum = A[0];
for(int i =1 ;i <N; i++){
if(sum > 0){
if(sum+A[i] >= 0){
var aim = sum*(-1)-1;
ans += (long) Math.Abs(A[i]-aim);
A[i] = aim;
}
}else{
if(sum+A[i] <= 0){
var aim = sum*(-1)+1;
ans += (long) Math.Abs(A[i]-aim);
A[i] = aim;
}
}
sum += A[i];
}
long ans2 = (long)Math.Abs(B[0])+1;
long sum2 = 0;
if(B[0] > 0){
sum2 = -1;
}else{
sum2 = 1;
}
for(int i =1 ;i <N; i++){
if(sum2 > 0){
if(sum2+B[i] >= 0){
var aim = sum2*(-1)-1;
ans2 += (long) Math.Abs(B[i]-aim);
B[i] = aim;
}
}else{
if(sum2+B[i] <= 0){
var aim = sum2*(-1)+1;
ans2 += (long) Math.Abs(B[i]-aim);
B[i] = aim;
}
}
sum2 += B[i];
}
if(ans2 < ans){
Console.WriteLine(ans2);
}else{
Console.WriteLine(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 n;
int flag[100005], k[100005];
long long a[100005], sum[100005], ans, b[100005], tot[100005], ant;
int main() {
int m = 0;
scanf("%d", &n);
scanf("%lld", &a[1]);
b[1] = a[1];
sum[1] = a[1];
tot[1] = sum[1];
if (sum[1] > 0) flag[1] = 1;
if (sum[1] < 0) flag[1] = 0;
if (sum[1] == 0) m = 1;
if (m == 0) {
for (int i = 2; i <= n; i++) {
scanf("%lld", &a[i]);
sum[i] = a[i] + sum[i - 1];
if (sum[i] > 0) flag[i] = 1;
if (sum[i] < 0) flag[i] = 0;
if (flag[i - 1] == 1) {
if (sum[i] >= 0) {
ans += sum[i] + 1;
sum[i] = -1;
flag[i] = 0;
}
} else {
if (sum[i] <= 0) {
ans += 1 - sum[i];
sum[i] = 1;
flag[i] = 1;
}
}
}
printf("%lld\n", ans);
} else {
for (int i = 2; i <= n; i++) {
scanf("%lld", &a[i]);
flag[1] = 0;
b[i] = a[i];
sum[i] = a[i] + sum[i - 1];
if (sum[i] > 0) flag[i] = 1;
if (sum[i] < 0) flag[i] = 0;
if (flag[i - 1] == 1) {
if (sum[i] >= 0) {
ans += sum[i] + 1;
sum[i] = -1;
flag[i] = 0;
}
} else {
if (sum[i] <= 0) {
ans += 1 - sum[i];
sum[i] = 1;
flag[i] = 1;
}
}
}
k[1] = 1;
for (int i = 2; i <= n; i++) {
tot[i] = b[i] + tot[i - 1];
if (tot[i] > 0) k[i] = 1;
if (tot[i] < 0) k[i] = 0;
if (k[i - 1] == 1) {
if (tot[i] >= 0) {
ant += tot[i] + 1;
tot[i] = -1;
k[i] = 0;
}
} else {
if (tot[i] <= 0) {
ant += 1 - tot[i];
tot[i] = 1;
k[i] = 1;
}
}
}
printf("%lld\n", min(ant, 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;
long long int sum = 0, prev = 0;
long long int ans = 0;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
sum += a;
if (prev > 0) {
if (sum >= 0) {
ans += abs(sum) + 1;
sum = -1;
}
} else if (prev < 0) {
if (sum <= 0) {
ans += abs(sum) + 1;
sum = 1;
}
}
prev = sum;
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long a[100000];
int n;
void solve() {
long long sum0 = a[0];
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 | java | import java.io.PrintWriter;
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
final int MOD = 1000000007;
final int INF = 1100000000;
//入力
int n = sc.nextInt();
int[] a = new int[n];
for(int i = 0; i < n; i++){
a[i] = sc.nextInt();
}
sc.close();
//処理
long ans = (1 << 30);
boolean bool = true;
for(int count = 0; count < 2; count++){
long temp = 0;
bool ^= true;
boolean f = bool;
long sum = 0;
for(int i = 0; i < n; i++){
sum += (long)a[i];
if(sum > 0 == f){
//nothing
}else{
temp += (long)Math.abs(sum) + 1;
if(f){
sum = 1;
}else{
sum = -1;
}
}
f ^= true;
}
ans = Math.min(ans, temp);
}
//出力
out.println(ans);
out.flush();
}
static class Pair{
int w,v;
public Pair(int a, int b){
this.w = a;
this.v = b;
}
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int alpha[26];
const int INF = 10000000;
int main() {
int n;
cin >> n;
vector<string> S(n);
for (int i = 0; i < n; i++) {
cin >> S[i];
}
for (int i = 0; i < 26; i++) {
alpha[i] = INF;
}
for (char c = 'a'; c <= 'z'; c++) {
if (alpha[c - 'a'] == 0) continue;
for (int i = 0; i < n; i++) {
int cnt = 0;
for (int j = 0; j < S[i].length(); j++) {
if (S[i][j] == c) {
cnt++;
}
}
alpha[c - 'a'] = min(alpha[c - 'a'], cnt);
}
}
string ans = "";
for (char c = 'a'; c <= 'z'; c++) {
int temp = alpha[c - 'a'];
while (temp > 0) {
ans += c;
temp--;
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n), b(n);
for (long long(i) = (0); (i) < (long long)(n); ++(i))
cin >> a[i], b[i] = a[i];
long long sum = a[0];
long long ans = 0;
if (sum == 0) sum++, ans++;
for (int i = 1; i < n; ++i) {
if ((sum > 0 and sum + a[i] < 0) or (sum < 0 and sum + a[i] > 0)) {
sum += a[i];
} else {
if (sum > 0) {
sum += a[i];
for (; sum >= 0; --sum) {
++ans;
}
} else {
sum += a[i];
for (; sum <= 0; ++sum) {
++ans;
}
}
}
}
long long ans2 = 0;
sum = a[0];
if (sum == 0) sum--, ans2++;
if (sum > 0) {
for (; sum >= 0; --sum) {
++ans2;
}
} else {
for (; sum <= 0; ++sum) {
++ans2;
}
}
for (int i = 1; i < n; ++i) {
if ((sum > 0 and sum + a[i] < 0) or (sum < 0 and sum + a[i] > 0)) {
sum += a[i];
} else {
if (sum > 0) {
sum += a[i];
for (; sum >= 0; --sum) {
++ans2;
}
} else {
sum += a[i];
for (; sum <= 0; ++sum) {
++ans2;
}
}
}
}
cout << min(ans, ans2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <bits/stdc++.h>
int main() {
int n;
int sequence[100000];
int counter_even;
int counter_odd;
int sum;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &sequence[i]);
}
counter_even = 0;
sum = 0;
for (int i = 0; i < n; i++) {
sum += sequence[i];
if (0 == (i + 1) % 2) {
if (sum < 0) {
counter_even = counter_even + abs(sum) + 1;
sum = 1;
}
} else {
if (sum >= 0) {
counter_even = counter_even + abs(sum) + 1;
sum = -1;
}
}
}
if (0 == sum) {
counter_even++;
}
counter_odd = 0;
sum = 0;
for (int i = 0; i < n; i++) {
sum += sequence[i];
if (1 == (i + 1) % 2) {
if (sum < 0) {
counter_odd = counter_odd + abs(sum) + 1;
sum = 1;
}
} else {
if (sum >= 0) {
counter_odd = counter_odd + abs(sum) + 1;
sum = -1;
}
}
}
if (0 == sum) {
counter_odd++;
}
if (counter_even < counter_odd) {
printf("%d\n", counter_even);
} else {
printf("%d\n", counter_odd);
}
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | from itertools import accumulate
import copy
def sol(acmA, sign=1):
add = 0
ans = 0
sumA = acmA[0]
for i in range(1,N):
acmA[i] += add
if sign == 1:
if (i%2==0 and acmA[i-1]<0 and 0<acmA[i]) or (i%2==1 and 0<acmA[i-1] and acmA[i]<0) :continue
else:
if (i%2==1 and acmA[i-1]<0 and 0<acmA[i]) or (i%2==0 and 0<acmA[i-1] and acmA[i]<0) :continue
tmp_add = -acmA[i]-1 if acmA[i] > 0 else -acmA[i]+1
#print(i, tmp_add, acmA[i])
acmA[i] += tmp_add
sumA += acmA[i]
add += tmp_add
ans +=abs(tmp_add)
if sumA == 0: return ans +1
else: return ans
N = int(input())
A = list(map(int,input().split()))
acmA = list(accumulate(A))
acmA2 = copy.deepcopy(acmA)
print(min(sol(acmA, 1),sol(acmA2,-1)))
#print(acmA)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
using System.Collections.Generic;
using static Assistant.Input;
using static Assistant.Debug;
using System.Linq;
using Assistant;
namespace ABC059C
{
class Program
{
static void Main(string[] args)
{
var n = RInt;
var a = RInts;
long ans = 0;
if (a[0] != 0)
{
ans = cand(a[0], a);
}
else
{
ans = Math.Min(cand(1, a), cand(-1, a)) + 1;
}
Console.WriteLine(ans);
}
static long cand(long sum, int[] a)
{
long ret = 0;
for (int i = 1; i < a.Length; i++)
{
if (sum < 0)
{
sum += a[i];
if (sum < 1)
{
ret += 1 - sum;
sum = 1;
}
}
else if (sum > 0)
{
sum += a[i];
if (sum > -1)
{
ret += sum + 1;
sum = -1;
}
}
}
return ret;
}
}
}
namespace Assistant
{
static class Input
{
static List<string> line = new List<string>();
static int index = 0;
static String RNext()
{
if (line.Count <= index) line.AddRange(Console.ReadLine().Split());
return line[index++];
}
public static int RInt => int.Parse(RNext());
public static long RLong => long.Parse(RNext());
public static int[] RInts => Console.ReadLine().Split().Select(int.Parse).ToArray();
public static long[] RLongs => Console.ReadLine().Split().Select(long.Parse).ToArray();
public static string RString => RNext();
//以下未テスト
public static int[] RIntsC(int c) => Enumerable.Repeat(0, c).Select(x => int.Parse(RNext())).ToArray();
public static long[] RLongsC(int c) => Enumerable.Repeat(0, c).Select(x => long.Parse(RNext())).ToArray();
public static char[][] RMap(int h) => Enumerable.Repeat(0, h).Select(x => Console.ReadLine().ToCharArray()).ToArray();
}
public struct Mlong
{
long _v;
const long mod = 1000000007;
public Mlong(long n = 0) : this() { _v = n >= mod ? n % mod : n; }
public static implicit operator Mlong(long _x) => new Mlong(_x);
public static implicit operator long(Mlong _x) => _x._v;
public static Mlong operator +(Mlong m1, Mlong m2) { long m = m1._v + m2._v; return m >= mod ? m - mod : m; }
public static Mlong operator -(Mlong m1, Mlong m2) { long m = m1._v - m2._v; return m >= 0 ? m : m + mod; }
public static Mlong operator *(Mlong m1, Mlong m2) => m1._v * m2._v % mod;
public static Mlong operator /(Mlong m1, Mlong m2) => m1._v * ModPow(m2._v, mod - 2) % mod;
public static long ModPow(long a, long n)
{
if (n == 0) return 1;
else if (n % 2 == 1) return a * ModPow(a, n - 1) % mod;
else return ModPow(a * a % mod, n / 2);
}
static Mlong[] fac, finv, inv;
public static void nCkInit(int max)
{
fac = new Mlong[max]; finv = new Mlong[max]; inv = new Mlong[max];
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;
inv[i] = mod - inv[mod % i] * (mod / i);
finv[i] = finv[i - 1] * inv[i];
}
}
public static Mlong nCk(int n, int k)
{
if (n < k) return 0;
if (n < 0 || k < 0) return 0;
return fac[n] * finv[k] * finv[n - k];
}
}
static class Debug
{
static public void Draw2D<T>(T[,] map, int mode = 1)
{
#if DEBUG
int W = map.GetLength(0);
int H = map.GetLength(1);
string[,] map2 = new string[W + 1, H + 1];
for (int i = 0; i < W + 1; i++)
{
for (int j = 0; j < H + 1; j++)
{
if (i == 0 && j == 0) map2[i, j] = 0.ToString();
else if (i == 0) map2[i, j] = (j - 1).ToString();
else if (j == 0) map2[i, j] = (i - 1).ToString();
else map2[i, j] = map[i - 1, j - 1].ToString();
}
}
for (int i = 0; i < W + 1; i++)
{
for (int j = 0; j < H + 1; j++)
{
if (mode == 0) Console.Write(map2[i, j].Last());
if (mode == 1) Console.Write(map2[i, j] + " ");
}
Console.WriteLine();
}
Console.WriteLine();
#endif
}
public static void Draw1D<T>(T[] array, int mode = 0)
{
#if DEBUG
Console.WriteLine(string.Join(" ", array));
#endif
}
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int a[100050];
int sum[100050];
int main() {
int n;
scanf("%d", &n);
int cnt1 = 0, cnt2 = 0;
memset(sum, 0, sizeof(sum));
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
sum[i] = sum[i - 1] + a[i];
}
int lazy1 = 0, lazy2 = 0;
for (int i = 0; i < n; i++) {
int sum1 = sum[i], sum2 = sum[i];
sum1 += lazy1;
sum2 += lazy2;
if (i % 2 == 0 && sum1 <= 0) {
lazy1 += 1 - sum1;
cnt1 += 1 - sum1;
}
if (i % 2 == 0 && sum2 >= 0) {
lazy2 -= 1 + sum2;
cnt2 += sum2 + 1;
}
if (i % 2 == 1 && sum1 >= 0) {
lazy1 -= 1 + sum1;
cnt1 += sum1 + 1;
}
if (i % 2 == 1 && sum2 <= 0) {
lazy2 += 1 - sum2;
cnt2 += 1 - sum2;
}
}
printf("%d\n", min(cnt1, cnt2));
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | (defun read-num (&optional (stream *standard-input*))
(parse-integer (read-line stream)))
(defun read-list-from-string (str)
(read-from-string (concatenate 'string "(" str ")")))
(defun read-list (&optional (stream *standard-input*))
(read-list-from-string (read-line stream)))
(defun satisfy-num (acc val)
(if (> acc 0)
(if (< (+ acc val) 0)
0
(- (+ 1 val acc)))
(if (> (+ acc val) 0)
0
(- 1 val acc))))
(defun count-shift (len)
(let ((acc (read))
(sum 0))
(dotimes (n (1- len) sum)
(let* ((e (read))
(shift (satisfy-num acc e)))
(incf sum (abs shift))
(incf acc (+ e shift))))))
(defun solve (len)
(format t "~D~%" (count-shift len)))
(solve (read-num)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long ans = 0, sum = 0, tmp;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
bool sign;
if (a[0] == 0 && a[1] >= 0) {
sum = -1;
sign = false;
ans++;
} else if (a[0] == 0 && a[1] < 0) {
sum = 1;
sign = true;
ans++;
}
for (int i = 1; i < n; i++) {
tmp = sum + a[i];
if (sign && tmp >= 0) {
sum = -1;
sign = false;
ans += abs(tmp) + 1;
} else if (!sign && tmp <= 0) {
sum = 1;
sign = true;
ans += abs(tmp) + 1;
} else if ((sign && tmp < 0) || (!sign && tmp > 0)) {
sum += a[i];
sign = !sign;
}
}
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;
constexpr auto INF = 100000000000;
constexpr auto mod = 1000000007;
struct edge {
int to, cost;
};
long long modpow(long long a, long long n, long long mod) {
long long res = 1;
while (n > 0) {
if (n & 1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
long long modinv(long long a, long long m) {
long long b = m, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
u %= m;
if (u < 0) u += m;
return u;
}
long long int c(long long int a, long long int b, long long int m) {
long long int ans = 1;
for (long long int i = 0; i < b; i++) {
ans *= a - i;
ans %= m;
}
for (long long int i = 1; i <= b; i++) {
ans *= modinv(i, m);
ans %= m;
}
return ans;
}
void dijkdtra(int s, int v, vector<int>& d, vector<vector<edge>>& G) {
priority_queue<pair<int, int>, vector<pair<int, int>>,
greater<pair<int, int>>>
que;
d[s] = 0;
que.push(pair<int, int>(0, s));
while (!que.empty()) {
pair<int, int> p = que.top();
que.pop();
int V = p.second;
if (d[V] < p.first) continue;
for (int i = 0; i < G[V].size(); i++) {
edge e = G[V][i];
if (d[e.to] > d[V] + e.cost) {
d[e.to] = d[V] + e.cost;
que.push(pair<int, int>(d[e.to], e.to));
}
}
}
}
long long int binary_search(vector<int>& s, long long int a) {
long long int l = -1;
long long int r = (int)s.size();
while (r - l > 1) {
long long int mid = l + (r - l) / 2;
if (s[mid] >= a)
r = mid;
else
l = mid;
}
return r;
}
int k(long long n) {
int x = 0;
while (n) {
x += n % 10;
n /= 10;
}
return x;
}
long long max(long long x, long long y) {
if (x < y) return y;
return x;
}
int main() {
long long n, ans;
cin >> n;
vector<long long> a(n), t(n), s(n);
for (int i = (0); i < (n); i++) {
cin >> a[i];
t[i] = a[i];
s[i] = a[i];
}
long long int w = a[0];
if (w <= 0) {
w = 1;
a[0] = 1;
}
for (int i = (1); i < (n); i++) {
if (i % 2 == 0) {
if (abs(w) >= a[i]) {
a[i] = abs(w) + 1;
}
w += a[i];
} else {
if (w >= abs(a[i])) {
a[i] = -1 * (w + 1);
}
w += a[i];
}
}
w = t[0];
if (w >= 0) {
w = -1;
t[0] = -1;
}
for (int i = (1); i < (n); i++) {
if (i % 2 == 1) {
if (abs(w) >= t[i]) {
t[i] = abs(w) + 1;
}
w += t[i];
} else {
if (w >= abs(t[i])) {
t[i] = -1 * (w + 1);
}
w += t[i];
}
}
long long cost1 = 0, cost2 = 0;
for (int i = (0); i < (n); i++) {
cost1 += abs(s[i] - a[i]);
cost2 += abs(s[i] - t[i]);
}
ans = min(cost1, cost2);
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #!/usr/bin/env ruby
STDIN.gets.chomp.to_i
array = STDIN.gets.chomp.split(' ').map(&:to_i)
def get_answer(first, array)
ans = 0
sum = first
array.each do |a|
if sum >= 0
if sum + a < 0
sum += a
else
ans += (-1 - (sum + a)).abs
sum = -1
end
else # sumがマイナス
if sum + a > 0
sum += a
else
ans += (1 - (sum + a)).abs
sum = 1
end
end
#puts "#{i}: sum = #{sum}, ans = #{ans}"
end
return ans
end
first = array.shift
if first == 0
ans = [get_answer(1, array), get_answer(-1, array)].min + 1
else
ans = get_answer(first, array)
end
puts ans
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
int main(int argc, char const *argv[]) {
int n;
std::cin >> n;
std::vector<int> v(n);
std::vector<int> sums(2, 0);
for (size_t i = 0; i < n; i++) {
std::cin >> v[i];
sums[i % 2] += v[i];
}
ull ans = 0;
if (sums[0] > sums[1] && v[0] <= 0) {
ans = ans + abs(v[0]) + 1;
v[0] = 1;
} else if (sums[0] < sums[1] && v[0] >= 0) {
ans = ans + abs(v[0]) + 1;
v[0] = -1;
} else if (v[0] == 0) {
ans += 1;
v[0] = 1;
}
ll now, pre;
now = pre = v[0];
for (size_t i = 1; i < n; i++) {
now += v[i];
if (pre * now >= 0) {
if (pre > 0) {
ans = ans + abs(now) + 1;
now = -1;
} else if (pre < 0) {
ans = ans + abs(now) + 1;
now = 1;
}
}
pre = now;
}
std::cout << ans << '\n';
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | 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
#print(a)
d=[a[0]]
cd=[a[0]]
for i in range(1,n):
val = 0
tempsum = sum+a[i]
d.append(tempsum)
if sum < 0 and tempsum <=0:
val = 1 - tempsum
if sum > 0 and tempsum >=0:
val = -1 - tempsum
sum = tempsum + val
cd.append(sum)
change += abs(val)
#print(d)
#print(cd)
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 | python3 | n = int(input())
a = list(map(int, input().split()))
L = [0 for _ in range(n)]
L[0] = a[0]
for i in range(1, n):
L[i] = L[i-1] + a[i]
delay = 0
all_over = 0
anti_delay = 0
anti_all_over = 0
if a[0] != 0:
antiL = L[:]
sign = (a[0] > 0) - (a[0] < 0)
for i in range(1, n):
L[i] += delay
if L[i] <= 0 and sign == -1:
delay += 1 - L[i]
all_over += 1 - L[i]
L[i] = 1
elif L[i] >= 0 and sign == 1:
delay -= L[i] + 1
all_over += L[i] + 1
L[i] = -1
sign *= -1
if antiL[0] < 0:
sign = 1
anti_delay = 1 - antiL[0]
anti_all_over = 1 - antiL[0]
else:
sign = -1
anti_delay = antiL[0] - 1
anti_all_over = antiL[0] - 1
for i in range(1, n):
antiL[i] += anti_delay
if antiL[i] <= 0 and sign == -1:
anti_delay += 1 - antiL[i]
anti_all_over += 1 - antiL[i]
antiL[i] = 1
elif antiL[i] >= 0 and sign == 1:
anti_delay -= antiL[i] + 1
anti_all_over += antiL[i] + 1
antiL[i] = -1
sign *= -1
print(min(anti_all_over, all_over))
else:
posL = L[:]
negL = L[:]
pos_delay, neg_delay = 1, -1
pos_all_over, neg_all_over = 1, 1
sign = 1
for i in range(1, n):
posL[i] += pos_delay
if posL[i] <= 0 and sign == -1:
pos_delay += 1 - posL[i]
pos_all_over += 1 - posL[i]
posL[i] = 1
elif posL[i] >= 0 and sign == 1:
pos_delay -= posL[i] + 1
pos_all_over += posL[i] + 1
posL[i] = -1
sign *= -1
sign = -1
for i in range(1, n):
negL[i] += neg_delay
if negL[i] <= 0 and sign == -1:
neg_delay += 1 - negL[i]
neg_all_over += 1 - negL[i]
negL[i] = 1
elif negL[i] >= 0 and sign == 1:
neg_delay -= negL[i] + 1
neg_all_over += negL[i] + 1
negL[i] = -1
sign *= -1
print(min(pos_all_over, neg_all_over)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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);
const long long mod = 1000000007;
int main() {
int N;
cin >> N;
vector<long long> A(N), B;
for (int i = (0); i < (N); ++i) {
cin >> A[i];
}
B = A;
long long sum = A[0];
long long num = 0;
for (int i = 1; i < N; i++) {
if (sum > 0) {
if (sum + A[i] >= 0) {
num += abs(A[i] + sum) + 1;
sum = -1;
} else {
sum += A[i];
}
} else if (sum < 0) {
if (sum + A[i] <= 0) {
num += abs(A[i] + sum) + 1;
sum = 1;
} else {
sum += A[i];
}
}
}
sum = (A[0] > 0) ? -1 : 1;
long long num2 = abs(A[0]) + 1;
for (int i = 1; i < N; i++) {
if (sum > 0) {
if (sum + A[i] >= 0) {
num2 += abs(A[i] + sum) + 1;
sum = -1;
} else {
sum += A[i];
}
} else if (sum < 0) {
if (sum + A[i] <= 0) {
num2 += abs(A[i] + sum) + 1;
sum = 1;
} else {
sum += A[i];
}
}
}
cout << min(num, num2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
int pm;
int cnt = 0;
int currentSum = 0;
for (int i = 0; i < n; i++) {
currentSum += a[i];
if (a[0] > 0) {
if (i % 2 == 0) {
if (currentSum > 0)
continue;
else {
while (currentSum <= 0) {
currentSum++;
cnt++;
}
}
} else {
if (currentSum < 0)
continue;
else {
while (currentSum >= 0) {
currentSum--;
cnt++;
}
}
}
} else {
if (i % 2 == 0) {
if (currentSum < 0)
continue;
else {
while (currentSum >= 0) {
currentSum--;
cnt++;
}
}
} else {
if (currentSum > 0)
continue;
else {
while (currentSum <= 0) {
currentSum++;
cnt++;
}
}
}
}
}
cout << cnt << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
void d_err() { cerr << endl; }
template <typename H, typename... T>
void d_err(H h, T... t) {
cerr << h << " ";
d_err(t...);
}
long long INF = 1e16;
int inf = 1e9;
long long MOD = 1e9 + 7;
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(20);
int N;
cin >> N;
long long ap[N], an[N];
long long s = 0;
for (int i = (0); i < (N); ++i) {
long long A;
cin >> A;
s += A;
ap[i] = s;
an[i] = s;
}
long long ans_p = 0;
bool p = false;
for (int i = (0); i < (N); ++i) {
p ^= 1;
long long c = 0;
if (p) {
if (ap[i] > 0) continue;
c = -ap[i] + 1;
ap[i + 1] += c;
ans_p += c;
} else {
if (ap[i] < 0) continue;
c = ap[i] + 1;
ap[i + 1] -= c;
ans_p += c;
}
}
long long ans_n = 0;
p = true;
for (int i = (0); i < (N); ++i) {
p ^= 1;
if (p) {
if (an[i] > 0) continue;
long long c = -an[i] + 1;
an[i + 1] += c;
ans_n += c;
} else {
if (an[i] < 0) continue;
long long c = an[i] + 1;
an[i + 1] += c;
ans_n += c;
}
}
cout << min(ans_p, ans_n) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
const int INF = 1e9;
using namespace std;
int main() {
long long n;
cin >> n;
long long a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long sum = 0, ans = 0;
if (a[0] > 0) {
for (long long i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 0 && sum <= 0) {
ans += (1 - sum);
sum = 1;
} else if (i % 2 == 1 && sum >= 0) {
ans += sum + 1;
sum = -1;
}
}
} else {
for (long long i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 1 && sum <= 0) {
ans += (1 - sum);
sum = 1;
} else if (i % 2 == 0 && sum >= 0) {
ans += sum + 1;
sum = -1;
}
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> a(N);
for (int i = 0; i < N; i++) cin >> a.at(i);
int count = 0;
if (a.at(0) == 0) {
a.at(0) = 1;
count++;
}
int sum = a.at(0);
for (int i = 1; i < N; i++) {
if (sum > 0 && sum + a.at(i) >= 0) {
while (sum + a.at(i) >= 0) {
a.at(i)--;
count++;
}
} else if (sum < 0 && sum + a.at(i) <= 0) {
while (sum + a.at(i) <= 0) {
a.at(i)++;
count++;
}
}
sum += a.at(i);
}
cout << count << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.typecons;
import std.numeric, std.math;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } }
bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } }
long mod = 10^^9 + 7;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto n = RD;
auto a = RDR.ARR;
long f(long x)
{
auto dp = new long[](n+1);
dp[1] = x;
long cnt;
foreach (i; 1..n)
{
dp[i+1] = dp[i] + a[i];
if (-sgn(dp[i+1]) != sgn(dp[i]))
{
cnt += abs(dp[i+1]) + 1;
dp[i+1] = -sgn(dp[i]);
}
}
//stderr.writeln(dp);
//stderr.writeln(cnt);
return cnt;
}
long ans1 = f(a[0]);
long ans2 = f(-sgn(a[0])) + abs(a[0]) + 1;
writeln(min(ans1, ans2));
stdout.flush();
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n=int(input())
l=list(map(int,input().split()))
if l[0]!=0:
count=0
suml=l[0]
for i in range(1,n):
if suml>0 and suml+l[i]<0:
suml+=l[i]
continue
elif suml<0 and suml+l[i]>0:
suml+=l[i]
continue
elif suml>0 and suml+l[i]>=0:
count+=suml+l[i]+1
suml=-1
else:
count+=-suml-l[i]+1
suml=1
print(count)
else:
count=1
suml1=1
for i in range(1,n):
if suml1>0 and suml1+l[i]<0:
suml1+=l[i]
continue
elif suml1<0 and suml1+l[i]>0:
suml1+=l[i]
continue
elif suml1>0 and suml1+l[i]>=0:
count+=suml1+l[i]+1
suml1=-1
else:
count+=-suml1-l[i]+1
suml1=1
k1=count
count=1
suml2=-1
for i in range(1,n):
if suml2>0 and suml2+l[i]<0:
suml2+=l[i]
continue
elif suml2<0 and suml2+l[i]>0:
suml2+=l[i]
continue
elif suml2>0 and suml2+l[i]>=0:
count+=suml2+l[i]+1
suml2=-1
else:
count+=-suml2-l[i]+1
suml2=1
k2=count
print(min(k1,k2))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, a[100010];
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int total = 0, count_f = 0, count_l = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
total += a[i];
if (total <= 0) {
count_f += 1 - total;
total = 1;
}
} else {
total += a[i];
if (total >= 0) {
count_f += 1 + total;
total = -1;
}
}
}
total = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
total += a[i];
if (total >= 0) {
count_l += 1 + total;
total = -1;
}
} else {
total += a[i];
if (total <= 0) {
count_l += 1 - total;
total = 1;
}
}
}
cout << min(count_f, count_l) << 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 | from numpy import cumsum,copy
n = int(input())
a = cumsum([int(i) for i in input().split()])
b = copy(a)
ans1 = ans2 = 0
sign = 1
for i in range(n):
if a[i] * sign <= 0:
k = abs(a[i] - sign)
a += sign*k
ans1 += k
sign *= -1
sign = -1
for i in range(n):
if b[i] * sign <= 0:
k = abs(b[i] - sign)
b += sign*k
ans2 += k
sign *= -1
print(min(ans1, ans2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int sum = 0;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sum += a[0];
int count = 0;
bool pos = true;
if (sum == 0) {
count++;
sum = -1;
}
if (sum <= 0) {
pos = false;
}
for (int i = 1; i < n; i++) {
if (!pos) {
sum += a[i];
if (sum <= 0) {
count += abs(sum) + 1;
sum = 1;
}
pos = true;
} else {
sum += a[i];
if (sum >= 0) {
count += sum + 1;
sum = -1;
}
pos = false;
}
}
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 | 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()
sum = 0
c = 0
for i in range(0, n-1):
sum += a[i]
sum2 = sum + a[i+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
c += h+l
print(c)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
char m[51][51];
int num[51][51] = {0};
int count(bool b, vector<int> a) {
int n = a.size();
int sum = 0;
int cnt = 0;
for (int i = 0; i < n; i++) {
int nsum = a[i] + sum;
if ((i % 2 == 1) ^ b) {
if (nsum >= 0) {
cnt += nsum + 1;
nsum = -1;
}
} else if (nsum <= 0) {
cnt += -nsum + 1;
nsum = 1;
}
sum = nsum;
}
return cnt;
}
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
cout << min(count(true, a), count(false, a)) << 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 | python2 | n=int(raw_input())
a=map(int,raw_input().split(' '))
c=0
for i in range(n):
s=sum(a[0:i+1])
if s==0:
if i==n-1:
a[i]+=1
c+=1
elif a[i+1]>=0:
a[i]-=1
c+=1
else:
a[i]+=1
c+=1
if i==(n-1): break
s=sum(a[0:i+1])
n_s=s+a[i+1]
if s*n_s>0:
if s>=0:
while n_s>=0:
a[i+1]-=1
c+=1
n_s=s+a[i+1]
else:
while n_s<=0:
a[i+1]+=1
c+=1
n_s=s+a[i+1]
print a
print c,a |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
lis=[]
now=0
for num in a:
now+=num
lis.append(now)
ans=10**10
cnt=0
sm=0
for i in range(len(lis)):
if i%2==0:
if lis[i]+sm >= 0:
add = lis[i]+sm+1
cnt+= add
sm-=add
else:
if lis[i]+sm <= 0:
add = 1-lis[i]-sm
cnt+= add
sm+=add
ans=min(ans,cnt)
cnt=0
sm=0
for i in range(len(lis)):
if i%2==1:
if lis[i]+sm >= 0:
add = lis[i]+sm+1
cnt+= add
sm-=add
else:
if lis[i]+sm <= 0:
add = 1-lis[i]-sm
cnt+= add
sm+=add
ans=min(ans,cnt)
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | fun main(args: Array<String>) {
val N = nextInt()
val A = listOfLong()
val B = A.toMutableList()
var sum = A[0]
var sign = A[0].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()
sum = A[0]
sign = A[0].sign()
val ans2 =
if (sign == -1) {
sum = 1
sign = 1
resolv((1 - A[0]))
} else {
sum = -1
sign = -1
resolv((1 + A[0]))
}
println(Math.min(ans1, ans2))
}
fun Long.sign() = if (this > 0) 1 else if (this < 0) -1 else 0
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 | cpp | #include <bits/stdc++.h>
using namespace std;
int solve(int *a, int n) {
int count = 0;
int calc = 0;
int state, pstate;
for (int i = 0; i < n; i++) cout << a[i] << " ";
cout << endl;
if (a[0] < 0) state = -1;
if (a[0] > 0) state = 1;
for (int i = 1; i < n; i++) {
pstate = state;
int tmp = a[i] + calc;
if (tmp < 0) state = -1;
if (tmp == 0) state = 0;
if (tmp > 0) state = 1;
if (pstate == state) {
if (state == -1) {
count += 1 - tmp;
calc += 1 - tmp;
state = 1;
} else if (state == 1) {
count += tmp + 1;
calc += -1 - tmp;
state = -1;
}
}
if (state == 0) {
if (pstate == -1) {
count += 1;
calc += 1;
state = 1;
} else if (pstate == 1) {
count += 1;
calc += -1;
state = -1;
}
}
}
return count;
}
int main() {
int n;
int ans;
int *a;
cin >> n;
a = new int[n];
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 1; i < n; i++) a[i] = a[i - 1] + a[i];
if (a[0] == 0) {
int bs, cs;
int *b = new int[n];
int *c = new int[n];
for (int i = 0; i < n; i++) b[i] = a[i] + 1;
for (int i = 0; i < n; i++) c[i] = a[i] - 1;
bs = solve(b, n);
cs = solve(c, n);
ans = bs < cs ? bs : cs;
} else
ans = solve(a, n);
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;
class C {
public:
template <typename T>
int sgn(T val) {
return (T(0) < val) - (val < T(0));
}
void solve(std::istream& in, std::ostream& out) {
ios::sync_with_stdio(false);
int n;
in >> n;
vector<long long int> a(n), p(n);
for (int i = 0; i < n; ++i) {
in >> a[i];
}
long long int steps = 0;
long long int steps2 = 0;
p[0] = a[0];
if (a[0] != 0) {
for (int i = 0; i < n - 1; ++i) {
p[i + 1] = p[i] + a[i + 1];
if (sgn(p[i]) != -sgn(p[i + 1])) {
steps += abs(p[i + 1]) + 1;
p[i + 1] = -sgn(p[i]);
}
}
} else {
p[0] = 1;
for (int i = 0; i < n - 1; ++i) {
p[i + 1] = p[i] + a[i + 1];
if (sgn(p[i]) != -sgn(p[i + 1])) {
steps += abs(p[i + 1]) + 1;
p[i + 1] = -sgn(p[i]);
}
}
p[0] = -1;
for (int i = 0; i < n - 1; ++i) {
p[i + 1] = p[i] + a[i + 1];
if (sgn(p[i]) != -sgn(p[i + 1])) {
steps2 += abs(p[i + 1]) + 1;
p[i + 1] = -sgn(p[i]);
}
}
steps = min(steps, steps2);
}
if (p[n - 1] == 0) {
}
out << steps << endl;
}
};
int main() {
C solver;
std::istream& in(std::cin);
std::ostream& out(std::cout);
solver.solve(in, out);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | # coding: utf-8
# Here your code
N = int(input())
a = [int(i) for i in input().split()]
result_1 = 0
before_sum =a[0]
if a[0] == 0:
before_sum = 1
result_1 += 1
after_sum =a[0]
for i in range(1,N):
before_sum = after_sum
after_sum = before_sum + a[i]
if before_sum * after_sum > 0:
if after_sum < 0:
result_1 += 1 - after_sum
after_sum = 1
elif after_sum > 0:
result_1 += 1 + after_sum
after_sum = -1
elif before_sum * after_sum == 0:
result_1 += 1
if before_sum < 0:
after_sum = 1
else:
after_sum = -1
if a[0] < 0:
before_sum = 1
elif a[0] >= 0:
before_sum = -1
after_sum =a[0]
result_2 = 1 + abs(before_sum)
for i in range(1,N):
before_sum = after_sum
after_sum = before_sum + a[i]
if before_sum * after_sum > 0:
if after_sum < 0:
result_2 += 1 - after_sum
after_sum = 1
elif after_sum > 0:
result_2 += 1 + after_sum
after_sum = -1
elif before_sum * after_sum == 0:
result_2 += 1
if before_sum < 0:
after_sum = 1
else:
after_sum = -1
print(min(result_1,result_2))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
int C[200000];
long long count = 0;
cin >> N;
for (int i = 0; i < N; i++) {
cin >> C[i];
}
int sum = C[0];
for (int i = 1; i < N; i++) {
if (sum < 0) {
sum += C[i];
while (sum <= 0) {
sum++;
count++;
}
continue;
}
if (sum > 0) {
sum += C[i];
while (sum >= 0) {
sum--;
count++;
}
continue;
}
}
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;
template <typename T>
inline T GCD(T a, T b) {
T c;
while (b != 0) {
c = a % b;
a = b;
b = c;
}
return a;
}
template <typename T>
inline T LCM(T a, T b) {
T c = GCD(a, b);
a /= c;
return a * b;
}
template <typename T>
inline T nCr(T a, T b) {
T i, r = 1;
for (i = 1; i <= b; i++) {
r *= (a + 1 - i);
r /= i;
}
return r;
}
template <typename T>
inline T nHr(T a, T b) {
return nCr(a + b - 1, b);
}
template <typename T>
inline T POW(T a, T b) {
int i, r = 1;
for (i = 1; i <= b; i++) {
r *= a;
}
return r;
}
int main(void) {
cin.tie(0);
ios::sync_with_stdio(false);
long long n, a[100000], sum[100001], ans = 0;
cin >> n;
sum[0] = 0;
for (int i = 0; i < (n); ++i) {
cin >> a[i];
sum[i + 1] = sum[i] + a[i];
if (i != 0 and sum[i] * sum[i + 1] >= 0) {
ans += (sum[i] > 0 ? sum[i + 1] + 1 : 1 - sum[i + 1]);
sum[i + 1] = (sum[i] > 0 ? -1 : 1);
}
}
cout << ans << "\n";
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
as_ = list(map(int, input().split()))
ans = 0
sum_ = as_[0]
if sum_ == 0 and as_[1] <= 0:
sum_ += 1
ans += 1
elif sum_ == 0 and as_[1] < 0:
sum_ += 1
ans += 1
else:
pass
for i in range(1, n):
new_sum_ = sum_+as_[i]
if sum_ > 0 and new_sum_ >= 0:
as_[i] -= (1+new_sum_)
ans += 1+new_sum_
sum_ = -1
elif sum_ < 0 and new_sum_ <= 0:
as_[i] += (1-new_sum_)
ans += 1-new_sum_
sum_ = 1
else:
sum_ = new_sum_
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long MOD = pow(10, 9) + 7;
long long mod(long long A, long long M) { return (A % M + M) % M; }
const long long INF = 1LL << 60;
template <class T>
bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T>
bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
long long divCeil(long long A, long long B) { return (A + (B - 1)) / B; }
long long myctoi(char C) { return C - '0'; }
char myitoc(long long N) { return '0' + N; }
signed main() {
long long N;
cin >> N;
vector<long long> A(N);
for (long long i = 0; i < N; i++) {
cin >> A.at(i);
}
long long ans0 = 0, sum = 1;
for (long long i = 0; i < N; i++) {
long long s = sum;
sum += A.at(i);
if (s > 0 && sum >= 0) {
ans0 += 1 + sum;
sum = -1;
} else if (s < 0 && sum <= 0) {
ans0 += 1 - sum;
sum = 1;
}
}
long long ans1 = 0;
sum = -1;
for (long long i = 0; i < N; i++) {
long long s = sum;
sum += A.at(i);
if (s > 0 && sum >= 0) {
ans1 += 1 + sum;
sum = -1;
} else if (s < 0 && sum <= 0) {
ans1 += 1 - sum;
sum = 1;
}
}
cout << min(ans0, ans1) - 1 << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n + 1, 0);
for (int i = 1; i <= n; i++) {
cin >> a[i];
a[i] += a[i - 1];
}
long long ans = 1LL << 50;
for (int k = -1; k <= 1; k += 2) {
int sign = k;
int plus = 0, minus = 0;
for (int i = 1; i <= n; i++) {
if (a[i] + plus - minus > 0) {
if (sign == -1) minus += a[i] + plus - minus + 1;
} else if (a[i] + plus - minus < 0) {
if (sign == 1) plus += -(a[i] + plus - minus) + 1;
} else {
if (sign == -1)
minus += a[i] + plus - minus + 1;
else if (sign == 1)
plus += -(a[i] + plus - minus) + 1;
}
sign *= -1;
}
if (ans > plus + minus) ans = plus + minus;
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N=int(input())
a=input().split()
ma=list(map(int,a))
A1=ma[0]
if ma[0]>0:
NEXT=-1-ma[0]
elif ma[0]<0:
NEXT=1-ma[0]
ans=0
for k in range(1,N):
if ma[0] > 0:
if k%2 ==0:
if NEXT<=ma[k]:
NEXT=NEXT-ma[k]-2
else:
ans+=abs(ma[k]-NEXT)
NEXT=-2
elif k%2 ==1:
if NEXT>=ma[k]:
NEXT=NEXT-ma[k]+2
else:
ans+=abs(ma[k]-NEXT)
NEXT=2
elif ma[0]<0:
if k%2 ==1:
if NEXT<=ma[k]:
NEXT=NEXT-ma[k]-2
else:
ans+=abs(ma[k]-NEXT)
NEXT=-2
elif k%2 ==0:
if NEXT>=ma[k]:
NEXT=NEXT-ma[k]+2
else:
ans+=abs(ma[k]-NEXT)
NEXT=2
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <bits/stdc++.h>
int main(void) {
double num[10 * 10 * 10 * 10 * 10];
int i, n, ssign;
double sum = 0;
double count = 0;
double fsum, fnum;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%lf", &num[i]);
}
if (num[0] == 0) {
num[0]++;
count++;
}
for (i = 1; i < n; i++) {
sum += num[i - 1];
fsum = fabs(sum);
fnum = fabs(num[i]);
while (1) {
if (fsum > fnum) {
if (sum < 0) {
num[i]++;
count++;
} else if (sum > 0) {
num[i]--;
count++;
}
} else if (fsum == fnum) {
if (sum < 0) {
num[i]++;
count++;
} else {
num[i]--;
count++;
}
} else if (fsum < fnum && sum > 0 && num[i] > 0) {
num[i]--;
count++;
} else if (fsum < fnum && sum < 0 && num[i] < 0) {
num[i]++;
count++;
} else
break;
}
}
for (i = 0; i < n; i++) {
sum += num[i];
if (sum == 0.0) {
if ((sum - num[i]) > 0)
num[i]--;
else
num[i]++;
count++;
}
}
printf("%.0f\n", count);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import sys
from itertools import accumulate
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007
def solve(A):
ans = 0
s = A[0]
for a in A[1:]:
prev, s = s, s + a
if prev > 0 and s >= 0:
ans += s + 1
s = -1
if prev < 0 and s <= 0:
ans += -s + 1
s = 1
return ans
def main():
N, *A = map(int, read().split())
if A[0] != 0:
ans = solve(A)
else:
A[0] = 1
ans1 = solve(A)
A[0] = -1
ans2 = solve(A)
ans = min(ans1, ans2)
print(ans)
return
if __name__ == '__main__':
main()
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
int main() {
int n;
cin >> n;
vector<long long> a(n + 1);
vector<long long> tot(n + 1, 0);
vector<long long> tota(n + 1, 0);
for (int i = 1; i <= (int)(n); i++) {
cin >> a.at(i);
}
long long count = 0;
for (int i = 1; i <= (int)(n); i++) {
if (i == 1) {
if (a.at(1) < 0) {
count += (1 - a.at(1));
tot.at(1) = 1;
continue;
}
tot.at(1) = a.at(1);
continue;
}
if (tot.at(i - 1) > 0) {
long long sum = tot.at(i - 1) + a.at(i);
if (sum >= 0) {
tot.at(i) = -1;
count += sum + 1;
} else
tot.at(i) = tot.at(i - 1) + a.at(i);
}
if (tot.at(i - 1) < 0) {
long long big = tot.at(i - 1) + a.at(i);
if (big <= 0) {
tot.at(i) = 1;
count += (1 - big);
} else
tot.at(i) = tot.at(i - 1) + a.at(i);
}
}
long long count2 = 0;
for (int i = 1; i <= (int)(n); i++) {
if (i == 1) {
if (a.at(1) > 0) {
count2 += (1 + a.at(1));
tota.at(1) = -1;
continue;
}
tota.at(1) = a.at(1);
continue;
}
if (tota.at(i - 1) > 0) {
long long sum = tota.at(i - 1) + a.at(i);
if (sum >= 0) {
tota.at(i) = -1;
count2 += sum + 1;
} else
tota.at(i) = tota.at(i - 1) + a.at(i);
}
if (tota.at(i - 1) < 0) {
long long big = tota.at(i - 1) + a.at(i);
if (big <= 0) {
tota.at(i) = 1;
count2 += (1 - big);
} else
tota.at(i) = tota.at(i - 1) + a.at(i);
}
}
cout << min(count, count2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> A(n, 0);
for (int i = 0; i<n; i++) cin >> A[i];
long long cnt1=0, acm1=0, cnt2=0, acm2=0;
for(int i = 0; i<n; i++) {
acm1+=A[i];
acm2+=A[i];
if(i%2) {
if(acm1>0);
else {
cnt1 += abs(acm1) + 1;
acm1 = 1;
}
if(acm2<0);
else {
cnt2 += abs(acm2) + 1;
acm2 = -1;
}
else {
if(acm1<0);
else {
cnt1 += abs(acm1) + 1;
acm1 = -1;
}
if(acm2>0);
else {
cnt2 += abs(acm2) + 1;
acm2 = 1;
}
}
cout << min(cnt1, cnt2) << "\n";
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | def sign(X):
if X==0:
return 0
else:
return [-1,1][X>0]
N = int(input())
A = [int(T) for T in input().split()]
Count = 0
SumNow = 0
SumRes = 0
for TA in range(0,N):
SumNow += A[TA]
if SumNow==0 or sign(SumNow)==sign(SumRes):
if SumNow==0 and sign(SumRes)==1:
Count += 1
SumNow = -1
elif SumNow==0 and sign(SumRes)==-1:
Count += 1
SumNow = 1
elif sign(SumNow)==1 and sign(SumRes)==1:
Count += SumNow+1
SumNow = -1
elif sign(SumNow)==-1 and sign(SumRes)==-1:
Count += 1-SumNow
SumNow = 1
SumRes = SumNow
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;
using namespace std;
int main() {
int n;
cin >> n;
long long int a[n];
long long int sum = 0;
for (long long i = 0; i < n; i++) {
int x;
cin >> x;
sum += x;
a[i] = sum;
}
int f = 1;
long long int ans1 = 0, ans2 = 0;
long long int fix = 0;
for (long long i = 0; i < n; i++) {
if (f == 1) {
if (a[i] + fix <= 0) {
ans1 += 1 - (fix + a[i]);
fix += 1 - (fix + a[i]);
}
f = -1;
cout << a[i] + fix << endl;
} else {
if (a[i] + fix >= 0) {
ans1 += (fix + a[i]) + 1;
fix -= ((fix + a[i]) + 1);
}
f = 1;
}
}
f = -1;
fix = 0;
for (long long i = 0; i < n; i++) {
if (f == 1) {
if (a[i] + fix <= 0) {
ans2 += 1 - (fix + a[i]);
fix += 1 - (fix + a[i]);
}
f = -1;
} else {
if (a[i] + fix >= 0) {
ans2 += (fix + a[i]) + 1;
fix -= ((fix + a[i]) + 1);
}
f = 1;
}
}
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;
int main() {
long long n;
cin >> n;
vector<long long> A(n), sum(n);
for (long long i = (0); i < (long long)(n); i++) cin >> A[i];
long long cnt1 = 0;
if (A[0] > 0) {
sum[0] = A[0];
} else {
sum[0] = 1;
cnt1 += abs(1 - A[0]);
}
for (long long i = (1); i < (long long)(n); i++) {
if (sum[i - 1] > 0) {
if (sum[i - 1] + A[i] < 0) {
sum[i] = sum[i - 1] + A[i];
} else {
sum[i] = -1;
cnt1 += abs(sum[i - 1] + A[i] - (-1));
}
} else {
if (sum[i - 1] + A[i] > 0) {
sum[i] = sum[i - 1] + A[i];
} else {
sum[i] = -1;
cnt1 += abs(1 - (sum[i - 1] + A[i]));
}
}
}
long long cnt2 = 0;
if (A[0] < 0) {
sum[0] = A[0];
} else {
sum[0] = 1;
cnt2 += abs(A[0] - (-1));
}
for (long long i = (1); i < (long long)(n); i++) {
if (sum[i - 1] > 0) {
if (sum[i - 1] + A[i] < 0) {
sum[i] = sum[i - 1] + A[i];
} else {
sum[i] = -1;
cnt2 += abs(sum[i - 1] + A[i] - (-1));
}
} else {
if (sum[i - 1] + A[i] > 0) {
sum[i] = sum[i - 1] + A[i];
} else {
sum[i] = -1;
cnt2 += abs(1 - (sum[i - 1] + A[i]));
}
}
}
long long cnt3 = 0;
if (A[n - 1] > 0) {
sum[n - 1] = A[n - 1];
} else {
sum[n - 1] = 1;
cnt3 += abs(1 - A[n - 1]);
}
for (long long i = n - 2; i >= 0; i--) {
if (sum[i + 1] > 0) {
if (sum[i + 1] + A[i] < 0) {
sum[i] = sum[i + 1] + A[i];
} else {
sum[i] = -1;
cnt3 += abs(sum[i + 1] + A[i] - (-1));
}
} else {
if (sum[i + 1] + A[i] > 0) {
sum[i] = sum[i + 1] + A[i];
} else {
sum[i] = -1;
cnt3 += abs(1 - (sum[i + 1] + A[i]));
}
}
}
long long cnt4 = 0;
if (A[n - 1] < 0) {
sum[n - 1] = A[n - 1];
} else {
sum[n - 1] = -1;
cnt4 += abs(A[n - 1] - (-1));
}
for (long long i = n - 2; i >= 0; i--) {
if (sum[i + 1] > 0) {
if (sum[i + 1] + A[i] < 0) {
sum[i] = sum[i + 1] + A[i];
} else {
sum[i] = -1;
cnt4 += abs(sum[i + 1] + A[i] - (-1));
}
} else {
if (sum[i + 1] + A[i] > 0) {
sum[i] = sum[i + 1] + A[i];
} else {
sum[i] = -1;
cnt4 += abs(1 - (sum[i + 1] + A[i]));
}
}
}
cout << min(min(cnt1, cnt2), min(cnt3, cnt4)) << 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()))
count = 0
sum_i = a[0]
if sum_i < 0:
sum_i = 1
pos_count = 1 - sum_i
for i in range(1, n):
if sum_i < 0:
sum_i += a[i]
if sum_i <= 0:
pos_count += 1 - sum_i
sum_i = 1
elif sum_i > 0:
sum_i += a[i]
if sum_i >= 0:
pos_count += sum_i + 1
sum_i = -1
sum_i = a[0]
neg_count = 0
for i in range(1, n):
if sum_i < 0:
sum_i += a[i]
if sum_i <= 0:
neg_count += 1 - sum_i
sum_i = 1
elif sum_i > 0:
sum_i += a[i]
if sum_i >= 0:
neg_count += sum_i + 1
sum_i = -1
elif sum_i > 0:
sum_i = a[0]
pos_count = 0
for i in range(1, n):
if sum_i < 0:
sum_i += a[i]
if sum_i <= 0:
pos_count += 1 - sum_i
sum_i = 1
elif sum_i > 0:
sum_i += a[i]
if sum_i >= 0:
pos_count += sum_i + 1
sum_i = -1
sum_i = -1
neg_count = sum_i + 1
for i in range(1, n):
if sum_i < 0:
sum_i += a[i]
if sum_i <= 0:
neg_count += 1 - sum_i
sum_i = 1
elif sum_i > 0:
sum_i += a[i]
if sum_i >= 0:
neg_count += sum_i + 1
sum_i = -1
else:
sum_i = 1
pos_count = 1
for i in range(1, n):
if sum_i < 0:
sum_i += a[i]
if sum_i <= 0:
pos_count += 1 - sum_i
sum_i = 1
elif sum_i > 0:
sum_i += a[i]
if sum_i >= 0:
pos_count += sum_i + 1
sum_i = -1
sum_i = -1
neg_count = 1
for i in range(1, n):
if sum_i < 0:
sum_i += a[i]
if sum_i <= 0:
neg_count += 1 - sum_i
sum_i = 1
elif sum_i > 0:
sum_i += a[i]
if sum_i >= 0:
neg_count += sum_i + 1
sum_i = -1
if neg_count < pos_count:
count = neg_count
else:
count = pos_count
print(count)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.*;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] nums = new int[n];
for(int i = 0; i < n; i++){
nums[i] = sc.nextInt();
}
int result = 0;
int sum = 0;
// + - + -
for(int i = 0; i < n; i++){
if(i % 2 == 0 && sum + nums[i] <= 0){
result += Math.abs(1 - (sum + nums[i]));
sum = 1;
}
else if(i % 2 == 1 && sum + nums[i] >= 0){
result += Math.abs(-1 - (sum + nums[i]));
sum = -1;
}
else{
sum += nums[i];
}
}
int result2 = 0;
sum = 0;
// - + - +
for(int i = 0; i < n; i++){
if(i % 2 == 1 && sum + nums[i] <= 0){
result2 += Math.abs(1 - (sum + nums[i]));
sum = 1;
}
else if(i % 2 == 0 && sum + nums[i] >= 0){
result2 += Math.abs(-1 - (sum + nums[i]));
sum = -1;
}
else{
sum += nums[i];
}
}
System.out.println(Math.min(result, result2));
sc.close();
}
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.