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 | java | import java.io.File;
import java.io.IOException;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Deque;
import java.util.List;
import java.util.Scanner;
public class Main {
//ABC059
public static void main(String[] args) throws IOException {
//File file = new File("input.txt");
//Scanner in = new Scanner(file);
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] a = new int[n];
long[] sum = new long[n];
long ans = 0;
for(int i = 0; i < n; i++){
a[i] = in.nextInt();
if(i == 0) sum[0] = a[0];
else sum[i] = sum[i-1] + a[i];
}
/*
for(int j = 0; j < n; j++) System.out.print(sum[j] + " ");
System.out.println();
*/
if(sum[0] == 0){
if(sum[1] > 0){
for(int i = 0; i < n; i++) sum[i]--;
ans++;
}else{
for(int i = 0; i < n; i++) sum[i]++;
ans++;
}
}
long diff_0 = 0;
for(int i = 1; i < n; i++){
sum[i] += diff_0;
if(sum[i] == 0){
if(sum[i-1] < 0){
sum[i]++;
diff_0++;
ans++;
}else{
sum[i]--;
diff_0--;
ans++;
}
}
}
/*
for(int j = 0; j < n; j++) System.out.print(sum[j] + " ");
System.out.println();
*/
long diff = 0;
for(int i = 1; i < n; i++){
sum[i] += diff;
/*
System.out.print(i + ":");
for(int j = 0; j < n; j++) System.out.print(sum[j] + " ");
System.out.println();
*/
if(sum[i-1] < 0 && sum[i] <= 0){
long d = - sum[i] + 1;
sum[i] += d;
diff += d;
ans += Math.abs(d);
}else if(sum[i-1] > 0 && sum[i] >= 0){
long d = - sum[i] - 1;
sum[i] += d;
diff += d;
ans += Math.abs(d);
}
}
/*
System.out.println();
for(int i = 0; i < n; i++) System.out.print(sum[i] + " ");
System.out.println();
*/
System.out.println(ans);
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long int MAX_N = 1 << 17;
using namespace std;
long long int dy[] = {0, 0, 1, -1, 0};
long long int dx[] = {1, -1, 0, 0, 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;
}
long long int gcd(long long int a, long long int b) {
return b ? gcd(b, a % b) : a;
}
struct aaa {
aaa() {
cin.tie(0);
ios::sync_with_stdio(0);
cout << fixed << setprecision(20);
};
} aaaaaaa;
signed main() {
long long int n;
std::cin >> n;
std::vector<long long int> a;
for (long long int(i) = 0, i_len = (n); (i) < i_len; (i)++) {
long long int temp;
std::cin >> temp;
a.push_back(temp);
}
long long int sum = 0;
long long int out = 0;
long long int outt = 0;
sum = a[0];
if (sum == 0) {
out++;
sum++;
}
for (long long int i = 1; i < n; i++) {
if (sum < 0) {
if (a[i] + sum > 0) {
sum = a[i] + sum;
} else {
out += abs(sum + a[i]) + 1;
sum = 1;
}
} else {
if (a[i] + sum < 0) {
sum = a[i] + sum;
} else {
out += abs(sum + a[i]) + 1;
sum = -1;
}
}
}
if (sum < 0) {
sum = 1;
outt += abs(a[0]) + 1;
} else {
sum = -1;
outt += abs(a[0]) + 1;
}
for (long long int i = 1; i < n; i++) {
if (sum < 0) {
if (a[i] + sum > 0) {
sum = a[i] + sum;
} else {
outt += abs(sum + a[i]) + 1;
sum = 1;
}
} else {
if (a[i] + sum < 0) {
sum = a[i] + sum;
} else {
outt += abs(sum + a[i]) + 1;
sum = -1;
}
}
}
std::cout << min(out, outt) << std::endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n = 0;
cin >> n;
vector<int> num(n);
for (int i = 0; i < n; i++) cin >> num[i];
int now = 0;
int wa = 0;
now = 1;
int count = 0;
int res = 0;
res = INT_MAX;
for (int i = 0; i < n; i++) {
wa += num[i];
if (now == 1 && wa < now)
count += now - wa, wa = 1;
else if (now == -1 && wa > now)
count += wa - now, wa = -1;
now *= -1;
}
res = count;
wa = 0;
now = -1;
count = 0;
for (int i = 0; i < n; i++) {
wa += num[i];
if (now == 1 && wa < now)
count += now - wa, wa = 1;
else if (now == -1 && wa > now)
count += wa - now, wa = -1;
now *= -1;
}
res = min(count, res);
cout << res << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
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 += -ans + 1
ans = 1
s == 1
if ans >= 0 and s == 1:
cnt += ans + 1
ans = -1
s == -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 | python3 | import functools
n = int(input())
a = list(map(int, input().split()))
@functools.lru_cache()
def long_func(n):
r = a[0]
cnt = 0
for i in range(1,n):
if r>0:
if r+a[i]<0:
r+=a[i]
else:
cnt += a[i] +r +1
r = -1
else:
if r+a[i]>0:
r+=a[i]
else:
cnt += -a[i] - r+1
r = 1
return cnt
print(long_func(n)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
a_orig = a[:]
ans1 = 0
ans2 = 0
tot = [0 for i in range(n)]
tot[0] = a[0]
for i in range(1, n):
tot[i] = tot[i-1] + a[i]
if i % 2 == 0:
if tot[i] <= 0:
tot[i] = 1
a[i] = tot[i] - tot[i-1]
else:
if tot[i] >= 0:
tot[i] = -1
a[i] = tot[i] - tot[i-1]
for i in range(n):
ans1 += abs(a[i]-a_orig[i])
a = a_orig[:]
tot = [0 for i in range(n)]
tot[0] = a[0]
for i in range(1, n):
tot[i] = tot[i-1] + a[i]
if i % 2 == 1:
if tot[i] <= 0:
tot[i] = 1
a[i] = tot[i] - tot[i-1]
else:
if tot[i] >= 0:
tot[i] = -1
a[i] = tot[i] - tot[i-1]
for i in range(n):
ans2 += abs(a[i]-a_orig[i])
print(min(ans1, ans2))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vll = vector<long long>;
using vp = vector<pair<int, int>>;
using vvi = vector<vector<int>>;
using vvc = vector<vector<char>>;
using vvll = vector<vector<long long>>;
template <class T>
inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
inline bool chmin(T &a, T b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vll a(n);
for (int i = 0; i < (int)(n); i++) cin >> a[i];
auto f = [&](ll x) {
ll sm = x;
ll res = 0;
for (int i = 1; i < n; ++i) {
if (sm > 0) {
if (!(a[i] < -sm)) {
res += a[i] - (-sm - 1);
a[i] = -sm - 1;
}
} else {
if (!(-sm < a[i])) {
res += (-sm + 1) - a[i];
a[i] = -sm + 1;
}
}
sm += a[i];
}
return res;
};
ll ans;
if (a[0] == 0) {
ll res1 = f(-1) + 1;
ll res2 = f(1) + 1;
ans = min(res1, res2);
} else {
ans = f(a[0]);
}
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 | UNKNOWN | using System;
using System.Linq;
class Program
{
static void Main(string[] args)
{
int n = int.Parse(Console.ReadLine());
int[] an = Console.ReadLine().Split(' ').Select(e => int.Parse(e)).ToArray();
int sum = 0;
int cs1 = 0;
int t = 1;
for (int i = 0; i < n; i++)
{
sum += an[i];
if (sum * t <= 0)
{
int tmp = Math.Abs(sum - t);
sum = t;
cs1 += tmp;
}
t *= -1;
}
int cs2 = 0;
t = -1;
sum = 0;
for (int i = 0; i < n; i++)
{
sum += an[i];
if (sum * t <= 0)
{
int tmp = Math.Abs(sum - t);
sum = t;
cs2 += tmp;
}
t *= -1;
}
Console.WriteLine(Math.Min(cs1, cs2));
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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 int MOD = 1000000007;
using long long = 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;
}
void print(const std::vector<int> &v) {
std::for_each(v.begin(), v.end(), [](int x) { std::cout << x << " "; });
std::cout << std::endl;
}
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (long long i = 0; i < (long long)n; i++) {
cin >> a[i];
}
int ans = 0;
if (a[0] > 0) {
int s = a[0];
for (int i = 1; i < n; i++) {
s += a[i];
if (i % 2 == 1) {
if (s >= 0) {
ans += s + 1;
s = -1;
}
} else {
if (s <= 0) {
ans += -s + 1;
s = 1;
}
}
}
} else if (a[0] < 0) {
int s = a[0];
for (int i = 1; i < n; i++) {
s += a[i];
if (i % 2 == 0) {
if (s >= 0) {
ans += s + 1;
s = -1;
}
} else {
if (s <= 0) {
ans += -s + 1;
s = 1;
}
}
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N=int(input())
s=list(map(int,input().split()))
if s[0]<=0:
t=1
elif s[0]>0:
t=-1
ss=s[0]
w=0
for i in range(N-1):
if t==1:
if ss+s[i+1]>=t:
ss=ss+s[i+1]
pass
else:
w+=t-ss-s[i+1]
ss=1
t=-1
elif t==-1:
if ss+s[i+1]<=t:
ss=ss+s[i+1]
pass
else:
w+=ss+s[i+1]-t
ss=-1
t=1
print(w)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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()))
kp=0
asum=0
flag=-1
for i in range(n):
asum=asum+a[i]
if flag==-1:
if asum>=0:
kp=kp+asum+1
asum=-1
else:
if asum<=0:
kp=kp+1-asum
asum=1
flag=-flag
print(asum)
print("-----",kp)
km=0
asum=0
flag=1
for i in range(n):
asum=asum+a[i]
if flag==-1:
if asum>=0:
km=km+asum+1
asum=-1
else:
if asum<=0:
km=km+1-asum
asum=1
flag=-flag
print(asum)
print("-----",km)
print(min(kp,km))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (long long(i) = 0; (i) < (n); (i)++) cin >> a[i];
long long ans = LLONG_MAX;
long long total = 0;
long long tmp = 0;
for (int i = 0; i < n; ++i) {
tmp += a[i];
if (i % 2) {
if (tmp > 0) {
total += abs(tmp) + 1;
tmp = -1;
}
} else {
if (tmp < 0) {
total += abs(tmp) + 1;
tmp = 1;
}
}
}
ans = total;
total = 0;
tmp = 0;
for (int i = 1; i <= n; ++i) {
tmp += a[i];
if (i % 2 == 0) {
if (tmp > 0) {
total += abs(tmp) + 1;
tmp = -1;
}
} else {
if (tmp < 0) {
total += abs(tmp) + 1;
tmp = 1;
}
}
}
ans = min(total, ans);
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | import Control.Monad
import Data.List
main=do
_<-getLine
(a:as)<-map read.words<$>getLine::IO[Integer]
print.sum.snd$ mapAccumL f a as
f a b
| signum a * signum (a+b) < 0 = (a+b,0)
| a < 0 = (1, 1-(a+b))
| otherwise = ((-1), 1+(a+b))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> a(N);
for (int i = 0; i < N; i++) cin >> a.at(i);
int t = 0, res1 = 0, res2 = 0;
for (int i = 0; i < N; i++) {
int b = a.at(i);
if (i % 2 == 0) {
if (t + b <= 0) {
b = abs(t) + 1;
res1 += b - a.at(i);
}
} else {
if (t + b >= 0) {
b = -abs(t) - 1;
res1 += abs(b - a.at(i));
}
}
t += b;
}
t = 0;
for (int i = 0; i < N; i++) {
int b = a.at(i);
if (i % 2 == 1) {
if (t + b <= 0) {
b = abs(t) + 1;
res2 += b - a.at(i);
}
} else {
if (t + b >= 0) {
b = -abs(t) - 1;
res2 += abs(b - a.at(i));
}
}
t += b;
}
int res = min(res1, res2);
cout << res << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import numpy
n=int(input())
a=[int(i) for i in input().split()]
ans=0
sum=0
if a[0]==0:
a[0]=1
ans+=1
sum=1
for j in a[1:]:
if numpy.sign(sum)==numpy.sign(sum+j) or numpy.sign(sum+j)==0:
ans+=abs(sum+j)+1
sum=-numpy.sign(sum)
else:
sum+=j
pans=ans
a[0]=-1
ans+=1
sum=-1
for j in a[1:]:
if numpy.sign(sum)==numpy.sign(sum+j) or numpy.sign(sum+j)==0:
ans+=abs(sum+j)+1
sum=-numpy.sign(sum)
else:
sum+=j
mans=ans
ans=min(pans,mans)
else:
for j in a:
if numpy.sign(sum)==numpy.sign(sum+j) or numpy.sign(sum+j)==0:
ans+=abs(sum+j)+1
sum=-numpy.sign(sum)
else:
sum+=j
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < (n); ++i) cin >> a[i];
long long total = a[0];
long long total2 = a[0];
long long ans = 0;
for (int i = (1); i < (n); ++i) {
total += a[i];
if (total * total2 >= 0) {
if (total2 > 0) {
ans += total + 1;
total = -1;
} else {
ans += -total + 1;
total = 1;
}
}
total2 = total;
}
total2 = a[0];
long long ans2 = 0;
if (total2 > 0) {
ans2 += total2 + 1;
total2 = -1;
} else {
ans2 += -total2 + 1;
total2 = 1;
}
total = total2;
for (int i = (1); i < (n); ++i) {
total += a[i];
if (total * total2 >= 0) {
if (total2 > 0) {
ans2 += total + 1;
total = -1;
} else {
ans2 += -total + 1;
total = 1;
}
}
total2 = total;
}
cout << min(ans, ans2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long a[100010];
long long ans = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
if (i != 0) {
a[i] += a[i - 1];
if (a[i] == 0) {
if (a[i - 1] > 0) {
ans++;
a[i]--;
} else {
ans++;
a[i]++;
}
} else if (a[i - 1] > 0) {
if (a[i] > 0) {
ans += llabs(-1 - a[i]);
a[i] = -1;
}
} else if (a[i - 1] < 0) {
if (a[i] < 0) {
ans += 1 - a[i];
a[i] = 1;
}
}
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
a = list(map(int,input().split()))
presum = a[0]
summ = 0
ans = 0
for i in range(1,N):
nsum = a[i] + presum
if presum * nsum >= 0:
if nsum == 0:
if presum >= 0:
a[i] -= 1
ans += 1
nsum = -1
elif presum < 0:
a[i] += 1
ans += 1
nsum = 1
elif nsum > 0:
ans += 1+presum+a[i]
a[i] = -1 - presum
nsum = -1
elif nsum < 0:
nsum = 1
ans += 1 - presum - a[i]
a[i] = 1-presum
presum = nsum
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | n = gets.to_i
digits = gets.split.map(&:to_i)
sums = []
digits.each do |digit|
sums << (sums.empty? ? digit : sums[-1] + digit)
end
cnt = 0
(1...sums.size).each do |i|
next if sums[i - 1] * sums[i] < 0
target = (sums[i - 1] > 0 ? -1 : 1)
diff = target - sums[i]
cnt += diff.abs
(i...sums.size).each{|j| sums[j] += diff}
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;
const long long mod = 1e9 + 7;
const long long INF = 1e18;
const double pi = acos(-1.0);
int main(void) {
long long n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < (n); ++i) cin >> a[i];
long long ans = 0;
for (int i = 0; i < (n); ++i) {
if (i + 1 < n && a[i] == 0) {
a[i] += min(a[i + 1] - 1, a[i + 1] - (-1));
++ans;
continue;
} else if (i + 1 < n && a[i] < 0) {
while (i + 1 < n && a[i] + a[i + 1] <= 0) {
a[i + 1]++;
ans++;
}
} else if (i + 1 < n && a[i] > 0) {
while (i + 1 < n && a[i] + a[i + 1] >= 0) {
a[i + 1]--;
ans++;
}
}
a[i + 1] += a[i];
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> a(n), tot(n);
for (int i = 0; i < (n); ++i) cin >> a[i];
tot[0] = a[0];
for (int i = 0; i < (n - 1); ++i) tot[i + 1] += tot[i] + a[i + 1];
long long ans = 1LL << 60, now = 0;
long long wa = 0;
int p;
if (tot[0] != 0) {
p = tot[0] / abs(tot[0]);
for (int i = 0; i < (n); ++i) {
tot[i] += wa;
if (p == 1) {
if (tot[i] <= 0) {
wa += abs(tot[i]) + 1;
now += abs(tot[i]) + 1;
}
} else {
if (tot[i] >= 0) {
wa -= abs(tot[i]) + 1;
now += abs(tot[i]) + 1;
}
}
p *= -1;
}
ans = min(ans, now);
p = tot[0] / abs(tot[0]) * -1;
now = abs(tot[0]) + 1;
wa = p * (abs(tot[0]) + 1);
for (int i = 0; i < (n); ++i) {
tot[i] += wa;
if (p == 1) {
if (tot[i] <= 0) {
wa += abs(tot[i]) + 1;
now += abs(tot[i]) + 1;
}
} else {
if (tot[i] >= 0) {
wa -= abs(tot[i]) + 1;
now += abs(tot[i]) + 1;
}
}
p *= -1;
}
ans = min(ans, now);
} else {
now = 1;
wa = 1;
p = 1;
for (int i = 0; i < (n); ++i) {
tot[i] += wa;
if (p == 1) {
if (tot[i] <= 0) {
wa += abs(tot[i]) + 1;
now += abs(tot[i]) + 1;
}
} else {
if (tot[i] >= 0) {
wa -= abs(tot[i]) + 1;
now += abs(tot[i]) + 1;
}
}
p *= -1;
}
ans = min(ans, now);
now = 1;
wa = -1;
p = -1;
for (int i = 0; i < (n); ++i) {
tot[i] += wa;
if (p == 1) {
if (tot[i] <= 0) {
wa += abs(tot[i]) + 1;
now += abs(tot[i]) + 1;
}
} else {
if (tot[i] >= 0) {
wa -= abs(tot[i]) + 1;
now += abs(tot[i]) + 1;
}
}
p *= -1;
}
ans = min(ans, now);
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < (n); ++i) cin >> a[i];
long long total = a[0];
long long total2 = a[0];
long long ans = 0;
for (int i = (1); i < (n); ++i) {
total += a[i];
if (total * total2 >= 0) {
if (total2 > 0) {
ans += total + 1;
total = -1;
} else {
ans += -total + 1;
total = 1;
}
}
total2 = total;
}
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 long n = 0, a[100000] = {}, b = 0;
cin >> n;
for (int i = 0; i < (int)n; ++i) {
cin >> a[i];
}
int count_p = 0, count_q = 0;
for (int i = 0; i < (int)n; ++i) {
b += a[i];
if (i % 2 == 0) {
if (b >= 0) {
count_p += abs(-1 - b);
b = -1;
}
} else {
if (b <= 0) {
count_p += abs(1 - b);
b = 1;
}
}
}
for (int i = 0; i < (int)n; ++i) {
b += a[i];
if (i % 2 == 0) {
if (b <= 0) {
count_q += abs(1 - b);
b = 1;
}
} else {
if (b >= 0) {
count_q += abs(-1 - b);
b = -1;
}
}
}
cout << std::min(count_p, count_q) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
using namespace std;
template <typename T>
inline void priv(vector<T> a) {
for (int i = 0; i < a.size(); i++) {
cerr << a[i] << ((i == a.size() - 1) ? "\n" : " ");
}
}
inline void fastio() {
cin.tie(nullptr);
cout.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
}
int_fast64_t gcd(int_fast64_t a, int_fast64_t b) {
int_fast64_t c = max(a, b);
int_fast64_t d = min(a, b);
return c == 0 || d == 0 ? c : gcd(c % d, d);
}
int_fast64_t lcm(int_fast64_t a, int_fast64_t b) {
return a == 0 || b == 0 ? 0 : a * b / gcd(a, b);
}
int_fast64_t modfact(int_fast64_t a) {
int_fast64_t b = 1;
for (int i = 2; i <= a; i++) b = b * i % 1000000007LL;
return b;
}
int_fast64_t modpow(int_fast64_t a, int_fast64_t n) {
int_fast64_t b = 1;
while (n > 0) {
if (n & 1) b = b * a % 1000000007LL;
a = a * a % 1000000007LL;
n >>= 1;
}
return b;
}
int_fast64_t modcomb(int_fast64_t n, int_fast64_t k) {
int_fast64_t b = 1;
k = min(n - k, k);
for (int i = n; i >= n - k + 1; i--) b = b * i % 1000000007LL;
return b * modpow(modfact(k), 1000000007LL - 2) % 1000000007LL;
}
int_fast64_t N, ans;
int_fast64_t A[100001];
int_fast64_t solve(int_fast64_t S_init) {
int_fast64_t S = S_init;
int_fast64_t res = abs(S - A[0]);
for (int i = 1; i <= N - 1; i++) {
if (S * (S + A[i]) < 0) {
S += A[i];
} else {
if (S > 0) {
res += abs(A[i] - (-S - 1));
S = -1;
} else {
res += abs(A[i] - (-S + 1));
S = 1;
}
}
}
return res;
}
int main() {
fastio();
cin >> N;
for (int i = 0; i < N; i++) cin >> A[i];
if (A[0] > 0) {
ans = min(solve(A[0]), solve(-1));
} else {
ans = min(solve(A[0]), solve(+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;
const int MOD = 1000000007;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
queue<pair<int, int> > que;
int main() {
int n;
cin >> n;
long long ans = 0;
long long sum = 0;
cin >> sum;
for (int i = 1; i < n; i++) {
int ai;
cin >> ai;
if (sum > 0 and sum + ai > 0) {
ans += 2 * ai + 1;
sum = -1;
} else if (sum < 0 and sum + ai < 0) {
ans += 2 * ai + 1;
sum = 1;
} else if (sum + ai == 0) {
ans += 1;
if (sum > 0) {
sum = -1;
} else if (sum < 0) {
sum = 1;
}
} else {
sum += ai;
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
cin.sync_with_stdio(false);
int n;
cin >> n;
vector<ll> a(n);
ll sum = 0;
int flag = true;
for (int i = 0; i < n; i++) {
cin >> a[i];
if (sum * (sum + a[i]) > 0) {
flag = false;
}
sum += a[i];
if (sum == 0) {
flag = false;
}
}
if (flag) {
cout << 0 << endl;
return 0;
}
ll count1 = 0;
ll count2 = 0;
sum = a[0];
sum += a[1];
if (sum == 0) {
count1 += 1;
} else if (sum < 0) {
count1 += -1 - sum;
} else {
count1 += sum + 1;
}
sum = -1;
for (int i = 2; i < n; i++) {
if (sum < 0 && sum + a[i] < 0) {
count1 += abs(sum) - a[i] + 1;
sum = 1;
} else if (sum > 0 && sum + a[i] > 0) {
count1 += abs(sum + a[i] + 1);
sum = -1;
} else {
sum += a[i];
}
if (sum == 0) {
count1 += 1;
}
}
sum = a[0];
sum += a[1];
if (sum == 0) {
count2 += 1;
} else if (sum < 0) {
count2 += 1 - sum;
} else {
count2 += sum - 1;
}
sum = 1;
for (int i = 2; i < n; i++) {
if (sum < 0 && sum + a[i] < 0) {
count2 += abs(sum) - a[i] + 1;
sum = 1;
} else if (sum > 0 && sum + a[i] > 0) {
count2 += abs(sum + a[i] + 1);
sum = -1;
} else {
sum += a[i];
}
if (sum == 0) {
count2 += 1;
}
}
cout << min(count1, count2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | #
# Written by NoKnowledgeGG @YlePhan
# ('ω')
#
#import math
#mod = 10**9+7
#import itertools
#import fractions
#import numpy as np
#mod = 10**4 + 7
"""def kiri(n,m):
r_ = n / m
if (r_ - (n // m)) > 0:
return (n//m) + 1
else:
return (n//m)"""
""" n! mod m 階乗
mod = 1e9 + 7
N = 10000000
fac = [0] * N
def ini():
fac[0] = 1 % mod
for i in range(1,N):
fac[i] = fac[i-1] * i % mod"""
"""mod = 1e9+7
N = 10000000
pw = [0] * N
def ini(c):
pw[0] = 1 % mod
for i in range(1,N):
pw[i] = pw[i-1] * c % mod"""
"""
def YEILD():
yield 'one'
yield 'two'
yield 'three'
generator = YEILD()
print(next(generator))
print(next(generator))
print(next(generator))
"""
"""def gcd_(a,b):
if b == 0:#結局はc,0の最大公約数はcなのに
return a
return gcd_(a,a % b) # a = p * b + q"""
"""def extgcd(a,b,x,y):
d = a
if b!=0:
d = extgcd(b,a%b,y,x)
y -= (a//b) * x
print(x,y)
else:
x = 1
y = 0
return d"""
def readInts():
return list(map(int,input().split()))
mod = 10**9 + 7
def main():
n = int(input())
A = readInts()
# 符号 positive?
#po_ = True
# 変わったか変わってないか
if A[0] >= 0: # if positive
po_ = True
else: # negative
po_ = False
Cost = 0
for i in range(1,n):
#print(sum(A[:i+1]),A[i],po_)
if sum(A[:i+1]) >= 0 and not po_: # sumがpositiveで前がnegativeだった
po_ = True # positiveに
if sum(A[:i+1]) == 0:
A[i] += 1
Cost += 1
# これで終わり
elif sum(A[:i+1]) >= 0 and po_: # posi : posi ?
# 負にしなければならない
Cost += abs(-1 - sum(A[:i+1])) # 先にこれやれ
A[i] += -1 - sum(A[:i+1])
po_ = False
elif sum(A[:i+1]) < 0 and not po_: #nega : nega
# -1 はここ
# print(A[i])
Cost += abs(1 - sum(A[:i+1])) # 先にこれやれ
A[i] += 1 - sum(A[:i+1])
po_ = True
else: # nega: pos
po_ = False
if sum(A[:i+1]) == 0:
A[i] -=1
Cost -=1
#print(A[i])
print(Cost)
if __name__ == '__main__':
main() |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> A(N);
for (int i = 0; i < N; i++) {
cin >> A[i];
}
int sum = 0;
int cnt1 = 0;
for (int i = 0; i < N; i++) {
sum += A[i];
if (i % 2 == 0 && sum <= 0) {
cnt1 += 1 - sum;
sum = 1;
}
if (i % 2 == 1 && sum >= 0) {
cnt1 += sum + 1;
sum = -1;
}
}
int sum2 = 0;
int cnt2 = 0;
for (int i = 0; i < N; i++) {
sum += A[i];
if (i % 2 == 0 && sum2 >= 0) {
cnt2 += sum2 + 1;
sum2 = -1;
}
if (i % 2 == 1 && sum2 <= 0) {
cnt2 += 1 - sum2;
sum2 = 1;
}
}
cout << min(sum, 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;
int main() {
int n, a[100000], ans, sumb = 0, suma = 0;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) {
sumb = suma;
suma += a[i];
if (suma == 0) {
if (sumb > 0)
suma--;
else
suma++;
ans++;
} else if (suma * sumb > 0) {
if (sumb > 0) {
ans += suma + 1;
suma -= suma + 1;
} else if (sumb < 0) {
ans += 1 - suma;
suma -= 1 - suma;
}
} 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 | cpp | #include <bits/stdc++.h>
long body(std::vector<long>& a) {
long ans = 0;
std::vector<long> s(a.size());
s.at(0) = a.at(0);
for (unsigned long i = 1; i < a.size(); i++) {
s.at(i) = s.at(i - 1) + a.at(i);
}
long diff = 0;
for (unsigned long i = 1; i < s.size(); i++) {
s.at(i) += diff;
long n = 0;
if (s.at(i - 1) > 0 && s.at(i) >= 0) {
n = s.at(i) + 1;
ans += n;
diff -= n;
s.at(i) += diff;
} else if (s.at(i - 1) < 0 && s.at(i) <= 0) {
n = -s.at(i) + 1;
ans += n;
diff += n;
s.at(i) += diff;
}
}
return ans;
}
int main(int argc, char** argv) {
long n;
std::cin >> n;
std::vector<long> a(n);
for (long i = 0; i < n; i++) {
std::cin >> a.at(i);
}
long a0 = a.at(0);
long ans_a, ans_b;
{
a.at(0) = a0;
if (a.at(0) > 0) {
ans_a = body(a);
} else {
a.at(0) = 1;
ans_a = body(a) + (-a0 + 1);
}
}
{
a.at(0) = a0;
if (a.at(0) < 0) {
ans_b = body(a);
} else {
a.at(0) = -1;
ans_b = body(a) + (a0 + 1);
}
}
long ans = std::min(ans_a, ans_b);
std::cout << ans << 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 | python2 | if __name__ == '__main__':
N = input()
array = map(int, raw_input().split(" "))
ans = 0
total = array[0]
totalZero = False
if total == 0:
totalZero = True
flag = False
if total > 0:
flag = True
for a in array[1:]:
if totalZero == True:
ans += 1
if a > 0:
total = -1
else:
total = 1
totalZero = False
if total + a > 0 and total > 0:
total += a
ans += total + 1
total = -1
elif total + a < 0 and total < 0:
total += a
ans += abs(total - 1)
total = 1
elif total + a == 0:
totalZero = True
else:
total += a
if totalZero == True:
ans += 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 | def c(ints):
for i in range(len(ints)):
if ints[i] != 0:
sig = 1 if ints[i] > 0 else -1
sig_ = -sig
total = ints[i]
total_ = -sig
mov = i
mov_ = abs(total) + 1
if i > 0:
mov += 1
mov_ += 1
j = i
break
if i == len(ints) - 1:
return i + 1
for i_ in ints[j+1:]:
tmp = total + i_
tmp_ = total_ + i_
if tmp == 0:
mov +=1
tmp = -sig
elif sig * tmp > 0:
mov += abs(tmp) + 1
tmp = -sig
if tmp_ == 0:
mov_ +=1
tmp_ = -sig_
elif sig_ * tmp_ > 0:
mov_ += abs(tmp_) + 1
tmp_ = -sig_
sig *= -1
total = tmp
sig_ *= -1
total_ = tmp_
return min(mov, mov_)
_ = input()
inp = input()
inp = inp.split(' ')
inp = [int(i_) for i_ in inp]
print(c(inp)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | from sys import stdin
def main():
#入力
readline=stdin.readline
n=int(readline())
a=list(map(int,readline().split()))
s=[a[0]]*n
for i in range(1,n):
s[i]=s[i-1]+a[i]
flag=True
#1,-1,...
for i in range(n):
if i%2==0:
if s[i]<=0:
flag=False
break
else:
if s[i]>=0:
flag=False
break
if flag:
print(0)
else: #-1,1,...
flag=True
for i in range(n):
if i%2==0:
if s[i]>=0:
flag=False
break
else:
if s[i]<=0:
flag=False
break
if flag:
print(0)
else:
cnt1=0
s1=s[:]
f1=0
#1,-1,...
for i in range(n):
if i%2==0:
if s1[i]+f1<=0:
cnt1+=abs(1-s1[i]-f1)
f1+=1-s1[i]
s1[i]=1
else:
if s1[i]+f1>=0:
cnt1+=abs(-1-s1[i]-f1)
f1+=-1-s1[i]
s1[i]=-1
cnt2=0
s2=s[:]
f2=0
#-1,1,...
for i in range(n):
if i%2==0:
if s2[i]+f2>=0:
cnt2+=abs(-1-s2[i]-f2)
f2+=-1-s2[i]
s2[i]=-1
else:
if s2[i]+f2<=0:
cnt2+=abs(1-s2[i]-f2)
f2+=1-s2[i]
s2[i]=1
print(min(cnt1,cnt2))
if __name__=="__main__":
main() |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<ll> v(n, 0);
for (int i = (int)(0); i < (int)(n); i++) cin >> v[i];
vector<int> p1(n + 1, 1);
for (int i = (int)(0); i < (int)(n + 1); i++) {
if (i % 2 == 0) p1[i] *= -1;
}
int c[2];
vector<ll> sum_until(n + 1, 0);
int cnt = 0;
for (int i = 1; i <= n; i++) {
sum_until[i] = sum_until[i - 1] + v[i - 1];
if (sum_until[i] * p1[i] < 0) {
int plus = abs(sum_until[i]);
sum_until[i] += plus * p1[i] + p1[i];
cnt += abs(plus * p1[i]) + 1;
} else if (sum_until[i] == 0) {
sum_until[i] = p1[i];
cnt += 1;
}
}
c[0] = cnt;
fill(sum_until.begin(), sum_until.end(), 0ll);
cnt = 0;
for (int i = (int)(0); i < (int)(n + 1); i++) {
if (i % 2 == 1) p1[i] *= -1;
}
for (int i = 1; i <= n; i++) {
sum_until[i] = sum_until[i - 1] + v[i - 1];
if (sum_until[i] * p1[i] < 0) {
int plus = abs(sum_until[i]);
sum_until[i] += plus * p1[i] + p1[i];
cnt += abs(plus * p1[i]) + 1;
} else if (sum_until[i] == 0) {
sum_until[i] = p1[i];
cnt += 1;
}
}
c[1] = cnt;
cerr << "("
"c[0]"
","
"c[1]"
"):("
<< c[0] << "," << c[1] << ")" << endl;
cout << min(c[1], c[0]) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
int inf = 1000000007;
using namespace std;
int main() {
int n;
cin >> n;
vector<int> data(n);
int ans = 0;
for (int i = 0; i < n; i++) {
cin >> data.at(i);
}
int64_t sum = data.at(0);
int64_t sump = sum;
for (int i = 1; i < n; i++) {
sump += data.at(i);
cout << sump << " " << sum << endl;
if (sum * sump > 0) {
int c = sump;
if (c < 0) c *= -1;
c++;
ans += c;
if (sump > 0) {
data.at(i) -= c;
sump -= c;
} else {
data.at(i) += c;
sump += c;
}
}
sum += data.at(i);
}
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 | void main() {
auto N = ri;
auto a = readAs!(int[]);
ulong res;
long S;
ulong tmp;
// Even is positive
foreach(i; 0..N) {
S += a[i];
if(i%2) { // Odd is negative
if(S >= 0) {
tmp += 1 + S;
S = -1;
}
} else {
if(S <= 0) {
tmp += 1 - S;
S = 1;
}
}
debug tmp.writeln;
}
res = tmp;
tmp = 0;
S = 0;
foreach(i; 0..N) {
S += a[i];
if(i%2==1) {
if(S >= 0) {
tmp += 1 + S;
S = -1;
}
} else {
if(S <= 0) {
tmp += 1 - S;
S = 1;
}
}
debug tmp.writeln;
}
res = min(res, tmp);
writeln(res);
}
// ===================================
import std.stdio;
import std.string;
import std.functional;
import std.conv;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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 REP(i, n) for(ll i = 0; i < (ll)n; i++)
#define FOR(i, a, b) for(ll i = (a); i < (ll)b; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define INF (1ll << 60)
#define sz(x) int(x.size())
using namespace std;
typedef long long ll;
typedef double db;
typedef string str;
typedef pair<ll, ll> p;
constexpr int MOD = 1000000007;
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;
}
void print(const std::vector<int> &v) {
std::for_each(v.begin(), v.end(), [](int x) { std::cout << x << " "; });
std::cout << std::endl;
}
int main() {
int n;
cin >> n;
vector<ll> a(n);
REP(i, n) { cin >> a[i]; }
ll res = INF;
ll ans = 0LL;
ll s = a[0];
//はじめは正
for(int i = 1; i < n; i++) {
s += a[i];
if(i % 2 == 1) {
//負に
if(s >= 0) {
ans += s + 1;
s = -1;
}
} else {
//正に
if(s <= 0) {
ans += -s + 1;
s = 1;
}
}
}
res = min(res, ans);
// cout << s << endl;
ans = 0;
ll s = a[0];
for(int i = 1; i < n; i++) {
s += a[i];
if(i % 2 == 0) {
if(s >= 0) {
ans += s + 1;
s = -1;
}
} else {
if(s <= 0) {
ans += -s + 1;
s = 1;
}
}
}
res = min(res, ans);
cout << res << endl;
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n=int(input())
a=list(map(int,input().split()))
b=a[:]
x=0#pmpm
y=0
for i in range(n):
if(i%2==0):
if(sum(a[:i+1])<=0):
x+=1-sum(a[:i+1])
a[i]+=1-sum(a[:i+1])
if(sum(b[:i+1])>=0):
y+=sum(b[:i+1])+1
b[i]-=sum(b[:i+1])+1
else:
if(sum(a[:i+1])>=0):
x+=sum(a[:i+1])+1
a[i]-=1+sum(a[:i+1])
if(sum(b[:i+1])<=0):
y+=1-sum(b[:i+1])
b[i]+=1-sum(b[:i+1])
print(min(x,y)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
int b[n];
int a[n];
for (int i = 1; i <= n; i++) {
cin >> a[i];
b[i] = a[i];
}
long long int sum = 0;
long long int count = 0, count1 = 0;
for (int i = 1; i <= n; i++) {
if (i != 1) {
if (i % 2 == 0) {
if ((sum + a[i]) <= 0) {
count += abs(sum + a[i]) + 1;
sum = 1;
} else {
sum += a[i];
}
} else {
if ((sum + a[i]) >= 0) {
count += abs(sum + a[i]) + 1;
sum = -1;
} else {
sum += a[i];
}
}
} else {
if (a[i] >= 0) {
sum = -1;
count += (a[i] + 1);
} else if (a[i] < 0) {
sum += a[i];
}
}
}
for (int i = 1; i <= n; i++) a[i] = b[i];
sum = 0;
for (int i = 1; i <= n; i++) {
if (i != 1) {
if (i % 2 != 0) {
if (sum + a[i] <= 0) {
count1 += abs(sum + a[i]) + 1;
sum = 1;
} else {
sum += a[i];
}
} else {
if (sum + a[i] >= 0) {
count1 += abs(sum + a[i]) + 1;
sum = -1;
} else {
sum += a[i];
}
}
} else {
if (a[i] <= 0) {
sum += 1;
count1 += abs(a[i] + 1);
} else if (a[i] > 0) {
sum += a[i];
}
}
}
cout << min(count, count1) << '\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;
int N[100000], E[100000];
cin >> n;
for (int i = 0; i < n; i++) {
cin >> N[i];
}
for (int i = 0; i < n; i++) {
E[i] = N[i];
}
int sumA = 0, sumB = 0;
int ansA = 0, ansB = 0;
for (int i = 0; i < n; i++) {
sumA = sumA + N[i];
if (i % 2 == 0 && sumA <= 0) {
N[i] = N[i] - sumA + 1;
ansA = ansA - sumA + 1;
sumA = 1;
}
if (i % 2 == 1 && sumA >= 0) {
N[i] = N[i] - sumA - 1;
ansA = ansA + sumA + 1;
sumA = -1;
}
}
for (int i = 0; i < n; i++) {
sumB = sumB + N[i];
if (i % 2 == 0 && sumB >= 0) {
E[i] = E[i] - sumB - 1;
ansB = ansB + sumB + 1;
sumB = -1;
}
if (i % 2 == 1 && sumB <= 0) {
E[i] = E[i] - sumB + 1;
ansB = ansB - sumB + 1;
sumB = 1;
}
}
int ans;
ans = min(ansA, ansB);
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>
using namespace std;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
int a[100000];
for (long long i = 0; i < n; i++) cin >> a[i];
int ans = 1 << 31 - 1;
int sum[100001] = {};
for (long long p = 0; p < 2; p++) {
int cnt = 0;
for (long long i = 0; i < n; i++) {
int border = 1 + (p + i) % 2 * -2;
sum[i + 1] = sum[i] + a[i];
if (border == 1 && sum[i + 1] >= border) continue;
if (border == -1 && sum[i + 1] <= border) continue;
cnt += abs(border - sum[i + 1]);
sum[i + 1] = border;
}
ans = min(ans, cnt);
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | # coding: utf-8
# Your code here!
n = int(input())
a = list(map(int,input().split()))
def operate(ls,x):
sm = 0
ans = 0
ans += abs(ls[0]-x)
k = x
for i in range(1,len(ls)):
sm += k
if sm > 0:
if abs(ls[i]) <= sm or sm < ls[i]:
ans += abs(ls[i]+sm+1)
k = -sm-1
else:
k = ls[i]
if sm < 0:
if abs(ls[i]) <= abs(sm) or sm > ls[i]:
ans += abs(-sm+1-ls[i])
k = -sm+1
else:
k = ls[i]
return ans
anstot = []
if a[0] > 0:
inv = -1
if a[0] < 0:
inv = 1
if a[0] != 0:
anstot.append(operate(a,a[0]))
anstot.append(operate(a,inv))
if a[0] == 0:
for i in range(n):
if a[i] == 0:
a[i] = -1**(i%2)
if a[i] != 0:
r = i
break
anstot.append(operate(r+a,a[0]))
for i in range(r):
a[i] = -1**((1+i)%2)
anstot.append(operate(r+a,a[0]))
print(min(anstot)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | # -*- coding: utf-8 -*-
# 整数の入力
n=int(input())
a=input().split()
counter=0
# 出力
for i in range(1,n):
S=0
for j in range(0,i):
S=S+int(a[j])
if S<0 and S+int(a[i])<=0:
counter=counter-S-int(a[i])+1
a[i]=-S+1
elif S>0 and S+int(a[i])>=0:
counter=counter+S+int(a[i])+1
a[i]=-S-1
print(counter) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
scanf("%d", &n);
vector<long long> a(n, 0);
for (int i = 0; i < n; i++) {
cin >> a.at(i);
}
bool target_sign = false;
if (a.at(0) > 0) {
target_sign = true;
} else {
target_sign = false;
}
long long ans = 0;
long long sum = a.at(0);
for (int i = 1; i < n; i++) {
sum += a.at(i);
if (target_sign) {
if (sum >= 0) {
while (sum >= 0) {
sum--;
ans++;
}
}
target_sign = false;
} else {
if (sum <= 0) {
while (sum <= 0) {
sum++;
ans++;
}
}
target_sign = true;
}
}
printf("%lld", ans);
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <bits/stdc++.h>
int main() {
long long int n, a, i, S[2] = {}, C[2] = {};
scanf("%lld", &n);
for (i = 0; i < n; i++) {
scanf("%lld", &a);
S[0] += a;
S[1] += a;
if (i == 0)
;
else {
if (i % 2 == 1) {
if (S[0] <= 0) {
C[0] += llabs(S[0]) + 1;
S[0] = 1;
}
if (S[1] >= 0) {
C[1] += llabs(S[1]) + 1;
S[1] = -1;
}
} else {
if (S[0] >= 0) {
C[0] += llabs(S[0]) + 1;
S[0] = -1;
}
if (S[1] <= 0) {
C[1] += llabs(S[1]) + 1;
S[1] = 1;
}
}
}
}
printf("%lld\n", C[0] < C[1] ? C[0] : C[1]);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | def decision(i):
sum_for_i = sum(A[:i+1]) # 0~i までの sum
sum_before_i = sum(A[:i]) # 0~i-1までの sum
if sum_for_i == 0:
if sum_before_i <0:
return 1
if sum_before_i >0:
return 2
if sum_before_i > 0 and sum_for_i >0:
return 3
if sum_before_i < 0 and sum_for_i <0:
return 4
return 0
n = int(input())
A = [int(x) for x in input().split()]
count = 0
for i in range(1, n):
result = decision(i)
#print(i)
#print(i, A[i], result)
if result == 0:
count += 0
elif result == 1 or result == 4:
#print('加算するケース')
while decision(i) != 0:
A[i] += 1
count += 1
elif result == 2 or result == 3:
#print('減算するケース')
while decision(i) != 0:
A[i] -= 1
count += 1
#print(A)
print(count) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
bool sortbysec(const pair<long long, long long> &a,
const pair<long long, long long> &b) {
return (a.second < b.second);
}
void func(void) {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
int main() {
long long n;
cin >> n;
long long a[n];
for (long long i = 0; i < n; i = i + 1) {
cin >> a[i];
}
long long count1 = 0;
long long count2 = 0;
long long sum1 = 0;
if (a[0] == 0) {
sum1 = 1;
count1++;
for (long long i = 1; i < n; i = i + 1) {
long long d = 0;
long long dif = 0;
if (sum1 > 0) {
if (a[i] + sum1 >= 0) {
d = -1;
long long s = d - sum1;
dif = abs(a[i] - s);
count1 = count1 + dif;
sum1 = d;
} else {
sum1 = sum1 + a[i];
}
} else {
if (a[i] + sum1 <= 0) {
d = 1;
long long s = d - sum1;
dif = abs(a[i] - s);
count1 = count1 + dif;
sum1 = d;
} else {
sum1 = sum1 + a[i];
}
}
}
sum1 = -1;
count2++;
for (long long i = 1; i < n; i = i + 1) {
long long d = 0;
long long dif = 0;
if (sum1 > 0) {
if (a[i] + sum1 >= 0) {
d = -1;
long long s = d - sum1;
dif = abs(a[i] - s);
count2 = count2 + dif;
sum1 = d;
} else {
sum1 = sum1 + a[i];
}
} else {
if (a[i] + sum1 <= 0) {
d = 1;
long long s = d - sum1;
dif = abs(a[i] - s);
count2 = count2 + dif;
sum1 = d;
} else {
sum1 = sum1 + a[i];
}
}
}
count1 = min(count1, count2);
} else {
sum1 = a[0];
for (long long i = 1; i < n; i = i + 1) {
long long d = 0;
long long dif = 0;
if (sum1 > 0) {
if (a[i] + sum1 >= 0) {
d = -1;
long long s = d - sum1;
dif = abs(a[i] - s);
count1 = count1 + dif;
sum1 = d;
} else {
sum1 = sum1 + a[i];
}
} else {
if (a[i] + sum1 <= 0) {
d = 1;
long long s = d - sum1;
dif = abs(a[i] - s);
count1 = count1 + dif;
sum1 = d;
} else {
sum1 = sum1 + a[i];
}
}
}
}
cout << count1 << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | object Main {
def main(args: Array[String]): Unit = {
import scala.io.StdIn.readLine
val _ = readLine
val datA = readLine.split(" ").map(_.toLong).dropWhile(_ == 0)
def judgement(acc: (Long, Long), cur: (Long, Long)): (Long, Long) = {
val sum = acc._1
val count = acc._2
val a = cur._1
if (sum > 0) {
if (sum + a >= 0) (-1, count + math.abs(sum + a) + 1)
else (sum + a, count)
}
else if (sum < 0) {
if (sum + a <= 0) (1, count + math.abs(sum + a) + 1)
else (sum + a, count)
} else {
(a, count)
}
}
val posA = datA.map(a => (a, 0L)).foldLeft((0L, 0L)){ judgement(_, _) }
val negA = datA.tail.map(a => (a, 0L)).foldLeft(
if(datA.head > 0) -1L else 1L, math.abs(datA.head)
){ judgement(_, _) }
val ans = math.min(posA._2, negA._2)
println(ans)
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int count = 0;
int sum = 0;
vector<int> a(n);
for (int i = 0; i < (int)(n); i++) cin >> a.at(i);
int Ah = 0;
int Bh = 0;
int sumA = 0;
int sumB = 0;
if (a.at(0) == 0) {
if (a.at(1) > 0)
a.at(0) = -1;
else
a.at(0) = 1;
count++;
}
for (int i = 0; i < n - 1; i++) {
sumA += a.at(i);
Ah = 0;
Bh = 0;
for (;;) {
sumB = sumA + a.at(i + 1);
if (sumA > 0)
Ah = 1;
else
Ah = -1;
if (sumB > 0)
Bh = 1;
else if (sumB < 0)
Bh = -1;
else
Bh = 0;
if ((Ah == 1 && Bh == -1) || (Ah == -1 && Bh == 1))
break;
else if (Ah == 1 && Bh != -1)
a.at(i + 1) -= 1;
else if (Ah == -1 && Bh != 1)
a.at(i + 1) += 1;
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() {
int n;
cin >> n;
vector<int> a(n);
long long sum1 = 0;
long long sum2 = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int ans1 = 0;
int ans2 = 0;
for (int i = 0; i < n; i++) {
sum1 += a[i];
if (i % 2 == 0 && sum1 <= 0) {
ans1 += (1 - sum1);
sum1 += (1 - sum1);
if (sum1 == 0) {
sum1++;
ans1++;
}
} else if (i % 2 == 1 && sum1 >= 0) {
ans1 += (sum1 + 1);
sum1 -= (1 + sum1);
if (sum1 == 0) {
sum1--;
ans1++;
}
}
}
for (int i = 0; i < n; i++) {
sum2 += a[i];
if (i % 2 == 0 && sum2 >= 0) {
ans2 += (sum2 + 1);
sum2 -= (1 + sum2);
if (sum2 == 0) {
sum2--;
ans2++;
}
} else if (i % 2 == 1 && sum2 <= 0) {
ans2 += (1 - sum2);
sum2 += (1 - sum2);
if (sum2 == 0) {
sum2++;
ans2++;
}
}
}
if (sum2 == 0) ans2++;
if (ans1 >= ans2)
cout << ans2 << endl;
else
cout << ans1 << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AtCoder
{
class Code3
{
static void Main(string[] args)
{
string s1 = Console.ReadLine();
string s2 = Console.ReadLine();
Console.WriteLine(funcMain(s1,s2));
}
static private string funcMain(string arg1, string arg2)
{
long ret = 0;
long sum = 0;
foreach (string buf in arg2.Split())
{
if (sum == 0)
sum = long.Parse(buf);
else
{
if (sum > 0)
{
sum += long.Parse(buf);
if (sum >= 0)
{
ret += sum + 1;
sum = -1;
}
}
else
{
sum += long.Parse(buf);
if (sum <= 0)
{
ret += (sum * -1) + 1; // 絶対値の関数探すのがめんどくさかった
sum = 1;
}
}
}
}
return ret.ToString();
}
static private void test()
{
string arg1, arg2;
arg1 = "4";
arg2 = "1 -3 1 0";
Console.WriteLine("4" == funcMain(arg1, arg2));
arg1 = "5";
arg2 = "3 -6 4 -5 7";
Console.WriteLine("0" == funcMain(arg1, arg2));
arg1 = "6";
arg2 = "-1 4 3 2 -5 4";
Console.WriteLine("8" == funcMain(arg1, arg2));
Console.ReadKey();
}
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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 int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
long int sum=arr[0];
long int ans=0;
if(sum==0)
{ if(arr[i]>=0)
{ ans++;
sum=-1;
}
else
{ ans++;
sum=1;
}
}
for(int i=1;i<n;i++)
{ if(sum<0)
{
sum=sum+arr[i];
if(sum>0)
continue;
else
{ ans+=abs(sum)+1;
sum=1;
}
}
else
{
sum+=arr[i];
if(sum<0)
continue;
else
{ ans+=sum+1;
sum=-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 | java | import java.util.*;
public class Main{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[] a = new int[n];
for(int i=0; i<n; i++){
a[i] = scan.nextInt();
}
int count = 0;
int sum = 0;
for(int i=0; i<n-1; i++){
sum = sum+a[i];
if(sum>0){
if(sum+a[i+1]<0){continue;}
int p = -1-a[i+1]-sum;
count = count + Math.abs(p);
a[i+1] = a[i+1] + p;
}
else if(sum<0){
if(sum+a[i+1]>0){continue;}
int b = 1-a[i+1]-sum;
count = count + Math.abs(b);
a[i+1] = a[i+1] + b;
}
}
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;
long long mod = 1000000007;
int main() {
int n;
cin >> n;
long long cnt = 0;
long long sum = 0;
for (int i = 0; i < n; ++i) {
long long t;
cin >> t;
if (i == 0) {
sum += t;
} else {
long long tsum = sum + t;
long long sign = (sum > 0) ? 1 : -1;
if (tsum * sum > 0) {
cnt += abs(tsum) + 1;
sum = -1 * sign;
} else {
sum = tsum;
if (sum == 0) {
sum += -1 * sign;
cnt += 1;
}
}
}
}
cout << cnt << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int zerocount(vector<int>& a, int i, int n) {
cout << "strart" << endl;
if (a.at(i) != 0) {
return 0;
}
if (i == n - 1) {
a.at(i) = 1;
return 1;
}
int count = 0;
count += zerocount(a, i + 1, n);
if (a.at(i + 1) > 0) {
a.at(i) = -1;
} else {
a.at(i) = 1;
}
count++;
return count;
}
int main() {
int n;
cin >> n;
vector<int> a(n);
for (auto& x : a) {
cin >> x;
}
bool hugou;
int sum = 0;
int count = 0;
if (a.at(0) < 0) {
hugou = false;
sum += a.at(0);
}
if (a.at(0) > 0) {
hugou = true;
sum += a.at(0);
}
if (a.at(0) == 0) {
count += zerocount(a, 0, n);
}
for (int i = 0; i < n - 1; i++) {
int i_sum;
i_sum = sum + a.at(i + 1);
if (i_sum >= 0) {
if (hugou) {
sum = -1;
count += (i_sum + 1);
hugou = false;
} else {
hugou = true;
sum = i_sum;
}
}
if (i_sum <= 0) {
if (!hugou) {
sum = 1;
count += (-1) * (i_sum - 1);
hugou = true;
} else {
hugou = false;
sum = i_sum;
}
}
}
if (sum == 0) count++;
cout << count << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <math.h>
#include <limits.h>
#include <map>
#include <algorithm>
#include <functional>
using namespace std;
int main() {
int n;
vector<long long> S;
vector<long long> A;
bool is_plus;
int ans;
int ans1 = 0;
int ans2 = 0;
long long sum = 0;
cin >> n;
for ( int i = 0; i < n; i++ ) {
long long a;
cin >> a;
A.push_back(a);
}
for ( int i = 0; i < n; i++ ) {
if ( !i ) {
if ( A[i] == 0 ) {
}
else if ( A[i] < 0 ) {#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <math.h>
#include <limits.h>
#include <map>
#include <algorithm>
#include <functional>
using namespace std;
int main() {
int n;
vector<long long> S;
vector<long long> A;
int j;
bool is_plus;
long long ans = 0;
long long sum = 0;
cin >> n;
S.push_back(0);
for ( int i = 0; i < n; i++ ) {
long long a;
cin >> a;
A.push_back(a);
}
for ( j = 0; j < n; j++ ) {
if ( abs(A[j]) ) { break; }
}
if ( j == n ) {
cout << A.size()*2-1 << endl;
return 0;
}
if ( j ) {
ans += ( j+1 )*2 - 1;
sum = ( A[j] > 0 ) ? -1: 1;
}
else {
sum = 0;
ans = 0;
}
for ( int i = j; i < n; i++ ) {
if ( !i ) {
sum = A[i];
continue;
}
bool is_plus = sum > 0;
sum += A[i];
if ( sum == 0 ) {
ans += 1;
sum = is_plus ? -1 : 1;
}
else if ( is_plus == (sum > 0) ) {
ans += abs(sum)+1;
sum = is_plus ? -1 : 1;
}
}
cout << ans << endl;
return 0;
}
ans1 += abs(A[i]) + 1;
}
}
else {
if ( i%2 && A[i] > 0 )
}
}
for ( int i = 0; i < n; i++ ) {
}
cout << ans << endl;
return 0;
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int MOD = 1000000007;
const int mod = 1000000007;
const int INF = 1000000000;
const long long LINF = 1e18;
const int MAX = 510000;
int code(int n) {
if (n < 0)
return 1;
else if (n > 0)
return 0;
else
return 2;
}
int main() {
int n;
long long int sum = 0;
long long int ans = 0;
long long int ans2 = 0;
cin >> n;
vector<long long int> a(n);
for (int i = 0; i < n; i++) {
cin >> a.at(i);
}
sum = a.at(0);
if (sum != 0) {
for (int i = 1; i < n; i++) {
if (sum + a.at(i) == 0) {
ans++;
if (sum > 0)
sum = -1;
else if (sum < 0)
sum = 1;
} else if (code(sum + a.at(i)) == code(sum)) {
ans += abs(sum + a.at(i)) + 1;
if (sum > 0)
sum = -1;
else if (sum < 0)
sum = 1;
} else {
sum = a.at(i) + sum;
}
}
cout << ans << endl;
return 0;
} else if (sum == 0) {
sum = -1;
ans = 1;
for (int i = 1; i < n; i++) {
if (sum + a.at(i) == 0) {
ans++;
if (sum > 0)
sum = -1;
else if (sum < 0)
sum = 1;
} else if (code(sum + a.at(i)) == code(sum)) {
ans += abs(sum + a.at(i)) + 1;
if (sum > 0)
sum = -1;
else if (sum < 0)
sum = 1;
} else {
sum = a.at(i) + sum;
}
}
sum = 1;
ans2 = 1;
for (int i = 1; i < n; i++) {
if (sum + a.at(i) == 0) {
ans2++;
if (sum > 0)
sum = -1;
else if (sum < 0)
sum = 1;
} else if (code(sum + a.at(i)) == code(sum)) {
ans2 += abs(sum + a.at(i)) + 1;
if (sum > 0)
sum = -1;
else if (sum < 0)
sum = 1;
} else {
sum = a.at(i) + sum;
}
}
}
cout << min(ans, 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;
static uint64_t calc_count(vector<long long> &vec, int64_t sign) {
unsigned long long count = 0;
long long total = 0;
for (uint64_t i = 1; i < vec.size(); i++) {
total += vec[i];
if ((total == 0) || ((sign * total) < 0)) {
count += abs(sign - total);
total = sign;
}
sign *= -1;
}
return count;
}
int32_t main() {
uint64_t N;
cin >> N;
vector<long long> vec;
for (uint64_t i = 0; i < N; i++) {
long long val;
cin >> val;
vec.push_back(val);
}
cout << min(calc_count(vec, 1), calc_count(vec, -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 main() {
int N;
cin >> N;
int a[N];
for (int i = 0; i < N; i++) cin >> a[i];
int cnt = 0;
int sum = a[0];
for (int i = 1; i < N; i++) {
int next = a[i] + sum;
if (0 < sum) {
while (0 <= next) {
next--;
cnt++;
}
} else {
while (next <= 0) {
next++;
cnt++;
}
}
sum = next;
}
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 | UNKNOWN | #include <bits/stdc++.h>
int main() {
int n;
scanf("%d", &n);
long long a[n];
int i;
for (i = 0; i < n; i++) scanf("%lld", &a[i]);
long long sum[n];
int j;
sum[0] = a[0];
for (i = 0; i < n - 1; i++) {
sum[i + 1] = sum[i] + a[i + 1];
}
long long ans = 0;
for (i = 1; i < n; i++) {
if (sum[i] >= 0 && sum[i - 1] > 0) {
ans += (sum[i] + 1);
a[i] = -1 - sum[i - 1];
for (j = i + 1; j < n; j++) sum[j] -= (sum[i] + 1);
sum[i] = -1;
} else if (sum[i] <= 0 && sum[i - 1] < 0) {
ans += (-sum[i] + 1);
a[i] = (-sum[i - 1] + 1);
for (j = i + 1; j < n; j++) sum[j] += (-sum[i] + 1);
sum[i] = 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"
#define readInt(x) scanf_s("%d", &x)
#define readStr(x) scanf("%s", x)
#define rep(i, n) for(int i=0; i<(n); ++i)
#define chmax(x, y) (x > (y) ? x : x = (y))
#define chmin(x, y) (x < (y) ? x : x = (y))
#define write(x) cout << (x) << endl
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int INF = INT32_MAX / 2;
const int MAX = 1e5 + 10;
int n;
int a[MAX];
int main()
{
readInt(n);
int sump = 0, sumn = 0;
ll cntp = 0, cntn = 0;
rep(i, n) {
cin >> a[i];
sump += a[i];
sumn += a[i];
if (i % 2) {
int r = max(sump + 1, 0);
sump -= r;
cntp += r;
r = max(1 - sumn, 0);
sumn += r;
cntn += r;
}
else {
int r = max(1 - sump, 0);
sump += r;
cntp += r;
r = max(sumn + 1, 0);
sumn -= r;
cntn += r;
}
}
write(min(cntp, cntn));
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long a[110000];
for (int i = 0; i < n; i++) cin >> a[i];
long long b[110000];
long long sum = 0;
if (a[0] == 0) {
if (a[1] >= 0) {
sum++;
a[0] = -1;
} else {
sum++;
a[0] = 1;
}
}
b[0] = a[0];
for (int i = 1; i < n; i++) {
b[i] = b[i - 1] + a[i];
if (b[i - 1] > 0) {
if (b[i] < 0)
continue;
else {
sum += b[i] + 1;
b[i] = -1;
}
} else {
if (b[i] > 0)
continue;
else {
sum += -b[i] + 1;
b[i] = 1;
}
}
}
cout << sum << 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;
vector<long long> A;
long long ans1 = 0;
long long ans2 = 0;
long long sum = 0;
bool is_plus;
cin >> n;
for (int i = 0; i < n; i++) {
long long a;
cin >> a;
A.push_back(a);
}
is_plus = true;
for (int i = 0; i < n; i++) {
if (i) {
is_plus = sum > 0;
}
sum += A[i];
if (sum == 0) {
ans1++;
sum = is_plus ? -1 : 1;
} else if (is_plus == (sum > 0)) {
ans1 += abs(sum) + 1;
sum = is_plus ? -1 : 1;
}
}
is_plus = false;
for (int i = 0; i < n; i++) {
if (i) {
is_plus = sum > 0;
}
sum += A[i];
if (sum == 0) {
ans2++;
sum = is_plus ? -1 : 1;
} else if (is_plus == (sum > 0)) {
ans2 += abs(sum) + 1;
sum = is_plus ? -1 : 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 long long mod = 1e9 + 7;
int main() {
int n;
cin >> n;
vector<long long> a(n + 1);
vector<long long> tot(n + 1, 0);
for (int i = 1; i <= (int)(n); i++) {
cin >> a.at(i);
}
long long count = 0;
for (int i = 1; i <= (int)(n); i++) {
if (i == 1) {
tot.at(1) = a.at(1);
continue;
}
if (tot.at(i - 1) > 0) {
long long sum = tot.at(i - 1) + a.at(i);
if (sum >= 0) {
tot.at(i) = -1;
count += sum + 1;
} else
tot.at(i) = sum;
}
if (tot.at(i - 1) < 0) {
long long big = tot.at(i - 1) + a.at(i);
if (big <= 0) {
tot.at(i) = 1;
count = count + (1 - big);
} else
tot.at(i) = big;
}
}
cout << count << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int sign(long long N) {
if (N < 0) return -1;
if (N > 0) return 1;
if (N = 0) return 0;
}
int main() {
long long n;
cin >> n;
long long N = n;
vector<long long> a(n);
for (int(i) = (0); (i) < (int)(n); (i)++) cin >> a.at(i);
vector<long long> S(n);
S.at(0) = a.at(0);
vector<long long> S2(n);
S2.at(0) = a.at(0);
for (int i = 1; i < N; i++) S.at(i) = a.at(i) + S.at(i - 1);
for (int i = 0; i < N; i++) S2.at(i) = S.at(i);
long long res1, res2;
if (S.at(0) == 0) {
res1 = 1;
res2 = 1;
for (int(i) = (0); (i) < (int)(n); (i)++) S.at(i)++;
for (int(i) = (0); (i) < (int)(n); (i)++) S2.at(i)--;
} else {
res1 = 0;
res2 = abs(S2.at(0)) + 1;
if (S2.at(0) > 0)
for (int(i) = (0); (i) < (int)(n); (i)++) S2.at(i) -= abs(S2.at(0)) + 1;
if (S2.at(0) < 0)
for (int(i) = (0); (i) < (int)(n); (i)++) S2.at(i) += abs(S2.at(0)) + 1;
}
int tempo;
for (int i = 1; i < N; i++) {
if (S.at(i) * S.at(0) * pow(-1, i) <= 0) {
tempo = abs(S.at(i)) + 1;
res1 += tempo;
for (int j = i; j < N; j++) S.at(j) += tempo * sign(S.at(0)) * pow(-1, i);
}
if (S2.at(i) * S2.at(0) * pow(-1, i) <= 0) {
tempo = abs(S2.at(i)) + 1;
res2 += tempo;
for (int j = i; j < N; j++)
S2.at(j) += tempo * sign(S2.at(0)) * pow(-1, i);
}
}
if (res1 > res2) {
cout << res2;
} else {
cout << res1;
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int MOD = 1000000007;
const int mod = 1000000007;
const int INF = 1000000000;
const long long LINF = 1e18;
const int MAX = 510000;
bool code(long long int n) {
if (n < 0)
return 1;
else if (n > 0)
return 0;
}
int main() {
int n;
long long int sum = 0;
long long int ans = 0;
long long int ans2 = 0;
cin >> n;
vector<long long int> a(n);
for (int i = 0; i < n; i++) {
cin >> a.at(i);
}
if (a.at(0) != 0) {
sum = a.at(0);
for (int i = 1; i < n; i++) {
if (sum + a.at(i) == 0) {
ans++;
if (sum > 0)
sum = -1;
else if (sum < 0)
sum = 1;
} else if (code(sum + a.at(i)) == code(sum)) {
ans += abs(sum + a.at(i)) + 1;
if (sum > 0)
sum = -1;
else if (sum < 0)
sum = 1;
} else {
sum = a.at(i) + sum;
}
}
cout << ans << endl;
return 0;
} else if (a.at(0) == 0) {
sum = -1;
ans = 1;
for (int i = 1; i < n; i++) {
if (sum + a.at(i) == 0) {
ans++;
if (sum > 0)
sum = -1;
else if (sum < 0)
sum = 1;
} else if (code(sum + a.at(i)) == code(sum)) {
ans += abs(sum + a.at(i)) + 1;
if (sum > 0)
sum = -1;
else if (sum < 0)
sum = 1;
} else {
sum = a.at(i) + sum;
}
}
sum = 1;
ans2 = 1;
for (int i = 1; i < n; i++) {
if (sum + a.at(i) == 0) {
ans2++;
if (sum > 0)
sum = -1;
else if (sum < 0)
sum = 1;
} else if (code(sum + a.at(i)) == code(sum)) {
ans2 += abs(sum + a.at(i)) + 1;
if (sum > 0)
sum = -1;
else if (sum < 0)
sum = 1;
} else {
sum = a.at(i) + sum;
}
}
if (ans > ans2)
cout << ans2 << endl;
else {
cout << ans << endl;
}
}
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | 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];
if (data[0] > 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++;
}
}
}
} else {
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++;
}
}
}
}
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() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
int min_ans = INT_MAX;
for (int mod = 0; mod < 2; ++mod) {
int ans = 0;
int sum = 0;
for (int i = 0; i < n; ++i) {
int sign = ((i % 2) == mod) * -2 + 1;
sum += a[i];
if (sign * sum <= 0) {
int diff = sign - sum;
sum = sign;
ans += abs(diff);
}
}
min_ans = min(min_ans, ans);
}
cout << min_ans << "\n";
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = [int(i) for i in input().split()]
num,answer = a[0],0
if num==0:
num = -a[1]//abs(a[1])
answer = 1
for i in range(1,n):
if num*(num+a[i]) >= 0:
x = (-num)//abs(num)
answer += abs((x-num)-a[i])
num = x
else:
num += a[i]
print(answer) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int calc(vector<int>& t, bool topPlus) {
int parityA, parityB;
if (topPlus) {
parityA = 0;
parityB = 1;
} else {
parityA = 1;
parityB = 0;
}
int sum = 0;
int cnt = 0;
for (int i = 0; i < t.size(); ++i) {
sum += t.at(i);
if (i % 2 == parityA && sum <= 0) {
cnt += (1 - sum);
sum = 1;
} else if (i % 2 == parityB && sum >= 0) {
cnt += (1 + sum);
sum = -1;
}
}
return cnt;
}
int main() {
int N;
cin >> N;
vector<int> t(N);
for (long i = 0; i < N; ++i) {
cin >> t.at(i);
}
int cnt1 = calc(t, true);
int cnt2 = calc(t, false);
int cnt = min(cnt1, cnt2);
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 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;
}
}
}
cum = 0;
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;
cout << ans1 << endl;
cout << 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;
int d[n];
for(int i=0;i<n;i++) {
cin >> d[i];
}
int count=0;
int sum=d[0];
int f =0;
if(d[0]>0){
f=-1;
}
if(d[0]<0){
f=1;
}
for(int i=1;i<n;i++){
sum+=d[i];
if(sum==0){
if(f==1){
count++;
f=-1;
sum=1;
continue;
}
if(f==-1){
count++;
f=1;
sum=-1;
continue;
}
}
if(sum>0){
if(f==1){
f=-1;
continue;
}
if(f==-1){
count+=sum+1;
sum=-1;
f=1;
continue;
}
}
if(sum<0){
if(f==-1){
f=1;
continue;
}
if(f==1){
count+=1-sum;
sum=1;
f=-1;
continue;
}
}
}
int ccount=0;
int ssum=;
int ff =0;
if(d[0]>0){
ff=1;
ccount=-1-d[0];
ssum=-1;
}
if(d[0]<0){
ff=-1;
ccount=1-d[0];
ssum=1;
}
for(int i=1;i<n;i++){
sum+=d[i];
if(ssum==0){
if(ff==1){
ccount++;
ff=-1;
ssum=1;
continue;
}
if(ff==-1){
ccount++;
ff=1;
ssum=-1;
continue;
}
}
if(ssum>0){
if(f==1){
ff=-1;
continue;
}
if(ff==-1){
ccount+=sum+1;
ssum=-1;
ff=1;
continue;
}
}
if(ssum<0){
if(ff==-1){
ff=1;
continue;
}
if(ff==1){
ccount+=1-sum;
ssum=1;
ff=-1;
continue;
}
}
}
cout << min(count,ccount) << 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 INF = 1e9 + 7;
int main() {
int n;
cin >> n;
vector<long long> a(n + 1), sum(n + 1, 0);
for (int i = 1; i <= n; i++) {
cin >> a[i];
sum[i] = sum[i - 1] + a[i];
}
long long ans = 0;
for (int i = 1; i < n; i++) {
if (sum[i] * sum[i + 1] >= 0) {
ans += abs(sum[i + 1]) + 1;
if (sum[i] > 0)
sum[i + 1] = -1;
else
sum[i + 1] = 1;
}
if (i < n - 1) sum[i + 2] = sum[i + 1] + a[i + 2];
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
long long ans = 0;
if (a[0] == 0) {
if (a[1] >= a[0])
a[0] = -1;
else
a[0] = 1;
ans++;
}
for (int i = 1; i <= n - 1; i++) {
a[i] += a[i - 1];
if (a[i] == 0) {
if (a[i - 1] < 0) a[i] = 1;
if (a[i - 1] > 0) a[i] = -1;
ans++;
} else if (a[i] > 0 && a[i - 1] > 0) {
ans += a[i] + 1;
a[i] = -1;
} else if (a[i] < 0 && a[i - 1] < 0) {
ans += 1 - a[i];
a[i] = 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 | java | import java.util.Scanner;
class Main {
public static void main(String[] args) {
new Main().compute();
}
void compute() {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int sum = sc.nextInt();
int ans = 0;
for (int i = 0; i < N - 1; i++) {
int cur = sc.nextInt();
if (Math.signum(sum) == Math.signum(sum + cur) || Math.signum(sum + cur) == 0) {
int tmp = -(int) Math.signum(sum);
ans += Math.abs(tmp - sum - cur);
sum = tmp;
} else {
sum += cur;
}
}
System.out.println(ans);
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N, a[100000];
cin >> N;
for (int i = 0; i < N; ++i) cin >> a[i];
long long counter = 0;
long long sum = 0;
if (a[0] >= 0) {
for (int i = 0; i < N; ++i) {
sum += a[i];
if (i % 2 == 0) {
while (sum <= 0) {
++sum;
++counter;
}
} else {
while (sum >= 0) {
--sum;
++counter;
}
}
}
} else {
for (int i = 0; i < N; ++i) {
sum += a[i];
if (i % 2 == 0) {
while (sum >= 0) {
--sum;
++counter;
}
} else {
while (sum <= 0) {
++sum;
++counter;
}
}
}
}
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 posi(long long x) {
if (x > 0) return 1;
if (x < 0) return -1;
return 0;
}
int main() {
int N;
cin >> N;
vector<long long> a(N);
for (auto &i : a) cin >> i;
long long ans = 0, tmp = 0;
long long sum = a[0];
for (int i = 1; i < N; i++) {
if (posi(sum + a[i]) * posi(sum) != -1 || sum + a[i] == 0) {
tmp += abs(sum + a[i]) + 1;
sum = (sum > 0) ? -1 : 1;
} else
sum += a[i];
}
ans = tmp;
tmp = abs(a[0]) + 1;
sum = (a[0] > 0) ? -1 : 1;
for (int i = 1; i < N; i++) {
if (posi(sum + a[i]) * posi(sum) != -1 || sum + a[i] == 0) {
tmp += abs(sum + a[i]) + 1;
sum = (sum > 0) ? -1 : 1;
} else
sum += a[i];
}
ans = min(ans, tmp);
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int,input().split()))
a_1 = a
ans = 0
ans_2 = 0
o = 0
for i in range(n):
if i == 0:
if a[i] == 0:
f = "+"
a[i] = 1
elif a[0] > 0:
f = "+"
elif a[0] < 0:
f = "-"
else:
o += a[i-1]
if f == "+":
if a[i] + o > 0:
c = -1 - o
ans += abs(c - a[i])
a[i] = c
f = "-"
else:
if a[i] + o == 0:
a[i] -= 1
ans += 1
f = "-"
elif f == "-":
if a[i] + o < 0:
c = 1 - o
ans += abs(c - a[i])
a[i] = c
f = "+"
else:
if a[i] + o == 0:
a[i] += 1
ans += 1
f = "+"
o = 0
a = a_1
for i in range(n):
if i == 0:
if a[i] == 0:
f = "+"
a[i] = 1
elif a[0] > 0:
f = "-"
c = -1 - a[0]
ans_2 += abs(c - a[0])
a[i] = c
elif a[0] < 0:
c = 1 - a[0]
ans_2 += abs(c - a[0])
a[i] = c
f = "+"
else:
o += a[i-1]
if f == "+":
if a[i] + o > 0:
c = -1 - o
ans_2 += abs(c - a[i])
a[i] = c
f = "-"
else:
if a[i] + o == 0:
a[i] -= 1
ans += 1
f = "-"
elif f == "-":
if a[i] + o < 0:
c = 1 - o
ans_2 += abs(c - a[i])
a[i] = c
f = "+"
else:
if a[i] + o == 0:
a[i] += 1
ans += 1
f = "+"
#print(a)
print(min(ans,ans_2))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n=int(input())
l=list(map(int,input().split()))
s=[l[0]]
for i in range(1,n):
s.append(s[i-1]+l[i])
x,y=0,0
for i in range(n):
if s[i]*((-1)**i)<0:
x+=abs(s[i]-(-1)**i)
s[i]=(-1)**i
if i+1<=n-1:
s[i+1]=s[i]+l[i+1]
for i in range(n):
if s[i]*((-1)**(i+1))<0:
y+=abs(s[i]-(-1)**i)
s[i]=(-1)**(i+1)
if i+1<=n-1:
s[i+1]=s[i]+l[i+1]
print(min(x,y)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int solve(vector<int> vec) {
long long int n = vec.size();
long long int sum = vec[0];
int ans = 0;
for (long long int i = 1; i < n; i++) {
if (sum > 0) {
if (sum + vec[i] >= 0) {
ans += 1 + (sum + vec[i]);
sum = -1;
} else {
sum += vec[i];
}
} else if (sum < 0) {
if (sum + vec[i] <= 0) {
ans += 1 - (sum + vec[i]);
sum = 1;
} else {
sum += vec[i];
}
}
}
return ans;
}
int main() {
int n, Ans;
cin >> n;
vector<int> as;
for (int i = 0; i < n; i++) {
int t;
cin >> t;
as.push_back(t);
}
vector<int> as1, as2;
copy(as.begin(), as.end(), back_inserter(as1));
copy(as.begin(), as.end(), back_inserter(as2));
as1[0] = 1;
as2[0] = -1;
Ans = min(solve(as),
min(solve(as1) + abs(1 - as[0]), solve(as2) + abs(-1 - as[0])));
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 | #!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')
#A
def A():
a = input().split()
a = list(map(lambda x: x.capitalize(), a))
a,b,c = a
print(a[0]+b[0]+c[0])
return
#B
def B():
a = II()
b = II()
if a > b:
print("GREATER")
if a < b:
print("LESS")
if a == b:
print("EQUAL")
return
#C
def C():
II()
a = LI()
def f(suma, b):
for i in a[1:]:
if suma * (suma + i) < 0:
suma += i
continue
b += abs(suma + i) + 1
suma = -1 * (suma < 0) or 1
return b
if a[0] == 0:
ans = min(f(1, 1), f(-1, 1))
else:
ans = min(f(a[0], 0), f(-a[0], 2 * abs(a[0])))
print(ans)
return
#D
def D():
s = S()
for i in range(len(s) - 1):
if s[i] == s[i+1]:
print(i + 1, i + 2)
return
for i in range(len(s) - 2):
if s[i] == s[i + 2]:
print(i + 1, i + 3)
return
print(-1, -1)
return
#Solve
if __name__ == '__main__':
C()
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = [int(i) for i in input().split()]
cur = 0
ans1, ans2 = 0,0
for i, x in enumerate(a):
cur += x
if i%2 == 0:
if cur >= 1:
continue
else:
ans1 += abs(1-cur)
cur = 1
else:
if cur <= -1:
continue
else:
ans1 += abs(-1-cur)
cur = -1
for i, x in enumerate(a):
cur += x
if i%2 != 0:
if cur >= 1:
continue
else:
ans2 += abs(1-cur)
cur = 1
else:
if cur <= -1:
continue
else:
ans2 += abs(-1-cur)
cur = -1
print(min(ans1, ans2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<long> vec(N);
vector<long> svec(N);
cin >> vec[0];
svec[0] = vec[0];
for (int i = 1; i < N; ++i) {
cin >> vec[i];
svec[i] = svec[i - 1] + vec[i];
}
long ans = 0;
long tmp = 0;
bool tem = false;
if (vec[0] < 0) tem = true;
for (int i = 1; i < N; ++i) {
int to = 0;
if (tem) {
if (svec[i] + tmp <= 0) {
to = abs(svec[i] + tmp - 1);
ans += to;
tmp += to;
vec[i] += to;
}
tem = false;
} else {
if (svec[i] + tmp >= 0) {
to = abs(svec[i] + tmp + 1);
ans += to;
tmp -= to;
vec[i] -= to;
}
tem = true;
}
}
if (accumulate(vec.begin(), vec.end(), 0) == 0) {
ans++;
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import sys
input = sys.stdin.readline
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def main():
mod=10**9+7
N=I()
a=LI()
ans=0
pos=-1
S=a[0]
if S==0:
S=1
if S>0:
pos=1
for i in range(N-1):
S+=a[i+1]
if S>0:
Sp=1
elif S<0:
Sp=-1
else:
Sp=0
if Sp!=pos*(-1):
target=-1*pos
ans+=abs(target-S)
S=target
pos*=-1
if a[0]==0:
S=-1
for i in range(N-1):
S+=a[i+1]
if S>0:
Sp=1
elif S<0:
Sp=-1
else:
Sp=0
if Sp!=pos*(-1):
target=-1*pos
ans+=abs(target-S)
S=target
pos*=-1
print(ans)
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 | UNKNOWN | step_sum::[Int] -> [Int]
step_sum [] = []
step_sum (x:xs) = x:(map (x+) (step_sum xs) )
--今のカウント手数、前の値、食べるリスト
check::Int -> Int -> [Int] -> Int
check st _ [] = st
check st pre xs = case ((pre > 0),((head xs) > 0)) of
(True,True) -> let dec = (head xs)+1 in check (dec+st) (-1) (map (\x-> x-dec) (tail xs))
(True,False) -> check st (head xs) (tail xs)
(False,True) -> check st (head xs) (tail xs)
(False,False)-> let inc = 1-(head xs) in check (inc+st) 1 (map (\x-> x+inc) (tail xs))
solver::[Int]->Int
solver xs = let steps = step_sum xs in
check 0 (head steps) (tail steps)
main::IO()
main=do
_<-getLine
datc<-getLine
print (solver (map read (words datc))) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
import static java.util.Comparator.*;
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
MyInput in = new MyInput(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Solver solver = new Solver();
solver.solve(1, in, out);
out.close();
}
// ======================================================================
static class Solver
{
long[] A = null;
public void solve(int testNumber, MyInput in, PrintWriter out) {
int N = in.nextInt();
A = new long[N];
long a;
long ans = 0;
boolean mflag = false;
for(int i=0; i < N; i++) {
a = in.nextLong();
if(i == 0) {
A[i] = a;
if(a > 0) mflag = true;
else mflag = false;
continue;
}
A[i] = A[i-1] + a;
if(mflag) {
if(A[i] >= 0) {
ans += Math.abs(A[i]) + 1;
A[i] = -1;
}
mflag = false;
} else {
if(A[i] <= 0) {
ans += Math.abs(A[i]) + 1;
A[i] = 1;
}
mflag = true;
}
}
out.println(ans);
}
}
// ======================================================================
static class Pair<K, V> extends AbstractMap.SimpleEntry<K, V> {
/** serialVersionUID. */
private static final long serialVersionUID = 6411527075103472113L;
public Pair(final K key, final V value) {
super(key, value);
}
public String getString() {
return "[" + getKey() + "] [" + getValue() + "]";
}
}
static class MyInput {
private final BufferedReader in;
private static int pos;
private static int readLen;
private static final char[] buffer = new char[1024 * 8];
private static char[] str = new char[500 * 8 * 2];
private static boolean[] isDigit = new boolean[256];
private static boolean[] isSpace = new boolean[256];
private static boolean[] isLineSep = new boolean[256];
static {
for (int i = 0; i < 10; i++) {
isDigit['0' + i] = true;
}
isDigit['-'] = true;
isSpace[' '] = isSpace['\r'] = isSpace['\n'] = isSpace['\t'] = true;
isLineSep['\r'] = isLineSep['\n'] = true;
}
public MyInput(InputStream is) {
in = new BufferedReader(new InputStreamReader(is));
}
public int read() {
if (pos >= readLen) {
pos = 0;
try {
readLen = in.read(buffer);
} catch (IOException e) {
throw new RuntimeException();
}
if (readLen <= 0) {
throw new MyInput.EndOfFileRuntimeException();
}
}
return buffer[pos++];
}
public int nextInt() {
int len = 0;
str[len++] = nextChar();
len = reads(len, isSpace);
int i = 0;
int ret = 0;
if (str[0] == '-') {
i = 1;
}
for (; i < len; i++) ret = ret * 10 + str[i] - '0';
if (str[0] == '-') {
ret = -ret;
}
return ret;
}
public long nextLong() {
int len = 0;
str[len++] = nextChar();
len = reads(len, isSpace);
int i = 0;
long ret = 0L;
if (str[0] == '-') {
i = 1;
}
for (; i < len; i++) ret = ret * 10 + str[i] - '0';
if (str[0] == '-') {
ret = -ret;
}
return ret;
}
public String nextString() {
String ret = new String(nextDChar()).trim();
return ret;
}
public char[] nextDChar() {
int len = 0;
len = reads(len, isSpace);
char[] ret = new char[len + 1];
for (int i=0; i < len; i++) ret[i] = str[i];
ret[len] = 0x00;
return ret;
}
public char nextChar() {
while (true) {
final int c = read();
if (!isSpace[c]) {
return (char) c;
}
}
}
int reads(int len, boolean[] accept) {
try {
while (true) {
final int c = read();
if (accept[c]) {
break;
}
if (str.length == len) {
char[] rep = new char[str.length * 3 / 2];
System.arraycopy(str, 0, rep, 0, str.length);
str = rep;
}
str[len++] = (char) c;
}
} catch (MyInput.EndOfFileRuntimeException e) {
}
return len;
}
static class EndOfFileRuntimeException extends RuntimeException {
}
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
namespace ABC059_C
{
class Program
{
static void Main(string[] args)
{
var n = int.Parse(Console.ReadLine());
var a = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);
var ans1 = 0;
var ans2 = 0;
var sum1 = 0;
var sum2 = 0;
for (var i=0;i<n;i++)
{
sum1 += a[i];
if(i%2 == 0)
{
if(sum1<=0)
{
ans1 += Math.Abs(sum1) + 1;
sum1 = 1;
}
}
else
{
if(sum1>=0)
{
ans1 += sum1 + 1;
sum1 = -1;
}
}
}
for(var i=0;i<n;i++)
{
sum2 += a[i];
if (i % 2 == 0)
{
if (sum2 >= 0)
{
ans2 += sum2 + 1;
sum2 = -1;
}
}
else
{
if (sum2 <= 0)
{
ans2 += Math.Abs(sum2) + 1;
sum2 = 1;
}
}
}
var ans = Math.Min(ans1, ans2);
Console.WriteLine(ans);
}
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int getS(int val) {
if (val < 0) return -1;
if (val > 0) return 1;
return 0;
}
int minNum(vector<int> &arr, int n, int sign) {
int acu, sol = 0;
for (int i = 0; i < n; i++) {
acu += arr[i];
if (getS(acu) != sign) {
sol += abs(sign - acu);
acu = sign;
}
sign = sign * -1;
}
return sol;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vector<int> arr(n);
for (int i = 0; i < n; i++) cin >> arr[i];
int a1 = minNum(arr, n, -1);
int a2 = minNum(arr, n, 1);
cout << (a1 < a2 ? a1 : a2) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
struct BIT {
int N;
vector<int64_t> bit;
BIT(vector<int64_t> s) : N(s.size()), bit(N, 0) {
for (int64_t i = (0); i < (N); ++i) add(i, s[i]);
}
void add(int a, int w) {
for (int x = a; x < N; x |= x + 1) bit[x] += w;
}
int sum(int a) {
int ret = 0;
for (int x = a - 1; x >= 0; x = (x & (x + 1)) - 1) ret += bit[x];
return ret;
}
int parsum(int a, int b) { return sum(b) - sum(a); }
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<int64_t> a(n);
for (int64_t i = (0); i < (n); ++i) cin >> a[i];
int64_t cnt = 0;
BIT b(a);
if (b.sum(1) == 0) {
b.add(0, b.sum(2) / abs(b.sum(2)) * -1);
++cnt;
}
for (int64_t i = (2); i < (n + 1); ++i) {
int64_t bsumi = b.sum(i), bsumi1 = b.sum(i - 1);
if (bsumi) {
int signi = bsumi / abs(bsumi);
if (signi == bsumi1 / abs(bsumi1)) {
b.add(i - 1, -bsumi - signi);
cnt += abs(bsumi) + 1;
}
} else {
b.add(i - 1, bsumi1 / abs(bsumi1) * -1);
++cnt;
}
}
cout << cnt << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import numpy as np
n = int(input())
list1 = (input().split())
list2 = [int(i) for i in list1]
cnt=0
for i in range(n):
#最初だけ、ゼロの場合は次を見て判定
if i <= 0 and list2[0] == 0:
cnt += 1
list2[0] = 1 if list2[1] <=0 else -1
#i項までの和がゼロ
if i > 0 and sum(list2[0:i+1]) == 0:
cnt += 1
#手前までの和が正なら1減算、負なら1加算
list2[i] -= np.sign(sum(list2[0:i]))
#i項までの和が手前の和と符号が同じ
elif i > 0 and np.sign(sum(list2[0:i+1])) == np.sign(sum(list2[0:i])):
cnt += abs(sum(list2[0:i+1]))+1
list2[i] = list2[i] + ( abs(sum(list2[0:i+1])) + 1 ) * -np.sign(sum(list2[0: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;
long chk(long a[], int N, bool t) {
long total = 0;
long ops = 0;
for (int i = 0; i < N; i++) {
total += a[i];
if (t == true && (total < 1)) {
ops += (1 - total);
total = 1;
} else if (t == false && (total > -1)) {
ops += (total + 1);
total = -1;
}
t = !t;
}
return ops;
}
int main() {
long N;
cin >> N;
long a[100001];
for (long i = 0; i < N; i++) {
cin >> a[i];
}
printf("%d\n", min(chk(a, N, true), chk(a, N, false)));
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n, i, j, ans = 0, sum = 0, flag;
cin >> n;
vector<long long> a(n);
for (i = 0; i < n; i++) {
cin >> a[i];
}
sum += a[0];
if (sum == 0) {
ans++;
for (i = 0; i < n; i++) {
if (a[i] > 0) {
if (i % 2 == 0) {
sum = 1;
} else {
sum = -1;
}
break;
} else if (a[i] < 0) {
if (i % 2 != 0) {
sum = 1;
} else {
sum = -1;
}
break;
}
}
}
for (i = 1; i < n; i++) {
if (sum > 0) {
flag = 1;
} else {
flag = 0;
}
if (flag == 1) {
sum += a[i];
if (sum >= 0) {
ans += (sum + 1);
sum = -1;
}
} else {
sum += a[i];
if (sum <= 0) {
ans += (1 - sum);
sum = 1;
}
}
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long a[100000] = {};
long long calc(long long *, long long);
int get_sign(long long);
int main() {
long long n, ans;
cin >> n;
for (long long i = 0; i < n; i++) {
cin >> a[i];
}
ans = calc(a, n);
cout << ans << endl;
return 0;
}
long long calc(long long *a, long long n) {
long long cnt = 0;
int sign = 0;
long long tmp;
if (a[0] > 0) {
sign = 1;
}
tmp = a[0];
for (long long i = 0; i < n - 1; i++) {
tmp = tmp + a[i + 1];
if (sign == 1) {
if (tmp >= 0) {
cnt += tmp + 1;
tmp = -1;
}
} else {
if (tmp <= 0) {
cnt += (-1 * tmp) + 1;
tmp = 1;
}
}
sign = get_sign(tmp);
}
return cnt;
}
int get_sign(long long tmp) {
if (tmp < 0)
return 0;
else
return 1;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Hamza Hasbi
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
C_Sequence solver = new C_Sequence();
solver.solve(1, in, out);
out.close();
}
static class C_Sequence {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.nextInt();
long[] a = new long[n + 1];
long[] pre = new long[n + 1];
Arrays.fill(a, 0);
Arrays.fill(pre, 0);
boolean f = true;
int ans = 0;
for (int i = 1; i <= n; i++) {
a[i] = in.nextLong();
pre[i] = pre[i - 1] + a[i];
if (pre[i] == 0) {
if (pre[i - 1] > 0) pre[i]--;
else pre[i]++;
ans++;
continue;
}
if (pre[i - 1] > 0 && pre[i] > 0) {
pre[i] -= a[i];
long curr = -1 * pre[i - 1] - 1;
ans += Math.abs(a[i] - curr);
pre[i] = pre[i - 1] + curr;
}
if (pre[i - 1] < 0 && pre[i] < 0) {
pre[i] -= a[i];
long curr = pre[i - 1] + 1;
ans += Math.abs(a[i] - curr);
pre[i] = pre[i - 1] + curr;
}
}
out.printLine(ans);
}
}
static class InputReader {
BufferedReader reader;
StringTokenizer st;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
st = null;
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
String line = reader.readLine();
if (line == null) {
return null;
}
st = new StringTokenizer(line);
} catch (Exception e) {
throw new RuntimeException();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void printLine(int i) {
writer.println(i);
}
public void close() {
writer.close();
}
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n=int(input())
arr1=list(map(int,input().split()))
arr2=arr1[:]
ans1=0
ans2=0
if arr1[0]<=0:
arr1[0]=1
ans1+=abs(arr1[0])+1
if arr2[0]>=0:
arr2[0]=-1
ans2+=arr2[0]+1
sum1=arr1[0]
for i in range(1,n):
tmp=sum1+arr1[i]
if i%2==1:
if tmp>=0:
ans1+=tmp+1
sum1=-1
else:
sum1=tmp
else:
if tmp<=0:
ans1+=abs(tmp)+1
sum1=1
else:
sum1=tmp
sum2=arr2[0]
for i in range(1,n):
tmp=sum2+arr2[i]
if i%2==0:
if tmp>=0:
ans2+=tmp+1
sum2=-1
else:
sum2=tmp
else:
if tmp<=0:
ans2+=abs(tmp)+1
sum2=1
else:
sum2=tmp
print(min(ans1,ans2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | n = gets.to_i
a = gets.chomp.split.map(&:to_i)
sum1 = 0
sum2 = 0
ans = 0
for i in 0..n-2
sum1 += a[i]
if sum1 == 0
if a[i+1] > 1
a[i] -= 1
sum1 -= 1
ans += 1
else
a[i] += 1
sum1 += 1
ans += 1
end
end
sum2 = sum1 + a[i+1]
if sum2 * sum1 >= 0
if sum1 < 0
if sum1 <= sum2
a[i+1] += 1 - sum2
ans += 1 - sum2
else
a[i] += -sum1 + 1
ans += -sum1 + 1
sum1 = 1
end
else
if sum1 >= sum2
a[i+1] -= sum2 + 1
ans += sum2 + 1
else
a[i] -= 1 + sum1
ans += 1 + sum1
sum1 = -1
end
end
end
end
puts a.join(" ")
puts ans |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
A = [int(i) for i in input().split()]
# a_1 > 0 の場合を試す
res, sum = 0, 0
if 0 < A[0]: sum = A[0]
else: res, sum = -A[0] + 1, 1
for i in range(1, N):
if 0 < sum:
if -1 < sum + A[i]: # a_i を足して、符号が変わらない
res += abs((sum + 1) - A[i])
sum = -1
else: # a_i を足して符号が変わる
sum += A[i]
else:
if sum + A[i] < 1:
res += abs((sum + 1) - A[i])
sum = 1
else:
sum += A[i]
ans = res
# a_1 < 0 の場合を試す
res, sum = 0, 0
if A[0] < 0: sum = A[0]
else: res, sum = A[0] + 1, -1
for i in range(1, N):
if 0 < sum:
if -1 < sum + A[i]: # a_i を足して、符号が変わらない
res += abs((sum + 1) - A[i])
sum = -1
else: # a_i を足して符号が変わる
sum += A[i]
else:
if sum + A[i] < 1:
res += abs((sum + 1) - A[i])
sum = 1
else:
sum += A[i]
ans = min(ans, res)
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T>
inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
template <class T>
inline T sqr(T x) {
return x * x;
}
int main(void) {
int n;
cin >> n;
long long a[n + 1];
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
long long S1, S2;
long long ans[2];
if (a[1] == 0) {
S1 = 1;
ans[0] = 1;
for (int i = (2); i < (n + 1); ++i) {
S2 = S1 + a[i];
if ((S1 < 0 && S2 > 0) || (S1 > 0 && S2 < 0)) {
S1 = S2;
} else {
ans[0] += llabs(S2) + 1;
if (S1 < 0)
S2 = 1;
else
S2 = -1;
S1 = S2;
}
}
S1 = -1;
ans[1] = 1;
for (int i = (2); i < (n + 1); ++i) {
S2 = S1 + a[i];
if ((S1 < 0 && S2 > 0) || (S1 > 0 && S2 < 0)) {
S1 = S2;
} else {
ans[1] += llabs(S2) + 1;
if (S1 < 0)
S2 = 1;
else
S2 = -1;
S1 = S2;
}
}
cout << min(ans[0], ans[1]) << endl;
} else {
S1 = a[1];
ans[0] = 0;
for (int i = (2); i < (n + 1); ++i) {
S2 = S1 + a[i];
if ((S1 < 0 && S2 > 0) || (S1 > 0 && S2 < 0)) {
S1 = S2;
} else {
ans[0] += llabs(S2) + 1;
if (S1 < 0)
S2 = 1;
else
S2 = -1;
S1 = S2;
}
}
cout << ans[0] << 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 A[N];
for (int i = 0; i < N; i++) cin >> A[i];
bool loop = true;
int delta = 0;
while (loop) {
int sum[N];
bool sign = (A[0] > 0);
sum[0] = A[0];
loop = false;
for (int i = 1; i < N; i++) {
sum[i] = sum[i - 1] + A[i];
sign = !sign;
if (sign && sum[i] <= 0) {
delta += abs(1 - sum[i]);
A[i] = A[i] + (1 - sum[i]);
loop = true;
break;
} else if (!sign && sum[i] >= 0) {
delta += abs(-1 - sum[i]);
A[i] = A[i] - (1 + sum[i]);
loop = true;
break;
}
}
}
cout << delta << 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.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.OutputStream;
import java.io.IOException;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Arrays;
import java.util.Collections;
import java.util.ArrayList;
import java.util.List;
import java.util.HashSet;
import java.util.Comparator;
import java.util.Set;
import java.util.HashMap;
import java.util.Map;
public class Main {
// 標準入力
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// 標準入力数値配列用 int
static int[] inputval() throws Exception {
String[] strarray = br.readLine().trim().split(" ");
int[] intarray = new int[strarray.length];
for (int i = 0; i < intarray.length; i++) {
intarray[i] = Integer.parseInt(strarray[i]);
}
return intarray;
}
/* 標準入力数値配列用 long */
static long[] inputLongArr() throws Exception {
String[] strarray = br.readLine().trim().split(" ");
long[] longarray = new long[strarray.length];
for (int i = 0; i < longarray.length; i++) {
longarray[i] = Long.parseLong(strarray[i]);
}
return longarray;
}
// 標準入力数値リスト用 int
static List<Integer> inputIntList() throws Exception {
List<String> strList = Arrays.asList(br.readLine().trim().split(" "));
List<Integer> intList = new ArrayList<Integer>();
for (String elem : strList){
intList.add(Integer.parseInt(elem));
}
return intList;
}
// 標準入力数値配列用 integer 降順ソート用
static Integer[] inputvalInteger() throws Exception {
String[] strarray = br.readLine().trim().split(" ");
Integer[] intarray = new Integer[strarray.length];
for (int i = 0; i < intarray.length; i++) {
intarray[i] = Integer.parseInt(strarray[i]);
}
return intarray;
}
/*標準入力long*/
static long inputLong() throws Exception {
return Long.parseLong(br.readLine());
}
/*標準入力long*/
static int inputInt() throws Exception {
return Integer.parseInt(br.readLine());
}
public static void main(String[] args) throws Exception {
// write your code here
int n = inputInt();
long [] al = inputLongArr();
long sum = al[0];
long sum2 = -al[0];
long ans = 0;
long ans2 = Math.abs(al[0] - (-al[0]));
boolean nextPlusF = al[0] < 0;
for(int i=1;i<n;i++){
sum += al[i];
if(nextPlusF && sum <=0){
ans += 1-sum;
sum += ans;
}else if ((! nextPlusF) && sum >= 0){
ans += sum +1;
sum -= ans;
}
nextPlusF = !nextPlusF;
}
nextPlusF = !(al[0] < 0);
for(int i=1;i<n;i++){
sum2 += al[i];
if(nextPlusF && sum2 <=0){
ans2 += 1-sum2;
sum2 += ans2;
}else if ((! nextPlusF) && sum2 >= 0){
ans2 += sum2 +1;
sum2 -= ans2;
}
nextPlusF = !nextPlusF;
}
System.out.println(Math.min(ans,ans2));
}
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.