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.at(i);
}
int ans = 0;
int S = a.at(0);
int start = 0;
while (start < N && a.at(start) == 0) {
start++;
}
if (start != 0) {
ans = start * 2 - 1;
if (start < N) {
if (a.at(start) > 0) {
S = -1 + a.at(start);
if (!S) {
ans++;
S = 1;
}
} else {
S = 1 + a.at(start);
if (!S) {
ans++;
S = -1;
}
}
}
}
for (int i = (start + 1); i < (N); ++i) {
if (S > 0) {
S += a.at(i);
if (S >= 0) {
ans += (S + 1);
S = -1;
}
} else {
S += a.at(i);
if (S <= 0) {
ans += (-S + 1);
S = 1;
}
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
long long a[100010];
long long sum[100010] = {0};
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
int ans = 0;
if (a[0] >= 0) {
for (int i = 0; i < n; i++) {
int j = i;
while (j >= 0) {
sum[i] += a[j];
j--;
}
if (i % 2 == 0) {
if (sum[i] <= 0) {
while (sum[i] <= 0) {
sum[i]++;
a[i]++;
ans++;
}
}
} else {
if (sum[i] >= 0) {
while (sum[i] >= 0) {
sum[i]--;
a[i]--;
ans++;
}
}
}
}
if (sum[n - 1] == 0) ans++;
} else {
for (int i = 0; i < n; i++) {
int j = i;
while (j >= 0) {
sum[i] += a[j];
j--;
}
if (i % 2 == 0) {
if (sum[i] >= 0) {
while (sum[i] >= 0) {
sum[i]--;
a[i]--;
ans++;
}
}
} else {
if (sum[i] <= 0) {
while (sum[i] <= 0) {
sum[i]++;
a[i]++;
ans++;
}
}
}
}
if (sum[n - 1] == 0) ans++;
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = (0); i < (n); ++i) cin >> a[i];
ll sum = a[0];
ll ans = 0;
for (int i = (1); i < (n); ++i) {
sum += a[i];
if (sum * (sum - a[i]) < 0)
;
else {
if (sum >= 0) {
ans += sum + 1;
sum = -1;
} else {
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() {
long long dptemp[100010];
long long s1[100010], dp[100010];
long long mi = 0x3f3f3f3f, n, a, sum, pri1, pri2, all;
scanf("%lld", &n);
dp[0] = 0;
for (a = 1; a <= n; a++) {
scanf("%lld", &s1[a]);
dp[a] = s1[a] + dp[a - 1];
dptemp[a] = dp[a];
}
sum = 0;
all = 0;
if (dp[1] == 0) {
dp[1]++;
sum++;
all = 1;
for (a = 2; a <= n; a++) {
dp[a] = (dp[a - 1] + s1[a]);
if (dp[a - 1] > 0) {
if (dp[a] >= 0) {
sum -= (dp[a] + 1);
all += (dp[a] + 1);
dp[a] = -1;
}
} else {
if (dp[a] <= 0) {
sum += (-dp[a] + 1);
all += (-dp[a] + 1);
dp[a] = 1;
}
}
}
if (all < mi) mi = all;
for (a = 1; a <= n; a++) dp[a] = dptemp[a];
dp[1]--;
sum--;
all = 1;
for (a = 2; a <= n; a++) {
dp[a] = (dp[a - 1] + s1[a]);
if (dp[a - 1] > 0) {
if (dp[a] > 0) {
sum -= (dp[a] + 1);
all += (dp[a] + 1);
dp[a] = -1;
}
} else {
if (dp[a] <= 0) {
sum += (-dp[a] + 1);
all += (-dp[a] + 1);
dp[a] = 1;
}
}
}
if (all < mi) mi = all;
} else if (dp[1] > 0) {
sum = 0;
all = 0;
for (a = 1; a <= n; a++) dp[a] = dptemp[a];
for (a = 2; a <= n; a++) {
dp[a] = (dp[a - 1] + s1[a]);
if (dp[a - 1] > 0) {
if (dp[a] >= 0) {
sum -= (dp[a] + 1);
all += (dp[a] + 1);
dp[a] = -1;
}
} else {
if (dp[a] <= 0) {
sum += (-dp[a] + 1);
all += (-dp[a] + 1);
dp[a] = 1;
}
}
}
if (all < mi) mi = all;
} else {
sum = 0;
all = 0;
for (a = 2; a <= n; a++) {
dp[a] = (dp[a - 1] + s1[a]);
if (dp[a - 1] > 0) {
if (dp[a] > 0) {
sum -= (dp[a] + 1);
all += (dp[a] + 1);
dp[a] = -1;
}
} else {
if (dp[a] <= 0) {
sum += (-dp[a] + 1);
all += (-dp[a] + 1);
dp[a] = 1;
}
}
}
if (all < mi) mi = all;
}
printf("%lld\n", mi);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void) {
int n;
cin >> n;
vector<long long int> a(n);
for (int i = 0; i < (n); i++) {
cin >> a[i];
}
long long int oddcount = 0, evencount = 0;
long long int oddsum = 0, evensum = 0;
bool odd = true, even = false;
for (int i = 0; i < (n); i++) {
oddsum += a[i];
evensum += a[i];
if (odd && oddsum <= 0) {
oddcount += 1 - oddsum;
oddsum = 1;
}
if (even && oddsum >= 0) {
oddcount += 1 + oddsum;
oddsum = -1;
}
if (even && evensum <= 0) {
evencount += 1 - evensum;
evensum = 1;
}
if (odd && evensum >= 0) {
evencount += 1 + evensum;
evensum = -1;
}
odd = !odd;
even = !even;
}
assert(false);
cout << fmin(oddcount, evencount) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | n = gets.to_i
arr = gets.chomp.split(" ").map(&:to_i)
$count = [0,0]
def check(i,arr,t)
if i > arr.size - 1
arr[t] += 1
$count += 1
return
end
if arr[i] > 0
arr[t] -= 1
$count += 1
elsif arr[i] < 0
arr[t] += 1
$count += 1
else
check(i+1,arr,t)
end
end
flg = true
2.times do |j|
tmp_arr = Marshal.load(Marshal.dump(arr))
sum = tmp_arr[0] + tmp_arr[1]
if sum == 0
if flg
tmp_arr[1] -= 1
else
tmp_arr[1] += 1
end
$count[j] += 1
end
if flg
if sum > 0
tmp_arr[1] -= sum+1
$count[j] += sum+1
end
else
if sum < 0
tmp_arr[1] += sum+1
$count[j] += sum+1
end
end
sum = tmp_arr[0] + tmp_arr[1]
(2...tmp_arr.size).each do |i|
diff = sum + tmp_arr[i]
# puts %(sum : #{sum})
# puts %(diff : #{diff})
if sum > 0
if diff > 0
tmp_arr[i] -= diff.abs+1
$count[j] += diff.abs+1
elsif diff == 0
tmp_arr[i] -= 1
$count[j] += 1
end
else
if diff < 0
tmp_arr[i] += diff.abs+1
$count[j] += diff.abs+1
elsif diff == 0
tmp_arr[i] += 1
$count[j] += 1
end
end
sum += tmp_arr[i]
# p tmp_arr
end
flg = false
end
#p $count
#p arr
puts $count.min |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int n, option;
ll ch[2];
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cin >> n;
vector<ll> a(n);
for (auto& i : a) cin >> i;
for (int i = 0; i < n; ++i) {
if (a[i] >= 0) ch[i % 2] += a[i] + 1;
}
option = (ch[0] >= ch[1]);
ll cur = 0, ans = 0;
for (int i = 0; i < n; ++i) {
cur += a[i];
if (i % 2 == option && cur >= 0) {
ans += cur + 1;
cur = -1;
} else if (i % 2 != option && cur <= 0) {
ans += -1ll * (cur - 1);
cur = 1;
}
}
cout << ans;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
int main() {
size_t N;
std::cin >> N;
std::vector<int> A(N);
for (size_t n = 0; n < N; ++n) {
std::cin >> A[n];
}
int64_t a = 0;
int64_t c = A[0];
for (size_t n = 1; n < N; ++n) {
if (c < 0) {
if (c + A[n] <= 0) {
a += -(c + A[n]) + 1;
c = 1;
} else {
c = c + A[n];
}
} else {
if (c + A[n] >= 0) {
a += c + A[n] + 1;
c = -1;
} else {
c = c + A[n];
}
}
}
std::cout << a << std::endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long INF = (1LL << 62);
long long N;
vector<long long> A, W;
long long S[100002] = {INF * (-1)};
long long dp[100002] = {0};
int sign = 0;
int b;
void calcDP(int n) {
if (n == 1) {
if (sign == 1) {
if (W[1] > 0) {
dp[1] = 0;
} else {
dp[1] = abs(1 - W[1]);
W[1] = 1;
}
} else if (sign == -1) {
if (W[1] < 0) {
dp[1] = 0;
} else {
dp[1] = abs(-1 - W[1]);
W[1] = -1;
}
}
S[1] = W[1];
return;
} else {
S[n] = S[n - 1] + W[n];
if ((S[n - 1] < 0 && S[n] > 0) || (S[n - 1] > 0 && S[n] < 0)) {
dp[n] = dp[n - 1];
} else {
if (S[n - 1] > 0) {
dp[n] = dp[n - 1] + abs(-1 - S[n - 1] - W[n]);
W[n] = -1 - S[n - 1];
} else {
dp[n] = dp[n - 1] + abs(1 - S[n - 1] - W[n]);
W[n] = 1 - S[n - 1];
}
S[n] = S[n - 1] + W[n];
}
return;
}
}
int main(int argc, char* argv[]) {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> N;
W.push_back(0);
S[0] = 0;
long long m = 0;
b = 1;
for (int i = 1; i <= N; i++) {
long long a;
cin >> a;
A.push_back(a);
W.push_back(a);
if (i == 1) {
S[1] = a;
} else {
S[i] = S[i - 1] + a;
}
if (abs(a) > m) {
m = abs(a);
b = i;
}
}
if ((b % 2) == 1) {
sign = abs(W[b]) / W[b];
} else {
sign = (abs(W[b]) / W[b]) * (-1);
}
for (int i = 1; i <= N; i++) {
calcDP(i);
}
printf("%lld\n", dp[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() {
int n, i, c1 = 0, c2 = 0;
cin >> n;
int a[n], s[2];
for (i = 0; i < n; i++) {
cin >> a[i];
}
s[0] = a[0], s[1] = 0;
for (; s[0] > -1; c1++) s[0]--;
for (i = 1; i < n; i++) {
s[1] = s[0] + a[i];
if ((s[0] < 0 && s[1] <= 0) || (s[0] > 0 && s[1] >= 0)) {
if (s[0] < 0)
for (; s[1] < 1;) s[1]++, c1++;
else
for (; s[1] > -1;) s[1]--, c1++;
}
s[0] = s[1];
}
cout << endl;
s[0] = a[0], s[1] = 0;
for (; s[0] < 1; c2++) s[0]++;
for (i = 1; i < n; i++) {
s[1] = s[0] + a[i];
if ((s[0] < 0 && s[1] <= 0) || (s[0] > 0 && s[1] >= 0)) {
if (s[0] < 0)
for (; s[1] < 1;) s[1]++, c2++;
else
for (; s[1] > -1;) s[1]--, c2++;
}
s[0] = s[1];
}
cout << min(c1, c2);
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>
const int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
using namespace std;
int main() {
int n;
cin >> n;
long long a[n];
long long sums[n];
cin >> a[0];
sums[0] = a[0];
for (int i = 1; i < n; ++i) {
cin >> a[i];
sums[i] = sums[i - 1] + a[i];
}
long long cnt = 0;
long long v = 0;
long long diff;
for (int i = 1; i < n; i++) {
sums[i] += v;
if (sums[i - 1] * sums[i] >= 0) {
if (sums[i - 1] < 0) {
diff = 1 - sums[i];
} else {
diff = -1 - sums[i];
}
sums[i] += diff;
v += diff;
cnt += abs(diff);
}
}
cout << cnt << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | # encoding:utf-8
import copy
import random
import bisect #bisect_left これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
mod = 10**9+7
n = int(input())
a = [int(i) for i in input().split()]
if a[0] > 0:
status_pos = True
else:
status_pos = False
tmp = 0
ans = 0
for i in range(n):
tmp += a[i]
if tmp == 0:
ans += 1
if status_pos:
tmp = -1
else:
tmp = 1
elif status_pos and tmp < 0:
ans += 1+abs(tmp)
tmp = 1
elif status_pos == False and tmp > 0:
ans += 1+abs(tmp)
tmp = -1
status_pos = not(status_pos)
print(ans)
# ans2 = abs(a[0])+1
# if a[0] > 0:
# a[0] = -1
# else:
# a[0] = 1
#
# tmp = a[0]
# if a[0] > 0:
# status_pos = True
# else:
# status_pos = False
# for i in range(1,n):
# tmp += a[i]
# if status_pos and tmp <= 0:
# ans2 += 1-tmp
# tmp = 1
# elif status_pos == False and tmp >= 0:
# ans2 += 1+tmp
# tmp = -1
# status_pos = not(status_pos)
#
# print(min(ans,ans2))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <bits/stdc++.h>
long long int n;
long long int count(long long int a0) {
long long int a, i, S[2] = {}, C[2] = {};
S[0] = a0;
S[1] = a0;
for (i = 1; i < n; i++) {
scanf("%lld", &a);
S[0] += a;
S[1] += a;
if (i % 2 == 1) {
if (S[0] <= 0) {
C[0] += -1 * S[0] + 1;
S[0] = 1;
}
if (S[1] >= 0) {
C[1] += S[1] + 1;
S[1] = -1;
}
} else {
if (S[0] >= 0) {
C[0] += S[0] + 1;
S[0] = -1;
}
if (S[1] <= 0) {
C[1] += -1 * S[1] + 1;
S[1] = 1;
}
}
}
if (C[0] < C[1])
return C[0];
else
return C[1];
}
int main() {
long long int a0;
scanf("%lld %lld", &n, &a0);
printf("%lld\n", count(a0));
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | def main():
N = int(input())
A = [int(i) for i in input().split()]
ans1 = 0
S1 = [0] * N
S1[0] = A[0]
for i in range(1, N):
S1[i] = S1[i-1] + A[i]
if S1[i]*S1[i-1] < 0:
continue
if S1[i-1] > 0:
S1[i] = -1
ans1 += abs((-1) - (S1[i-1]+A[i]))
else:
S1[i] = 1
ans1 += abs(1 - (S1[i-1]+A[i]))
S2 = [0] * N
ans2 = 0
if A[0] > 0:
S2[0] = -1
ans2 += abs((-1) - A[0])
else:
S2[0] = 1
ans2 += abs(1 - A[0])
for i in range(1, N):
S2[i] = S2[i-1] + A[i]
if S2[i]*S2[i-1] < 0:
continue
if S2[i-1] > 0:
S2[i] = -1
ans2 += abs((-1) - (S2[i-1]+A[i]))
else:
S2[i] = 1
ans2 += abs(1 - (S2[i-1]+A[i]))
print(min(ans1, ans2))
if __name__ == '__main__':
main()
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int,input().split()))
s = 0
sign = a[0]//abs(a[0])
ans = 0
for i in range(n):
s += a[i]
if s == 0:
s = sign
ans += 1
elif s//abs(s) == sign:
pass
else:
ans += abs(sign-s)
s = sign
sign *= -1
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 | n1=int(input())
l1=list(map(int,input().split()))
total=l1[0]
Num=0
for j in range(1,n1):
if total ==0:
total=total+1
Num=Num+1
pretotal=total
total=total+l1[j]
while (pretotal*total>0) or (total ==0):
if pretotal<=0:
Num=Num+1
total=total+1
elif pretotal>0:
total=total-1
Num=Num+1
print(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 | java | import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner no=new Scanner(System.in);
int n=no.nextInt();
int arr[]=new int[n];
// int sum1[]=new int[n];
for(int i=0;i<n;i++){
arr[i]=no.nextInt();
/* if(i==0){
sum1[i]=arr[i];
}
else{
sum1[i]=sum1[i]+sum1[i-1];
}*/
}
int sum=0;
int count=0;
if(arr[0]>0){
sum=arr[0];
for(int i=1;i<n;i++){
if(i%2==1&&sum+arr[i]>=0){
int t=arr[i];
arr[i]=(sum+1)*-1;
count=count+Math.abs((t-arr[i]));
// sum=sum+arr[i];
}
else if(i%2==0&&sum+arr[i]<=0){
int t=arr[i];
arr[i]=(Math.abs(sum)+1);
count=count+Math.abs((Math.abs(t)-arr[i]));
//sum=sum+arr[i];
}
/*else if(sum+arr[i]==0){
count++;
}*/
sum=sum+arr[i];
}
}
else if(arr[0]<0){
sum=arr[0];
for(int i=1;i<n;i++){
if(i%2==1&&sum+arr[i]<=0){
int t=arr[i];
arr[i]=(Math.abs(sum)+1);
count=count+Math.abs((Math.abs(t)-arr[i]));
// sum=sum+arr[i];
// System.out.println(count);
}
else if(i%2==0&&sum+arr[i]>=0){
int t=arr[i];
arr[i]=(sum+1)*-1;
count=count+Math.abs((t-arr[i]));
//System.out.println(count);
//sum=sum+arr[i];
}
/* else if(sum+arr[i]==0){
count++;
}*/
sum=sum+arr[i];
}
}
System.out.println(count);
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
const int N = 1e5 + 10;
using namespace std;
int mod = 1e9 + 7;
int num[N], num2[N];
long long sum[N], sum2[N];
int main() {
int n;
while (~scanf("%d", &n)) {
long long ans = 0, ans2 = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", num + i);
num2[i] = num[i];
sum[i] = sum[i - 1] + num[i];
sum2[i] = sum2[i - 1] + num[2];
}
if (num[1] == 0) {
ans += 1;
num[1] = 1;
sum[1]++;
}
if (num2[1] == 0) {
ans2 += 1;
num2[1] = -1;
sum2[1]--;
}
for (int i = 2; i <= n; i++) {
sum[i] = sum[i - 1] + num[i];
int a = sum[i - 1] < 0;
int b = sum[i] < 0;
if (sum[i] == 0) {
if (sum[i - 1] < 0)
num[i]++;
else
num[i]--;
sum[i] = sum[i - 1] + num[i];
ans++;
} else if (!(a ^ b)) {
if (sum[i] < 0) {
num[i] -= sum[i];
num[i]++;
ans -= sum[i];
sum[i] = sum[i - 1] + num[i];
ans++;
} else if (sum[i] > 0) {
num[i] -= sum[i];
num[i]--;
ans += sum[i];
ans++;
sum[i] = num[i] + sum[i - 1];
}
}
}
for (int i = 2; i <= n; i++) {
sum2[i] = sum2[i - 1] + num2[i];
int a = sum2[i - 1] < 0;
int b = sum2[i] < 0;
if (sum2[i] == 0) {
if (sum2[i - 1] < 0)
num2[i]++;
else
num2[i]--;
sum2[i] = sum2[i - 1] + num2[i];
ans2++;
} else if (~a ^ b) {
if (sum2[i] < 0) {
num2[i] -= sum2[i];
num2[i]++;
ans2 -= sum2[i];
sum2[i] = sum2[i - 1] + num2[i];
ans2++;
} else if (sum2[i] > 0) {
num2[i] -= sum2[i];
num2[i]--;
ans2 += sum2[i];
ans2++;
sum2[i] = num2[i] + sum2[i - 1];
}
}
}
printf("%lld\n", min(ans, ans2));
}
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
li = list(map(int,input().split()))
ans = 0
cnt = 0
s = 0
for i in range(n):
if i == 0:
ans += li[i]
if ans > 0:
s = 1
else:
s = -1
else:
ans += li[i]
if ans <= 0 and s == -1:
cnt += abs(ans) + 1
ans = 1
if ans >= 0 and s == 1:
cnt += abs(ans) + 1
ans = -1
s *= -1
print(cnt) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long solve(long long *a, int n) {
long long count = 0;
long long calc = 0;
int state, pstate;
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;
}
}
cout << a[i] + calc << endl;
}
return count;
}
int main() {
int n;
long long ans;
long long *a;
cin >> n;
a = new long long[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) {
long long bs, cs;
long long *b = new long long[n];
long long *c = new long long[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) + 1;
cs = solve(c, n) + 1;
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;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
long n;
cin >> n;
long long a;
long long sum = 0;
long long ans = 0;
long long x;
bool isPlus = true;
long i;
cin >> a;
sum += a;
if (a < 0) isPlus = false;
for (i = 1; i < n; i++) {
cin >> a;
if (isPlus) {
if (sum + a < 0) {
sum += a;
} else {
x = abs(sum - a) + 1;
if (a < 0)
ans += x;
else
ans += (a + a + x);
sum -= (a + x);
}
} else {
if (0 < sum + a) {
sum += a;
} else {
x = ((sum + a) < 0 ? (sum + a) * -1 : (sum + a)) + 1;
ans += x;
sum += (a + x);
}
}
cerr << "x = " << x << endl;
cerr << sum << endl;
isPlus = !isPlus;
}
if (sum == 0) {
if (a < 0)
ans--;
else
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 | python3 | n=int(input())
a= (list(map(int,input().split())))
sm1=sm2=0
cnt1=cnt2=0
for i ,num in enumerate(a,1):
sm1+=num
if sm1<1 and i%2!=0:
cnt1+=1-sm1
sm=1
elif sm1>-1 and i%2==0:
cnt1+=1+sm1
sm=-1
sm2+=num
if sm2<1 and i%2==0:
cnt2+=1-sm2
sm=1
elif sm2>-1 and i%2==1:
cnt2+=1+sm2
sm2=-1
print(min(cnt1,cnt2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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 | n1=int(input())
l1=list(map(int,input().split()))
total=l1[0]
Num=0
for j in range(1,n1):
pretotal=total
total=total+l1[j]
if pretotal ==0:
total=total+(total)/abs(total)
Num=Num+1
while (pretotal*total>0) or (total ==0):
if total==0:
Num=Num+1
if pretotal<0:
total=1
else:
total=-1
elif pretotal<0:
Num=Num-total+1
total=+1
elif pretotal>0:
Num=Num+total+1
total=-1
print(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;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
int sum = 0, ans1 = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 0 && sum <= 0) {
ans1 += 1 - sum;
sum = 1;
}
if (i % 2 == 1 && sum >= 0) {
ans1 += 1 + sum;
sum = -1;
}
}
sum = 0;
int ans2 = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 1 && sum <= 0) {
ans2 += 1 - sum;
sum = 1;
}
if (i % 2 == 0 && sum >= 0) {
ans2 += 1 + sum;
sum = -1;
}
}
cout << min(ans1, ans2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> a(N);
for (int i = 0; i < N; i++) cin >> a[i];
if (a[0] < 0)
for (int i = 0; i < N; i++) a[i] *= -1;
long long ans = 0, sum = 0, idx = 0;
if (a[0] == 0) {
a[0] = 1;
ans += 1;
}
sum = a[0];
for (int i = 1; i < N; i++) {
if (i % 2 == 0) {
sum += a[i];
if (sum <= 0) {
ans += 1 - sum;
sum = 1;
}
} else {
sum += a[i];
if (sum >= 0) {
ans += sum + 1;
sum = -1;
}
}
}
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 main() {
bool ch = false;
int N;
long long ans = 0, a, count = 0;
cin >> N;
cin >> a;
ans += a;
if (ans > 0)
ch = true;
else
ch = false;
for (int i = 1; i < N; i++) {
cin >> a;
if (ch) {
if (ans >= -a) {
count += ans + a + 1;
ans = -1;
} else
ans += a;
ch = false;
} else {
if (ans <= -a) {
count += -ans - a + 1;
ans = 1;
} else
ans += a;
ch = true;
}
}
cout << count << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
A2 = list(map(int,input().split()))
#print(A)
def getSign(a):
if a < 0:
return -1
elif a == 0:
return 0
else:
return 1
counts = []
for j in range(2):
A = list(A2)
count = 0
sumN = A[0]
beforeSign = getSign(A[0])
if j == 0:
add = -A[j] - getSign(A[j])
A[j] += add
count += abs(add)
for i in range(1,N):
sumN += A[i]
#print("be",i,sumN,A[i],count)
if 0 <= beforeSign * sumN:
add = -sumN - beforeSign
A[i] += add
sumN += add
count += abs(add)
beforeSign = getSign(sumN)
#print("af",i,sumN,A[i],count)
counts.append(count)
print(min(counts)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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;
long a[100001];
for (long i = 0; i < N; i++) {
cin >> a[i];
}
long total = a[0];
long ops = 0;
if (total == 0) {
ops++;
if (a[1] > 0) {
total = -1;
} else {
total = 1;
}
}
for (long i = 1; i < N; i++) {
if (total > 0 && (total + a[i]) >= 0) {
ops += a[i] + total + 1;
total = -1;
} else if (total < 0 && (total + a[i]) <= 0) {
ops += -(a[i] + total) + 1;
total = 1;
} else {
total += a[i];
}
}
printf("%d\n", ops);
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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 <string>
#include <vector>
#include <numeric>
#include <queue>
#include <unordered_map>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <sstream>
#include <stack>
#include <map>
#include <set>
#include <ios>
#include <cctype>
#include <cstdio>
#include <functional>
#include <cassert>
#define REP(i,a) for(int i = 0;i < (a);++i)
#define FOR(i,a,b) for(int i = (a);i < (b); ++i)
#define FORR(i,a,b) for(int i = (a) - 1;i >=(b);--i)
#define ALL(obj) (obj).begin(),(obj).end()
#define SIZE(obj) (int)(obj).sizeT()
#define YESNO(cond,yes,no){cout <<((cond)?(yes):(no))<<endl; }
#define SORT(list) sort(ALL((list)));
#define RSORT(list) sort((list).rbegin(),(list).rend())
#define ASSERT(cond,mes) assert(cond && mes)
using namespace std;
using ll = long long;
constexpr int MOD = 1'000'000'007;
constexpr int INF = 1'050'000'000;
template<typename T>
T round_up(const T& a, const T& b) {
return (a + (b - 1)) / b;
}
template <typename T1, typename T2>
istream& operator>>(istream& is, pair<T1, T2>& p) {
is >> p.first >> p.second;
return is;
}
template <typename T1, typename T2>
ostream& operator<<(ostream& os, pair<T1, T2>& p) {
os << p.first << p.second;
return os;
}
template <typename T>
istream& operator>>(istream& is, vector<T>& v) {
REP(i, (int)v.size())is >> v[i];
return is;
}
template <typename T>
ostream& operator<<(ostream& os, vector<T>& v) {
REP(i, (int)v.size())os << v[i] << endl;
return os;
}
template <typename T>
T clamp(T& n, T a, T b) {
if (n < a)n = a;
if (n > b)n = b;
return n;
}
template <typename T>
static T GCD(T u, T v) {
T r;
while (v != 0) {
r = u % v;
u = v;
v = r;
}
return u;
}
template <typename T>
static T LCM(T u, T v) {
return u / GCD(u, v) * v;
}
template <typename T>
static int sign(T t) {
if (t > 0)return 1;
else if (t < 0)return -1;
else return 0;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
std::cout << fixed << setprecision(20);
int N;
cin >> N;
vector<int>A(N);
cin >> A;
int cnt = 0;
int sum = A[0];
FOR(i, 1, N) {
int next = sum + A[i];
if (sign(next) == 0 || sign(next) == sign(sum)) {
cnt += abs(next) + 1;
next = -sign(sum);
}
sum = next;
}
sum = -sign(A[0]);
int cnt2 = A[0] + 1;
FOR(i, 1, N) {
int next = sum + A[i];
if (sign(next) == 0 || sign(next) == sign(sum)) {
cnt2 += abs(next) + 1;
next = -sign(sum);
}
sum = next;
}
cout << min(cnt, 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;
long long int sum = 0, in, ans = 0;
cin >> n >> sum;
for (int i = 1; i < n; i++) {
cin >> in;
if (sum * in < 0) {
sum += in;
continue;
}
ans += abs(sum) + abs(in) + 1;
if (sum < 0) {
sum = 1;
} else {
sum = -1;
}
}
if (sum == 0) ans++;
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int,input().split()))
if n == 1:
print(0)
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 | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int N;
cin >> N;
vector<ll> A(N);
for (int i = 0; i < N; i++) cin >> A[i];
ll ret = 0;
ll sum = A[0];
for (int i = 1; i < N; i++) {
ll after = sum + A[i];
if (after * sum < 0) {
sum = after;
} else {
ret += abs(after) + 1;
if (sum > 0)
sum = -1;
else
sum = 1;
}
}
cout << ret << 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()))
#マイナススタート
now = 0
count = 0
for i,j in enumerate(a):
now += j
if i%2 == 0:
if now >= 0:
count += abs(now)+1
now = -1
else:
if now < 0:
count += abs(now)+1
now = 1
#プラススタート
now = 0
count2 = 0
for i,j in enumerate(a):
now += j
if i%2 == 0:
if now < 0:
count2 += abs(now)+1
now = 1
else:
if now >= 0:
count2 += abs(now)+1
now = -1
print(min(count,count2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.Scanner;
public class Main {
@SuppressWarnings("resource")
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++) {
a[i]=scanner.nextInt();
}
int r[]=new int[n];
r[0]=a[0];
int cnt1=0;
if(r[0]>=0) {
cnt1+=Math.abs(-1-r[0]);
r[0]=-1;
}
for(int i=1;i<n;i++) {
r[i]=r[i-1]+a[i];
if(i%2==1&&r[i]<=0) {
cnt1+=Math.abs(1-r[i-1]-a[i]);
r[i]=1;
}
if(i%2==0&&r[i]>=0) {
cnt1+=Math.abs(-1-r[i-1]-a[i]);
r[i]=-1;
}
}
r=new int[n];
r[0]=a[0];
int cnt2=0;
if(r[0]<=0) {
cnt2+=Math.abs(1-r[0]);
r[0]=1;
}
for(int i=1;i<n;i++) {
r[i]=r[i-1]+a[i];
if(i%2==1&&r[i]>=0) {
cnt2+=Math.abs(-1-r[i-1]-a[i]);
r[i]=-1;
}
if(i%2==0&&r[i]<=0) {
cnt2+=Math.abs(1-r[i-1]-a[i]);
r[i]=1;
}
}
System.out.println(Math.min(cnt1, cnt2));
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void) {
long long int n;
cin >> n;
vector<long long int> a;
for (int i = 0; i < (n); i++) {
cin >> a[i];
}
long long int oddcount = 0, evencount = 0;
long long int oddsum = 0, evensum = 0;
bool oddplus = true, evenplus = false;
for (int i = 0; i < (n); i++) {
oddsum += a[i];
evensum += a[i];
if (oddplus && oddsum <= 0) {
oddcount += 1 - oddsum;
oddsum = 1;
} else if (!oddplus && oddsum >= 0) {
oddcount += 1 + oddsum;
oddsum = -1;
}
if (evenplus && evensum <= 0) {
evencount += 1 - evensum;
evensum = 1;
} else if (!evenplus && evensum >= 0) {
evencount += 1 + evensum;
evensum = -1;
}
oddplus = !oddplus;
evenplus = !evenplus;
}
cout << fmin(oddcount, evencount) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const long long LINF = 1e18;
const double EPS = 1e-9;
const double PI = M_PI;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
void yes() { cout << "Yes" << endl; }
void no() { cout << "No" << endl; }
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<long long> a(n);
for (long long(i) = 0; (i) < (long long)(n); i++) {
cin >> a[i];
}
vector<long long> sum(n);
sum[0] = a[0];
long long ans = 0;
if (sum[0] == 0) {
ans++;
}
for (long long(i) = 1; (i) < (long long)n; i++) {
if (sum[i - 1] == 0 && a[i] == 0) {
ans += 2;
}
sum[i] = sum[i - 1] + a[i];
if (sum[i] * sum[i - 1] < 0) {
continue;
} else if (sum[i] * sum[i - 1] > 0) {
if (sum[i] > 0 && sum[i - 1] > 0) {
ans += sum[i] + 1;
sum[i] = -1;
} else {
ans += -sum[i] + 1;
sum[i] = 1;
}
} else {
ans++;
if (sum[i - 1] > 0) {
sum[i] = -1;
} else {
sum[i] = 1;
}
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
ans1, ans2 = 0, 0
f = 0
f += a[0]
if f <= 0:
f = 1
ans1 += 1 - a[0]
for i in range(1, N):
if f * (f + a[i]) < 0:
f += a[i]
continue
ans1 += abs(f + a[i]) + 1
if f > 0:
f = -1
else:
f = 1
f = 0
f += a[0]
if f >= 0:
f = -1
ans2 += 1 - a[i]
for i in range(1, N):
if f * (f + a[i]) < 0:
f += a[i]
continue
ans2 += abs(f + a[i]) + 1
if f > 0:
f = -1
else:
f = 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>
#pragma GCC optimize("-O3")
using namespace std;
void _main();
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
_main();
}
const int inf = INT_MAX / 2;
const long long infl = 1LL << 60;
template <class T>
bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
enum PosiNega { POSITIVE = 0, NEGATIVE = 1 };
int solve(int N, int *a, PosiNega odd_posinega) {
int ans = 0;
int sum = 0;
PosiNega posi_nega = odd_posinega;
for (int i = 0; i < N; i++) {
if (POSITIVE == posi_nega) {
sum += a[i];
if (0 >= sum) {
ans += 1 - sum;
sum = 1;
}
posi_nega = NEGATIVE;
} else {
sum += a[i];
if (0 <= sum) {
ans += 1 + sum;
sum = -1;
}
posi_nega = POSITIVE;
}
}
return ans;
}
void _main() {
int N;
cin >> N;
int a[N];
for (int i = 0; i < N; i++) cin >> a[i];
int candidate1 = solve(N, a, POSITIVE);
int candidate2 = solve(N, a, NEGATIVE);
int ans = (candidate1 < candidate2) ? candidate1 : candidate2;
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 | UNKNOWN | object Main {
def main(args: Array[String]): Unit = {
solve
}
def solve(): Unit = {
val sc = new java.util.Scanner(System.in)
val n = sc.nextInt
sc.nextLine
val a = sc.nextLine.split(" ").map(_.toInt).toList
var prevSum = a(0)
var opeCount = 0
for (i <- 1 until n) {
val currSum = prevSum + a(i)
if (prevSum < 0 && currSum < 0) {
opeCount += math.abs(currSum) + 1
prevSum = 1
} else if (prevSum > 0 && currSum > 0) {
opeCount += math.abs(currSum) + 1
prevSum = -1
} else {
if (currSum == 0) {
opeCount += 1
if (prevSum < 0) {
prevSum = 1
} else {
prevSum = -1
}
} else {
prevSum = prevSum + a(i)
}
}
}
println(opeCount)
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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 maxn = 1e5 + 10;
long long s[maxn];
long long ans[maxn];
int main() {
int n, j;
cin >> n;
long long sum = 0;
for (int i = 1; i <= n; i++) {
cin >> s[i];
}
if (s[0]) {
for (int i = 1; i < n; i++) {
ans[i] = ans[i - 1] + s[i];
if (ans[i] > 0) {
if (s[i + 1] >= 0) {
sum += (s[i + 1] + ans[i] + 1);
s[i + 1] = -(ans[i] + 1);
} else {
if (abs(s[i + 1]) > ans[i]) {
} else {
sum += (s[i + 1] + ans[i] + 1);
s[i + 1] = -(ans[i] + 1);
}
}
} else if (ans[i] < 0) {
if (s[i + 1] > 0) {
if (abs(ans[i]) < s[i + 1]) {
} else {
sum += (1 - ans[i] - s[i + 1]);
s[i + 1] = -ans[i] + 1;
}
} else {
sum += (1 - ans[i] - s[i + 1]);
s[i + 1] = -ans[i] + 1;
}
}
}
cout << sum << endl;
} else if (!s[0]) {
sum = 0;
long long anss = 0;
ans[1] = 1;
s[2] = -2;
sum += 3;
for (int i = 2; i < n; i++) {
ans[i] = ans[i - 1] + s[i];
if (ans[i] > 0) {
if (s[i + 1] >= 0) {
sum += (s[i + 1] + ans[i] + 1);
s[i + 1] = -(ans[i] + 1);
} else {
if (abs(s[i + 1]) > ans[i]) {
} else {
sum += (s[i + 1] + ans[i] + 1);
s[i + 1] = -(ans[i] + 1);
}
}
} else if (ans[i] < 0) {
if (s[i + 1] > 0) {
if (abs(ans[i]) < s[i + 1]) {
} else {
sum += (1 - ans[i] - s[i + 1]);
s[i + 1] = -ans[i] + 1;
}
} else {
sum += (1 - ans[i] - s[i + 1]);
s[i + 1] = -ans[i] + 1;
}
}
}
ans[1] = -1;
s[2] = 2;
anss += 3;
for (int i = 2; i < n; i++) {
ans[i] = ans[i - 1] + s[i];
if (ans[i] > 0) {
if (s[i + 1] >= 0) {
anss += (s[i + 1] + ans[i] + 1);
s[i + 1] = -(ans[i] + 1);
} else {
if (abs(s[i + 1]) > ans[i]) {
} else {
anss += (s[i + 1] + ans[i] + 1);
s[i + 1] = -(ans[i] + 1);
}
}
} else if (ans[i] < 0) {
if (s[i + 1] > 0) {
if (abs(ans[i]) < s[i + 1]) {
} else {
anss += (1 - ans[i] - s[i + 1]);
s[i + 1] = -ans[i] + 1;
}
} else {
anss += (1 - ans[i] - s[i + 1]);
s[i + 1] = -ans[i] + 1;
}
}
}
cout << min(anss, sum) << 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 rep(i,n) for(int i=0;i<n;i++)
#define REP(i,s,n) for(int i=(s);i<=(n);i++)
#define repr(i,n) for(int i=n-1;i>=0;i--)
#define REPR(i,s,n) for(int i=(s);i>=(n);i--)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define Eunique(v) v.erase(unique(all(v)),v.end())
#define Eback(s) s.erase(s.end()-1,s.end())
#define rev(v) reverse(all(v))
#define minvec(v) *min_element(all(v))
#define maxvec(v) *max_element(all(v))
#define sumvec(v) accumulate(all(v),0)
#define mapmax(v) v.rbegin()->first
#define mapmin(v) v.begin()->first
#define pb push_back
#define pf push_front
#define mod 1000000007
#define m_p make_pair
#define DOUBLE fixed << setprecision(15)
#define OK puts("OK")
#define OK1 puts("OK1")
#define OK2 puts("OK2")
#define SIZE(s) (int)s.size()
#define INF ((1LL<<62)-(1LL<<31))
#define zero(x,n) setw(x) << setfill('0') << n
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<double> vd;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
typedef vector<pair<int,int>> vpii;
typedef vector<pair<ll,ll>> vpll;
const double pi = acos(-1.0);
template<class A, class B>
ostream& operator<<(ostream& ost, const pair<A, B>&p) {
ost << "{" << p.first << ", " << p.second << "} ";
return ost;
}
template<class T>
ostream& operator<<(ostream& ost, const vector<T>&v) {
ost << "{";
for (int i = 0; i<(int)v.size(); i++) {
if (i)ost << " ";
ost << v[i];
}
ost << "} \n";
return ost;
}
template<class A, class B>
ostream& operator<<(ostream& ost, const map<A, B>&v) {
ost << "{";
for (auto p:v) {
ost << "{" << p.first << ", " << p.second << "} ";
}
ost << "} ";
return ost;
}
template<class T>
inline bool chmax(T& a, T b){if(a<b){a=b;return true;} return false;}
template<class T>
inline bool chmin(T& a, T b){if(a>b){a=b;return true;} return false;}
ll gcd(ll x, ll y) {return __gcd(x,y);}
ll lcm(ll x, ll y) {return x/__gcd(x,y)*y;}
int main()
{
ll n,sum1=0,sum2=0;
cin >> n;
vll a(n);
rep(i,n) cin >> a[i];
vll b=a;
for(ll i=0;i<n;i++){
if(i%2){
if(a[i]>=0){
sum1+=a[i]+1;
a[i]-=a[i]+1;
}
}
else{
if(a[i]<=0){
sum1+=1-a[i];
a[i]+=1-a[i];
}
}
if(n!=n-1) a[i+1]+=a[i];
}
for(ll i=0;i<n;i++){
if(i%2==0){
if(b[i]>=0){
sum2+=b[i]+1;
b[i]-=b[i]+1;
}
}
else{
if(b[i]<=0){
sum2+=1-b[i];
b[i]+=1-b[i];
}
}
if(n!=n-1) b[i+1]+=b[i];
}
cout << min(sum1,sum2) << 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 INF = 1e12;
vector<long long> v;
vector<long long> sum;
int n;
long long add(long long idx, long long minus_max, long long plus_min,
long long flag) {
if (idx == 0) {
if (minus_max >= 0)
return minus_max + 1;
else if (plus_min <= 0)
return -plus_min + 1;
return 0;
} else if (flag == 0) {
long long change = max(0LL, sum[idx - 1] - (plus_min - 2));
if (idx == n - 1) {
return change + add(idx - 1, sum[idx - 1], plus_min + change, 1LL);
}
return change + add(idx - 1, max(sum[idx - 1], minus_max + change),
plus_min + change, 1LL);
} else {
long long change = max(0LL, (minus_max + 2) - sum[idx - 1]);
if (idx == n - 1) {
return change + add(idx - 1, minus_max - change, sum[idx - 1], 0LL);
}
return change + add(idx - 1, minus_max - change,
min(sum[idx - 1], plus_min - change), 0LL);
}
}
int main(void) {
cin >> n;
v.resize(n);
sum.resize(n);
for (int i = 0; i < (n); i++) cin >> v[i];
for (int i = 0; i < (n); i++) {
if (i == 0)
sum[i] = v[i];
else
sum[i] = v[i] + sum[i - 1];
}
long long ans = INF;
ans = min(ans, add(n - 1, sum[n - 1], sum[n - 1], 0LL));
ans = min(ans, add(n - 1, sum[n - 1], sum[n - 1], 1LL));
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import numpy as np
n = int(input())
a = np.array(list(map(int, input().split())))
ans = 0
sum_list1 = np.zeros(n)
to_minus1 = np.zeros(n)
to_plus1 = np.zeros(n)
s = 0
for i in range(n):
s += a[i]
sum_list1[i] += s
to_minus1[i] = sum_list1[i] + 1
to_plus1[i] = sum_list1[i] - 1
def check(sum_list, to_minus, to_plus, t, ans):
for i in range(n):
if (i+t) % 2 == 0:
if to_minus[i] <= 0:
continue
else:
hiku = to_minus[i]
to_minus[i::] -= hiku
to_plus[i::] -= hiku
ans += abs(hiku)
else:
if to_plus[i] >= 0:
continue
else:
tasu = abs(to_plus[i])
to_minus[i::] += tasu
to_plus[i::] += tasu
ans += abs(tasu)
return ans
t = np.copy(to_minus1)
u = np.copy(to_plus1)
ans1 = check(sum_list1, to_minus1, to_plus1, 0, 0)
ans2 = check(sum_list1, t, u, 1, 0)
print(int(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 dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const int MAX_N = int(1e5);
long long n, a[MAX_N], dp[MAX_N];
void solve() {
long long sum_diff = 0, ans = 0;
for (long long i = 0; i < (long long)(n - 1); i++) {
long long diff = 0;
dp[i + 1] += sum_diff;
if (dp[i] * dp[i + 1] > 0) {
if (dp[i + 1] > 0) {
diff = -1 - dp[i + 1];
sum_diff += diff;
dp[i + 1] = -1;
} else {
diff = 1 - dp[i + 1];
sum_diff += diff;
dp[i + 1] = 1;
}
}
if (dp[i + 1] == 0) {
if (dp[i] > 0) {
sum_diff++, diff = 1;
dp[i + 1] = 1;
} else {
sum_diff--, diff = -1;
dp[i + 1] = -1;
}
}
ans += abs(diff);
}
cout << ans << endl;
}
int main() {
cin >> n;
for (long long i = 0; i < (long long)(n); i++) {
cin >> a[i];
if (i == 0)
dp[0] = a[0];
else
dp[i] = dp[i - 1] + a[i];
}
solve();
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
if (scanf("%d", &n) < 1) return 0;
int tmp;
int sm = 0;
int cnt = 0;
for (int i = 0; i < n; i++) {
if (scanf("%d", &tmp) < 1) return 0;
if ((0 <= sm + tmp) && (0 < sm)) {
cnt = cnt + (1 + sm + tmp);
sm = -1;
} else if ((sm + tmp <= 0) && (sm < 0)) {
cnt = cnt + (1 - sm - tmp);
sm = 1;
} else
sm = sm + tmp;
}
printf("%d\n", cnt);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
vector<long long> inp;
long long cnt(bool oddPos) {
long long ret = 0;
long long sum = 0;
bool odd = true;
for (long long i : inp) {
sum += i;
if ((sum <= 0) == (odd == oddPos)) {
ret += (abs(sum) + 1);
sum = (odd == oddPos) ? 1 : -1;
}
odd = !odd;
}
return ret;
}
int main() {
long long n;
cin >> n;
for (long long i = 0; i < n; i++) {
long long buf;
cin >> buf;
inp.push_back(buf);
}
cout << min(cnt(false), cnt(true)) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void) {
int n;
cin >> n;
long long a[n];
for (int(i) = 0; (i) < (n); (i)++) cin >> a[i];
long long ans1 = 0, ans2 = 0, ans3 = 0, ans4 = 0;
long long now = a[0];
for (int(i) = 0; (i) < (n - 1); (i)++) {
if (i == 0 && now == 0) {
now = -1;
ans1 += 1;
}
if ((now + a[i + 1]) * now < 0) {
now += a[i + 1];
continue;
} else {
if (now < 0) {
ans1 += (1 - now) - a[i + 1];
now = 1;
} else {
ans1 += a[i + 1] + (now + 1);
now = -1;
}
}
}
now = a[0];
for (int(i) = 0; (i) < (n - 1); (i)++) {
if (i == 0 && now == 0) {
now = 1;
ans2 += 1;
}
if ((now + a[i + 1]) * now < 0) {
now += a[i + 1];
continue;
} else {
if (now < 0) {
ans2 += (1 - now) - a[i + 1];
now = 1;
} else {
ans2 += a[i + 1] + (now + 1);
now = -1;
}
}
}
now = a[0];
for (int(i) = 0; (i) < (n - 1); (i)++) {
if (i == 0 && now >= 0) {
now = -1;
ans3 += 1 + now;
}
if ((now + a[i + 1]) * now < 0) {
now += a[i + 1];
continue;
} else {
if (now < 0) {
ans3 += (1 - now) - a[i + 1];
now = 1;
} else {
ans3 += a[i + 1] + (now + 1);
now = -1;
}
}
}
now = a[0];
for (int(i) = 0; (i) < (n - 1); (i)++) {
if (i == 0 && now <= 0) {
now = 1;
ans4 += 1 - now;
}
if ((now + a[i + 1]) * now < 0) {
now += a[i + 1];
continue;
} else {
if (now < 0) {
ans4 += (1 - now) - a[i + 1];
now = 1;
} else {
ans4 += a[i + 1] + (now + 1);
now = -1;
}
}
}
cout << min(min(ans1, ans2), min(ans3, ans4)) << 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.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Pradyumn Agrawal coderbond007
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastReader in = new FastReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
static class TaskC {
public void solve(int testNumber, FastReader in, PrintWriter out) {
int n = in.nextInt();
int[] a = in.nextIntArray(n);
long[] sum = new long[n];
long ret = 0;
for (int i = 0; i < n; i++) {
sum[i] += a[i];
if (i == 0) {
if (sum[i] == 0) {
if (i + 1 < n) {
if (a[i + 1] > 0) {
a[i]--;
sum[i]--;
ret++;
} else {
a[i]++;
sum[i]++;
ret++;
}
}
}
} else {
sum[i] += sum[i - 1];
if (sum[i] > 0 && sum[i - 1] > 0) {
ret += sum[i] + 1;
sum[i] = -1;
} else if (sum[i] < 0 && sum[i - 1] < 0) {
ret += -sum[i] + 1;
sum[i] = 1;
} else if (sum[i] == 0) {
if (sum[i - 1] > 0) {
ret++;
sum[i]--;
} else {
sum[i]++;
ret++;
}
}
}
}
out.println(ret);
}
}
static class FastReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int pnumChars;
private FastReader.SpaceCharFilter filter;
public FastReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (pnumChars == -1) {
throw new InputMismatchException();
}
if (curChar >= pnumChars) {
curChar = 0;
try {
pnumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (pnumChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c == ',') {
c = read();
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int[] array = new int[n];
for (int i = 0; i < n; i++) {
array[i] = nextInt();
}
return array;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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];
}
long long ans1 = 0, def1 = 0;
if (a[0] == 0) {
ans1++;
def1++;
}
for (int i = 1; i < n; i++) {
if (i % 2 == 0 && a[i] + def1 <= 0) {
ans1 += 1 - (a[i] + def1);
def1 += 1 - (a[i] + def1);
} else if (i % 2 == 1 && a[i] + def1 >= 0) {
ans1 += a[i] + def1 + 1;
def1 -= a[i] + def1 + 1;
}
}
long long ans2 = 0, def2 = 0;
if (a[0] == 0) {
ans2++;
def2--;
}
for (int i = 1; i < n; i++) {
if (i % 2 == 1 && a[i] + def2 <= 0) {
ans2 += 1 - (a[i] + def2);
def2 += 1 - (a[i] + def2);
} else if (i % 2 == 0 && a[i] + def2 >= 0) {
ans2 += a[i] + def2 + 1;
def2 -= a[i] + def2 + 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 | python3 | import numpy as np
n = int(input())
a = list(map(int,input().split()))
arr = np.array(a)
# 奇数がプラス、偶数がマイナス
cost1 = 0
cum = arr.cumsum()
for i in range(n):
tmp = abs(cum[i]) + 1
if i%2==1 and cum[i]<=0: cost1 += tmp ; cum[i:] += tmp
if i%2==0 and cum[i]>=0: cost1 += tmp ; cum[i:] -= tmp
# 奇数がマイナス、偶数がプラス
cost2 = 0
cum = arr.cumsum()
for i in range(n):
tmp = abs(cum[i]) + 1
if i%2==1 and cum[i]>=0: cost2 += tmp ; cum[i:] -= tmp
if i%2==0 and cum[i]<=0: cost2 += tmp ; cum[i:] += tmp
ans = min(cost1, cost2)
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;
constexpr long long MOD = 1e9 + 7;
long long dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
long long dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
long long A, B, C, D, E, F, G, H, N, M, L, K, P, Q, R, W, X, Y, Z;
string S, T;
long long ans = 0;
template <typename T>
istream &operator>>(istream &is, vector<T> &vec) {
for (T &x : vec) is >> x;
return is;
}
signed main() {
cin >> N;
vector<long long> a(N);
cin >> a;
for (long long i = 0; i < (long long)(N - 1); i++) {
a[i + 1] = a[i] + a[i + 1];
}
if (a[0] < 0) {
for (long long i = 0; i < (long long)(N); i++) a[i] *= -1;
};
long long base = 0;
if (a[0] == 0) base++;
for (long long i = 0; i < (long long)(N); i++) {
if (i == 0) continue;
if (i & 1) {
long long tmp = (a[i] + base) - (-1);
if (tmp > 0) {
ans += tmp;
base -= tmp;
}
} else {
long long tmp = 1 - (a[i] + base);
if (tmp > 0) {
ans += tmp;
base += tmp;
}
}
}
long long hoge = ans;
ans = 0;
for (long long i = 0; i < (long long)(N); i++) a[i] *= -1;
;
base = 1 - a[0];
for (long long i = 0; i < (long long)(N); i++) {
if (i == 0) continue;
if (i & 1) {
long long tmp = (a[i] + base) - (-1);
if (tmp > 0) {
ans += tmp;
base -= tmp;
}
} else {
long long tmp = 1 - (a[i] + base);
if (tmp > 0) {
ans += tmp;
base += tmp;
}
}
};
ans = min((long long)hoge, ans);
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 | python2 | n = int(raw_input())
A = map(int, raw_input().split())
ps = A[0]
op = 0
if ps == 0 and A[1] >= 0:
ps = -1
op += 1
elif ps == 0 and A[1] < 0:
ps = 1
op += 1
for i in range(1, n):
if ps <= 0 and ps + A[i] <= 0:
op += abs(1-(ps+A[i]))
ps = 1
elif ps >= 0 and ps + A[i] >= 0:
op += abs(-1 - (ps+A[i]))
ps = -1
else:
ps = ps + A[i]
print op |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n;
int a[100000];
void solve() {
int tmp1 = 0, tmp2 = 0;
int sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 0) {
if (sum <= 0) {
tmp1 += -sum + 1;
sum = 1;
}
} else {
if (sum >= 0) {
tmp1 += sum + 1;
sum = -1;
}
}
}
sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (i % 2 == 1) {
if (sum <= 0) {
tmp2 += -sum + 1;
sum = 1;
}
} else {
if (sum >= 0) {
tmp2 += sum + 1;
sum = -1;
}
}
}
int ans = min(tmp1, tmp2);
cout << ans << endl;
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
solve();
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
b = [int(x) for x in input().split()]
a = list()
temp = 0
count1 = 0
count2 = 0
a = b.copy()
if a[0] == 0:
a[0] = 1
count1 = 1
sum = a[0]
for i in range(1, n):
if abs(a[i]) <= abs(sum) or a[i] * sum >= 0:
if sum > 0:
temp = -1 * abs(sum) - 1
count1 += abs(temp - a[i])
else:
temp = abs(sum) + 1
count1 += abs(temp - a[i])
a[i] = temp
sum += a[i]
a = b.copy()
count2 = abs(a[0]) + 1
if a[0] >= 0:
a[0] = -1
else:
a[0] = 1
sum = a[0]
for i in range(1, n):
if abs(a[i]) <= abs(sum) or a[i] * sum >= 0:
count2 += abs(sum - a[i]) + 1
if sum > 0:
temp = -1 * abs(sum) - 1
count2 += abs(temp - a[i])
else:
temp = abs(sum) + 1
count2 += abs(temp - a[i])
a[i] = temp
sum += a[i]
print(min(count1, count2))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<signed long long> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
signed long long ans = 0;
if (a[0] == 0) {
if (a[1] <= 0) {
++ans;
a[0] = 1;
} else {
++ans;
a[0] = -1;
}
}
if (a[0] > 0) {
signed long long sum = a[0];
for (int i = 1; i < n; ++i) {
if (i % 2 == 1) {
if (sum + a[i] < 0) {
sum += a[i];
} else {
ans += sum + a[i] + 1;
sum = -1;
}
} else {
if (sum + a[i] > 0) {
sum += a[i];
} else {
ans += abs(sum + a[i] - 1);
sum = 1;
}
}
}
} else {
signed long long sum = a[0];
for (int i = 1; i < n; ++i) {
if (i % 2 == 1) {
if (sum + a[i] > 0) {
sum += a[i];
} else {
ans += abs(sum + a[i] - 1);
sum = 1;
}
} else {
if (sum + a[i] < 0) {
sum += a[i];
} else {
ans += sum + a[i] + 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 | import numpy as np
n = int(input())
a = list(map(int,input().split()))
b = list(a)
sum = 0
p = 0
cnt_g=0
cnt_k=0
if a[0]<0:
for i in range(n):
sum += a[i]
if i>0:
p = a[i]
if (sum - a[i]) > 0:
if sum >= 0:
a[i] = -(sum-a[i])-1
cnt_g += np.abs(p-a[i])
sum = -1
else:
if sum <= 0:
a[i] = -(sum-a[i])+1
cnt_g += np.abs(p-a[i])
sum = 1
sum = 0
cnt_k = -b[0]+1
sum = 1
for i in range(1,n):
sum += b[i]
p = b[i]
if (sum - b[i]) > 0:
if sum >= 0:
b[i] = -(sum-b[i])-1
cnt_k += np.abs(p-b[i])
sum = -1
else:
if sum <= 0:
b[i] = -(sum-b[i])+1
cnt_k += np.abs(p-b[i])
sum = 1
else:
cnt_g = a[0]+1
sum = -1
for i in range(1,n):
sum += a[i]
p = a[i]
if (sum - a[i]) > 0:
if sum >= 0:
a[i] = -(sum-a[i])-1
cnt_g += np.abs(p-a[i])
sum = -1
else:
if sum <= 0:
a[i] = -(sum-a[i])+1
cnt_g += np.abs(p-a[i])
sum = 1
sum = 0
for i in range(n):
sum += b[i]
if i>0:
p = b[i]
if (sum - b[i]) > 0:
if sum >= 0:
b[i] = -(sum-b[i])-1
cnt_k += np.abs(p-b[i])
sum = -1
else:
if sum <= 0:
b[i] = -(sum-b[i])+1
cnt_k += np.abs(p-b[i])
sum = 1
if cnt_k<cnt_g:
print (cnt_k)
else : print (cnt_g) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
bool cmp(pair<long long, long long> a, pair<long long, long long> b) {
if (a.first == b.first)
return a.second < b.second;
else
return a.first < b.first;
}
long long exp(long long a, long long b) {
long long ans = 1;
while (b != 0) {
if (b % 2 == 1) ans = ans * a;
a = a * a;
b /= 2;
}
return ans;
}
long long arr[(long long)(1e5 + 5)];
long long tmp[(long long)(1e5 + 5)];
int main() {
ios_base::sync_with_stdio;
cin.tie(NULL);
long long n;
cin >> n;
for (long long i = (long long)0; i < (long long)n; i++) {
cin >> arr[i];
tmp[i] = arr[i];
}
long long ans = 0;
long long ans2 = 0;
if (arr[0] == 0) {
ans++;
arr[0] = 1;
}
long long sum = arr[0];
for (long long i = (long long)1; i < (long long)n; i++) {
if (sum + arr[i] == 0) {
long long s1 = abs(sum) / sum;
if (s1 == 1) {
ans += 1;
arr[i] += -1;
} else {
ans += 1;
arr[i] += 1;
}
}
long long s1 = abs(sum) / sum;
long long s2 = abs(sum + arr[i]) / (sum + arr[i]);
if (s1 == s2) {
if (s1 == 1) {
ans += abs(-1 - sum - arr[i]);
arr[i] += -1 - sum - arr[i];
} else {
ans += 1 - sum - arr[i];
arr[i] += (1 - sum - arr[i]);
}
}
sum += arr[i];
}
for (long long i = (long long)0; i < (long long)n; i++) {
arr[i] = tmp[i];
}
if (arr[0] == 0) {
ans2++;
arr[0] = -1;
}
sum = arr[0];
for (long long i = (long long)1; i < (long long)n; i++) {
if (sum + arr[i] == 0) {
long long s1 = abs(sum) / sum;
if (s1 == 1) {
ans2 += 1;
arr[i] += -1;
} else {
ans2 += 1;
arr[i] += 1;
}
}
long long s1 = abs(sum) / sum;
long long s2 = abs(sum + arr[i]) / (sum + arr[i]);
if (s1 == s2) {
if (s1 == 1) {
ans2 += abs(-1 - sum - arr[i]);
arr[i] += -1 - sum - arr[i];
} else {
ans2 += 1 - sum - arr[i];
arr[i] += (1 - sum - arr[i]);
}
}
sum += arr[i];
}
cout << min(ans2, ans) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import sys
input = sys.stdin.readline
def main():
n = int(input())
a_list = list(map(int, input().split()))
a_sum = a_list[0]
if a_list[0] > 0:
sign = "plus"
else:
sign = "minus"
ans1 = 0
for i in range(1, n):
if sign == "plus":
sign = "minus"
if a_sum + a_list[i] == 0:
ans1 += 1
a_sum = -1
elif a_sum + a_list[i] > 0:
ans1 += a_sum + 1 + a_list[i]
a_sum = -1
else:
a_sum += a_list[i]
elif sign == "minus":
sign = "plus"
if a_sum + a_list[i] == 0:
ans1 += 1
a_sum = 1
elif a_sum + a_list[i] < 0:
ans1 += -1 * a_sum + 1 + -1 * a_list[i]
a_sum = 1
else:
a_sum += a_list[i]
ans2 = 0
for i in range(0, n):
if sign == "plus":
sign = "minus"
if a_sum + a_list[i] == 0:
ans2 += 1
a_sum = -1
elif a_sum + a_list[i] > 0:
ans2 += a_sum + 1 + a_list[i]
a_sum = -1
else:
a_sum += a_list[i]
elif sign == "minus":
sign = "plus"
if a_sum + a_list[i] == 0:
ans2 += 1
a_sum = 1
elif a_sum + a_list[i] < 0:
ans2 += -1 * a_sum + 1 + -1 * a_list[i]
a_sum = 1
else:
a_sum += a_list[i]
print(min(ans1, ans2))
if __name__ == '__main__':
main()
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | def sign(number):
if number < 0:
return -1
elif number > 0:
return 1
else:
return 0
n = int(input())
a = list(map(int, input().split()))
csum_lst = [0 for i in range(n)]
csum_lst[0] = a[0]
cnt = 0
for i in range(1, n):
if a[i] == 0:
new_a = -(csum_lst[i - 1] + 1 * sign(csum_lst[i - 1]))
cnt += abs(new_a - a[i])
a[i] = new_a
elif sign(csum_lst[i-1]) == sign(a[i]) or abs(csum_lst[i-1]) >= abs(a[i]):
new_a = -(csum_lst[i-1]+1*sign(csum_lst[i-1]))
cnt += abs(new_a-a[i])
a[i] = new_a
csum_lst[i] = csum_lst[i-1] + a[i]
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 | def main():
n, *a = map(int, open(0).read().split())
def chk(a, t):
ans = 0
x = 0
for i in a:
x += i
if t and x < 1:
ans += 1 - x
x = 1
elif t and x > -1:
ans += x + 1
x = -1
t = not t
return ans
print(min(chk(a, True), chk(a, False)))
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>
int calc(bool firstPositive, const int a[], int n) {
bool positive = firstPositive;
int cost = 0;
long sum = 0;
for (int i = 0; i < n; ++i) {
int v = a[i];
bool sumpos = (sum + v) >= 0;
if (sumpos != positive) {
cost += abs(sum + a[i]) + 1;
v = -(sum + v) + (positive ? -1 : 1);
}
if ((sum + v) == 0) {
v += sumpos ? -1 : 1;
++cost;
}
sum += v;
positive = !positive;
}
return cost;
}
int main(int argc, char *argv[]) {
int n;
std::cin >> n;
int a[1 << 20];
for (int i = 0; i < n; ++i) {
std::cin >> a[i];
}
std::cout << std::min(calc(true, a, n), calc(false, a, n)) << std::endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n;
unsigned long long func(vector<long long int>& s, vector<int>& hugo,
long long int k) {
unsigned long long int ret = 0;
for (int i = 1; i < n; i++) {
if (s[i] == k) {
if (hugo[i - 1] == 0) {
hugo[i] = 1;
ret++;
k++;
} else {
hugo[i] = 0;
ret++;
k--;
}
} else if (s[i] > k) {
if (hugo[i - 1] == 0) {
hugo[i] = 1;
ret += s[i] - k + 1;
k += s[i] - k + 1;
} else {
hugo[i] = 0;
}
} else {
if (hugo[i - 1] == 0) {
hugo[i] = 1;
} else {
hugo[i] = 0;
ret += k - s[i] + 1;
k -= k - s[i] + 1;
}
}
}
return ret;
}
void solve() {
cin >> n;
vector<long long int> v(n), sum(n), sum2(n);
for (int i = 0; i < n; i++) {
cin >> v[i];
if (i == 0)
sum[i] = v[i];
else
sum[i] = sum[i - 1] + v[i];
}
vector<int> hugo(n);
sum2 = sum;
unsigned long long int ans;
if (sum[0] == 0) {
vector<int> hugo2(n);
hugo[0] = 0;
ans = min(func(sum, hugo, -1), func(sum2, hugo2, 1));
} else if (sum[0] > 0) {
hugo[0] = 0;
ans = func(sum, hugo, 0);
} else {
hugo[0] = 1;
ans = func(sum, hugo, 0);
}
cout << ans << endl;
return;
}
int main() {
solve();
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n,a=int(input()),list(map(int,input().split()));ans=0
if a[0]==0:
bol=True
for i in range(1,n):
if a[i-1]!=a[i]:
bol=False
if a[i]>0:a[0]=(1if i%2==0else-1)
else:a[0]=(1if i%2!=0else-1)
ans+=1;break
if bol:print(n*2-1);exit()
b=[a[0]];m=("+"if a[0]<0else"-")
for i in range(1,n):
while b[-1]*(b[-1]+a[i])>=0:
ans+=1
a[i]+=eval(m+"1")
b.append(b[-1]+a[i])
m=("+"if b[i]<0else"-")
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n;
vector<long long int> v;
int solve(int first_pon) {
long long int pon = first_pon, sum = 0, result = 0;
for (int i = 0; (i) < (n); i++) {
sum += v[i];
if (pon > 0 && sum >= 0) {
result += sum + 1;
sum = -1;
} else if (pon < 0 && sum <= 0) {
result += (-sum) + 1;
sum = 1;
}
if (sum > 0) {
pon = 1;
} else {
pon = -1;
}
}
return result;
}
int main() {
cin >> n;
v = vector<long long int>(n);
for (int i = 0; (i) < (n); i++) {
cin >> v[i];
}
cout << (long long int)min(solve(1), solve(-1)) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int keta(int num){
int ans= 0 ;
int rem;
for (int i = 4; i >= 0 ; i--){
rem = pow(10,i);
ans += (num / rem);
num = num % rem;
}
return ans;
}
int main() {
int n;
cin >> n;
vector<int64_t> ar(n);
for(int i = 0; i < n; i++){
cin >> ar[i];
}
int ans1, ans2 = 0;
int cum = 0;
//偶数が正の場合
for(int i = 0; i < n; i++){
int next = cum + ar[i];
if(i % 2 == 0){
if(next <= 0){
ans1 += abs(next - 1);
cum = 1;
}else{
cum = next;
}
}else{
if(next >= 0){
ans1 += abs(next + 1);
cum = -1;
}else{
cum = next;
}
}
//偶数が負の場合
for(int i = 0; i < n; i++){
int next = cum + ar[i];
if(i % 2 == 0){
if(next >= 0){
ans2 += abs(next + 1);
cum = -1;
}else{
cum = next;
}
}else{
if(next <= 0){
ans2 += abs(next - 1);
cum = 1;
}else{
cum = next;
}
}
cout << min(ans1, ans2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include<bits/stdc++.h>
#define FF ios_base::sync_with_stdio(0);cin.tie(0)
#define binary(value, size) cout << bitset<size>(value) << '\n'
#define PI acos(-1.0) //3.1416(180 degree to radian)
#define PIby2 asin(1) //(3.1416/2) for angle(90 degree to radian)
#define eps 1e-67
#define pf printf
#define sf scanf
#define sz size()
#define rr read
#define ww write
#define clr(arr) memset((arr),0,(sizeof(arr)))
#define rep(i,a,b) for(long long int i=a;i<b;i++)
#define repb(i,a,b) for(long long int i=a;i>=b;i--)
#define repa(i,a,b,c) for(long long int i=a;i<b;i=i+c)
#define reps(i,a,b,c) for(long long int i=a;i>b;i=i-c)
#define asort(a) sort(a.begin(),a.end())
#define asort2(a,comp) sort(a.begin(),a.end(),comp)
#define arev(a) reverse(a.begin(),a.end())
#define all(v) (v).begin(),(v).end()
#define all2(v,a,b) (v).begin()+a,(v).end()-b
#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define pbb pop_back
#define mp make_pair
#define mt make_tuple
#define BS(v,x) binary_search(v.begin(),v.end(),x) //return true /false
#define LB(v,x) lower_bound(v.begin(),v.end(),x) //found and that value and not found than greater value pos
#define UB(v,x) upper_bound(v.begin(),v.end(),x) //found and greater value pos && not found also greater pos
#define convertlower(c) towlower(c)
#define root(x) sqrt(x)
#define power(a,n) pow(a,n)
#define tu(c) towupper(c)
#define sq(a) ((a)*(a))
#define cube(a) ((a)*(a)*(a))
#define mx 1000
#define MX 100000
#define mod 1000000007
#define INF 2000000000
#define N 10000000
#define Ceil(n) (long long int)ceil(n)
#define Floor(n) (long long int)floor(n)
#define deb(x) std::cout << #x << " = " << x << std::endl;
#define out(ans) cout<<ans<<endl
#define outs(ans) cout<<ans<<" "<<endl
using namespace std;
typedef string str;
typedef long long int ll;
typedef double lf;
typedef long double llf;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int,int> pi;
typedef pair<ll,ll> pll;
typedef vector<pll> vpll;
typedef char ch;
typedef map<ll,ll> mpl;
int main()
{
ll tc;
cin>>n;
vll v;
rep(i,0,n)
{
ll d; cin>>d; v.pb(d);
}
ll sign=1,sum=0,ans1=0,ans2=0;
rep(i,0,n)
{
sum+=v[i];
if(sign*sum<=0) ans1+=labs(sign-sum),sum=sign;
sign*=-1;
}
sign=-1,sum=0;
rep(i,0,n)
{
sum+=v[i];
if(sign*sum<=0) ans2+=labs(sign-sum),sum=sign;
sign*=-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;
const int MAX_N = 300000;
const unsigned long long mod = 1000000000 + 7;
int main() {
long long N;
cin >> N;
vector<long long> A(N);
for (int i = 0; i < (int)N; ++i) {
cin >> A[i];
}
if (A[0] == 0) {
long long S = 1;
long long S_pre = 1;
long long resA = 1;
for (int i = 1; i < N; i++) {
S += A[i];
if (S * S_pre >= 0) {
if (S_pre > 0) {
resA += (S + 1);
S = -1;
} else {
resA += (1 - S);
S = 1;
}
}
S_pre = S;
}
S = -1;
S_pre = -1;
long long resB = 1;
for (int i = 1; i < N; i++) {
S += A[i];
if (S * S_pre >= 0) {
if (S_pre > 0) {
resB += (S + 1);
S = -1;
} else {
resB += (1 - S);
S = 1;
}
}
S_pre = S;
}
long long res = min(resA, resB);
cout << res << endl;
} else {
long long S = A[0];
long long S_pre = A[0];
long long res = 0;
for (int i = 1; i < N; i++) {
S += A[i];
if (S * S_pre >= 0) {
if (S_pre > 0) {
res += (S + 1);
S = -1;
} else {
res += (1 - S);
S = 1;
}
}
S_pre = S;
}
cout << res << endl;
}
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long int a[n], c = 0, d, sa[n], p = 0, k = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
k += a[i];
}
c = a[0];
for (int i = 1; i < n; i++) {
d = c + a[i];
if (c > 0) {
while (d >= 0) {
if (c != 1) {
c--;
continue;
} else {
d--;
p++;
}
}
} else if (c < 0) {
while (d <= 0) {
if (c != -1) {
c++;
continue;
} else {
d++;
p++;
}
}
} else if (c == 0) {
if (k < 1)
c--;
else
c++;
}
c = d;
}
cout << p << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int sum;
int input;
cin >> sum;
int ans = 0;
int reigai = 0;
int ans1, ans2, sum1, sum2;
if (sum == 0) {
ans1 = 1;
ans2 = 1;
sum1 = 1;
sum2 = -1;
reigai = 1;
}
for (int i = 1; i < n; ++i) {
cin >> input;
if (sum * (sum + input) >= 0) {
ans += abs(sum + input) + 1;
if (sum < 0)
sum = 1;
else
sum = -1;
} else
sum += input;
if (reigai == 1) {
if (sum1 * (sum1 + input) >= 0) {
ans1 += abs(sum1 + input) + 1;
if (sum1 < 0)
sum1 = 1;
else
sum1 = -1;
} else
sum1 += input;
if (sum2 * (sum2 + input) >= 0) {
ans2 += abs(sum2 + input) + 1;
if (sum2 < 0)
sum2 = 1;
else
sum2 = -1;
} else
sum2 += input;
}
}
if (reigai == 0)
cout << ans << endl;
else
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())
a=list(map(int, input().split()))
cnt =0
sum_tmp=a[0]
def check(a,i,cnt,sum_tmp):
sum1=sum_tmp
sum2 = sum_tmp+a[i]
if sum1<0 and sum2 <=0:
cnt += abs(sum2)+1
sum_tmp =1
elif sum1>0 and sum2 >=0:
cnt += abs(sum2)+1
sum_tmp=-1
else:
sum_tmp=sum2
return cnt,sum_tmp
for i in range(1,n):
cnt,sum_tmp=check(a,i,cnt,sum_tmp)
print(cnt) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | # main
n = gets.to_i
ary = gets.split(' ').map(&:to_i)
sum = ary[0]
cnt = 0
if ary[0] == 0
if ary[1] > 0
sum = -1
else
sum = 1
end
cnt = 1
end
(1...n).each{ |i|
if sum < 0
sum += ary[i]
if sum <= 0
cnt += 1-sum
sum = 1
end
else
sum += ary[i]
if sum >= 0
cnt += sum+1 # sum-(-1)
sum = -1
end
end
}
puts cnt |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[100001];
for (int i = 0; i < n; ++i) cin >> a[i];
int cnt1 = 0;
int cnt2 = 0;
int sum = 0;
for (int i = 0; i < n; ++i) {
sum += a[i];
if (i % 2 == 0) {
while (sum <= 0) {
sum++;
cnt1++;
}
} else {
while (sum >= 0) {
sum--;
cnt1++;
}
}
}
sum = 0;
for (int i = 0; i < n; ++i) {
sum += a[i];
if (i % 2 == 1) {
while (sum <= 0) {
sum++;
cnt2++;
}
} else {
while (sum >= 0) {
sum--;
cnt2++;
}
}
}
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 | python3 | n = int(input())
a = [int(x) for x in input().split()]
ans = n * 1000000000
s = a[0]
cnt = 0
for i in range(1,n):
ns = s + a[i]
if s * ns < 0:
s = ns
continue
else:
cnt += abs(ns)+1
s = -1 if ns > 0 else 1
ans = min(ans, cnt)
s = -1 if a[0] > 0 else 1
cnt = abs(a[0])+1
for i in range(1,n):
ns = s + a[i]
if s * ns < 0:
s = ns
continue
else:
cnt += abs(ns)+1
s = -1 if ns > 0 else 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 | cpp | #include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 111;
int n;
int a[MAX];
long long calc(int id, long long sum) {
long long ans = 0;
for (int i = id; i < n; ++i) {
long long cur = sum + a[i];
if (sum < 0 && cur > 0) {
sum = cur;
continue;
}
if (sum > 0 && cur < 0) {
sum = cur;
continue;
}
if (cur == 0) {
if (sum < 0)
cur = 1;
else
cur = -1;
sum = cur;
ans++;
continue;
}
if (sum < 0) {
ans += 1 - cur;
cur = 1;
} else {
ans += cur + 1;
cur = -1;
}
sum = cur;
}
return ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
int id = -1;
for (int i = 0; i < n; ++i) {
cin >> a[i];
if (a[i] != 0 && id == -1) id = i;
}
if (id == -1) {
cout << 1 + (n - 1) * 2;
return 0;
}
if (id != 0) {
long long ans1 = calc(id, -1);
long long ans2 = calc(id, 1);
cout << min(ans1, ans2) + 1 + 2 * (id - 1);
} else {
long long ans1 = calc(1, a[0]);
cout << ans1;
}
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>
const int MGN = 8;
const int ARY_SZ_MAX = 10000000;
using namespace std;
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvvb = vector<vvb>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vd = vector<double>;
using vs = vector<string>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using psi = pair<string, int>;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
vl A(N);
for (int i = int(0); i < int(N); ++i) cin >> A[i];
vl s(N);
int ans = (INT_MAX / 2);
int cost = 0;
s[0] = A[0];
for (int i = int(1); i < int(N); ++i) {
if (i % 2 == 0 && s[i - 1] <= 0) {
cost += abs(s[i - 1]) + 1;
s[i - 1] = 1;
} else if (i % 2 == 1 && s[i - 1] >= 0) {
cost += abs(s[i - 1]) + 1;
s[i - 1] = -1;
}
s[i] = s[i - 1] + A[i];
}
if (N % 2 == 0 && s[N - 1] <= 0) {
cost += abs(s[N - 1]) + 1;
s[N - 1] = 1;
} else if (N % 2 == 1 && s[N - 1] >= 0) {
cost += abs(s[N - 1]) + 1;
s[N - 1] = -1;
}
ans = min(ans, cost);
cost = 0;
s[0] = A[0];
for (int i = int(1); i < int(N); ++i) {
if (i % 2 == 0 && s[i - 1] >= 0) {
cost += abs(s[i - 1]) + 1;
s[i - 1] = -1;
} else if (i % 2 == 1 && s[i - 1] <= 0) {
cost += abs(s[i - 1]) + 1;
s[i - 1] = 1;
}
s[i] = s[i - 1] + A[i];
}
if (N % 2 == 0 && s[N - 1] >= 0) {
cost += abs(s[N - 1]) + 1;
s[N - 1] = -1;
} else if (N % 2 == 1 && s[N - 1] <= 0) {
cost += abs(s[N - 1]) + 1;
s[N - 1] = 1;
}
ans = min(ans, cost);
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 | n=gets.chomp.to_i
a=gets.split.map(&:to_i)
a.map!{|e| -e} if a[0]<0
s, m=a[0], 0
(1...n).each do |i|
s+=a[i]
if i%2==0
if s<=0
m+=(-s+1)
s=1
end
else
if s>=0
m+=s+1
s=-1
end
end
end
s, m2=-1, a[0]+1
(1...n).each do |i|
s+=a[i]
if i%2==0
if s>=0
m2+=s+1
s=1
end
else
if s<=0
m2+=(-s+1)
s=-1
end
end
end
puts [m, m2].min |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int ans1 = (a[0] > 0) ? 0 : -a[0] + 1;
int sum = (a[0] > 0) ? a[0] : 1;
for (int i = 1; i < n; i++) {
if (sum * (sum + a[i]) < 0) {
sum += a[i];
} else {
ans1 += abs(sum + a[i]) + 1;
sum = (sum > 0) ? -1 : 1;
}
}
int ans2 = (a[0] < 0) ? 0 : -a[0] + 1;
sum = (a[0] < 0) ? a[0] : -1;
for (int i = 1; i < n; i++) {
if (sum * (sum + a[i]) < 0) {
sum += a[i];
} else {
ans2 += abs(sum + a[i]) + 1;
sum = (sum > 0) ? -1 : 1;
}
}
cout << min(ans1, ans2) << endl;
return;
}
int main(int argc, char const* argv[]) {
solve();
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long counter = 0;
int a[100100], b[100100];
bool hugou = true;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
b[0] = a[0];
if (b[0] == 0) {
counter++;
b[0]++;
hugou = true;
}
if (b[0] < 0) {
hugou = false;
}
if (hugou) {
for (int i = 1; i < n; i++) {
b[i] = b[i - 1] + a[i];
if (i % 2 == 0) {
if (b[i] < 1) {
counter += 1 - b[i];
b[i] = 1;
}
} else {
if (b[i] > -1) {
counter += b[i] - (-1);
b[i] = -1;
}
}
}
} else {
for (int i = 1; i < n; i++) {
b[i] = b[i - 1] + a[i];
if (i % 2 == 0) {
if (b[i] > -1) {
counter += b[i] - (-1);
b[i] = -1;
}
} else {
if (b[i] < 1) {
counter += 1 - b[i];
b[i] = 1;
}
}
}
}
cout << counter << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++) cin >> a[i];
vector<int> a1;
a1 = a;
int sum1 = 0, ans1 = 0;
for (int i = 1; i <= n; i++) {
sum1 += a1[i];
if (i == 1 && sum1 <= 0) {
int plus = 1 - sum1;
sum1 += plus;
ans1 += plus;
continue;
}
if (i % 2 == 0 && sum1 >= 0) {
int minus = 1 + sum1;
sum1 -= minus;
ans1 += minus;
} else if (i % 2 == 1 && sum1 <= 0) {
int plus = 1 - sum1;
sum1 += plus;
ans1 += plus;
}
}
vector<int> a2;
a2 = a;
int sum2 = 0, ans2 = 0;
for (int i = 1; i <= n; i++) {
sum2 += a2[i];
if (i == 1 && sum2 >= 0) {
int minus = 1 + sum2;
sum2 -= minus;
ans2 += minus;
continue;
}
if (i % 2 == 0 && sum2 <= 0) {
int plus = 1 - sum2;
sum2 += plus;
ans2 += plus;
} else if (i % 2 == 1 && sum2 >= 0) {
int minus = 1 + sum2;
sum2 -= minus;
ans2 += minus;
}
}
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 | java | import java.util.Scanner;
import java.util.Arrays;
public class Main{
static int n;
static int[] a;
static final boolean DEBUG = false;
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
a = new int[n];
for(int i = 0; i < n; i++){
a[i] = sc.nextInt();
}
int count = 0, count2 = 0;
int sum = a[0];
int sum2 = a[0] < 0 ? 1 : -1;
count2 = Math.abs(sum2 - a[0]);
for(int i = 1; i < n; i++){
int val = a[i], val2 = a[i];
if(!((sum > 0 && sum + a[i] < 0) ||
(sum < 0 && sum + a[i] > 0))){
val = -sum + ((sum < 0) ? 1 : -1);
count += Math.abs(val - a[i]);
}
if(!((sum2 > 0 && sum2 + a[i] < 0) ||
(sum2 < 0 && sum2 + a[i] > 0))){
val2 = -sum2 + ((sum2 < 0) ? 1 : -1);
count2 += Math.abs(val2 - a[i]);
}
sum += val;
sum2 += val2;
}
if(DEBUG){
System.out.println(Arrays.toString(a));
}
System.out.println(Math.min(count, count2));
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java |
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++)a[i]=sc.nextInt();
int sum=0;
int count=0;
for(int i=0;i<n-1;i++){
sum+=a[i];
if(sum>0){
if(sum+a[i+1]>=0){
count+=sum+a[i+1]+1;
a[i+1]-=sum+a[i+1]+1;
}
}else if(sum<0){
if(sum+a[i+1]<=0){
count+=sum+a[i+1]+1;
a[i+1]+=sum+a[i+1]+1;
}
}
}
System.out.println(count);
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
ll n;
cin >> n;
vector<ll> a(n);
for (ll i = 0; i < n; i++) cin >> a[i];
ll sumeven = 0, sumodd = 0;
for (ll i = 0; i < n; i++) {
if (i % 2 == 0)
sumodd += a[i];
else
sumeven += a[i];
}
cout << (abs(sumeven) < abs(sumodd) ? 0 : abs(abs(sumeven) - abs(sumodd)) + 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;
const long long mod = 1000000007;
const long long LINF = 1LL << 60;
const int INF = 1 << 30;
int main() {
long long n, l;
cin >> n;
vector<long long> a(n);
cin >> a[0];
for (long long i = 1; i < n; i++) {
cin >> l;
a[i] = a[i - 1] + l;
}
long long tmp = 0;
long long count = 0;
for (long long i = 1; i < n; i++) {
if ((a[i] + tmp) * (a[i - 1] + tmp) >= 0) {
if ((a[i] + tmp) <= 0) {
count += abs(a[i] + tmp - 1);
tmp -= (a[i] + tmp - 1);
} else {
count += abs(a[i] + tmp + 1);
tmp -= (a[i] + tmp + 1);
}
}
}
cout << count << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | import qualified Data.ByteString.Char8 as C
import Data.List
main = C.interact $ put . sol . get
get = unfoldr (C.readInt . C.dropWhile (==' ')) . last . C.lines
put = C.pack . show
sol (a:as)
| a==0 = 2 + sol as
| otherwise = fst $ foldl' f (0,a) as
f (c,s) b
| s*s'<0 = (c,s')
| s>0 = (c+1+abs s',-1)
| s<0 = (c+1+abs s',1)
where
s' = s+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 | python3 | n=int(input())
a=list(map(int,input().split()))
sum=[0,]*n
sum[0]=a[0]
zoubun=0
cnt=0
for i in range(1,n):
sum[i]=sum[i-1]+a[i]
if(sum[i]*sum[i-1]>=0):
cnt+=abs(sum[i])+1
if sum[i]>=0:
sum[i]=-1
else:
sum[i]=1
print(sum)
print(str(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 | java | import java.util.*;
public class Main {
static long ans;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int [] num = new int [N];
long sum = 0;
ans=0;
for(int i=0; i<N; i++){
num[i] = sc.nextInt();
sum+=num[i];
if(i!=0)sum=function(i,num,sum);
}
long tmp = ans;
ans = Math.abs(num[0])+1;
if(num[0]>0){
sum = -1;
}else{
sum = 1;
}
for(int i=1; i<N; i++){
sum+=num[i];
sum=function(i,num,sum);
}
ans = Math.min(tmp,ans);
System.out.println(ans);
}
static long function(int i, int[]num, long sum){
int a = sign((sum-num[i]));
int b = sign(sum);
int t = a*b;
if(t==0){
ans++;
if(a>0){
return -1;
}else{
return 1;
}
}else if(t<0){
return sum;
}else{
ans+=(Math.abs(sum)+1);
if(sum>0){
return -1;
}else{
return 1;
}
}
}
static int sign(long A){
if(A>0){
return 1;
}else if(A<0){
return -1;
}else{
return 0;
}
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
ans = 0
key = a[0]
if key == 0:
flag = 1
a[0] = 1
ans += 1
elif a[0] > 0:
flag = 1
else:
flag = -1
dp = [0 for i in range(n)]
dp[0] = a[0]
for i in range(1, n):
dp[i] = dp[i - 1] + a[i]
if dp[i] == 0:
dp[i] = flag * -1
ans += 1
elif flag == 1 and dp[i] > 0:
ans += abs(-1 - dp[i])
dp[i] = -1
elif flag == -1 and dp[i] < 0:
ans += abs(1 - dp[i])
dp[i] = 1
flag *= -1
ans1 = ans
ans = 0
if key == 0:
flag = -1
a[0] = -1
ans += 1
elif a[0] > 0:
flag = 1
else:
flag = -1
dp = [0 for i in range(n)]
dp[0] = a[0]
for i in range(1, n):
dp[i] = dp[i - 1] + a[i]
if dp[i] == 0:
dp[i] = flag * -1
ans += 1
elif flag == 1 and dp[i] > 0:
ans += abs(-1 - dp[i])
dp[i] = -1
elif flag == -1 and dp[i] < 0:
ans += abs(1 - dp[i])
dp[i] = 1
flag *= -1
print(min(ans1, 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 INF = 1001001001;
const long long LINF = 1001001001001001001;
const long long D4[] = {0, 1, 0, -1, 0};
const long long D8[] = {0, 1, 1, 0, -1, -1, 1, -1, 0};
template <class T>
bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
template <class T>
using pq = priority_queue<T, vector<T>, greater<T>>;
void solve();
int main() {
cin.tie(0);
ios::sync_with_stdio(0);
solve();
}
void solve() {
long long n;
cin >> n;
vector<long long> a(n);
for (long long i = 0; i < (n); ++i) cin >> a[i];
vector<long long> ans(2), sum = {a[0], a[0]};
for (long long i = (1); i < (n); ++i)
for (long long j = 0; j < (2); ++j) {
sum[j] += a[i];
if ((i + j) % 2) {
if (sum[j] < 1) {
ans[j] += 1 - sum[j];
sum[j] = 1;
}
} else {
if (sum[j] > -1) {
ans[j] += sum[j] + 1;
sum[j] = -1;
}
}
}
cout << min(ans[0], ans[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 | python3 | import numpy as np
n = input()
a_s = [int(x) for x in input().split()]
sum_i = 0
num = 0
for a in a_s:
new_sum_i = sum_i + a
# print(f"a:{a}, tmp_sum_i:{new_sum_i}")
if (new_sum_i == 0):
new_sum_i = 1 if sum_i == 0 else np.sign(sum_i)*(-1)
adjusted_a = new_sum_i - sum_i
num += abs(max(adjusted_a, a) - min(adjusted_a, a))
sum_i = new_sum_i
elif new_sum_i * sum_i > 0:
new_sum_i = np.sign(new_sum_i)*-1
adjusted_a = new_sum_i - sum_i
num += abs(max(adjusted_a, a) - min(adjusted_a, a))
sum_i = new_sum_i
else:
sum_i = new_sum_i
# print(f"a:{a}, new_sum_i:{sum_i}, num:{num}")
# print()
print(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;
vector<int> data(N);
for (int i = 0; i < N; i++) cin >> data[i];
int count = 0;
int ans = data[0];
int saisyo;
for (int i = 1; i < N; i++) {
ans += data[i];
if (i % 2 == 0) {
while (ans <= 0) {
ans++;
count++;
}
} else {
while (ans >= 0) {
ans--;
count++;
}
}
}
saisyo = count;
count = 0;
ans = data[0];
for (int i = 1; i < N; i++) {
ans += data[i];
if (i % 2 != 0) {
while (ans <= 0) {
ans++;
count++;
}
} else {
while (ans >= 0) {
ans--;
count++;
}
}
}
saisyo = min(saisyo, count);
cout << 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 | n = int(input())
a = list(map(int,input().split()))
A = [j for j in a]
cnt1 = 0
cnt2 = 0
S = 0
if a[0] > 0:
for i in range(n-1):
S += a[i]
if a[i] > 0:
if S + a[i+1] >= 0:
cnt1 += a[i+1] + S + 1 if a[i+1] + S + 1 > 0 else -(a[i+1] + S + 1)
a[i+1] = -1-S
elif a[i] < 0:
if S + a[i+1] <= 0:
cnt1 += a[i+1] + S - 1 if a[i+1] + S - 1 > 0 else -(a[i+1] + S - 1)
a[i+1] = 1-S
A[0] = -1
cnt2 = a[0] + 1
for i in range(n-1):
S += A[i]
if A[i] > 0:
if S + A[i+1] >= 0:
cnt2 += A[i+1] + S + 1 if A[i+1] + S + 1 > 0 else -(A[i+1] + S + 1)
A[i+1] = -1-S
elif A[i] < 0:
if S + A[i+1] <= 0:
cnt2 += a[i+1] + S - 1 if a[i+1] + S - 1 > 0 else -(a[i+1] + S - 1)
A[i+1] = 1-S
elif a[0] < 0:
for i in range(n-1):
S += a[i]
if a[i] > 0:
if S + a[i+1] >= 0:
cnt1 += a[i+1] + S + 1 if a[i+1] + S + 1 > 0 else -(a[i+1] + S + 1)
a[i+1] = -1-S
elif a[i] < 0:
if S + a[i+1] <= 0:
cnt1 += a[i+1] + S - 1 if a[i+1] + S - 1 > 0 else -(a[i+1] + S - 1)
a[i+1] = 1-S
A[0] = 1
cnt2 = -a[0] + 1
for i in range(n-1):
S += A[i]
if A[i] > 0:
if S + A[i+1] >= 0:
cnt2 += A[i+1] + S + 1 if A[i+1] + S + 1 > 0 else -(A[i+1] + S + 1)
A[i+1] = -1-S
elif A[i] < 0:
if S + A[i+1] <= 0:
cnt2 += a[i+1] + S - 1 if a[i+1] + S - 1 > 0 else -(a[i+1] + S - 1)
A[i+1] = 1-S
else:
a[0] = 1
cnt1 = 1
for i in range(n-1):
S += a[i]
if a[i] > 0:
if S + a[i+1] >= 0:
cnt1 += a[i+1] + S + 1 if a[i+1] + S + 1 > 0 else -(a[i+1] + S + 1)
a[i+1] = -1-S
elif a[i] < 0:
if S + a[i+1] <= 0:
cnt1 += a[i+1] + S - 1 if a[i+1] + S - 1 > 0 else -(a[i+1] + S - 1)
a[i+1] = 1-S
A[0] = -1
cnt2 = 1
for i in range(n-1):
S += A[i]
if A[i] > 0:
if S + A[i+1] >= 0:
cnt2 += A[i+1] + S + 1 if A[i+1] + S + 1 > 0 else -(A[i+1] + S + 1)
A[i+1] = -1-S
elif A[i] < 0:
if S + A[i+1] <= 0:
cnt2 += a[i+1] + S - 1 if a[i+1] + S - 1 > 0 else -(a[i+1] + S - 1)
A[i+1] = 1-S
cnt = cnt2 if cnt1 >= cnt2 else cnt1
print(cnt)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long store[100007];
int main() {
long long n;
cin >> n;
for (long long i = 1; i <= n; i++) cin >> store[i];
long long cnt = 0;
long long sum = store[1];
for (long long i = 2; i <= n; i++) {
long long tmp = store[i] + sum;
if (tmp)
if ((tmp < 0 && sum > 0) || (tmp > 0 && sum < 0)) {
sum = tmp;
continue;
}
long long need;
if (sum > 0) {
need = -1 - sum, sum = -1;
if (!need) need = -2 - sum, sum = -2;
} else {
need = abs(1 - sum), sum = 1;
if (!need) need = abs(2 - sum), sum = 2;
}
cnt += abs(store[i] - need);
}
cout << cnt << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a.at(i);
int count = 0;
int total = a.at(0);
int sign0;
if (a.at(0) > 0)
sign0 = 1;
else
sign0 = -1;
for (int i = 1; i < n; i++) {
total += a.at(i);
if (i % 2 == 1) {
while (total * sign0 >= 0) {
total -= sign0;
count++;
}
} else {
while (total * sign0 <= 0) {
total += sign0;
count++;
}
}
}
cout << count << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
uint n;
cin >> n;
vector<int> a(n, 0);
vector<int> S(n, 0);
for (size_t i = 0; i < a.size(); ++i) cin >> a[i];
int operation = 0;
S[0] = a[0];
if (S[0] == 0) {
size_t j = 0;
while (j < a.size()) {
if (a[j] != 0) break;
++j;
}
if (j == a.size()) {
S[0] = 1;
++operation;
} else if (j % 2 == 0) {
S[0] = a[j] / abs(a[j]);
++operation;
} else {
S[0] = -a[j] / abs(a[j]);
++operation;
}
}
for (size_t i = 1; i < a.size(); ++i) {
S[i] = S[i - 1] + a[i];
if (S[i] == 0) {
S[i] = -(S[i - 1] / abs(S[i - 1]));
++operation;
} else {
if (S[i - 1] * S[i] > 0) {
operation = operation + abs(S[i]) + 1;
S[i] = -(S[i] / abs(S[i]));
}
}
}
cout << operation << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | package main
import (
"bufio"
"errors"
"fmt"
"math"
"os"
"strconv"
"strings"
)
var sc = bufio.NewScanner(os.Stdin)
// NextLine reads a line text from stdin, and then returns its string.
func NextLine() string {
sc.Scan()
return sc.Text()
}
// NextIntsLine reads a line text, that consists of **ONLY INTEGERS DELIMITED BY SPACES**, from stdin.
// And then returns intergers slice.
func NextIntsLine() []int {
ints := []int{}
intsStr := NextLine()
tmp := strings.Split(intsStr, " ")
for _, s := range tmp {
integer, _ := strconv.Atoi(s)
ints = append(ints, integer)
}
return ints
}
func NextStringsLine() []string {
strs := []string{}
stringsStr := NextLine()
tmp := strings.Split(stringsStr, " ")
for _, s := range tmp {
strs = append(strs, s)
}
return strs
}
// NextRunesLine reads a line text, that consists of **ONLY CHARACTERS ARRANGED CONTINUOUSLY**, from stdin.
// Ant then returns runes slice.
func NextRunesLine() []rune {
return []rune(NextLine())
}
// Max returns the max integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Max(integers ...int) int {
m := integers[0]
for i, integer := range integers {
if i == 0 {
continue
}
if m < integer {
m = integer
}
}
return m
}
// Min returns the min integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Min(integers ...int) int {
m := integers[0]
for i, integer := range integers {
if i == 0 {
continue
}
if m > integer {
m = integer
}
}
return m
}
// PowInt is integer version of math.Pow
func PowInt(a, e int) int {
if a < 0 || e < 0 {
panic(errors.New("[argument error]: PowInt does not accept negative integers"))
}
fa := float64(a)
fe := float64(e)
fanswer := math.Pow(fa, fe)
return int(fanswer)
}
// AbsInt is integer version of math.Abs
func AbsInt(a int) int {
fa := float64(a)
fanswer := math.Abs(fa)
return int(fanswer)
}
// DeleteElement returns a *NEW* slice, that have the same and minimum length and capacity.
// DeleteElement makes a new slice by using easy slice literal.
func DeleteElement(s []int, i int) []int {
if i < 0 || len(s) <= i {
panic(errors.New("[index error]"))
}
// appendのみの実装
n := make([]int, 0, len(s)-1)
n = append(n, s[:i]...)
n = append(n, s[i+1:]...)
return n
}
// Concat returns a *NEW* slice, that have the same and minimum length and capacity.
func Concat(s, t []rune) []rune {
n := make([]rune, 0, len(s)+len(t))
n = append(n, s...)
n = append(n, t...)
return n
}
// sort package (snippets)
//sort.Sort(sort.IntSlice(s))
//sort.Sort(sort.Reverse(sort.IntSlice(s)))
//sort.Sort(sort.Float64Slice(s))
//sort.Sort(sort.StringSlice(s))
// copy function
//a = []int{0, 1, 2}
//b = make([]int, len(a))
//copy(b, a)
var rdr = bufio.NewReaderSize(os.Stdin, 1000000)
func readLine() string {
buf := make([]byte, 0, 1000000)
for {
l, p, e := rdr.ReadLine()
if e != nil {
panic(e)
}
buf = append(buf, l...)
if !p {
break
}
}
return string(buf)
}
/*******************************************************************/
var n int
var A []int
func main() {
tmp := NextIntsLine()
n = tmp[0]
// A = NextIntsLine()
A = make([]int, 0, 1000000)
str := readLine()
tmp2 := strings.Split(str, " ")
for _, s := range tmp2 {
integer, _ := strconv.Atoi(s)
A = append(A, integer)
}
S := make([]int, len(A))
S[0] = A[0]
for i := 1; i < len(A); i++ {
sum := S[i-1]
S[i] = sum + A[i]
}
// 最初を正とする場合と負とする場合の両方を試す
answers := []int{}
for _, firstSign := range []int{1, -1} {
comp, answer := 0, 0
if (firstSign == 1 && S[0] <= 0) || (firstSign == -1 && S[0] >= 0) {
comp = firstSign - S[0]
answer = AbsInt(comp)
}
for i := 1; i < len(S); i++ {
var befSign int
if S[i-1]+comp < 0 {
befSign = -1
} else {
befSign = 1
}
if (befSign == -1 && S[i]+comp > 0) || (befSign == 1 && S[i]+comp < 0) {
continue
}
x := -befSign - (S[i] + comp)
answer += AbsInt(x)
comp += x
}
answers = append(answers, answer)
}
fmt.Println(Min(answers...))
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
int main() {
int n;
std::cin >> n;
int counter = 0;
int seq = 0;
scanf("%d ", &seq);
int first_sum = seq;
bool prevsign = first_sum >= 0 ? true : false;
for (int i = 1; i < n; i++) {
scanf("%d", &seq);
if (i < n - 1) scanf(" ");
if (!(prevsign ^ (first_sum + seq > 0 ? true : false)) ||
!(first_sum + seq)) {
int nseq = (!prevsign ? 1 : -1) - first_sum;
counter += (int)abs(nseq - seq);
first_sum += nseq;
} else
first_sum += seq;
prevsign = !prevsign;
}
std::cout << counter;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.Scanner;
class Main {
int n;
int[] a;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Main m = new Main(sc);
m.solve();
sc.close();
}
Main(Scanner sc) {
n = sc.nextInt();
a = new int[n];
for(int i=0;i<n;i++){
a[i] = sc.nextInt();
}
}
void solve() {
int sign = (a[0]>0)?1:-1;
int cnt = 0;
int sum = a[0];
for(int i=1;i<n;i++){
sum += a[i];
if(sum*sign>=0){
cnt += Math.abs(sum) + 1;
sum = -sign;
}
sign *= -1;
}
System.out.println(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 | java | import java.util.Scanner;
public class Main {
void run() {
try (Scanner sc = new Scanner(System.in)) {
int n = sc.nextInt();
int[] a = new int[n];
for(int i=0;i<n;i++) {
a[i] = sc.nextInt();
}
int sum1 = 0;
int cnt1 = 0;
// a[0] > 0
for(int i=0;i<n;i++) {
sum1 += a[i];
if(i%2==0 && sum1 <= 0) {
cnt1 += (1-sum1);
sum1 = 1;
}else if(i%2==1 && sum1 >= 0) {
cnt1 += (1+sum1);
sum1 = -1;
}
}
int sum2 = 0;
int cnt2 = 0;
// a[0] < 0
for(int i=0;i<n;i++) {
sum2 += a[i];
if(i%2==1 && sum2 <= 0) {
cnt2 += (1-sum2);
sum2 = 1;
}else if(i%2==0 && sum2 >= 0) {
cnt2 += (1+sum2);
sum2 = -1;
}
}
System.out.println(Math.min(cnt1, cnt2));
}
}
public static void main(String[] args) {
new Main().run();
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
long long a[100010];
int main() {
int n;
while (scanf("%d", &n) != EOF) {
long long sum = 0;
for (int i = 0; i < n; i++) {
scanf("%lld", &a[i]);
}
int tmp = -1;
int flagg = 0;
for (int i = 0; i < n; i++) {
if (a[i] != 0) {
tmp = i;
flagg = 1;
break;
}
}
if (!flagg) {
printf("%d\n", (n - 1) * 2 + 1);
continue;
}
if (tmp != 0 && a[tmp] > 0) {
if (tmp % 2)
a[0] = -1;
else
a[0] = 1;
sum++;
} else if (tmp != 0 && a[tmp] < 0) {
if (tmp % 2)
a[0] = 1;
else
a[0] = -1;
sum++;
}
long long oo = a[0], flag;
if (a[0] > 0)
flag = 1;
else if (a[0] < 0)
flag = -1;
for (int i = 1; i < n; i++) {
oo += a[i];
if (flag == 1) {
if (oo >= 0) {
sum += oo + 1;
oo = -1;
}
flag = -1;
} else if (flag == -1) {
if (oo <= 0) {
sum += 0 - oo + 1;
oo = 1;
}
flag = 1;
}
}
printf("%lld\n", sum);
}
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import math
"""
e.g. N=3 a=[-1 1 1]
-1 2 1 :1
-1 2 -2 :4
e.g. N=3 a=[0 0 -1]
1 0 -1 :1
1 -2 -1 :3
1 -2 2 :6
-1 0 -1 :1
-1 2 -1 :3
-1 2 -2 :4
e.g. N=3 a=[0 0 1]
1 0 1 :1
1 -2 1 :3
1 -2 2 :4
-1 0 1 :1
-1 2 1 :3
-1 2 -2 :6
e.g. N=4 a=[0 0 0 -1]
1 0 0 -1 :1
1 -2 0 -1 :3
1 -2 2 -1 :5
1 -2 2 -2 :6
-1 0 0 -1 :1
-1 2 0 -1 :3
-1 2 -2 -1 :5
-1 2 -2 2 :8
e.g. N=4 a=[0 0 0 1]
1 0 0 1 :1
1 -2 0 1 :3
1 -2 2 1 :5
1 -2 2 -2 :8
-1 0 0 1 :1
-1 2 0 1 :3
-1 2 -2 1 :5
-1 2 -2 2 :6
"""
def main():
n = int(input())
a = list(map(int, input().split()))
ans = 0
# 最初から0続きのとき、操作が少なくなるように同じ符号にする
def sign(x):
if x == 0:
return 0
return int(math.copysign(1, x))
if a[0] == 0:
for i in range(1, n):
if a[i] != 0:
sign_a = sign(a[i])
if i % 2 == 0:
# odd: 1-indexed
a[0] = sign_a
else:
a[0] = -sign_a
ans += 1
break
s = a[0]
for i in range(1, n):
s1 = s + a[i]
sign_s = sign(s)
sign_s1 = sign(s1)
if s1 == 0 or sign_s == sign_s1:
# 逆方向に変更
# new_s1 = -sign_s = s+a[i] + diff
diff = -(sign_s + s + a[i])
a[i] += diff
ans += abs(diff)
s += a[i]
#print(i, a, sep="\t")
print(ans)
if __name__ == '__main__':
main()
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.