Search is not available for this dataset
name
stringlengths 2
88
| description
stringlengths 31
8.62k
| public_tests
dict | private_tests
dict | solution_type
stringclasses 2
values | programming_language
stringclasses 5
values | solution
stringlengths 1
983k
|
---|---|---|---|---|---|---|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, buf, max, ans;
int flg, flg1;
ans = 0;
max = 0;
buf = 0;
cin >> n;
vector<long long int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
if (i == 0 && a[i] == 0) {
if (a[1] < 0) {
a[i] = 1;
ans = 1;
} else if (a[1] > 0) {
a[i] = -1;
ans = 1;
}
}
buf += a[i];
if (i == 0) {
if (a[i] < 0) flg = -1;
if (a[i] > 0) flg = 1;
} else {
if (buf < 0) flg = -1;
if (buf > 0) flg = 1;
}
if (i != 0) {
if (flg == flg1) {
if (buf > 0) {
ans += ((buf) > 0 ? (buf) : (buf * -1)) + 1;
buf = -1;
flg = -1;
} else if (buf < 0) {
ans += ((buf) > 0 ? (buf) : (buf * -1)) + 1;
buf = 1;
flg = 1;
} else if (buf == 0) {
ans += 1;
if (flg1 == 1) flg = -1;
if (flg1 == -1) flg = 1;
}
}
}
flg1 = flg;
}
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;
import java.util.Arrays;
public class Main{
static int n;
static long[] a;
static final boolean DEBUG = false;
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
a = new long[n];
for(int i = 0; i < n; i++){
a[i] = sc.nextInt();
}
long count = 0, count2 = 0;
long sum = a[0];
long sum2 = a[0] < 0 ? 1 : -1;
count2 = Math.abs(sum2 - a[0]);
for(int i = 1; i < n; i++){
long val = a[i], val2 = a[i];
if(!((sum > 0 && sum + a[i] < 0) ||
(sum < 0 && sum + a[i] > 0))){
val = -sum + ((sum < 0) ? 1 : -1);
count += Math.abs(val - a[i]);
}
if(!((sum2 > 0 && sum2 + a[i] < 0) ||
(sum2 < 0 && sum2 + a[i] > 0))){
val2 = -sum2 + ((sum2 < 0) ? 1 : -1);
count2 += Math.abs(val2 - a[i]);
}
sum += val;
sum2 += val2;
}
if(DEBUG){
System.out.println(Arrays.toString(a));
}
System.out.println(Math.min(count, count2));
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> data;
while (n--) {
int a;
cin >> a;
data.push_back(a);
}
int sum1 = 0, sum2 = 0;
int ans1 = 0, ans2 = 0;
for (int i = 0; i < data.size(); i++) {
sum1 += data[i], sum2 += data[i];
if (i % 2 == 0) {
if (sum1 <= 0) {
ans1 += 1 - sum1;
sum1 = 1;
}
if (sum2 >= 0) {
ans2 += sum2 - (-1);
sum2 = -1;
}
} else {
if (sum1 >= 0) {
ans1 += sum1 - (-1);
sum1 = -1;
}
if (sum2 <= 0) {
ans2 += 1 - sum2;
sum2 = 1;
}
}
}
cout << min(ans1, ans2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | N = int(input())
A = [int(_) for _ in input().split()]
dp = A[0]
count = 0
is_positive = dp > 0
for i in range(1, N):
dp += A[i]
if (dp > 0) == is_positive:
count += abs(dp)+1
dp = 1-2*is_positive
is_positive = dp > 0
print(count)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> vec;
for (int i = 0; i < n; i++) {
int aux;
cin >> aux;
vec.push_back(aux);
}
long long sumA = 0;
long long sumB = 0;
long long auxA = vec[0];
long long auxB;
if (auxA > 0) {
auxB = -1;
sumB += auxA + 1;
} else if (auxA < 0) {
auxB = 1;
sumB += abs(auxA) + 1;
} else {
auxA = 0;
auxB = 0;
sumA++;
sumB++;
}
long long sumAuxA = auxA;
long long sumAuxB = auxB;
for (int i = 1; i < n; i++) {
if (sumAuxA > 0) {
sumAuxA += vec[i];
sumAuxB += vec[i];
if (sumAuxA >= 0) {
sumA += sumAuxA + 1;
sumAuxA = -1;
}
if (sumAuxB <= 0) {
sumB += abs(sumAuxB) + 1;
sumAuxB = 1;
}
} else {
sumAuxA += vec[i];
sumAuxB += vec[i];
if (sumAuxA <= 0) {
sumA += abs(sumAuxA) + 1;
sumAuxA = 1;
}
if (sumAuxB >= 0) {
sumB += sumAuxB + 1;
sumAuxB = -1;
}
}
}
cout << min(sumA, sumB);
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void) {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
vector<int> rev_a = a;
int result = 0;
bool isPlus = a[0] > 0 ? true : false;
int sum = a[0];
for (int i = 1; i < n; i++) {
int temp_sum = sum + a[i];
if (isPlus) {
if (temp_sum >= 0) {
result += temp_sum + 1;
a[i] -= temp_sum + 1;
}
} else {
if (temp_sum <= 0) {
result += -temp_sum + 1;
a[i] += -temp_sum + 1;
}
}
isPlus = !isPlus;
sum += a[i];
}
int rev_result = 0;
isPlus = a[0] > 0 ? true : false;
if (isPlus) {
rev_result += a[0] + 1;
a[0] -= a[0] + 1;
isPlus = !isPlus;
}
for (int i = 1; i < n; i++) {
int temp_sum = sum + rev_a[i];
if (isPlus) {
if (temp_sum >= 0) {
rev_result += temp_sum + 1;
rev_a[i] -= temp_sum + 1;
}
} else {
if (temp_sum <= 0) {
rev_result += -temp_sum + 1;
rev_a[i] += -temp_sum + 1;
}
}
isPlus = !isPlus;
sum += rev_a[i];
}
if (rev_result < result)
cout << rev_result << endl;
else
cout << result << 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()))
num = a[0]
ans = 0
if a[0]>0:
for i in range(1,n):
num += a[i]
if i%2==1:
if num>=0:
ans += num+1
num = -1
else:
if num<=0:
ans += 1-num
num = 1
else:
for i in range(1,n):
num += a[i]
if i%2==1:
if num <= 0:
ans += 1-num
num = 1
else:
if num >= 0:
ans += num+1
num = -1
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include<cstdio>
using namespace std;
int main(){
int n, d, a = 0, b = 0, cnta = 0, cntb = 0, i, ans;
scnaf("%d", &n);
for(i = 0; i < n; i++){
scanf("%d", &d);
a += d; b += d;
if(i%2){
if(a > -1){cnta += a + 1; a = -1;}
if(b < 1){cntb += 1 - b; b = 1;}
}else{
if(b > -1){cntb += b + 1; b = -1;}
if(a < 1){cnta += 1 - a; a = 1;}
}
}
ans = (cnta > cntb) ? cnta : cntb;
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 | #include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <ctype.h>
#include <stdint.h>
#include <string.h>
#include <wchar.h>
#include <math.h>
#define N_MAX (100)
#define P_MAX (100)
#define DP_ARRAY_SIZE (N_MAX * P_MAX / 32 + 1)
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define ABS(a) ((a) < 0 ? -(a) : (a))
#define ABSS(a, b) ((a) > (b) ? (a) - (b) : (b) - (a))
int compare_sz_asc(const void* a, const void* b) {
return *((size_t*)a) < *((size_t*)b) ? -1 : 1;
}
int compare_sz_desc(const void* a, const void* b) {
return *((size_t*)a) > * ((size_t*)b) ? -1 : 1;
}
int compare_i64_asc(const void* a, const void* b) {
return *((int64_t*)a) < *((int64_t*)b) ? -1 : 1;
}
int compare_i64_desc(const void* a, const void* b) {
return *((int64_t*)a) > * ((int64_t*)b) ? -1 : 1;
}
int compare_c_asc(const void* a, const void* b) {
return *((char*)a) < *((char*)b) ? -1 : 1;
}
int compare_c_desc(const void* a, const void* b) {
return *((char*)a) > * ((char*)b) ? -1 : 1;
}
static size_t powSz(const size_t base, const size_t exp) {
if (exp == 0) {
return 1;
}
if (exp == 1) {
return base;
}
if (exp % 2 == 0) {
return powSz(base * base, exp / 2);
}
else {
return base * powSz(base, exp - 1);
}
}
static size_t comb(const size_t n, const size_t r) {
size_t result = 1;
for (size_t i = 0; i < r; i++) {
result *= n - i;
result /= i + 1;
}
return result;
}
static uint64_t combU64(const uint64_t n, const uint64_t r) {
uint64_t result = 1;
for (uint64_t i = 0; i < r; i++) {
result *= n - i;
result /= i + 1;
}
return result;
}
static size_t gcdZu(size_t m, size_t n) {
size_t temp;
while (m % n != 0) {
temp = n;
n = m % n;
m = temp;
}
return n;
}
static uint64_t gcdU64(uint64_t m, uint64_t n)
{
uint64_t temp;
while (m % n != 0) {
temp = n;
n = m % n;
m = temp;
}
return n;
}
static int64_t a[100000];
int main(void) {
size_t n;
scanf("%zu\n", &n);
for (size_t i = 0; i < n; i++) {
scanf("%"PRId64, &a[i]);
}
size_t cnt[2] = { 0,0 };
int64_t base[2] = { 1,-1 };
for (size_t i = 0; i < n; i++) {
cnt[0] = (size_t)ABSS(base[0], a[i]);
cnt[1] = (size_t)ABSS(base[1], a[i]);
base[0] = -base[0];
base[1] = -base[1];
}
printf("%zu", MIN(cnt[0], cnt[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 | cpp | #include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
template <class T>
void cout_vec(const vector<T> &vec1) {
for (long long i = 0; i < long long(vec1.size()); i++) {
cout << vec1[i] << ' ';
}
cout << '\n';
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
long long n;
cin >> n;
vector<long long> a(n + 1), sum(n + 1, 0);
for (long long i = 1; i < n + 1; i++) cin >> a[i];
long long ans = 0;
for (long long i = 1; i < n + 1; i++) {
sum[i] = sum[i - 1] + a[i];
if (sum[1] == 0) {
if (a[i] > 0) {
sum[1]--;
ans++;
} else {
sum[1]++;
ans++;
}
continue;
}
if (sum[i] == 0) {
if (sum[i - 1] < 0) {
sum[i]++;
ans++;
} else {
sum[i]--;
ans++;
}
}
if (sum[i] > 0 && sum[i - 1] > 0) {
sum[i] = -1;
ans += a[i] + sum[i - 1] + 1;
}
if (sum[i] < 0 && sum[i - 1] < 0) {
sum[i] = 1;
ans += 1 - a[i] - sum[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 | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long> a1(n);
vector<long> a2(n);
for (int i = 0; i < n; i++) {
cin >> a1.at(i);
a2.at(i) = a1.at(i);
}
int ans1 = 0;
int flag1 = 1;
long sum = 0;
for (int i = 0; i < n; i++) {
sum += a1.at(i);
if (flag1 == 1) {
flag1 = -1;
if (sum <= 0) {
ans1 += -sum + 1;
sum = 1;
}
} else if (flag1 == -1) {
flag1 = 1;
if (sum >= 0) {
ans1 += sum + 1;
sum = -1;
}
}
}
int ans2 = 0;
flag1 = -1;
sum = 0;
for (int i = 0; i < n; i++) {
sum += a2.at(i);
if (flag1 == 1) {
flag1 = -1;
if (sum <= 0) {
ans2 += -sum + 1;
sum = 1;
}
} else if (flag1 == -1) {
flag1 = 1;
if (sum >= 0) {
ans2 += sum + 1;
sum = -1;
}
}
}
cout << min(ans1, ans2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
even_is_negative = 0
odd_is_nagative = 0
for i in range(n):
if i % 2 == 0 and a[i] < 0:
even_is_negative += 1
elif i % 2 == 1 and a[i] < 0:
odd_is_nagative += 1
# print(even_is_negative)
# print(odd_is_nagative)
sum = a[0]
change = 0
if even_is_negative >= odd_is_nagative:
for i in range(1, n):
if i % 2 == 1 and sum + a[i] <= 0:
change += abs(sum + a[i]) + 1
sum = 1
# print("a " + str(sum))
elif i % 2 == 0 and sum + a[i] >= 0:
change += sum + a[i] + 1
sum = -1
# print("b " + str(sum))
else:
sum += a[i]
# print("c " + str(sum))
if even_is_negative < odd_is_nagative:
for i in range(1, n):
if i % 2 == 1 and sum + a[i] >= 0:
change += sum + a[i] + 1
sum = -1
# print("a " + str(sum) + str(change))
elif i % 2 == 0 and sum + a[i] <= 0:
change += abs(sum + a[i]) + 1
sum = 1
# print("b " + str(sum)+ str(change))
else:
sum += a[i]
# print("c " + str(sum)+ str(change))
if sum == 0:
print(change + 1)
else:
print(change) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
long long s[maxn];
long long ans[maxn];
int main() {
int n, j;
cin >> n;
long long sum = 0;
for (int i = 1; i <= n; i++) {
cin >> s[i];
}
for (int i = 1; i < n; i++) {
ans[i] = ans[i - 1] + s[i];
if (ans[i] > 0) {
if (s[i + 1] >= 0) {
sum += (s[i + 1] + ans[i] + 1);
s[i + 1] = -(ans[i] + 1);
} else {
if (abs(s[i + 1]) > ans[i]) {
} else {
sum += (s[i + 1] + ans[i] + 1);
s[i + 1] = -(ans[i] + 1);
}
}
} else if (ans[i] == 0) {
if (s[i + 1] < 0) {
sum += -s[i + 1] + 1;
ans[i] = 2;
s[i + 1] = -1;
} else if (s[i + 1] > 0) {
sum += s[i + 1] + 1;
ans[i] = -2;
s[i + 1] = 1;
} else {
s[i + 1] = -2;
ans[i] = 1;
sum += 3;
}
} else if (ans[i] < 0) {
if (s[i + 1] > 0) {
if (abs(ans[i]) < s[i + 1]) {
} else {
sum += (1 - ans[i] - s[i + 1]);
s[i + 1] = -ans[i] + 1;
}
} else {
sum += (1 - ans[i] - s[i + 1]);
s[i + 1] = -ans[i] + 1;
}
}
}
cout << sum << endl;
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())
X=list(map(int,input().split()))
res=X[0]
cnt=0
if X[0]==0:
cnt+=1
res=1
for i in range(1,n):
res+=X[i]
if res>=0 and res-X[i]>0:
cnt+=(1+res)
res=-1
elif res<=0 and res-X[i]<0:
cnt+=(1-res)
res=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 | n = int(input())
a = list(map(int,input().split()))
ans=10**10
s=0
cnt=0
for i in range(n):
s+=a[i]
if i%2==0:
if s>=0:
cnt += s + 1
s = -1
else:
if s<=0:
cnt += -s + 1
s = 1
ans=min(ans,cnt)
s=0
cnt=0
for i in range(n):
s+=a[i]
if i%2==1:
if s>=0:
cnt += s + 1
s = -1
else:
if s<=0:
cnt += -s + 1
s = 1
ans=min(ans,cnt)
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long ans = 0;
long long now;
cin >> now;
if (now == 0) {
now++;
ans++;
}
for (long long(i) = (0); (i) < (n - 1); ++i) {
long long tmp;
cin >> tmp;
if (now > 0) {
now += tmp;
if (now >= 0) {
ans += now + 1;
now = -1;
}
} else {
now += tmp;
if (now <= 0) {
ans -= (now - 1);
now = 1;
}
}
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
long long ans = 0;
long long s_i = 0;
for (int i = 0; i < n; i++) {
if (i == 0) {
if (a[i] == 0) {
if (a[i + 1] > 0) {
ans += 1;
s_i = -1;
} else {
s_i = 1;
}
} else {
s_i = a[i];
}
} else {
if (s_i > 0) {
if (s_i + a[i] <= -1) {
s_i = s_i + a[i];
} else {
ans += abs(-1 - s_i - a[i]);
s_i = -1;
}
} else {
if (s_i + a[i] >= 1) {
s_i = s_i + a[i];
} else {
ans += abs(1 - s_i - a[i]);
s_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 | cpp | #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i=0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using ll = long long;
using VL = vector<ll>;
using VVL = vector<vector<ll>>;
using P = pair<ll, ll>;
// chmin, chmax関数
template<typename T, typename U, typename Comp=less<>>
bool chmax(T& xmax, const U& x, Comp comp={}) {
if(comp(xmax, x)) {
xmax = x;
return true;
}
return false;
}
template<typename T, typename U, typename Comp=less<>>
bool chmin(T& xmin, const U& x, Comp comp={}) {
if(comp(x, xmin)) {
xmin = x;
return true;
}
return false;
}
//---------------------------
int main(){
ll n, tmp=0;
cin >> n;
VL sum(n);
rep(i,n){
ll a;
cin >> a;
tmp += a;
sum[i] = tmp;
}
ll ans = (ll)1 << 60; // INF
// cout << ans << endl;
if(sum[0] == 0){
// sum[0] = 1: even is pos, odd is neg
ll accum=1, cnt=1;
rep2(i,1,n){
ll x = sum[i] + accum;
if(i%2==0 && x <= 0){
accum += abs(x) + 1;
cnt += abs(x) + 1;
}else if(i%2==1 && x >= 0){
accum -= abs(x) + 1;
cnt += abs(x) + 1;
}
}
chmin(ans, cnt);
// sum[0] = -1: even is neg, odd is pos
accum=-1, cnt=1;
rep2(i,1,n){
ll x = sum[i] + accum;
if(i%2==0 && x >= 0){
accum -= abs(x) + 1;
cnt += abs(x) + 1;
}else if(i%2==1 && x <= 0){
accum += abs(x) + 1;
cnt += abs(x) + 1;
}
}
chmin(ans, cnt);
}else if(sum[0] > 0){
// even is pos, odd is neg
ll accum=0, cnt=0;
rep2(i,1,n){
ll x = sum[i] + accum;
if(i%2==0 && x <= 0){
accum += abs(x) + 1;
cnt += abs(x) + 1;
}else if(i%2==1 && x >= 0){
accum -= abs(x) + 1;
cnt += abs(x) + 1;
}
}
chmin(ans, cnt);
}else{
ll accum=-1, cnt=1;
rep2(i,1,n){
ll x = sum[i] + accum;
if(i%2==0 && x >= 0){
accum -= abs(x) + 1;
cnt += abs(x) + 1;
}else if(i%2==1 && x <= 0){
accum += abs(x) + 1;
cnt += abs(x) + 1;
}
}
chmin(ans, cnt);
}
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, chk;
long long ans = 0;
scanf("%d", &n);
vector<int> a(n);
for (auto& e : a) scanf("%d", &e);
chk = a[0];
for (int i = 1; i < n; i++) {
if (i % 2) {
chk += a[i];
if (chk >= 0) {
ans += chk + 1;
chk = -1;
}
} else {
chk += a[i];
if (chk <= 0) {
ans += -1 * chk + 1;
chk = 1;
}
}
}
printf("%lld", 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 rep(i,n) for(int i=0;i<n;i++)
#define FOR(i,start,end) for(int i=start;i<=end;i++)
const int INF = 1001001001;
typedef long long ll;
const ll MOD=1000000007;
using namespace std;
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 (a > b) { a = b; return 1; } return 0; }
template<class T>auto MAX(const T& a) { return *max_element(a.begin(),a.end()); }
template<class T>auto MIN(const T& a) { return *min_element(a.begin(),a.end()); }
template<class T, class U>U SUM(const T& a, const U& v) { return accumulate(a.begin(),a.end(), v); }
template<class T, class U>U COUNT(const T& a, const U& v) { return count(a.begin(),a.end(), v); }
template<class T, class U>int LOWER(const T& a, const U& v) { return lower_bound(a.begin(),a.end(), v) - a.begin(); }
template<class T, class U>int UPPER(const T& a, const U& v) { return upper_bound(a.begin(),a.end(), v) - a.begin(); }
int GCD(int a, int b) { return b ? GCD(b, a%b) : a; }
int LCM(int a, int b) { int g = GCD(a, b); return a / g * b; }
//---------------------------------------------------------------------------------------------------
template<int MOD> struct ModInt {
static const int Mod = MOD; unsigned x; ModInt() : x(0) { }
ModInt(signed sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
ModInt(signed long long sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
int get() const { return (int)x; }
ModInt &operator+=(ModInt that) { if ((x += that.x) >= MOD) x -= MOD; return *this; }
ModInt &operator-=(ModInt that) { if ((x += MOD - that.x) >= MOD) x -= MOD; return *this; }
ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; }
ModInt &operator/=(ModInt that) { return *this *= that.inverse(); }
ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
ModInt operator/(ModInt that) const { return ModInt(*this) /= that; }
ModInt inverse() const { long long a = x, b = MOD, u = 1, v = 0;
while (b) { long long t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); }
return ModInt(u); }
bool operator==(ModInt that) const { return x == that.x; }
bool operator!=(ModInt that) const { return x != that.x; }
ModInt operator-() const { ModInt t; t.x = x == 0 ? 0 : Mod - x; return t; }
};
template<int MOD> ostream& operator<<(ostream& st, const ModInt<MOD> a) { st << a.get(); return st; };
template<int MOD> ModInt<MOD> operator^(ModInt<MOD> a, unsigned long long k) {
ModInt<MOD> r = 1; while (k) { if (k & 1) r *= a; a *= a; k >>= 1; } return r; }
template<typename T, int FAC_MAX> struct Comb { vector<T> fac, ifac;
Comb(){fac.resize(FAC_MAX,1);ifac.resize(FAC_MAX,1);FOR(i,1,FAC_MAX-1)fac[i]=fac[i-1]*i;
ifac[FAC_MAX-1]=T(1)/fac[FAC_MAX-1];for(int i=FAC_MAX-2;i>=1;i--)ifac[i]=ifac[i+1]*T(i+1);}
T aPb(int a, int b) { if (b < 0 || a < b) return T(0); return fac[a] * ifac[a - b]; }
T aCb(int a, int b) { if (b < 0 || a < b) return T(0); return fac[a] * ifac[a - b] * ifac[b]; }
T nHk(int n, int k) { if (n == 0 && k == 0) return T(1); if (n <= 0 || k < 0) return 0;
return aCb(n + k - 1, k); } // nHk = (n+k-1)Ck : n is separator
T pairCombination(int n) {if(n%2==1)return T(0);return fac[n]*ifac[n/2]/(T(2)^(n/2));}
// combination of paris for n
};
typedef ModInt<1000000007> mint;
int main(void){
// Your code here!
int n; cin >> n;
vector<ll> a(n);
rep(i,n) cin >> a[i];
// +-+-....
ll sh = 0;
ll nw = 0;
rep(i,n) {
nw += a[i];
if(i%2 == 0) {
if(nw<= 0) {
sh += 1 - nw;
nw = 1;
}
}
if(i%2 != 0) {
if(nw>=0){
sh += nw + 1;
nw = -1;
}
}
}
// -+-+...
ll hs = 0;
nw = 0;
rep(i,n) {
if(i%2 == 0) if(nw>=0) hs += 1 + nw,nw = -1;
if(i%2 != 0) if(nw <= 0) hs+= 1 - nw,nw = 1;
}
cout << min(hs,sh) << endl;
}
// 基本的にはlong long を使いましょう |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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 = vec[0];
if (total == 0) {
total = sign;
count++;
} else {
sign = total / abs(total);
}
for (uint64_t i = 1; i < vec.size(); i++) {
sign *= -1;
total += vec[i];
if ((total == 0) || (sign * total < 0)) {
count += abs(sign - total);
total = sign;
}
}
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;
long long a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long s = a[0];
long long cnt = 0;
if (s == 0) {
if (a[1] > 0) {
s = -1;
cnt++;
} else {
s = 1;
cnt++;
}
}
for (int i = 1; i < n; i++) {
if (s > 0) {
if (s + a[i] < 0)
s += a[i];
else {
cnt += abs(s + a[i]) + 1;
s = -1;
}
} else {
if (s + a[i] > 0)
s += a[i];
else {
cnt += abs(s + a[i]) + 1;
s = 1;
}
}
}
cout << cnt;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; ++i) cin >> a.at(i);
long long cnt, tmp = 0, sum = a.at(0), x;
for (int j = 0; j < 2; j++) {
cnt = 0;
if (j == 1) {
cnt = abs(a.at(0)) + 1;
if (a.at(0) > 0)
a.at(0) = -1;
else
a.at(0) = 1;
sum = a.at(0);
}
if (a.at(0) >= 0)
for (int i = 1; i < n; i++) {
x = 0;
if (i % 2 == 1) {
if (a.at(i) >= 0 || sum + a.at(i) >= 0) x = -1 - a.at(i) - sum;
} else {
if (a.at(i) < 0 || sum + a.at(i) < 0) x = 1 - a.at(i) - sum;
}
cnt += abs(x);
sum += a.at(i) + x;
}
else {
for (int i = 1; i < n; i++) {
x = 0;
if (i % 2 == 1) {
if (a.at(i) <= 0 || sum + a.at(i) <= 0) x = 1 - a.at(i) - sum;
} else {
if (a.at(i) > 0 || sum + a.at(i) > 0) x = -1 - a.at(i) - sum;
}
cnt += abs(x);
sum += a.at(i) + x;
}
}
if (j == 0) tmp = cnt;
if (tmp > cnt) tmp = cnt;
}
cout << tmp << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long long int n;
cin >> n;
long long int a[n];
for (long long int i = 0; i < n; i++) cin >> a[i];
long long int f = 0;
long long int sum = a[0];
long long int op = 0;
if (sum > 0) f = 1;
for (long long int i = 1; i < n; i++) {
sum += a[i];
if (f == 1) {
if (sum < 0)
f = 0;
else {
op += abs(sum) + 1;
f = 0;
sum = -1;
}
} else {
if (sum > 0)
f = 1;
else {
op += abs(sum) + 1;
f = 1;
sum = 1;
}
}
}
cout << op;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
using System.Text;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using static System.Console;
using static System.Math;
namespace AtCorder
{
public class Program
{
public static void Main(string[] args)
{
new Program().Solve(new ConsoleInput(Console.In, ' '));
}
public void Solve(ConsoleInput cin)
{
var n = cin.ReadInt;
var a = cin.ReadLongArray(n);
var ans = 0L;
var pre = a[0];
for(int i = 1; i < n; i++)
{
var now = pre + a[i];
if(pre * now < 0)
{
pre += a[i];
continue;
}
if(now > 0)
{
ans += now + 1;
a[i] -= now + 1;
}
else if(now < 0)
{
ans += 1 - now;
a[i] += 1 - now;
}
else
{
if(i < n - 1)
{
if(a[i + 1] < 0)
{
a[i]--;
}
else
{
a[i]++;
}
}
else
{
a[i]++;
}
ans++;
}
pre += a[i];
}
WriteLine(ans);
}
public long C(int X, int Y)
{
if (Y == 0 || Y == X)
{
return 1;
}
if (X < Y)
{
return 0;
}
var Pascal = new long[X + 1, X + 1];
for (int i = 0; i <= X; i++)
{
Pascal[i, 0] = 1L;
Pascal[i, i] = 1L;
}
for (int i = 2; i <= X; i++)
{
for (int j = 1; j < i; j++)
{
Pascal[i, j] = Pascal[i - 1, j] + Pascal[i - 1, j - 1];
}
}
return Pascal[X, Y];
}
public class ConsoleInput
{
private readonly System.IO.TextReader _stream;
private char _separator = ' ';
private Queue<string> inputStream;
public ConsoleInput(System.IO.TextReader stream, char separator = ' ')
{
this._separator = separator;
this._stream = stream;
inputStream = new Queue<string>();
}
public string Read
{
get
{
if (inputStream.Count != 0) return inputStream.Dequeue();
string[] tmp = _stream.ReadLine().Split(_separator);
for (int i = 0; i < tmp.Length; ++i)
inputStream.Enqueue(tmp[i]);
return inputStream.Dequeue();
}
}
public string ReadLine { get { return _stream.ReadLine(); } }
public int ReadInt { get { return int.Parse(Read); } }
public long ReadLong { get { return long.Parse(Read); } }
public double ReadDouble { get { return double.Parse(Read); } }
public string[] ReadStrArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret; }
public int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret; }
public long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret; }
}
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
static long long int solve(const std::vector<long long int>& va,
long long int initSum, long long int initCnt = 0) {
long long int sum = initSum;
long long int cnt = initCnt;
for (std::remove_reference<decltype(va)>::type::size_type i = 1;
i < va.size(); i++) {
auto nextSum = sum + va[i];
if (nextSum >= 0 && sum > 0) {
cnt += nextSum + 1;
sum = -1;
} else if (nextSum <= 0 && sum < 0) {
cnt += -nextSum + 1;
sum = 1;
} else {
sum = nextSum;
}
}
return cnt;
}
signed main() {
std::cin.tie(nullptr);
std::ios::sync_with_stdio(false);
long long int n;
std::cin >> n;
std::vector<long long int> va(n);
for (auto&& e : va) {
std::cin >> e;
}
std::cout << std::min(solve(va, va[0]),
solve(va, va[0] > 0 ? -1 : 1, std::abs(va[0]) + 1))
<< std::endl;
return EXIT_SUCCESS;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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 String = std::string;
using llong = long long;
using boolean = bool;
using Pii = std::pair<int, int>;
using Vi = std::vector<int>;
using Vii = std::vector<Pii>;
constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr int dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
constexpr int INF = 0x3f3f3f3f;
constexpr llong LINF = 0x3f3f3f3f3f3f3f3fLL;
namespace {
template <class A, class B>
A power(llong x, llong n, llong mod) {
llong ans = 1;
while (n > 0) {
if (n & 1) ans = (ans * x) % mod;
x = (x * x) % mod;
n >>= 1;
}
return ans;
}
template <class A, class B>
A power(A x, B n) {
return power(x, n, 1000000007);
}
template <class A>
A gcd(A x, A y) {
return x % y ? gcd(y, x % y) : y;
}
template <class A, class B>
A lcm(A x, B y) {
return (x / gcd(x, y) * y);
}
template <class A>
inline A abs(A n) {
return (n < 0) ? -n : n;
}
template <class A, class B>
inline bool chmax(A &a, const B &b) {
return b > a ? a = b, true : false;
}
template <class A, class B>
inline bool chmin(A &a, const B &b) {
return b < a ? a = b, true : false;
}
inline boolean isMovable(int x, int y, int w, int h) {
return (x >= 0 && y >= 0 && x < w && y < h);
}
} // namespace
namespace Rlyeh {
llong a[100100], left;
llong cnt, tmp;
signed call_of_Cthulhu(signed datum) {
llong n;
std::cin >> n;
for (llong i = 0; i < n; i++) {
std::cin >> a[i];
}
left += a[0];
for (llong i = 1; i < n; i++) {
boolean isNegative = left < 0;
left += a[i];
if (isNegative && left <= 0) {
cnt += 1 - left;
left = 1;
} else if (!isNegative && 0 <= left) {
cnt += 1 + left;
left = -1;
}
}
std::cout << cnt << '\n';
return 0;
}
} // namespace Rlyeh
signed main() {
std::cin.tie(0);
std::ios::sync_with_stdio(false);
llong main_result = Rlyeh::call_of_Cthulhu(114514);
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 now;
cin >> now;
long long ans = 0;
for (long long(i) = (0); (i) < (n - 1); ++i) {
long long tmp;
cin >> tmp;
if (now > 0) {
now += tmp;
if (now >= 0) {
ans += now + 1;
now = -1;
}
} else {
now += tmp;
if (now <= 0) {
ans -= (now - 1);
now = 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 long long INFTY = 1001001001;
int main() {
int N;
cin >> N;
vector<long long> a(N + 1), b(N + 1);
for (int i = 0; i < N; ++i) {
cin >> a[i];
b[i] = a[i];
if (i != 0) {
a[i] += a[i - 1];
b[i] += b[i - 1];
}
}
long long ans = INFTY;
bool sign;
for (int j = 0; j < 2; ++j) {
sign = j;
if (sign) {
for (int i = 0; i < N + 1; ++i) {
a[i] = b[i];
}
}
long long v = 0, count = 0;
for (int i = 0; i < N; ++i) {
if (sign) {
if (a[i] >= 0) {
count += abs(-a[i] - 1);
v -= abs(-a[i] - 1);
}
} else {
if (a[i] <= 0) {
count += abs(-a[i] + 1);
v += abs(-a[i] + 1);
}
}
a[i + 1] += v;
sign = !(sign);
}
ans = min(ans, count);
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.*;
// ABC 6-C
// http://abc006.contest.atcoder.jp/tasks/abc006_3
public class Main {
public static void main (String[] args) throws java.lang.Exception {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = in.nextInt();
}
long answer = 0;
if (nums[0] == 0) {
answer = solve(nums, 0, 0);
} else {
answer = solve(nums, nums[0], 1);
}
System.out.println(answer);
//
// long sum = 0;
// long answer = 0;
//
// for (int i = 0; i < n; i++) {
// int a = in.nextInt();
//
// if (sum < 0 && sum + a < 0) {
// answer += 1 + Math.abs(sum + a);
// sum = 1;
// } else if (sum > 0 && sum + a > 0) {
// answer += 1 + sum + a;
// sum = -1;
// } else if (sum + a == 0) {
// answer++;
// if (sum < 0) {
// sum = 1;
// } else {
// sum = -1;
// }
// } else {
// sum += a;
// }
// }
// System.out.println(answer);
}
public static long solve(int[] nums, long sum, int index) {
if (index == nums.length) {
return 0;
}
if (sum < 0 && sum + nums[index] < 0) {
return 1 + Math.abs(sum + nums[index]) + solve(nums, 1, index + 1);
} else if (sum > 0 && sum + nums[index] > 0) {
return 1 + sum + nums[index] + solve(nums, -1, index + 1);
} else if (sum + nums[index] == 0 || sum == 0) {
return 1 + Math.min(solve(nums, 1, index + 1), solve(nums, -1, index + 1));
} else {
return solve(nums, sum + nums[index], index + 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 | UNKNOWN | n = gets.to_i
arr = gets.chomp.split(" ").map(&:to_i)
$count = [0,0]
def check(i,arr,t)
if i > arr.size - 1
arr[t] += 1
$count += 1
return
end
if arr[i] > 0
arr[t] -= 1
$count += 1
elsif arr[i] < 0
arr[t] += 1
$count += 1
else
check(i+1,arr,t)
end
end
flg = true
2.times do |j|
tmp_arr = Marshal.load(Marshal.dump(arr))
sum = tmp_arr[0] + tmp_arr[1]
if sum == 0
if flg
tmp_arr[1] -= 1
else
tmp_arr[2] += 1
end
$count[j] += 1
end
sum = tmp_arr[0] + tmp_arr[1]
(2...tmp_arr.size).each do |i|
diff = sum + tmp_arr[i]
# puts %(sum : #{sum})
# puts %(diff : #{diff})
if sum > 0
if diff > 0
tmp_arr[i] -= diff.abs+1
$count[j] += diff.abs+1
elsif diff == 0
tmp_arr[i] -= 1
$count[j] += 1
end
else
if diff < 0
tmp_arr[i] += diff.abs+1
$count[j] += diff.abs+1
elsif diff == 0
tmp_arr[i] += 1
$count[j] += 1
end
end
sum += tmp_arr[i]
end
end
#p $count
#p arr
puts $count.min |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.*;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] a = new int[n];
for(int i = 0; i<n; i++) {
a[i] = sc.nextInt();
}
int min = Integer.MAX_VALUE;
int tmp = 0;
int cnt = 0;
for(int i = 0; i<n; i++) {
tmp+=a[i];
System.err.println(i+" "+tmp);
if(i%2==0) {
if(tmp<=0) {
cnt += 1 - tmp;
tmp = 1;
}
}else {
if(tmp>=0) {
cnt += tmp + 1;
tmp = -1;
}
}
}
min = cnt;
cnt = 0;
tmp = 0;
System.err.println(min);
for(int i = 0; i<n; i++) {
tmp+=a[i];
if(i%2==1) {
if(tmp<=0) {
cnt += 1 - tmp;
tmp = 1;
}
}else {
if(tmp>=0) {
cnt += tmp + 1;
tmp = -1;
}
}
}
min = Math.min(min, cnt);
System.out.println(min);
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int check(vector<int> a) {
int time = 0;
int pre_sum = a.at(0);
int n = a.size();
if (a.at(0) < 0) {
for (int i = 1; i < n; i++) {
int sum = pre_sum + a.at(i);
if (i % 2 == 1 && sum <= 0) {
time += abs(sum - 1);
sum = 1;
} else if (i % 2 == 0 && sum >= 0) {
time += abs(sum + 1);
sum = -1;
}
pre_sum = sum;
}
} else if (a.at(0) > 0) {
for (int i = 1; i < n; i++) {
int sum = pre_sum + a.at(i);
if (i % 2 == 0 && sum <= 0) {
time += abs(sum - 1);
sum = 1;
} else if (i % 2 == 1 && sum >= 0) {
time += abs(sum + 1);
sum = -1;
}
pre_sum = sum;
}
}
return time;
}
int zerocheck(vector<int> a) {
a.at(0) = 1;
int time1 = check(a) + 1;
a.at(0) = -1;
int time2 = check(a) + 1;
int time = min(time1, time2);
return time;
}
int main() {
int n;
cin >> n;
int time = 0;
vector<int> a(n);
for (auto& x : a) {
cin >> x;
}
if (a.at(0) == 0) {
time = zerocheck(a);
} else {
time = check(a);
}
cout << time << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (auto& x : a) {
cin >> x;
}
bool start;
for (int i = 0; i < n; i++) {
if (a[i] > 0) {
start = (i % 2 == 0);
break;
} else if (a[i] < 0) {
start = (i % 2 == 1);
break;
}
if (i == n - 1) {
cout << n << endl;
return 0;
}
}
long long sum = 0;
long long ans = 0;
bool plus = start;
for (int i = 0; i < n; i++) {
sum += a[i];
if (plus && sum <= 0) {
ans += 1 - sum;
sum = 1;
} else if (!plus && sum >= 0) {
ans += sum + 1;
sum = -1;
}
plus = !plus;
}
vector<int> b(n);
for (int i = 0; i < n; i++) {
b[i] = -1 * a[i];
}
for (int i = 0; i < n; i++) {
if (b[i] > 0) {
start = (i % 2 == 0);
break;
} else if (b[i] < 0) {
start = (i % 2 == 1);
break;
}
if (i == n - 1) {
cout << n << endl;
return 0;
}
}
sum = 0;
long long ans2 = 0;
plus = start;
for (int i = 0; i < n; i++) {
sum += b[i];
if (plus && sum <= 0) {
ans2 += 1 - sum;
sum = 1;
} else if (!plus && sum >= 0) {
ans2 += sum + 1;
sum = -1;
}
plus = !plus;
}
cout << min(ans, ans2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
vector<int> a(100000);
int ans1 = 0, ans2 = 0, sum = 0;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) {
if (i % 2 != 0 && sum + a[i] >= 0) {
ans1 += abs(sum * (-1) - 1 - a[i]);
sum = -1;
} else if (i % 2 == 0 && sum + a[i] <= 0) {
ans1 += abs(sum * (-1) + 1 - a[i]);
sum = 1;
} else
sum += a[i];
}
sum = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0 && sum + a[i] >= 0) {
ans2 += abs(sum * (-1) - 1 - a[i]);
sum = -1;
} else if (i % 2 != 0 && sum + a[i] <= 0) {
ans2 += abs(sum * (-1) + 1 - a[i]);
sum = 1;
} else
sum += a[i];
}
cout << min(ans1, ans2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
list_a = list(map(int, input().split()))
sum_a = list_a[0]
count_t = 0
if sum_a < 0:
count_t += 1 + abs(sum_a)
for i in range(1, n):
if sum_a > 0:
if sum_a + list_a[i] >= 0:
diff = abs(-1 - list_a[i] - sum_a)
else:
diff = 0
sum_a = sum_a + list_a[i] - diff
count_t += diff
else:
if sum_a + list_a[i] <= 0:
diff = abs(1 - sum_a - list_a[i])
else:
diff = 0
sum_a = sum_a + list_a[i] + diff
count_t += diff
sum_a = list_a[0]
count_f = 0
if sum_a > 0:
count_f += 1 + abs(sum_a)
for i in range(1, n):
if sum_a > 0:
if sum_a + list_a[i] >= 0:
diff = abs(-1 - list_a[i] - sum_a)
else:
diff = 0
sum_a = sum_a + list_a[i] - diff
count_f += diff
else:
if sum_a + list_a[i] <= 0:
diff = abs(1 - sum_a - list_a[i])
else:
diff = 0
sum_a = sum_a + list_a[i] + diff
count_f += diff
print(min(count_t, count_f)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
s, ans, i = 0, 0, 0
while a[i] == 0 and i < n:
ans += 2
i += 1
ans = max(0, ans - 1)
if i == n:
print(ans)
exit()
if ans > 0:
if abs(a[i]) == 1:
ans += 1
s = a[i] // abs(a[i])
else:
s = a[0]
i += 1
for j in range(i, n):
if abs(a[j]) > abs(s) and a[j] // abs(a[j]) != s // abs(s):
s += a[j]
else:
pre_s = s
s = -1 * s // abs(s)
ans += abs(a[j] - s + pre_s)
print(ans) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long MOD = 1000000007;
const int MAX_N = 100000005;
int n;
int a[MAX_N];
int check(long long sum, long long ans) {
for (int i = 1; i < n; i++) {
long long t = sum + a[i];
if ((sum >= 0 && t < 0) || (sum < 0 && t >= 0)) {
sum = t;
if (sum == 0) {
sum = 1;
ans++;
}
continue;
}
long long at;
if (sum >= 0)
at = -1 - sum;
else
at = 1 - sum;
ans = ans + abs(a[i] - at);
sum = sum + at;
}
return ans;
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long another;
if (a[0] >= 0)
another = -1;
else
another = 1;
long long a1 = check(a[0], 0);
long long a2 = check(another, abs(a[0] - another));
long long ans = min(a1, a2);
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()))
sum = a[0]
change = 0
if a[0]==0:
idx=0
while a[idx]==0:
idx+=1
for i in range(idx-1,-1,-1):
val = 0
tempsum = sum+a[i]
if sum < 0 and tempsum <=0:
val = 1 - tempsum
if sum > 0 and tempsum >=0:
val = -1 - tempsum
sum = tempsum + val
change += abs(val)
sum = a[0]
for i in range(1,n):
val = 0
tempsum = sum+a[i]
if sum < 0 and tempsum <=0:
val = 1 - tempsum
if sum > 0 and tempsum >=0:
val = -1 - tempsum
sum = tempsum + val
change += abs(val)
print(change) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1LL << 60;
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < (int)(n); i++) {
cin >> a[i];
}
int sum = a[0];
int ans = 0;
for (int i = 1; i < n; i++) {
int tmp_sum = sum + a[i];
if (sum > 0) {
while (tmp_sum >= 0) {
--tmp_sum;
++ans;
}
} else if (sum < 0) {
while (tmp_sum <= 0) {
++tmp_sum;
++ans;
}
}
sum = tmp_sum;
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long int solve(const vector<int>& a) {
long long int ans = 0;
int cur = a[0];
for (int i = 1; i < a.size(); i++) {
if (cur < 0 and cur + a[i] <= 0) {
ans += 1 - (cur + a[i]);
cur = 1;
} else if (cur > 0 and cur + a[i] >= 0) {
ans += cur + a[i] + 1;
cur = -1;
} else {
cur += a[i];
}
}
return ans;
}
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
long long int ans = (1LL << 60);
if (a[0] == 0) {
a[0] = 1;
ans = min(ans, solve(a));
a[0] = -1;
ans = min(ans, solve(a));
ans++;
} else {
ans = solve(a);
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
using System.Collections.Generic;
using static Assistant.Input;
using static Assistant.Debug;
using System.Linq;
using Assistant;
namespace ABC059C
{
class Program
{
static void Main(string[] args)
{
var n = RInt;
var a = RInts;
long ans = 0;
if (a[0] != 0)
{
ans = cand(a[0], a);
}
else
{
ans = Math.Min(cand(1, a), cand(-1, a));
}
Console.WriteLine(ans);
}
static long cand(int sum, int[] a)
{
long ret = 0;
for (int i = 1; i < a.Length; i++)
{
if (sum < 0)
{
sum += a[i];
if (sum < 1)
{
ret += 1 - sum;
sum = 1;
}
}
else if (sum > 0)
{
sum += a[i];
if (sum > -1)
{
ret += sum + 1;
sum = -1;
}
}
}
return ret;
}
}
}
namespace Assistant
{
static class Input
{
static List<string> line = new List<string>();
static int index = 0;
static String RNext()
{
if (line.Count <= index) line.AddRange(Console.ReadLine().Split());
return line[index++];
}
public static int RInt => int.Parse(RNext());
public static long RLong => long.Parse(RNext());
public static int[] RInts => Console.ReadLine().Split().Select(int.Parse).ToArray();
public static long[] RLongs => Console.ReadLine().Split().Select(long.Parse).ToArray();
public static string RString => RNext();
//以下未テスト
public static int[] RIntsC(int c) => Enumerable.Repeat(0, c).Select(x => int.Parse(RNext())).ToArray();
public static long[] RLongsC(int c) => Enumerable.Repeat(0, c).Select(x => long.Parse(RNext())).ToArray();
public static char[][] RMap(int h) => Enumerable.Repeat(0, h).Select(x => Console.ReadLine().ToCharArray()).ToArray();
}
public struct Mlong
{
long _v;
const long mod = 1000000007;
public Mlong(long n = 0) : this() { _v = n >= mod ? n % mod : n; }
public static implicit operator Mlong(long _x) => new Mlong(_x);
public static implicit operator long(Mlong _x) => _x._v;
public static Mlong operator +(Mlong m1, Mlong m2) { long m = m1._v + m2._v; return m >= mod ? m - mod : m; }
public static Mlong operator -(Mlong m1, Mlong m2) { long m = m1._v - m2._v; return m >= 0 ? m : m + mod; }
public static Mlong operator *(Mlong m1, Mlong m2) => m1._v * m2._v % mod;
public static Mlong operator /(Mlong m1, Mlong m2) => m1._v * ModPow(m2._v, mod - 2) % mod;
public static long ModPow(long a, long n)
{
if (n == 0) return 1;
else if (n % 2 == 1) return a * ModPow(a, n - 1) % mod;
else return ModPow(a * a % mod, n / 2);
}
static Mlong[] fac, finv, inv;
public static void nCkInit(int max)
{
fac = new Mlong[max]; finv = new Mlong[max]; inv = new Mlong[max];
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < max; i++)
{
fac[i] = fac[i - 1] * i;
inv[i] = mod - inv[mod % i] * (mod / i);
finv[i] = finv[i - 1] * inv[i];
}
}
public static Mlong nCk(int n, int k)
{
if (n < k) return 0;
if (n < 0 || k < 0) return 0;
return fac[n] * finv[k] * finv[n - k];
}
}
static class Debug
{
static public void Draw2D<T>(T[,] map, int mode = 1)
{
#if DEBUG
int W = map.GetLength(0);
int H = map.GetLength(1);
string[,] map2 = new string[W + 1, H + 1];
for (int i = 0; i < W + 1; i++)
{
for (int j = 0; j < H + 1; j++)
{
if (i == 0 && j == 0) map2[i, j] = 0.ToString();
else if (i == 0) map2[i, j] = (j - 1).ToString();
else if (j == 0) map2[i, j] = (i - 1).ToString();
else map2[i, j] = map[i - 1, j - 1].ToString();
}
}
for (int i = 0; i < W + 1; i++)
{
for (int j = 0; j < H + 1; j++)
{
if (mode == 0) Console.Write(map2[i, j].Last());
if (mode == 1) Console.Write(map2[i, j] + " ");
}
Console.WriteLine();
}
Console.WriteLine();
#endif
}
public static void Draw1D<T>(T[] array, int mode = 0)
{
#if DEBUG
Console.WriteLine(string.Join(" ", array));
#endif
}
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll=long long;
#define int ll
#define FOR(i,a,b) for(int i=int(a);i<int(b);i++)
#define REP(i,b) FOR(i,0,b)
int read(){
int i;
scanf("%lld",&i);
return i;
}
int sign(int s){
return (s>0?1:-1);
}
signed main(){
// your code goes here
int N = read();
int a[N];
int sum[N]={0};
int count=0;
REP(i,N){
a[i] = read();
//cout << a[i];
}
if(a[0] == 0){
a[0] = -sign(a[0]);
count++;
}
sum[0] = a[0];
FOR(i,1,N){
sum[i] = sum[i-1]+a[i];
if(sum[i] == 0){
sum[i] -= sum[i-1];
count++;
}
else if(sign(sum[i])==sign(sum[i-1])){
count += abs(sum[i-1])+1;
sum[i] = sum[i]-a[i];
a[i] = -sign(sum[i])*(a[i]+sum[i]+1);
}
}
cout << count;
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | import sys
fastinput=sys.stdin.readline
n=int(fastinput())
ai=[int(i) for i in fastinput().split()]
#プラス始まり
goukei=0
sousa=0
for a in ai:
goukei+=a
if a%2:#even:plus
if goukei<=0:
sousa+=1-goukei
goukei=1
else:#odd:minus
if goukei>=0:
sousa+=goukei+1
goukei=-1
ans1=sousa
#マイナス始まり
goukei=0
sousa=0
for a in ai:
goukei+=a
if not a%2:#odd:plus
if goukei<=0:
sousa+=1-goukei
goukei=1
else:#even:minus
if goukei>=0:
sousa+=goukei+1
goukei=-1
ans2=sousa
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 | python3 | import numpy as np
n = int(input())
L = np.array([int(i) for i in input().split()])
if L[0] < 0:
L = -L
count = 0
s = L[0]
if L[0] == 0:
if L[1] > 0:
L[0] = -1
else:
L[0] = 1
count += 1
loopnum = n//2
if n%2 == 0:
loopnum -= 1
for i in range(loopnum):
s = s + L[2*i+1]
if s >= 0:
subt = s + 1
count += subt
s = s - subt
#print("s, count = {0}, {1}".format(s, count))
s = s + L[2*i+2]
if s <= 0:
subt = s - 1
count -= subt
s = s - subt
#print("s, count = {0}, {1}".format(s, count))
if n%2 == 0:
s = s + L[-1]
if s >= 0:
subt = s + 1
count += subt
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>
std::string IntToString(long long a) {
char x[100];
sprintf(x, "%lld", a);
std::string s = x;
return s;
}
long long StringToInt(std::string a) {
char x[100];
long long res;
strcpy(x, a.c_str());
sscanf(x, "%lld", &res);
return res;
}
inline void printIntVal(long long a) {
std::cout << "The Value is: " << IntToString(a) << std::endl;
}
inline void printStringVal(std::string a) {
std::cout << "The Value of is: " << a << std::endl;
}
inline std::string GetString() {
char x[1000005];
scanf("%s", x);
std::string s = x;
return s;
}
inline int GetInt() {
int x;
scanf("%d", &x);
return x;
}
inline std::string uppercase(std::string s) {
int n = (int)s.size();
for (int i = 0; i < n; i++)
if (s[i] >= 'a' && s[i] <= 'z') s[i] = s[i] - 'a' + 'A';
return s;
}
inline std::string lowercase(std::string s) {
int n = (int)s.size();
;
for (int i = 0; i < n; i++)
if (s[i] >= 'A' && s[i] <= 'Z') s[i] = s[i] - 'A' + 'a';
return s;
}
inline void OPEN(std::string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
inline long long CharToInt(char c) { return (c - '0'); }
inline void printVector(std::vector<int> in) {
std::cout << "[";
for (int i = 0; i < in.size(); i++) {
if (i == in.size() - 1) {
std::cout << in.at(i) << "]" << std::endl;
} else {
std::cout << in.at(i) << ",";
}
}
}
bool checkCons(std::vector<int> in) {
int sum = 0;
for (int i = 0; i < in.size(); i++) {
sum += in.at(i);
}
printVector(in);
std::cout << sum << std::endl;
if (sum == 0) {
return false;
}
int sum2 = 0;
for (int i = 0; i < in.size() - 1; i++) {
sum2 += in.at(i);
}
if ((sum >= 0 && sum2 < 0) || (sum < 0 && sum2 >= 0)) {
return true;
} else {
return false;
}
}
int main() {
int num_ops = 0;
std::vector<int> vals;
int n = GetInt();
for (int i = 0; i < n; i++) {
vals.push_back(GetInt());
}
bool pos;
int sum = 0;
for (int i = 0; i < vals.size() - 1; i++) {
sum += vals.at(i);
}
if (sum >= 0) {
pos = true;
} else {
pos = false;
}
int iter = 0;
while (!checkCons(vals)) {
num_ops++;
if (pos) {
vals.at(vals.size() - 1) -= 1;
} else {
vals.at(vals.size() - 1) += 1;
}
}
std::cout << num_ops << 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 | UNKNOWN | parseInt(x) = parse(Int, x)
function main()
n = readline() |> parseInt
a = map(parseInt, split(readline()))
b = Array{Int}(n)
b[1] = a[1]
k = 0
for i in 2:n
b[i] = a[i]+b[i-1]
if b[i]*b[i-1] >= 0
if b[i-1] < 0
k += abs(b[i]-1)
b[i] = 1
else
k += abs(b[i]+1)
b[i] = -1
end
end
end
c = Array{Int}(n)
l = 0
if a[1] > 0
c[1] = -1
l += abs(a[1]+1)
else
c[1] = 1
l += abs(a[1]-1)
end
for i in 2:n
c[i] = a[i]+c[i-1]
if c[i]*c[i-1] >= 0
if c[i-1] < 0
l += abs(c[i]-1)
c[i] = 1
else
l += abs(c[i]+1)
c[i] = -1
end
end
end
d = Array{Int}(n)
m = 0
if a[1] > 0
d[1] = 1
m += abs(a[1]-1)
else
d[1] = -1
m += abs(a[1]+1)
end
for i in 2:n
d[i] = a[i]+d[i-1]
if d[i]*d[i-1] >= 0
if d[i-1] < 0
m += abs(d[i]-1)
d[i] = 1
else
m += abs(d[i]+1)
m[i] = -1
end
end
end
print(min(k,l,m))
end
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() {
long long N;
cin >> N;
long long A[N];
for (long long i = 0; i < N; i++) cin >> A[i];
long long numSign = 1, sum = 0, actNum = 0;
for (long long i = 0; i < N; i++) {
if (A[i] > 0)
break;
else if (A[i] < 0) {
numSign *= -1;
break;
} else
numSign *= -1;
}
for (long long i = 0; i < N; i++) {
sum += A[i];
if (numSign == 1) {
if (sum <= 0) {
actNum += 1 - sum;
sum = 1;
}
} else {
if (sum >= 0) {
actNum += sum - -1;
sum = -1;
}
}
numSign *= -1;
}
cout << actNum << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int inf = 999999999;
const double pi = acos(-1);
long long a[100005] = {};
int main() {
long long n, ans = 0, wa = 0;
cin >> n;
for (int i = (0); i < (int)(n); i++) cin >> a[i];
wa = a[0];
for (int i = (1); i < (int)(n); i++) {
if (wa >= 0) {
long long tes = wa + a[i];
if (tes < 0) {
wa = tes;
} else {
ans += -(-1 - tes);
wa = -1;
}
} else {
long long tes = wa + a[i];
if (tes > 0) {
wa = tes;
} else {
ans += 1 - tes;
wa = 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 | UNKNOWN | program ec12;
var
a,s:array[0..100000] of longint;
n,m,i,j,ans:longint;
begin
readln(n);
ans:=0;
s[0]:=0;
for i:=1 to n do
read(a[i]);
for i:=1 to n do
begin
s[i]:=s[i-1]+a[i];
if i>1 then
begin
if s[i-1]<0 then
begin
if s[i]<=0 then
begin
if s[i]=0 then
begin
inc(ans);
s[i]:=1;
end
else
inc(ans,(-s[i])+1);
end;
end
else
begin
if s[i]>=0 then
begin
if s[i]=0 then
begin
inc(ans);
s[i]:=-1;
end
else
begin
inc(ans,s[i]+1);
s[i]:=-1;
end;
end;
end;
end
else
begin
if a[1]=0 then
begin
if a[2]>0 then
begin
inc(ans,a[2]+1);
s[1]:=-1;
end
else
begin
inc(ans,(-a[2])+1);
s[1]:=1;
end;
end;
end;
end;
writeln(ans);
end. |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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 gcd(int a, int b) { return (b == 0) ? a : gcd(b, a % b); }
int main() {
int n;
cin >> n;
;
vector<ll> a(n);
for (int i = 0; i < (n); i++) {
cin >> a[i];
};
ll ans = 0;
ll tempSum = a[0];
if (a[0] == 0) {
tempSum = 1;
ans = 1;
}
for (int i = 1; i < n; i++) {
if (tempSum > 0 && tempSum + a[i] >= 0) {
ans += fabs(tempSum + a[i]) + 1;
tempSum = -1;
} else if (tempSum < 0 && tempSum + a[i] <= 0) {
ans += fabs(tempSum + a[i]) + 1;
tempSum = 1;
} else {
tempSum += a[i];
}
}
ll ans1 = ans;
ans = 0;
tempSum = -a[0];
if (a[0] == 0) {
tempSum = -1;
ans = 1;
}
for (int i = 1; i < n; i++) {
if (tempSum > 0 && tempSum + a[i] >= 0) {
ans += fabs(tempSum + a[i]) + 1;
tempSum = -1;
} else if (tempSum < 0 && tempSum + a[i] <= 0) {
ans += fabs(tempSum + a[i]) + 1;
tempSum = 1;
} else {
tempSum += a[i];
}
}
cout << min(ans, ans1) << "\n";
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | def solve():
ans = 0
N = int(input())
A = list(map(int, input().split()))
total = A[0]
if total==0:
for i in range(1,N):
if A[i]!=0:
total = pow(-1,i)*A[i]//abs(A[i])
break
else:
return 2*N-1
for i in range(1,N):
new_total = total+A[i]
if total*(new_total)>=0:
new_total = (-1)*total//abs(total)
ans += abs(new_total-(total+A[i]))
total = new_total
return ans
print(solve()) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | using System;
using static System.Console;
using static System.Convert;
class Program
{
static void Main(string[] args)
{
var length = ToInt32(ReadLine());
long result = 0;
var nums = Array.ConvertAll(ReadLine().Split(' '), long.Parse);
var sum = nums[0];
if (sum == 0) { sum++; result++; }
var lastSum = sum;
for(var i = 1; i < length; i++)
{
sum += nums[i];
while (!IsDifferentSign(lastSum, sum)||sum==0)
{
if (!IsDifferentSign(lastSum, sum))
{ result += Math.Abs(sum) + 1; sum = lastSum > 0 ? -1 : 1;}
if (sum == 0) { sum = lastSum > 0 ? --sum : ++sum; result++; }
}
lastSum = sum;
}
WriteLine(result);
}
private static bool IsDifferentSign(long lastSum,long sum)
{
return (lastSum > 0 && sum < 0) || (lastSum < 0 && sum > 0);
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long num_operate(long n, long sum, long* a) {
long j;
for (long i = 1; i < n; i++) {
if (sum * (sum + a[i]) < 0)
sum += a[i];
else {
j += abs(sum + a[i]) + 1;
if (sum < 0)
sum = 1;
else if (sum > 0)
sum = -1;
}
}
return j;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
long n;
cin >> n;
vector<long> a(n);
for (long i = 0; i < n; i++) cin >> a[i];
long sum = a[0];
if (sum == 0) {
long cnt1 = num_operate(n, 1, &a.front());
long cnt2 = num_operate(n, -1, &a.front());
cout << min(cnt1, cnt2) << endl;
} else {
long cnt = num_operate(n, sum, &a.front());
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;
using ll = long long;
int main() {
int n;
cin >> n;
ll sum = 0;
cin >> sum;
ll ans = 0;
for (int i = 0; i < n - 1; ++i) {
int a;
cin >> a;
sum += a;
if (((sum - a > 0) == (sum > 0)) or ((sum - a < 0) == (sum < 0))) {
ll need = sum > 0 ? -(sum + 1) : -(sum - 1);
sum += need;
ans += abs(need);
} else if (sum == 0) {
ll need = sum - a > 0 ? -1 : 1;
sum += need;
ans += abs(need);
}
}
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 count(vector<int> a, bool positive) {
int total = 0, count = 0, diff = 0;
for (int i = 0; i < a.size(); i++) {
diff = 0;
if (positive && (a[i] + total <= 0)) {
diff = (1 - total) - a[i];
} else if (!positive && (a[i] + total >= 0)) {
diff = (-1 - total) - a[i];
}
positive = (positive) ? false : true;
a[i] += diff;
count += abs(diff);
total += a[i];
}
return count;
}
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
cout << min(count(a, true), count(a, false)) << 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()))
ans=0
tmp=0
for i in range(N):
tmp+=a[i]
if i%2==0:
if tmp<=0:
ans+=abs(tmp)+1
tmp=1
else:
if tmp>=0:
ans+=abs(tmp)+1
tmp=-1
tmp=0
tmp2=0
for i in range(N):
tmp+=a[i]
if i %2==0:
if tmp>=0:
tmp2+=abs(tmp)+1
tmp=-1
else:
if tmp<=0:
tmp2+=abs(tmp)+1
tmp=1
print(min(ans,tmp))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | def 解()
iN = int(input())
aA = [int(_) for _ in input().split()]
iL = len(aA)
iStart = 0
if sum(aA[0::2]) < sum(aA[2::2]):
iStart = 1
iC = 0
aD = [0]*iL
if 0 % 2 == iStart :
if aA[0] < 0:
aA[0] = 1
iC += -1 * aA[0] + 1
else:
if 0 < aA[0] :
aA[0] = -1
iC += aA[0] + 1
aD[0] = aA[0]
for i in range(1,iL):
aD[i] = aD[i-1]+aA[i]
if i % 2 == iStart:
if aD[i] <= 0:
iC += -1*aD[i] +1
aD[i] = 1
else:
if aD[i] >= 0:
iC += aD[i] +1
aD[i] = -1
print(iC)
解()
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = [int(x) for x in input().split()]
sum_a = 0
ans = 0
zero = 0 #数列aの先頭に並ぶ0の個数
for x in a:
if sum_a > 0 and sum_a + x >= 0:
ans += sum_a + x + 1 #sum_aを-1にするのにかかるコスト
sum_a = -1
elif sum_a < 0 and sum_a + x <= 0:
ans += -(sum_a + x) + 1 #sum_aを+1にするのにかかるコスト
sum_a = 1
else:
sum_a += x
if sum_a == 0:
zero += 1
if zero != 0:
ans += 2*zero - 1
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
scanf("%d", &n);
vector<int> a;
for (int i = 0; i < n; i++) {
int an;
scanf("%d", &an);
a.push_back(an);
}
if (a[0] != 0) {
long long op_count = 0;
long long now_sum = 0;
long long adding = a[0] > 0 ? -1 : 1;
for (int i = 0; i < n; i++) {
now_sum += a[i];
adding *= -1;
if (now_sum == 0) {
a[i] += adding;
now_sum += adding;
op_count++;
continue;
}
if (adding > 0) {
const long long last = 1 - now_sum;
if (last > 1) {
a[i] += last;
now_sum += last;
op_count += abs(last);
}
} else {
const long long last = -1 - now_sum;
if (last < -1) {
a[i] += last;
now_sum += last;
op_count += abs(last);
}
}
}
printf("%lld\n", op_count);
} else {
int n_copy = n;
vector<int> a_copy;
copy(a.begin(), a.end(), a_copy.begin());
long long final_op_count = INT_MAX;
for (int a0 = -1; a0 >= 1; a0 += 2) {
a[0] = a0;
n = n_copy;
copy(a_copy.begin(), a_copy.end(), a.begin());
long long op_count = 0;
long long now_sum = 0;
long long adding = a[0] > 0 ? -1 : 1;
for (int i = 0; i < n; i++) {
now_sum += a[i];
adding *= -1;
if (now_sum == 0) {
a[i] += adding;
now_sum += adding;
op_count++;
continue;
}
if (adding > 0) {
const long long last = 1 - now_sum;
if (last > 1) {
a[i] += last;
now_sum += last;
op_count += abs(last);
}
} else {
const long long last = -1 - now_sum;
if (last < -1) {
a[i] += last;
now_sum += last;
op_count += abs(last);
}
}
}
if (op_count < final_op_count) {
final_op_count = op_count;
}
}
printf("%lld\n", final_op_count);
}
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
long n;
scanf("%ld", &n);
long long a[n];
for (int i = 0; i < n; i++) scanf(" %lld", &a[i]);
int S = a[0];
int j = 0;
for (int i = 1; i < n; i++) {
if (S * (S + a[i]) < 0) {
S += a[i];
} else {
if (S < 0) {
j += 1 - S - a[i];
S = 1;
} else {
j += S + a[i] + 1;
S = -1;
}
}
}
printf("%d\n", j);
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 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{
static int[] dh = {0, 0, 1, -1, -1, -1, 1, 1};
static int[] dw = {-1, 1, 0, 0, -1, 1, -1, 1};
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long[] a = new long[n];
for(int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
long ans = 0;
long sum = 0;
for(int i = 0; i < n; i++) {
sum += a[i];
if(sum == 0) {
if(sum - a[i] < 0) sum += 1;
else sum -= 1;
ans++;
}
if(i == 0) continue;
if(sum - a[i] < 0 && sum < 0) {
ans += Math.abs(sum) + 1;
sum = 1;
}
else if(sum - a[i] > 0 && sum > 0){
ans += Math.abs(sum) + 1;
sum = -1;
}
}
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;
cin >> N;
vector<long long> L(N);
for (int i = 0; i < N; i++) {
cin >> L.at(i);
}
long long v = 0, res = 0, le = 0;
bool change_flag = true;
for (int i = 0; i < N; i++) {
if (i == 0) {
v = L.at(i);
} else {
if (v > 0 && v + L.at(i) >= 0) {
le = -1 - v - L.at(i);
L.at(i) += le;
v += L.at(i);
res += -le;
le = 0;
} else if (v < 0 && v + L.at(i) <= 0) {
le = 1 - v - L.at(i);
L.at(i) += le;
v += L.at(i);
res += le;
le = 0;
} else {
v += L.at(i);
}
}
}
cout << res << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
const int MGN = 8;
const int ARY_SZ_MAX = 10000000;
using namespace std;
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvvb = vector<vvb>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vd = vector<double>;
using vs = vector<string>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using psi = pair<string, int>;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
vl A(N);
for (int i = int(0); i < int(N); ++i) cin >> A[i];
ll ans = (INT_MAX / 2);
vl a(N, 0);
vl s(N, 0);
for (int i = int(0); i < int(N); ++i) a[i] = A[i];
ll cnt = 0;
s[0] = a[0];
if (s[0] <= 0) {
ll dif = abs(s[0]) + 1;
cnt += dif;
a[0] += dif;
s[0] = 1;
}
for (int i = int(1); i < int(N); ++i) {
s[i] = s[i - 1] + A[i];
if (i % 2 == 1 && s[i] >= 0) {
ll dif = abs(s[i]) + 1;
cnt += dif;
a[i] -= dif;
s[i] = -1;
} else if (i % 2 == 0 && s[i] <= 0) {
ll dif = abs(s[i]) + 1;
cnt += dif;
a[i] += dif;
s[i] = 1;
}
}
ans = min(ans, cnt);
for (int i = int(0); i < int(N); ++i) a[i] = A[i];
cnt = 0;
s[0] = A[0];
if (s[0] >= 0) {
ll dif = abs(s[0]) + 1;
cnt += dif;
a[0] -= dif;
s[0] = -1;
}
for (int i = int(1); i < int(N); ++i) {
s[i] = s[i - 1] + A[i];
if (i % 2 == 1 && s[i] <= 0) {
ll dif = abs(s[i]) + 1;
cnt += dif;
a[i] += dif;
s[i] = 1;
} else if (i % 2 == 0 && s[i] >= 0) {
ll dif = abs(s[i]) + 1;
cnt += dif;
a[i] -= dif;
s[i] = -1;
}
}
ans = min(ans, cnt);
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n=int(input())
A=list(map(int,input().split()))
b=0
count=0
x=0
if A[0]>0:
x=-1
else:
x=1
for a in A:
b+=a
if x==1:
if b<0:
x=-1
else:
count+=1+b
b=-1
x=-1
else:
if b>0:
x=1
else:
count+=1-b
b=1
x=1
print(count) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | fun main(args: Array<String>) {
val n = readLine()!!.toInt()
val l = readLine()!!.split(" ").map { it.toInt() }
var s_pls = 0
var s_mns = 0
var tmp = 0
var pls_cnt = 0
var mns_cnt = 0
for(i in 0 until n){
s_pls += l[i]
s_mns += l[i]
if(i % 2 == 0){
if(s_pls <= 0){
pls_cnt += 1 - s_pls
s_pls = 1
}
if(s_mns >= 0){
mns_cnt += 1 + s_mns
s_mns = -1
}
}
else{
if(s_mns <= 0){
mns_cnt += 1 - s_mns
s_mns = 1
}
if(s_pls >= 0){
pls_cnt += 1 + s_pls
s_pls = -1
}
}
}
println(Math.min(Math.abs(pls_cnt), Math.abs(mns_cnt)))
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.Scanner;
public class Main {
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
int count = 0;
int l[] = new int[scanner.nextInt()];
int x[] = new int[l.length];
for (int i = 0;i < l.length;++i){
l[i] = Integer.valueOf(scanner.next());
if(i > 0){
x[i] = l[i] + x[i - 1];
}
else{
x[i] = l[i];
}
}
for (int i = 1;i < l.length;++i){
int p = x[i - 1];
int q = x[i];
if(q == 0||(q < 0&&p < 0)||(q > 0&&p > 0)){
int c = 1 + ((p > 0) ? 1 : -1) * q;
count += c;
int d = ((p > 0) ? -1 : 1) * c;
l[i] += d;
for (int j = i;j < l.length;++j){
x[j] += d;
}
}
}
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 | UNKNOWN | #include <bits/stdc++.h>
int main(void) {
int num_integer;
if (scanf("%d", &num_integer) != 1) {
puts("num_integer input error.");
return 1;
}
int integer, sum_p = 0, sum_m = 0;
int unsigned op_count_p = 0, op_count_m = 0;
for (int i = 0; i < num_integer; i++) {
if (scanf("%d", &integer) != 1) {
puts("integer input error.");
}
sum_p += integer;
sum_m += integer;
if (i % 2 == 0) {
if (sum_p <= 0) {
op_count_p += -sum_p + 1;
sum_p = 1;
}
if (sum_m >= 0) {
op_count_m += sum_m + 1;
sum_m = -1;
}
} else {
if (sum_p >= 0) {
op_count_p += sum_p + 1;
sum_p = -1;
}
if (sum_m <= 0) {
op_count_m += -sum_m + 1;
sum_m = 1;
}
}
}
printf("%u", (op_count_p < op_count_m) ? op_count_p : op_count_m);
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], cntplus = 0, cntplus1 = 0, cntminus = 0, cntminus1 = 0;
for (int i = 0; i < n; i++) cin >> a[i];
if (a[0] > 0) {
int s = a[0];
for (int i = 1; i < n; i++) {
if (i % 2) {
if (0 <= s + a[i]) {
cntplus += s + a[i] + 1;
a[i] = -1 - s;
}
} else {
if (s + a[i] <= 0) {
cntplus += 1 - (s + a[i]);
a[i] = 1 - s;
}
}
s += a[i];
}
s = -1;
cntplus1 += a[0] + 1;
for (int i = 1; i < n; i++) {
if (i % 2) {
if (s + a[i] <= 0) {
cntplus1 += 1 - (s + a[i]);
a[i] = 1 - s;
}
} else {
if (0 <= s + a[i]) {
cntplus1 += s + a[i] + 1;
a[i] = -1 - s;
}
}
s += a[i];
}
cout << min(cntplus, cntplus1) << endl;
} else {
int s = a[0];
for (int i = 1; i < n; i++) {
if (i % 2) {
if (s + a[i] <= 0) {
cntminus += 1 - (s + a[i]);
a[i] = 1 - s;
}
} else {
if (0 <= s + a[i]) {
cntminus += s + a[i] + 1;
a[i] = -1 - s;
}
}
s += a[i];
}
s = 1;
cntminus1 += -a[0] + 1;
for (int i = 1; i < n; i++) {
if (i % 2) {
if (0 <= s + a[i]) {
cntminus1 += s + a[i] + 1;
a[i] = -1 - s;
}
} else {
if (s + a[i] <= 0) {
cntminus1 += 1 - (s + a[i]);
a[i] = 1 - s;
}
}
s += a[i];
}
cout << min(cntminus, cntminus1) << 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 -*-
"""
https://abc059.contest.atcoder.jp/tasks/arc072_a
WA
"""
import sys
from sys import stdin
input = stdin.readline
def check_sign(n):
if n > 0:
return 1
elif n < 0:
return -1
else:
return 0
def solve(A):
p_ans = 0
n_ans = 0
i_ans = 0
# 最初をプラス側に振った場合の解
total = A[0]
prev_sign = check_sign(total)
if prev_sign == 0:
p_ans += 1
total += 1
prev_sign = 1
for a in A[1:]:
total += a
sign = check_sign(total)
if sign == 0:
total -= prev_sign
p_ans += 1
elif prev_sign != sign:
prev_sign = sign
else:
p_ans += (abs(total) + 1)
if prev_sign < 0:
total = 1
prev_sign = 1
else:
total = -1
prev_sign = -1
# 最初をマイナス側に振った場合の解
total = A[0]
prev_sign = check_sign(total)
if prev_sign == 0:
n_ans += 1
total -= 1
prev_sign = -1
for a in A[1:]:
total += a
sign = check_sign(total)
if sign == 0:
total -= prev_sign
n_ans += 1
elif prev_sign != sign:
prev_sign = sign
else:
n_ans += (abs(total) + 1)
if prev_sign < 0:
total = 1
prev_sign = 1
else:
total = -1
prev_sign = -1
#
total = A[0]
prev_sign = check_sign(total)
if prev_sign == 0:
i_ans += 1
total += 1
prev_sign = 1
else:
i_ans += (total + 1)
if prev_sign > 0:
prev_sign = -1
total = -1
else:
prev_sign = 1
total = 1
for a in A[1:]:
total += a
sign = check_sign(total)
if sign == 0:
total -= prev_sign
i_ans += 1
elif prev_sign != sign:
prev_sign = sign
else:
i_ans += (abs(total) + 1)
if prev_sign < 0:
total = 1
prev_sign = 1
else:
total = -1
prev_sign = -1
return min(p_ans, n_ans, i_ans)
def main(args):
n = int(input())
A = [int(x) for x in input().split()]
ans = solve(A)
print(ans)
if __name__ == '__main__':
main(sys.argv[1:])
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int sum(int k, vector<int> &bit) {
k++;
int s = 0;
while (k >= 1) {
s += bit[k - 1];
k -= k & -k;
}
return s;
}
int add(int k, int x, int n, vector<int> &bit) {
k++;
while (k <= n) {
bit[k - 1] += x;
k += k & -k;
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, ans = 0, s = 0, aux;
bool swtc;
cin >> n;
vector<int> nums(n), soma(n), bit(n + 1, 0);
for (int i = 0; i < n; i++) {
cin >> nums[i];
add(i, nums[i], n, bit);
}
if (sum(0, bit) > 0)
swtc = true;
else
swtc = false;
for (int i = 1; i < n; i++) {
aux = sum(i, bit);
if (aux > 0 and !swtc)
swtc = true;
else if (aux < 0 and swtc)
swtc = false;
else {
if (swtc) {
ans += aux + 1;
add(i, -(aux + 1), n, bit);
swtc = false;
} else {
ans += abs(aux - 1);
add(i, abs(aux - 1), n, bit);
swtc = true;
}
}
}
cout << ans << "\n";
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long array[n];
for (int i = 0; i < n; ++i) cin >> array[i];
int min1 = 0;
int sum1 = 0;
for (int i = 0; i < n; ++i) {
sum1 += array[i];
if (i % 2 == 0) {
if (sum1 <= 0) {
min1 += abs(sum1) + 1;
sum1 = 1;
}
} else {
if (sum1 >= 0) {
min1 += abs(sum1) + 1;
sum1 = -1;
}
}
}
int min2 = 0;
int sum2 = 0;
for (int i = 0; i < n; ++i) {
sum2 += array[i];
if (i % 2 == 1) {
if (sum2 <= 0) {
min2 += abs(sum2) + 1;
sum2 = 1;
}
} else {
if (sum2 >= 0) {
min2 += abs(sum2) + 1;
sum2 = -1;
}
}
}
cout << min(min1, min2) << 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()))
cur=A[0]
ans=0
isplus=True
ind=1
if cur<0:
isplus=False
if cur==0:
# すべて0の場合
allzero=True
firstNotZero=0
firstNotZeroInd=0
for i in range(N):
if A[i]!=0:
firstNotZero=A[i]
firstNotZeroInd=i
allzero=False
break
if allzero:
print(2*N-1)
exit(0)
# 0以外が出てくる場合
ans+=(firstNotZeroInd)*2-1
if firstNotZero>0:
cur=-1
ind=firstNotZeroInd
isplus=False
else:
cur=1
ind=firstNotZeroInd
isplus=True
for i in range(ind,N):
if isplus:
if cur+A[i]>=0:
diff=abs((cur+A[i])-(-1))
ans+=diff
cur=-1
else:
cur+=A[i]
isplus=False
else:
if cur+A[i]<=0:
diff=abs((cur+A[i])-1)
ans+=diff
cur=1
else:
cur+=A[i]
isplus=True
print(ans)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <bits/stdc++.h>
int main() {
int n, a[100010];
long sgn = 1, cont = 0, ans = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
cont += a[0];
for (int i = 1; i < n; i++) {
cont += a[i];
if (cont * sgn < 0) {
sgn = -1 * sgn;
} else if (cont * sgn >= 0) {
ans = ans + 1 + cont * sgn;
sgn = -1 * sgn;
cont = sgn;
}
}
printf("%ld", ans);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
static const int NIL = -1;
int n;
static const int pos = 1;
static const int neg = -1;
bool judge(int tmp) {
if (tmp > 0)
return true;
else
return false;
}
int main(int argc, char const *argv[]) {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> n;
int a[n];
for (int i = (0); i < (n); ++i) cin >> a[i];
int tmp = 0;
int sum = 0;
tmp = a[0];
bool sign;
sign = judge(tmp);
for (int i = (1); i < (n); ++i) {
if (sign) {
tmp += a[i];
if (tmp >= 0) {
sum += abs(neg - tmp);
tmp = neg;
}
sign = judge(tmp);
} else {
tmp += a[i];
if (tmp <= 0) {
sum += abs(pos - tmp);
tmp = pos;
}
sign = judge(tmp);
}
}
cout << sum << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <bits/stdc++.h>
int main() {
int n;
scanf("%d", &n);
long long a[n];
for (int i = 0; i < n; i++) scanf("%lld", a + i);
long long initial_plus = ((-1 - a[0]) > (0) ? (-1 - a[0]) : (0));
long long initial_minus = ((1 + a[0]) > (0) ? (1 + a[0]) : (0));
long long sum = 0;
sum = ((a[0]) > (1) ? (a[0]) : (1));
for (int i = 1; i < n; i++) {
if ((sum + a[i]) * sum >= 0ll) {
if (sum < 0ll) {
initial_plus += 1 - sum - a[i];
sum = 1;
} else {
initial_plus += sum + a[i] + 1;
sum = -1;
}
} else {
sum += a[i];
}
}
sum = ((a[0]) > (-1) ? (-1) : (a[0]));
for (int i = 1; i < n; i++) {
if ((sum + a[i]) * sum >= 0ll) {
if (sum < 0ll) {
initial_minus += 1 - sum - a[i];
sum = 1;
} else {
initial_minus += sum + a[i] + 1;
sum = -1;
}
} else {
sum += a[i];
}
}
printf("%lld\n",
((initial_plus) > (initial_minus) ? (initial_minus) : (initial_plus)));
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> A(n, 0);
for (int i = 0; i < n; i++) cin >> A[i];
int cnt = 0, acm = 0, ans = 0;
for (int i = 0; i < n; i++) {
if (i % 2) {
if (acm + A[i] > 0)
acm += A[i];
else {
cnt += abs(acm + A[i]) + 1;
acm = 1;
}
} else {
if (acm + A[i] < 0)
acm += A[i];
else {
cnt += abs(acm + A[i]) + 1;
acm = -1;
}
}
}
ans = cnt;
cnt = 0;
acm = 0;
for (int i = 0; i < n; i++) {
if ((i + 1) % 2) {
if (acm + A[i] > 0)
acm += A[i];
else {
cnt += abs(acm + A[i]) + 1;
acm = 1;
}
} else {
if (acm + A[i] < 0)
acm += A[i];
else {
cnt += abs(acm + A[i]) + 1;
acm = -1;
}
}
}
ans = min(ans, cnt);
cout << ans << "\n";
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(map(int, input().split()))
p = a[0]
result = 0
for i in range(1, n):
t = p + a[i]
if p < 0 and t <= 0:
result += 1 - t
t = 1
elif p > 0 and t >= 0:
result += t + 1
t = -1
p = t
print(result)
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n=int(input())
a=list(map(int,input().split()))
cnt1=cnt2=sm1=sm2=0
for x,y in enumerate(a):
sm1+=y
if x%2 ==1:
if sm1<1:
cnt1 += 1-sm1
sm1=1
elif sm1>-1:
cnt2+= 1+sm2
sm2=-1
for x,y in enumerate(a):
sm2+=y
if x%2 ==0:
if sm2<1:
cnt2 += 1-sm2
sm2=1
elif sm1>-1:
cnt1 += 1+sm1
sm1=-1
print(min(cnt1,cnt2)) |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, temp;
long long count = 0;
long a[100000];
cin >> n;
long long sum = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n - 1; i++) {
sum += a[i];
if (sum + a[i + 1] == 0) {
if (sum > 0) {
a[i + 1] -= 1;
count += 1;
} else {
a[i + 1] += 1;
count += 1;
}
}
if (sum > 0 && sum + a[i + 1] > 0) {
temp = a[i + 1];
a[i + 1] = sum * (-1) - 1;
count += abs(a[i + 1] - temp);
} else if (sum < 0 && sum + a[i + 1] < 0) {
temp = a[i + 1];
a[i + 1] = 1 + sum * (-1);
count += abs(a[i + 1] - temp);
}
}
cout << count << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <bits/stdc++.h>
int main(void) {
long long int i, a, n, num;
long long int sum = 0, bsum = 0, ans = 0, m = 0;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &a);
bsum = sum;
sum += a;
if (bsum > 0) {
if (sum > 0) {
num = sum;
do {
num--;
ans++;
m++;
} while (num >= 0);
sum -= m;
m = 0;
}
if (sum = 0) {
ans++;
sum -= 1;
}
}
if (bsum < 0) {
if (sum < 0) {
num = sum;
do {
num++;
ans++;
m++;
} while (num <= 0);
sum += m;
m = 0;
}
if (sum = 0) {
ans++;
sum += 1;
}
}
}
printf("%d\n", ans);
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<long long>;
using vvl = vector<vl>;
using P = pair<long long, long long>;
using PP = pair<long long, P>;
using vp = vector<P>;
using vpp = vector<PP>;
using vs = vector<string>;
template <class T>
bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T>
bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
const long long MOD = 1000000007LL;
const int INF = 1 << 30;
const long long LINF = 1LL << 60;
int main() {
int n;
cin >> n;
vl vec(n);
for (int i = (0); i < (n); i++) {
cin >> vec[i];
}
long long ans = LINF;
long long sum = 0;
long long cnt = 0;
for (int i = (0); i < (n); i++) {
sum += vec[i];
if (i % 2) {
if (sum >= 0) {
sum = -1;
cnt += abs(sum) + 1;
}
} else {
if (sum <= 0) {
sum = 1;
cnt += abs(sum) + 1;
}
}
}
chmin(ans, cnt);
cerr << cnt << endl;
sum = 0;
cnt = 0;
for (int i = (0); i < (n); i++) {
sum += vec[i];
if (i % 2) {
if (sum <= 0) {
cnt += abs(sum) + 1;
sum = 1;
}
} else {
if (sum >= 0) {
cnt += abs(sum) + 1;
sum = -1;
}
}
}
chmin(ans, cnt);
cerr << cnt << endl;
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 vi = vector<int>;
int solve(int ans, int S, vi &a, int N) {
for (int i = (1); i < (N); ++i) {
if (S > 0) {
S += a.at(i);
if (S >= 0) {
ans += (S + 1);
S = -1;
}
} else {
S += a.at(i);
if (S <= 0) {
ans += (-S + 1);
S = 1;
}
}
}
return ans;
}
int main() {
int N;
cin >> N;
vi a(N);
for (int i = (0); i < (N); ++i) {
cin >> a.at(i);
}
int ans = 0;
int S = a.at(0);
if (S == 0) {
ans++;
ans = min(solve(ans, -1, a, N), solve(ans, 1, a, N));
} else {
ans = solve(ans, S, a, N);
}
cout << ans << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <vector>
#include <iostream>
#include <utility>
#include <algorithm>
#include <string>
#include <deque>
#include <queue>
#include <tuple>
#include <queue>
#include <functional>
#include <cmath>
#include <iomanip>
#include <map>
#include <set>
#include <numeric>
#include <unordered_map>
#include <unordered_set>
#include <complex>
#include <iterator>
#include <array>
#include <memory>
#include <stack>
#define vi vector<int>
#define vvi vector<vector<int> >
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,s,n) for(int i=s;i<n;i++)
#define all(in) in.begin(), in.end()
template<class T, class S> void cmin(T &a, const S &b) { if (a > b)a = b; }
template<class T, class S> void cmax(T &a, const S &b) { if (a < b)a = b; }
#define MAX 9999999
using namespace std;
typedef pair<int, int> pii;
typedef pair<double,double>pdd;
typedef pair<ll,ll>pll;
#define int ll
signed main(){
int n; cin>>n;
vi v(n);
bool flag = false;
rep(i,n)cin>>v[i];
vi sum(n);
int ans=0;
rep(i,n)sum[i]=v[i];
rep(i,n){
if(!i){
if(sum[0]>0)flag=true;
else if(sum[0]<0)flag=false;
else {
sum[0]=1;
ans++;
flag=true;
}
continue;
}
sum[i]+=sum[i-1];
if(flag){
if(sum[i]<0)flag=false;a
else{
ans+=(abs(sum[i])+1);
sum[i]= -1;
flag=false;
}
}else{
if(sum[i]>0)flag=true;
else {
ans+=(abs(sum[i])+1);
sum[i]= 1;
flag=true;
}
}
}
//rep(i,n)cout<<sum[i]<<" ";
//cout<<endl;
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>
int main() {
int n;
std::cin >> n;
int counter = 0;
long seq = 0;
scanf("%d ", &seq);
long first_sum = seq;
bool prevsign = first_sum >= 0 ? true : false;
for (int i = 1; i < n; i++) {
scanf("%d", &seq);
if (i < n - 1) scanf(" ");
if (!(prevsign ^ (first_sum + seq > 0 ? true : false)) ||
!(first_sum + seq)) {
long nseq = (!prevsign ? 1 : -1) - first_sum;
counter += (int)abs(nseq - seq);
first_sum += nseq;
} else
first_sum += seq;
prevsign = !prevsign;
}
std::cout << counter;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | from strutils import split, parseInt, parseFloat
from sequtils import map
import macros
macro unpack*(input: seq; count: static[int]): untyped =
result = quote do: ()
when NimMinor <= 13: # 本当にここが区切りかどうかは知らない
for i in 0..<count: result[0].add quote do: `input`[`i`]
else:
for i in 0..<count: result.add quote do: `input`[`i`]
# count == 0 のとき unpackしない
# count > 0 のとき count個分 unpack した結果の tuple を返す
type UnselectableTypeError = object of Exception
template input(typ: typedesc; count: static[Natural] = 0): untyped =
let line = stdin.readLine.split
when count == 0:
when typ is int: line.map(parseInt)
elif typ is float: line.map(parseFloat)
elif typ is string: line
else: raise newException(UnselectableTypeError, "You selected a type other than int, float or string")
else:
when typ is int: line.map(parseInt).unpack(count)
elif typ is float: line.map(parseFloat).unpack(count)
elif typ is string: line.unpack(count)
else: raise newException(UnselectableTypeError, "You selected a type other than int, float or string")
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
from math import nextPowerOfTwo
from sequtils import newSeqWith
type SegmentTree*[T: SomeNumber] = ref object of RootObj
tree: seq[T]
leafCount: Natural
initValue: T
mergeProc: proc (x, y: T): T {.closure.}
proc merge[T](this: SegmentTree[T]; l, r: Natural): T =
this.mergeProc(this.tree[l], this.tree[r])
proc toSegmentTree*[T](a: openArray[T]; initValue: T; mergeProc: proc (x, y: T): T {.closure.}): SegmentTree[T] =
let leafCount = a.len.nextPowerOfTwo
result = SegmentTree[T](tree: newSeqWith[T](2 * leafCount - 1, initValue),
leafCount: leafCount,
initValue: initValue,
mergeProc: mergeProc)
for i, ai in a:
result.tree[i + result.leafCount - 1] = ai
for i in countdown(result.leafCount - 2, 0):
result.tree[i] = result.merge(2 * i + 1, 2 * i + 2)
proc update*[T](this: SegmentTree[T]; i, v: int): SegmentTree[T] =
result = this
var j = result.leafCount + i - 1
result.tree[j] = v
while j > 0:
j = (j - 1) div 2
result.tree[j] = result.merge(2 * j + 1, 2 * j + 2)
proc update*[T](this: var SegmentTree[T]; i, v: int) =
var j = this.leafCount + i - 1
this.tree[j] = v
while j > 0:
j = (j - 1) div 2
this.tree[j] = this.merge(2 * j + 1, 2 * j + 2)
proc query*[T](this: SegmentTree[T]; requiredRange: Slice[int]; k = 0; coveredRange: Slice[Natural] = 0.Natural..int.high.Natural): T =
let
l = coveredRange.a
r = coveredRange.b
if r == int.high and this.leafCount != int.high:
return this.query(requiredRange, k, l..(this.leafCount - 1).Natural)
if r < requiredRange.a or requiredRange.b < l:
return this.initValue
if requiredRange.a <= l and r <= requiredRange.b:
return this.tree[k]
let
lv = this.query(requiredRange, 2 * k + 1, l..((l + r) div 2).Natural)
rv = this.query(requiredRange, 2 * k + 2, ((l + r + 1) div 2).Natural..r)
return this.mergeProc(lv, rv)
let
n = input(int, 1)
var
a = input(int, 0).toSegmentTree(0, proc (x, y: int): int = x + y)
result = 0
for i in 1..<n:
let
sum = a.query(0..i)
preSum = a.query(0..(i - 1))
if sum > 0 and preSum > 0 or sum < 0 and preSum < 0 or sum == 0:
let
p = preSum.abs - 1
c = sum.abs - p + 1
result += p + c
if sum <= 0:
a.update(i - 1, a.tree[a.leafCount + i - 2] + p)
a.update(i, a.tree[a.leafCount + i - 1] + c)
else:
a.update(i - 1, a.tree[a.leafCount + i - 2] - p)
a.update(i, a.tree[a.leafCount + i - 1] - c)
echo result
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long INF = 10E9;
const long long MOD = 1000000007;
const long double PI = 3.1415926;
template <class T>
T &chmin(T &a, const T &b) {
return a = min(a, b);
}
template <class T>
T &chmax(T &a, const T &b) {
return a = max(a, b);
}
long long int n, m, k, ans = 0, sum = 0, cnt = 0;
string s;
int main() {
long long int n;
cin >> n;
vector<long long int> acc(n);
long long int x = 0;
for (long long int i = (long long int)(0); i < (long long int)(n); i++) {
cin >> acc[i];
acc[i] += x;
x = acc[i];
}
bool minus = acc[0] > 0;
long long int tmp = 0;
for (long long int i = (long long int)(1); i < (long long int)(n); i++) {
if ((minus && acc[i] + tmp >= 0) || (!minus && acc[i] + tmp <= 0)) {
ans += llabs(acc[i] + tmp) + 1;
if (!minus)
tmp += (llabs(acc[i] + tmp) + 1);
else
tmp -= (llabs(acc[i] + tmp) + 1);
}
minus = !minus;
}
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;
int64_t min(int64_t a, int64_t b) {
if (a > b) {
return b;
} else {
return a;
}
}
int64_t solve(vector<int> a) {
bool nextposi = (a.at(0) < 0);
int64_t ans = 0;
int64_t sum = a.at(0);
for (int i = 1; i < a.size(); i++) {
sum += a.at(i);
if (nextposi != (sum > 0)) {
if (nextposi == 1) {
ans += abs(sum - 1);
sum = 1;
} else {
ans += abs(sum + 1);
sum = -1;
}
}
nextposi = !nextposi;
}
return ans;
}
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a.at(i);
}
int64_t ans = 0;
if (a.at(0) == 0) {
a.at(0) = 1;
ans = solve(a);
a.at(0) = -1;
ans = min(ans, solve(a)) + 1;
} else {
ans = solve(a);
}
cout << ans << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, ansa = 0, ansb = 0, suma = 0, sumb = 0;
cin >> n;
for (int i = 0; i < (n); i++) {
int c;
cin >> c;
if (i % 2 == 0) {
if (suma + c <= 0) {
ansa += 1 - c - suma;
suma = 1;
}
if (sumb + c >= 0) {
ansb += sumb + c + 1;
sumb = -1;
}
} else {
if (suma + c >= 0) {
ansa += suma + c + 1;
suma = 1;
}
if (sumb + c <= 0) {
ansb += 1 - c - sumb;
sumb = -1;
}
}
}
cout << min(ansa, ansb) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.util.Scanner;
class Main {
int n;
int[] a;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Main m = new Main(sc);
m.solve();
sc.close();
}
Main(Scanner sc) {
n = sc.nextInt();
a = new int[n];
for(int i=0;i<n;i++){
a[i] = sc.nextInt();
}
}
void solve() {
int sign = (a[0]>=0)?1:-1;
long cnt = (a[0]==0)?1:0;
long sum = (a[0]==0)?1:a[0];
for(int i=1;i<n;i++){
sum += a[i];
if(sum*sign>=0){
cnt += Math.abs(sum) + 1;
sum = -sign;
}
sign *= -1;
}
System.out.println(cnt);
}
} |
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
int main() {
int n;
std::cin >> n;
int64_t a[n];
for (int i = 0; i < n; ++i) {
std::cin >> a[i];
}
size_t cnt1 = 0;
int64_t sum = 0;
bool plus = true;
for (int i = 0; i < n; ++i) {
sum += a[i];
if (plus) {
if (sum == 0) {
++cnt1;
sum += 1;
} else if (sum < 0) {
cnt1 += (-sum + 1);
sum = 1;
}
plus = false;
} else {
if (sum == 0) {
--cnt1;
sum -= 1;
} else if (sum > 0) {
cnt1 += (sum + 1);
sum = -1;
}
plus = true;
}
}
size_t cnt2 = 0;
sum = 0;
plus = false;
for (int i = 0; i < n; ++i) {
sum += a[i];
if (plus) {
if (sum == 0) {
++cnt2;
sum += 1;
} else if (sum < 0) {
cnt2 += (-sum + 1);
sum = 1;
}
plus = false;
} else {
if (sum == 0) {
--cnt2;
sum -= 1;
} else if (sum > 0) {
cnt2 += (sum + 1);
sum = -1;
}
plus = true;
}
}
std::cout << std::min(cnt1, cnt2) << 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 | 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();
boolean nextPlusF = al[0] < 0;
long sum2;
long ans2;
if (nextPlusF){
sum2 = 1;
ans2 = 1 - (al[0]);
}else{
sum2 = -1;
ans2 = al[0] + 1;
}
long ans = 0;
long sum = al[0];
for(int i=1;i<n;i++){
sum += al[i];
if(nextPlusF && sum <=0){
ans += 1-sum;
sum += 1-sum;
}else if ((! nextPlusF) && sum >= 0){
ans += sum +1;
sum -= sum +1;
}
nextPlusF = !nextPlusF;
}
nextPlusF = !(al[0] < 0);
for(int i=1;i<n;i++){
sum2 += al[i];
if(nextPlusF && sum2 <=0){
ans2 += 1-sum2;
sum2 += 1-sum2;
}else if ((! nextPlusF) && sum2 >= 0){
ans2 += sum2 +1;
sum2 -= sum2 +1;
}
nextPlusF = !nextPlusF;
}
System.out.println(Math.min(ans,ans2));
}
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
int ans = 0;
int ans2 = 0;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int dp[10010];
dp[0] = 0;
bool flag = true;
for (int i = 0; i < n; i++) {
if (flag == true && dp[i] + a[i] >= 0) {
dp[i + 1] = -1;
ans += abs(a[i] - (-1 - dp[i]));
} else if (flag == false && dp[i] + a[i] <= 0) {
dp[i + 1] = 1;
ans += abs(a[i] - (1 - dp[i]));
} else {
dp[i + 1] = dp[i] + a[i];
}
flag = !flag;
}
flag = false;
for (int i = 0; i < n; i++) {
if (flag == true && dp[i] + a[i] >= 0) {
dp[i + 1] = -1;
ans2 += abs(a[i] - (-1 - dp[i]));
} else if (flag == false && dp[i] + a[i] <= 0) {
dp[i + 1] = 1;
ans2 += abs(a[i] - (1 - dp[i]));
} else {
dp[i + 1] = dp[i] + a[i];
}
flag = !flag;
}
cout << min(ans, ans2) << endl;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | def main():
_ = input()
a = tuple(int(s) for s in input().split())
print(solve(a))
def solve(a):
if a[0] > 0:
return min(rec(a[0], a[1:], 0), rec(-1, a[1:], a[0] + 1))
else:
return min(rec(a[0], a[1:], 0), rec(1, a[1:], 1 - a[0]))
def rec(s, a, r):
if not a:
return r
elif s < 0:
n = max(s + a[0], 1)
return rec(n, a[1:], r + (n - (s + a[0])))
else:
n = min(s + a[0], -1)
return rec(n, a[1:], r + s + a[0] - n)
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<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<string>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll INFF=0x3f3f3f3f3f3f3f3f;
ll a[1000010];
int n;
ll solve()
{
ll sum=0;
ll oo=a[0];
for(int i=1;i<n;i++)
{
if(oo<0)
{
oo+=a[i];
if(oo<=0)
{
sum+=1-oo;
oo=1;
}
continue;
}
else
{
oo+=a[i];
if(oo>=0)
{
sum+=oo+1;
oo=-1;
}
}
}
return sum;
}
int main()
{
scanf("%d",&n);
ll sum=0;
for(int i=0;i<n;i++)
{
scanf("%lld",&a[i]);
}
if(a[0]==0)
{
a[0]=1;
ll sum1=solve();
a[0]=-1;
ll sum2=solve();
sum=min(sum1,sum2)+1;
}
else
{
ll sum0=solve();
a[0]=1;
ll sum1=solve()+abs(1-a[0]);
a[0]=-1;
ll sum2=solve()+abs(-1-a[0]);
sum=min(sum0,min(sum1,sum2));
}
printf("%lld\n",sum);
}
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | #!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():
n = II()
a = LI()
if a[0] == 0:
suma = 1
b = 1
else:
suma = a[0]
b = 0
for i in a[1:]:
if (suma + i) * suma < 0:
suma += i
continue
b += abs(suma + i) + 1
suma = -1 * (suma > 0) or 1
ans = b
if a[0] == 0:
suma = -1
b = 1
else:
suma = -a[0]
b = 2 * abs(a[0])
for i in a[1:]:
if (suma + i) * suma < 0:
suma += i
continue
suma = -1 * (suma > 0) or 1
b += abs(suma + i) + 1
print(min(ans,b))
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 | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> v(n);
for (int i = 0; i < (int)(n); i++) cin >> v[i];
int ans = 0;
int crnt = v[0];
int pre = v[0];
if (v[0] < 0) {
ans += abs(v[0]) + 1;
crnt = 1;
pre = 1;
}
for (int i = 1; i < n; i++) {
pre = crnt;
crnt += v[i];
if (crnt * pre >= 0) {
if (pre > 0) {
ans += crnt + 1;
crnt -= crnt + 1;
} else {
ans += abs(crnt) + 1;
crnt += abs(crnt) + 1;
}
}
}
int fans = 0;
crnt = v[0];
pre = v[0];
if (crnt > 0) {
fans += v[0] + 1;
crnt = -1;
pre = -1;
}
for (int i = 1; i < n; i++) {
pre = crnt;
crnt += v[i];
if (crnt * pre >= 0) {
if (pre > 0) {
fans += crnt + 1;
crnt -= crnt + 1;
} else {
fans += abs(crnt) + 1;
crnt += abs(crnt) + 1;
}
}
}
cout << min(fans, ans) << endl;
return 0;
}
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | n = gets.to_i
a_list = gets.split.map(&:to_i)
count = 0
sum = nil
a_list.each.with_index do |a, i|
if i == 0
if a == 0
sum = a_list[1] > 0 ? -1 : 1
count += 1
else
sum = a
end
else
if sum > 0
# plus to minus
if a >= 0
count += (a + sum) + 1
sum = -1
else
if sum + a < 0
sum += a
else
count += (sum + a) + 1
sum = -1
end
end
else
# minus to plus
if a >= 0
if sum + a > 0
sum += a
else
count += (sum + a).abs + 1
sum = -1
end
else
count += (a + sum).abs + 1
sum = 1
end
end
end
end
puts count
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | n = int(input())
a = list(int(i) for i in input().split())
b = [i for i in a]
def solve(cnt,A,N):
for i in range(1, N):
if sum(A[0:i])>0:
if sum(A[0:i+1])>=0:
r = A[i]
A[i]=-sum(A[0:i])-1
cnt+=abs(r-A[i])
else:
if sum(A[0:i+1])<=0:
r = A[i]
A[i]=-sum(A[0:i])+1
cnt+=abs(r-A[i])
return cnt
cnt1=0
if b[0]<=0:
ini=b[0]
b[0]=1
cnt1=abs(1-ini)
ans1=solve(cnt1,b,n)
cnt2=0
if a[0]>=0:
ini=a[0]
a[0]=-1
cnt2=abs(-1-ini)
ans2=solve(cnt2,a,n)
print(min(ans1,ans2))
|
p03739 AtCoder Beginner Contest 059 - Sequence | You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one.
At least how many operations are necessary to satisfy the following conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.
Constraints
* 2 ≤ n ≤ 10^5
* |a_i| ≤ 10^9
* Each a_i is an integer.
Input
Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n
Output
Print the minimum necessary count of operations.
Examples
Input
4
1 -3 1 0
Output
4
Input
5
3 -6 4 -5 7
Output
0
Input
6
-1 4 3 2 -5 4
Output
8 | {
"input": [
"5\n3 -6 4 -5 7",
"4\n1 -3 1 0",
"6\n-1 4 3 2 -5 4"
],
"output": [
"0",
"4",
"8"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
long long chk1, chk2, ans1 = 0, ans2 = 0;
scanf("%d", &n);
vector<long> a(n);
for (auto& e : a) scanf("%ld", &e);
chk1 = a[0] + a[1];
chk2 = a[0] + a[1];
if (chk1 <= 0) {
ans1 = -1 * chk1 + 1;
chk1 = 1;
}
if (chk2 >= 0) {
ans2 = chk2 + 1;
chk2 = -1;
}
for (int i = 2; i < n; i++) {
chk1 += a[i];
chk2 += a[i];
if (i % 2) {
if (chk1 <= 0) {
ans1 += -1 * chk1 + 1;
chk1 = 1;
}
if (chk2 >= 0) {
ans2 += chk2 + 1;
chk2 = -1;
}
} else {
if (chk1 >= 0) {
ans1 += chk1 + 1;
chk1 = -1;
}
if (chk2 <= 0) {
ans2 += -1 * chk2 + 1;
chk2 = 1;
}
}
}
printf("%lld\n", min(ans1, ans2));
return 0;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.